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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.device.DeviceInfoEntity;
import com.qiuguo.iot.data.request.device.DeviceInfoRequest;
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-09-05
*/

@Service
@Slf4j
public class DeviceInfoService extends GenericReactiveCrudService<DeviceInfoEntity, Long> {


	public Mono<DeviceInfoEntity> selectDeviceInfoByRequest(DeviceInfoRequest request){
		ReactiveQuery<DeviceInfoEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getBatchId() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getBatchId, request.getBatchId());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getName, request.getName());
		}
		if(StringUtils.isNotEmpty(request.getSn())){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getSn, request.getSn());
		}
		if(StringUtils.isNotEmpty(request.getKey())){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getKey, request.getKey());
		}
		if(request.getStatus() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getStatus, request.getStatus());
		}
		if(StringUtils.isNotEmpty(request.getBtMac())){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getBtMac, request.getBtMac());
		}
		if(StringUtils.isNotEmpty(request.getWifiMac())){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getWifiMac, request.getWifiMac());
		}
		if(StringUtils.isNotEmpty(request.getFirmwareVersion())){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getFirmwareVersion, request.getFirmwareVersion());
		}
		if(request.getDeviceType() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getDeviceType, request.getDeviceType());
		}
		if(request.getOnLine() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getOnLine, request.getOnLine());
		}
		if(request.getLastOnLineTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getLastOnLineTime, request.getLastOnLineTime());
		}
		if(request.getProtocolType() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getProtocolType, request.getProtocolType());
		}
		if(request.getOperatingModeId() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getOperatingModeId, request.getOperatingModeId());
		}
		if(request.getOtaType() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getOtaType, request.getOtaType());
		}
		if(request.getOtaStartTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getOtaStartTime, request.getOtaStartTime());
		}
		if(request.getOtaEndTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getOtaEndTime, request.getOtaEndTime());
		}
		if(request.getFactoryTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getFactoryTime, request.getFactoryTime());
		}
		if(request.getSaleTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getSaleTime, request.getSaleTime());
		}
		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<DeviceInfoEntity>> selectDeviceInfosByRequest(DeviceInfoRequest request){


		ReactiveQuery<DeviceInfoEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceInfoRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceInfoRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceInfoRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceInfoRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getBatchId() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getBatchId, request.getBatchId());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.$like$(DeviceInfoRequest::getName, request.getName());
		}
		if(StringUtils.isNotEmpty(request.getSn())){
			reactiveQuery = reactiveQuery.$like$(DeviceInfoRequest::getSn, request.getSn());
		}
		if(StringUtils.isNotEmpty(request.getKey())){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getKey, request.getKey());
		}
		if(StringUtils.isNotEmpty(request.getBtMac())){
			reactiveQuery = reactiveQuery.$like$(DeviceInfoRequest::getBtMac, request.getBtMac());
		}
		if(StringUtils.isNotEmpty(request.getWifiMac())){
			reactiveQuery = reactiveQuery.$like$(DeviceInfoRequest::getWifiMac, request.getWifiMac());
		}
		if(StringUtils.isNotEmpty(request.getFirmwareVersion())){
			reactiveQuery = reactiveQuery.$like$(DeviceInfoRequest::getFirmwareVersion, request.getFirmwareVersion());
		}
		if(request.getDeviceType() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getDeviceType, request.getDeviceType());
		}
		if(request.getOnLine() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getOnLine, request.getOnLine());
		}
		if(request.getLastOnLineTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceInfoRequest::getLastOnLineTime, request.getLastOnLineTimeStart());
		}
		if(request.getLastOnLineTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceInfoRequest::getLastOnLineTime, request.getLastOnLineTimeEnd());
		}
		if(request.getProtocolType() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getProtocolType, request.getProtocolType());
		}
		if(request.getStatus() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getStatus, request.getStatus());
		}
		if(request.getOperatingModeId() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getOperatingModeId, request.getOperatingModeId());
		}
		if(request.getOtaType() != null){
			reactiveQuery = reactiveQuery.and(DeviceInfoRequest::getOtaType, request.getOtaType());
		}
		if(request.getOtaStartTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceInfoRequest::getOtaStartTime, request.getOtaStartTimeStart());
		}
		if(request.getOtaStartTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceInfoRequest::getOtaStartTime, request.getOtaStartTimeEnd());
		}
		if(request.getOtaEndTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceInfoRequest::getOtaEndTime, request.getOtaEndTimeStart());
		}
		if(request.getOtaEndTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceInfoRequest::getOtaEndTime, request.getOtaEndTimeEnd());
		}
		if(request.getFactoryTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceInfoRequest::getFactoryTime, request.getFactoryTimeStart());
		}
		if(request.getFactoryTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceInfoRequest::getFactoryTime, request.getFactoryTimeEnd());
		}
		if(request.getSaleTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceInfoRequest::getSaleTime, request.getSaleTimeStart());
		}
		if(request.getSaleTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceInfoRequest::getSaleTime, request.getSaleTimeEnd());
		}
		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<DeviceInfoEntity> selectDeviceInfoById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateDeviceInfoById(DeviceInfoEntity entity){
		ReactiveUpdate<DeviceInfoEntity> update = createUpdate()
                .set(DeviceInfoEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(DeviceInfoEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getBatchId() != null){
			update = update.set(DeviceInfoEntity::getBatchId, entity.getBatchId());
		}
		if(StringUtils.isNotEmpty(entity.getName())){
			update = update.set(DeviceInfoEntity::getName, entity.getName());
		}
		if(StringUtils.isNotEmpty(entity.getSn())){
			update = update.set(DeviceInfoEntity::getSn, entity.getSn());
		}
		if(StringUtils.isNotEmpty(entity.getKey())){
			update = update.set(DeviceInfoEntity::getKey, entity.getKey());
		}
		if(StringUtils.isNotEmpty(entity.getBtMac())){
			update = update.set(DeviceInfoEntity::getBtMac, entity.getBtMac());
		}
		if(StringUtils.isNotEmpty(entity.getWifiMac())){
			update = update.set(DeviceInfoEntity::getWifiMac, entity.getWifiMac());
		}
		if(StringUtils.isNotEmpty(entity.getFirmwareVersion())){
			update = update.set(DeviceInfoEntity::getFirmwareVersion, entity.getFirmwareVersion());
		}
		if(entity.getDeviceType() != null){
			update = update.set(DeviceInfoEntity::getDeviceType, entity.getDeviceType());
		}
		if(entity.getOnLine() != null){
			update = update.set(DeviceInfoEntity::getOnLine, entity.getOnLine());
		}
		if(entity.getLastOnLineTime() != null){
			update = update.set(DeviceInfoEntity::getLastOnLineTime, entity.getLastOnLineTime());
		}
		if(entity.getProtocolType() != null){
			update = update.set(DeviceInfoEntity::getProtocolType, entity.getProtocolType());
		}
		if(entity.getStatus() != null){
			update = update.set(DeviceInfoEntity::getStatus, entity.getStatus());
		}
		if(entity.getOperatingModeId() != null){
			update = update.set(DeviceInfoEntity::getOperatingModeId, entity.getOperatingModeId());
		}
		if(entity.getOtaType() != null){
			update = update.set(DeviceInfoEntity::getOtaType, entity.getOtaType());
		}
		if(entity.getOtaStartTime() != null){
			update = update.set(DeviceInfoEntity::getOtaStartTime, entity.getOtaStartTime());
		}
		if(entity.getOtaEndTime() != null){
			update = update.set(DeviceInfoEntity::getOtaEndTime, entity.getOtaEndTime());
		}
		if(entity.getFactoryTime() != null){
			update = update.set(DeviceInfoEntity::getFactoryTime, entity.getFactoryTime());
		}
		if(entity.getSaleTime() != null){
			update = update.set(DeviceInfoEntity::getSaleTime, entity.getSaleTime());
		}
		return update.where(DeviceInfoEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverDeviceInfoById(DeviceInfoEntity entity){
		ReactiveUpdate<DeviceInfoEntity> update = createUpdate()
                .set(DeviceInfoEntity::getModifyTime, new Date());
		update = update.set(DeviceInfoEntity::getIsDelete, entity.getIsDelete());
		update = update.set(DeviceInfoEntity::getBatchId, entity.getBatchId());
		update = update.set(DeviceInfoEntity::getName, entity.getName());
		update = update.set(DeviceInfoEntity::getSn, entity.getSn());
		update = update.set(DeviceInfoEntity::getBtMac, entity.getBtMac());
		update = update.set(DeviceInfoEntity::getWifiMac, entity.getWifiMac());
		update = update.set(DeviceInfoEntity::getFirmwareVersion, entity.getFirmwareVersion());
		update = update.set(DeviceInfoEntity::getDeviceType, entity.getDeviceType());
		update = update.set(DeviceInfoEntity::getOnLine, entity.getOnLine());
		update = update.set(DeviceInfoEntity::getLastOnLineTime, entity.getLastOnLineTime());
		update = update.set(DeviceInfoEntity::getProtocolType, entity.getProtocolType());
		update = update.set(DeviceInfoEntity::getOperatingModeId, entity.getOperatingModeId());
		update = update.set(DeviceInfoEntity::getOtaType, entity.getOtaType());
		update = update.set(DeviceInfoEntity::getOtaStartTime, entity.getOtaStartTime());
		update = update.set(DeviceInfoEntity::getOtaEndTime, entity.getOtaEndTime());
		update = update.set(DeviceInfoEntity::getOtaStartTime, entity.getOtaStartTime());
		update = update.set(DeviceInfoEntity::getKey, entity.getKey());
		update = update.set(DeviceInfoEntity::getStatus, entity.getStatus());
		update = update.set(DeviceInfoEntity::getSaleTime, entity.getSaleTime());
		return update.where(DeviceInfoEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
