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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.operation.OperatinConditionEntity;
import com.qiuguo.iot.data.request.operation.OperatinConditionRequest;
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 OperatinConditionService extends GenericReactiveCrudService<OperatinConditionEntity, Long> {


	public Mono<OperatinConditionEntity> selectOperatinConditionByRequest(OperatinConditionRequest request){
		ReactiveQuery<OperatinConditionEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getType() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getType, request.getType());
		}
		if(request.getOperationModeId() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getOperationModeId, request.getOperationModeId());
		}
		if(request.getOperationSceneId() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getOperationSceneId, request.getOperationSceneId());
		}
		if(StringUtils.isNotEmpty(request.getCondition())){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getCondition, request.getCondition());
		}
		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<OperatinConditionEntity>> selectOperatinConditionsByRequest(OperatinConditionRequest request){
		ReactiveQuery<OperatinConditionEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OperatinConditionRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OperatinConditionRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OperatinConditionRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OperatinConditionRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getType() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getType, request.getType());
		}
		if(request.getOperationModeId() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getOperationModeId, request.getOperationModeId());
		}
		if(request.getOperationSceneId() != null){
			reactiveQuery = reactiveQuery.and(OperatinConditionRequest::getOperationSceneId, request.getOperationSceneId());
		}
		if(StringUtils.isNotEmpty(request.getCondition())){
			reactiveQuery = reactiveQuery.$like$(OperatinConditionRequest::getCondition, request.getCondition());
		}
		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<OperatinConditionEntity> selectOperatinConditionById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateOperatinConditionById(OperatinConditionEntity entity){
		ReactiveUpdate<OperatinConditionEntity> update = createUpdate()
                .set(OperatinConditionEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(OperatinConditionEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getType() != null){
			update = update.set(OperatinConditionEntity::getType, entity.getType());
		}
		if(entity.getOperationModeId() != null){
			update = update.set(OperatinConditionEntity::getOperationModeId, entity.getOperationModeId());
		}
		if(entity.getOperationSceneId() != null){
			update = update.set(OperatinConditionEntity::getOperationSceneId, entity.getOperationSceneId());
		}
		if(StringUtils.isNotEmpty(entity.getCondition())){
			update = update.set(OperatinConditionEntity::getCondition, entity.getCondition());
		}
		return update.where(OperatinConditionEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverOperatinConditionById(OperatinConditionEntity entity){
		ReactiveUpdate<OperatinConditionEntity> update = createUpdate()
                .set(OperatinConditionEntity::getModifyTime, new Date());
		update = update.set(OperatinConditionEntity::getIsDelete, entity.getIsDelete());
		update = update.set(OperatinConditionEntity::getType, entity.getType());
		update = update.set(OperatinConditionEntity::getOperationModeId, entity.getOperationModeId());
		update = update.set(OperatinConditionEntity::getOperationSceneId, entity.getOperationSceneId());
		update = update.set(OperatinConditionEntity::getCondition, entity.getCondition());
		return update.where(OperatinConditionEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
