package com.qiuguo.iot.data.service.device;


import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.device.DeviceBatchEntity;
import com.qiuguo.iot.data.request.device.DeviceBatchRequest;
import lombok.extern.slf4j.Slf4j;;
import org.hswebframework.ezorm.rdb.mapping.ReactiveQuery;
import org.hswebframework.ezorm.rdb.mapping.ReactiveUpdate;
import org.hswebframework.ezorm.rdb.operator.dml.query.SortOrder;
import org.hswebframework.web.api.crud.entity.PagerResult;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.Date;
/**
* <p>
* 设备批次服务类
* </p>
*
* @author wulin
* @since 2023-08-08
*/

@Service
@Slf4j
public class DeviceBatchService extends GenericReactiveCrudService<DeviceBatchEntity, Long> {


	public Mono<DeviceBatchEntity> selectDeviceBatchByRequest(DeviceBatchRequest request){
		ReactiveQuery<DeviceBatchEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getModifyTime, request.getModifyTime());
		}
		if(StringUtils.isNotEmpty(request.getBatchNumber())){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getBatchNumber, request.getBatchNumber());
		}
		if(request.getDeviceType() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getDeviceType, request.getDeviceType());
		}
		if(StringUtils.isNotEmpty(request.getFirmwareVersion())){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getFirmwareVersion, request.getFirmwareVersion());
		}
		if(StringUtils.isNotEmpty(request.getHardwareVersion())){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getHardwareVersion, request.getHardwareVersion());
		}
		if(StringUtils.isNotEmpty(request.getChip())){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getChip, request.getChip());
		}
		if(request.getRam() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getRam, request.getRam());
		}
		if(request.getRom() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getRom, request.getRom());
		}
		if(request.getScreenType() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getScreenType, request.getScreenType());
		}
		if(request.getScreenW() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getScreenW, request.getScreenW());
		}
		if(request.getScreenH() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getScreenH, request.getScreenH());
		}
		if(request.getHornNum() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getHornNum, request.getHornNum());
		}
		if(request.getHornPower() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getHornPower, request.getHornPower());
		}
		if(request.getMicrophoneNum() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getMicrophoneNum, request.getMicrophoneNum());
		}
		if(request.getCamera() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getCamera, request.getCamera());
		}
		if(request.getDcVoltage() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getDcVoltage, request.getDcVoltage());
		}
		if(request.getDcCurrent() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getDcCurrent, request.getDcCurrent());
		}
		if(request.getAcVoltage() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getAcVoltage, request.getAcVoltage());
		}
		if(request.getAcCurrent() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getAcCurrent, request.getAcCurrent());
		}
		if(request.getPower() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getPower, request.getPower());
		}
		if(request.getSuppertWifi() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getSuppertWifi, request.getSuppertWifi());
		}
		if(StringUtils.isNotEmpty(request.getWifiVersion())){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getWifiVersion, request.getWifiVersion());
		}
		if(request.getSuppertBt() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getSuppertBt, request.getSuppertBt());
		}
		if(StringUtils.isNotEmpty(request.getBtVersion())){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getBtVersion, request.getBtVersion());
		}
		if(request.getBattery() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getBattery, request.getBattery());
		}
		if(request.getInnerBatty() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getInnerBatty, request.getInnerBatty());
		}
		if(StringUtils.isNotEmpty(request.getDescribe())){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getDescribe, request.getDescribe());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getRemark, request.getRemark());
		}
		if(request.getFactoryId() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getFactoryId, request.getFactoryId());
		}
		if(request.getStartProduceTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getStartProduceTime, request.getStartProduceTime());
		}
		if(StringUtils.isNotEmpty(request.getProduceMark())){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getProduceMark, request.getProduceMark());
		}
		if(StringUtils.isNotEmpty(request.getOperatingSystem())){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getOperatingSystem, request.getOperatingSystem());
		}
		SortOrder sortOrder = null;
		if(StringUtils.isNotEmpty(request.getOrder())){
			if(StringUtils.isNotEmpty(request.getSort()) && request.getSort().compareTo("0") == 0){
				sortOrder = SortOrder.desc(request.getOrder());
			}else{
				sortOrder = SortOrder.asc(request.getOrder());
			}
			reactiveQuery = reactiveQuery.orderBy(sortOrder);
		}
		return reactiveQuery.fetchOne();
	}



	public Mono<PagerResult<DeviceBatchEntity>> selectDeviceBatchsByRequest(DeviceBatchRequest request){
		ReactiveQuery<DeviceBatchEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceBatchRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceBatchRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceBatchRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceBatchRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(StringUtils.isNotEmpty(request.getBatchNumber())){
			reactiveQuery = reactiveQuery.$like$(DeviceBatchRequest::getBatchNumber, request.getBatchNumber());
		}
		if(request.getDeviceType() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getDeviceType, request.getDeviceType());
		}
		if(StringUtils.isNotEmpty(request.getFirmwareVersion())){
			reactiveQuery = reactiveQuery.$like$(DeviceBatchRequest::getFirmwareVersion, request.getFirmwareVersion());
		}
		if(StringUtils.isNotEmpty(request.getHardwareVersion())){
			reactiveQuery = reactiveQuery.$like$(DeviceBatchRequest::getHardwareVersion, request.getHardwareVersion());
		}
		if(StringUtils.isNotEmpty(request.getChip())){
			reactiveQuery = reactiveQuery.$like$(DeviceBatchRequest::getChip, request.getChip());
		}
		if(request.getRam() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getRam, request.getRam());
		}
		if(request.getRom() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getRom, request.getRom());
		}
		if(request.getScreenType() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getScreenType, request.getScreenType());
		}
		if(request.getScreenW() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getScreenW, request.getScreenW());
		}
		if(request.getScreenH() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getScreenH, request.getScreenH());
		}
		if(request.getHornNum() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getHornNum, request.getHornNum());
		}
		if(request.getHornPower() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getHornPower, request.getHornPower());
		}
		if(request.getMicrophoneNum() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getMicrophoneNum, request.getMicrophoneNum());
		}
		if(request.getCamera() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getCamera, request.getCamera());
		}
		if(request.getDcVoltage() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getDcVoltage, request.getDcVoltage());
		}
		if(request.getDcCurrent() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getDcCurrent, request.getDcCurrent());
		}
		if(request.getAcVoltage() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getAcVoltage, request.getAcVoltage());
		}
		if(request.getAcCurrent() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getAcCurrent, request.getAcCurrent());
		}
		if(request.getPower() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getPower, request.getPower());
		}
		if(request.getSuppertWifi() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getSuppertWifi, request.getSuppertWifi());
		}
		if(StringUtils.isNotEmpty(request.getWifiVersion())){
			reactiveQuery = reactiveQuery.$like$(DeviceBatchRequest::getWifiVersion, request.getWifiVersion());
		}
		if(request.getSuppertBt() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getSuppertBt, request.getSuppertBt());
		}
		if(StringUtils.isNotEmpty(request.getBtVersion())){
			reactiveQuery = reactiveQuery.$like$(DeviceBatchRequest::getBtVersion, request.getBtVersion());
		}
		if(request.getBattery() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getBattery, request.getBattery());
		}
		if(request.getInnerBatty() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getInnerBatty, request.getInnerBatty());
		}
		if(StringUtils.isNotEmpty(request.getDescribe())){
			reactiveQuery = reactiveQuery.$like$(DeviceBatchRequest::getDescribe, request.getDescribe());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.$like$(DeviceBatchRequest::getRemark, request.getRemark());
		}
		if(request.getFactoryId() != null){
			reactiveQuery = reactiveQuery.and(DeviceBatchRequest::getFactoryId, request.getFactoryId());
		}
		if(request.getStartProduceTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceBatchRequest::getStartProduceTime, request.getStartProduceTimeStart());
		}
		if(request.getStartProduceTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceBatchRequest::getStartProduceTime, request.getStartProduceTimeEnd());
		}
		if(StringUtils.isNotEmpty(request.getProduceMark())){
			reactiveQuery = reactiveQuery.$like$(DeviceBatchRequest::getProduceMark, request.getProduceMark());
		}
		if(StringUtils.isNotEmpty(request.getOperatingSystem())){
			reactiveQuery = reactiveQuery.$like$(DeviceBatchRequest::getOperatingSystem, request.getOperatingSystem());
		}
		SortOrder sortOrder = null;
		if(StringUtils.isNotEmpty(request.getOrder())){
			if(StringUtils.isNotEmpty(request.getSort()) && request.getSort().compareTo("0") == 0){
				sortOrder = SortOrder.desc(request.getOrder());
			}else{
				sortOrder = SortOrder.asc(request.getOrder());
			}
			reactiveQuery = reactiveQuery.orderBy(sortOrder);
		}
		QueryParamEntity param = QueryParamEntity.of(reactiveQuery.getParam());
		param.setPageIndex(request.getCurrPage());
		param.setPageSize(request.getPageSize());
		param.setPaging(true);
		param.setFirstPageIndex(1);

		return queryPager(param);
	}



	public Mono<DeviceBatchEntity> selectDeviceBatchById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



	public Mono<Integer> insertDeviceBatch(DeviceBatchEntity entity){
		entity.setCreateTime(null);
		entity.setModifyTime(null);
		return insert(entity);
	}



	public Mono<Integer> updateDeviceBatchById(DeviceBatchEntity entity){
		ReactiveUpdate<DeviceBatchEntity> update = createUpdate()
                .set(DeviceBatchEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(DeviceBatchEntity::getIsDelete, entity.getIsDelete());
		}
		if(StringUtils.isNotEmpty(entity.getBatchNumber())){
			update = update.set(DeviceBatchEntity::getBatchNumber, entity.getBatchNumber());
		}
		if(entity.getDeviceType() != null){
			update = update.set(DeviceBatchEntity::getDeviceType, entity.getDeviceType());
		}
		if(StringUtils.isNotEmpty(entity.getFirmwareVersion())){
			update = update.set(DeviceBatchEntity::getFirmwareVersion, entity.getFirmwareVersion());
		}
		if(StringUtils.isNotEmpty(entity.getHardwareVersion())){
			update = update.set(DeviceBatchEntity::getHardwareVersion, entity.getHardwareVersion());
		}
		if(StringUtils.isNotEmpty(entity.getChip())){
			update = update.set(DeviceBatchEntity::getChip, entity.getChip());
		}
		if(entity.getRam() != null){
			update = update.set(DeviceBatchEntity::getRam, entity.getRam());
		}
		if(entity.getRom() != null){
			update = update.set(DeviceBatchEntity::getRom, entity.getRom());
		}
		if(entity.getScreenType() != null){
			update = update.set(DeviceBatchEntity::getScreenType, entity.getScreenType());
		}
		if(entity.getScreenW() != null){
			update = update.set(DeviceBatchEntity::getScreenW, entity.getScreenW());
		}
		if(entity.getScreenH() != null){
			update = update.set(DeviceBatchEntity::getScreenH, entity.getScreenH());
		}
		if(entity.getHornNum() != null){
			update = update.set(DeviceBatchEntity::getHornNum, entity.getHornNum());
		}
		if(entity.getHornPower() != null){
			update = update.set(DeviceBatchEntity::getHornPower, entity.getHornPower());
		}
		if(entity.getMicrophoneNum() != null){
			update = update.set(DeviceBatchEntity::getMicrophoneNum, entity.getMicrophoneNum());
		}
		if(entity.getCamera() != null){
			update = update.set(DeviceBatchEntity::getCamera, entity.getCamera());
		}
		if(entity.getDcVoltage() != null){
			update = update.set(DeviceBatchEntity::getDcVoltage, entity.getDcVoltage());
		}
		if(entity.getDcCurrent() != null){
			update = update.set(DeviceBatchEntity::getDcCurrent, entity.getDcCurrent());
		}
		if(entity.getAcVoltage() != null){
			update = update.set(DeviceBatchEntity::getAcVoltage, entity.getAcVoltage());
		}
		if(entity.getAcCurrent() != null){
			update = update.set(DeviceBatchEntity::getAcCurrent, entity.getAcCurrent());
		}
		if(entity.getPower() != null){
			update = update.set(DeviceBatchEntity::getPower, entity.getPower());
		}
		if(entity.getSuppertWifi() != null){
			update = update.set(DeviceBatchEntity::getSuppertWifi, entity.getSuppertWifi());
		}
		if(StringUtils.isNotEmpty(entity.getWifiVersion())){
			update = update.set(DeviceBatchEntity::getWifiVersion, entity.getWifiVersion());
		}
		if(entity.getSuppertBt() != null){
			update = update.set(DeviceBatchEntity::getSuppertBt, entity.getSuppertBt());
		}
		if(StringUtils.isNotEmpty(entity.getBtVersion())){
			update = update.set(DeviceBatchEntity::getBtVersion, entity.getBtVersion());
		}
		if(entity.getBattery() != null){
			update = update.set(DeviceBatchEntity::getBattery, entity.getBattery());
		}
		if(entity.getInnerBatty() != null){
			update = update.set(DeviceBatchEntity::getInnerBatty, entity.getInnerBatty());
		}
		if(StringUtils.isNotEmpty(entity.getDescribe())){
			update = update.set(DeviceBatchEntity::getDescribe, entity.getDescribe());
		}
		if(StringUtils.isNotEmpty(entity.getRemark())){
			update = update.set(DeviceBatchEntity::getRemark, entity.getRemark());
		}
		if(entity.getFactoryId() != null){
			update = update.set(DeviceBatchEntity::getFactoryId, entity.getFactoryId());
		}
		if(entity.getStartProduceTime() != null){
			update = update.set(DeviceBatchEntity::getStartProduceTime, entity.getStartProduceTime());
		}
		if(StringUtils.isNotEmpty(entity.getProduceMark())){
			update = update.set(DeviceBatchEntity::getProduceMark, entity.getProduceMark());
		}
		if(StringUtils.isNotEmpty(entity.getOperatingSystem())){
			update = update.set(DeviceBatchEntity::getOperatingSystem, entity.getOperatingSystem());
		}
		return update.where(DeviceBatchEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverDeviceBatchById(DeviceBatchEntity entity){
		ReactiveUpdate<DeviceBatchEntity> update = createUpdate()
                .set(DeviceBatchEntity::getModifyTime, new Date());
		update = update.set(DeviceBatchEntity::getIsDelete, entity.getIsDelete());
		update = update.set(DeviceBatchEntity::getBatchNumber, entity.getBatchNumber());
		update = update.set(DeviceBatchEntity::getDeviceType, entity.getDeviceType());
		update = update.set(DeviceBatchEntity::getFirmwareVersion, entity.getFirmwareVersion());
		update = update.set(DeviceBatchEntity::getHardwareVersion, entity.getHardwareVersion());
		update = update.set(DeviceBatchEntity::getChip, entity.getChip());
		update = update.set(DeviceBatchEntity::getRam, entity.getRam());
		update = update.set(DeviceBatchEntity::getRom, entity.getRom());
		update = update.set(DeviceBatchEntity::getScreenType, entity.getScreenType());
		update = update.set(DeviceBatchEntity::getScreenW, entity.getScreenW());
		update = update.set(DeviceBatchEntity::getScreenH, entity.getScreenH());
		update = update.set(DeviceBatchEntity::getHornNum, entity.getHornNum());
		update = update.set(DeviceBatchEntity::getHornPower, entity.getHornPower());
		update = update.set(DeviceBatchEntity::getMicrophoneNum, entity.getMicrophoneNum());
		update = update.set(DeviceBatchEntity::getCamera, entity.getCamera());
		update = update.set(DeviceBatchEntity::getDcVoltage, entity.getDcVoltage());
		update = update.set(DeviceBatchEntity::getDcCurrent, entity.getDcCurrent());
		update = update.set(DeviceBatchEntity::getAcVoltage, entity.getAcVoltage());
		update = update.set(DeviceBatchEntity::getAcCurrent, entity.getAcCurrent());
		update = update.set(DeviceBatchEntity::getPower, entity.getPower());
		update = update.set(DeviceBatchEntity::getSuppertWifi, entity.getSuppertWifi());
		update = update.set(DeviceBatchEntity::getWifiVersion, entity.getWifiVersion());
		update = update.set(DeviceBatchEntity::getSuppertBt, entity.getSuppertBt());
		update = update.set(DeviceBatchEntity::getBtVersion, entity.getBtVersion());
		update = update.set(DeviceBatchEntity::getBattery, entity.getBattery());
		update = update.set(DeviceBatchEntity::getInnerBatty, entity.getInnerBatty());
		update = update.set(DeviceBatchEntity::getDescribe, entity.getDescribe());
		update = update.set(DeviceBatchEntity::getRemark, entity.getRemark());
		update = update.set(DeviceBatchEntity::getFactoryId, entity.getFactoryId());
		update = update.set(DeviceBatchEntity::getStartProduceTime, entity.getStartProduceTime());
		update = update.set(DeviceBatchEntity::getProduceMark, entity.getProduceMark());
		update = update.set(DeviceBatchEntity::getOperatingSystem, entity.getOperatingSystem());
		return update.where(DeviceBatchEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> deleteDeviceBatchById(Long id){
		return createUpdate()
				.set("is_delete", 1)
				.set("modify_time", new Date())
				.where("id", id)
				.execute();
	}



}
