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



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


	public Mono<OperatinSceneEntity> selectOperatinSceneByRequest(OperatinSceneRequest request){
		ReactiveQuery<OperatinSceneEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OperatinSceneRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OperatinSceneRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(OperatinSceneRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(OperatinSceneRequest::getModifyTime, request.getModifyTime());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.and(OperatinSceneRequest::getName, request.getName());
		}
		if(request.getOperationModeId() != null){
			reactiveQuery = reactiveQuery.and(OperatinSceneRequest::getOperationModeId, request.getOperationModeId());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.and(OperatinSceneRequest::getRemark, request.getRemark());
		}
		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<OperatinSceneEntity>> selectOperatinScenesByRequest(OperatinSceneRequest request){
		ReactiveQuery<OperatinSceneEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OperatinSceneRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OperatinSceneRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OperatinSceneRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OperatinSceneRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OperatinSceneRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OperatinSceneRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.$like$(OperatinSceneRequest::getName, request.getName());
		}
		if(request.getOperationModeId() != null){
			reactiveQuery = reactiveQuery.and(OperatinSceneRequest::getOperationModeId, request.getOperationModeId());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.$like$(OperatinSceneRequest::getRemark, request.getRemark());
		}
		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<OperatinSceneEntity> selectOperatinSceneById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



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



	public Mono<Integer> updateCoverOperatinSceneById(OperatinSceneEntity entity){
		ReactiveUpdate<OperatinSceneEntity> update = createUpdate()
                .set(OperatinSceneEntity::getModifyTime, new Date());
		update = update.set(OperatinSceneEntity::getIsDelete, entity.getIsDelete());
		update = update.set(OperatinSceneEntity::getName, entity.getName());
		update = update.set(OperatinSceneEntity::getOperationModeId, entity.getOperationModeId());
		update = update.set(OperatinSceneEntity::getRemark, entity.getRemark());
		return update.where(OperatinSceneEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
