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



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


	public Mono<OperatinActionEntity> selectOperatinActionByRequest(OperatinActionRequest request){
		ReactiveQuery<OperatinActionEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getOperationModeId() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getOperationModeId, request.getOperationModeId());
		}
		if(request.getOperationSceneId() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getOperationSceneId, request.getOperationSceneId());
		}
		if(request.getOperationConditionId() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getOperationConditionId, request.getOperationConditionId());
		}
		if(request.getType() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getType, request.getType());
		}
		if(StringUtils.isNotEmpty(request.getAction())){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getAction, request.getAction());
		}
		if(request.getConnectId() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getConnectId, request.getConnectId());
		}
		if(request.getActionTimes() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getActionTimes, request.getActionTimes());
		}
		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<OperatinActionEntity>> selectOperatinActionsByRequest(OperatinActionRequest request){
		ReactiveQuery<OperatinActionEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OperatinActionRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OperatinActionRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OperatinActionRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OperatinActionRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getOperationModeId() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getOperationModeId, request.getOperationModeId());
		}
		if(request.getOperationSceneId() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getOperationSceneId, request.getOperationSceneId());
		}
		if(request.getOperationConditionId() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getOperationConditionId, request.getOperationConditionId());
		}
		if(request.getType() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getType, request.getType());
		}
		if(StringUtils.isNotEmpty(request.getAction())){
			reactiveQuery = reactiveQuery.$like$(OperatinActionRequest::getAction, request.getAction());
		}
		if(request.getConnectId() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getConnectId, request.getConnectId());
		}
		if(request.getActionTimes() != null){
			reactiveQuery = reactiveQuery.and(OperatinActionRequest::getActionTimes, request.getActionTimes());
		}
		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<OperatinActionEntity> selectOperatinActionById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



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



	public Mono<Integer> updateCoverOperatinActionById(OperatinActionEntity entity){
		ReactiveUpdate<OperatinActionEntity> update = createUpdate()
                .set(OperatinActionEntity::getModifyTime, new Date());
		update = update.set(OperatinActionEntity::getIsDelete, entity.getIsDelete());
		update = update.set(OperatinActionEntity::getOperationModeId, entity.getOperationModeId());
		update = update.set(OperatinActionEntity::getOperationSceneId, entity.getOperationSceneId());
		update = update.set(OperatinActionEntity::getOperationConditionId, entity.getOperationConditionId());
		update = update.set(OperatinActionEntity::getType, entity.getType());
		update = update.set(OperatinActionEntity::getAction, entity.getAction());
		update = update.set(OperatinActionEntity::getConnectId, entity.getConnectId());
		update = update.set(OperatinActionEntity::getActionTimes, entity.getActionTimes());
		return update.where(OperatinActionEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
