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



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


	public Mono<OperatinModeEntity> selectOperatinModeByRequest(OperatinModeRequest request){
		ReactiveQuery<OperatinModeEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getModifyTime, request.getModifyTime());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getName, request.getName());
		}
		if(request.getStatus() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getStatus, request.getStatus());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getRemark, request.getRemark());
		}
		if(request.getOperationUserId() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getOperationUserId, request.getOperationUserId());
		}
		if(StringUtils.isNotEmpty(request.getOperationUserName())){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getOperationUserName, request.getOperationUserName());
		}
		if(request.getSceneCount() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getSceneCount, request.getSceneCount());
		}
		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<OperatinModeEntity>> selectOperatinModesByRequest(OperatinModeRequest request){
		ReactiveQuery<OperatinModeEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OperatinModeRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OperatinModeRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OperatinModeRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OperatinModeRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(StringUtils.isNotEmpty(request.getName())){
			reactiveQuery = reactiveQuery.$like$(OperatinModeRequest::getName, request.getName());
		}
		if(request.getStatus() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getStatus, request.getStatus());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.$like$(OperatinModeRequest::getRemark, request.getRemark());
		}
		if(request.getOperationUserId() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getOperationUserId, request.getOperationUserId());
		}
		if(StringUtils.isNotEmpty(request.getOperationUserName())){
			reactiveQuery = reactiveQuery.$like$(OperatinModeRequest::getOperationUserName, request.getOperationUserName());
		}
		if(request.getSceneCount() != null){
			reactiveQuery = reactiveQuery.and(OperatinModeRequest::getSceneCount, request.getSceneCount());
		}
		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<OperatinModeEntity> selectOperatinModeById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



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



	public Mono<Integer> updateCoverOperatinModeById(OperatinModeEntity entity){
		ReactiveUpdate<OperatinModeEntity> update = createUpdate()
                .set(OperatinModeEntity::getModifyTime, new Date());
		update = update.set(OperatinModeEntity::getIsDelete, entity.getIsDelete());
		update = update.set(OperatinModeEntity::getName, entity.getName());
		update = update.set(OperatinModeEntity::getStatus, entity.getStatus());
		update = update.set(OperatinModeEntity::getRemark, entity.getRemark());
		update = update.set(OperatinModeEntity::getOperationUserId, entity.getOperationUserId());
		update = update.set(OperatinModeEntity::getOperationUserName, entity.getOperationUserName());
		update = update.set(OperatinModeEntity::getSceneCount, entity.getSceneCount());
		return update.where(OperatinModeEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
