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



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


	public Mono<OperatinEventConfigEntity> selectOperatinEventConfigByRequest(OperatinEventConfigRequest request){
		ReactiveQuery<OperatinEventConfigEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getType() != null){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getType, request.getType());
		}
		if(StringUtils.isNotEmpty(request.getKey())){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getKey, request.getKey());
		}
		if(StringUtils.isNotEmpty(request.getUr())){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getUr, request.getUr());
		}
		if(request.getThirdConfigInfoId() != null){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getThirdConfigInfoId, request.getThirdConfigInfoId());
		}
		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<OperatinEventConfigEntity>> selectOperatinEventConfigsByRequest(OperatinEventConfigRequest request){
		ReactiveQuery<OperatinEventConfigEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OperatinEventConfigRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OperatinEventConfigRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(OperatinEventConfigRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(OperatinEventConfigRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getType() != null){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getType, request.getType());
		}
		if(StringUtils.isNotEmpty(request.getKey())){
			reactiveQuery = reactiveQuery.$like$(OperatinEventConfigRequest::getKey, request.getKey());
		}
		if(StringUtils.isNotEmpty(request.getUr())){
			reactiveQuery = reactiveQuery.$like$(OperatinEventConfigRequest::getUr, request.getUr());
		}
		if(request.getThirdConfigInfoId() != null){
			reactiveQuery = reactiveQuery.and(OperatinEventConfigRequest::getThirdConfigInfoId, request.getThirdConfigInfoId());
		}
		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<OperatinEventConfigEntity> selectOperatinEventConfigById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateOperatinEventConfigById(OperatinEventConfigEntity entity){
		ReactiveUpdate<OperatinEventConfigEntity> update = createUpdate()
                .set(OperatinEventConfigEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(OperatinEventConfigEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getType() != null){
			update = update.set(OperatinEventConfigEntity::getType, entity.getType());
		}
		if(StringUtils.isNotEmpty(entity.getKey())){
			update = update.set(OperatinEventConfigEntity::getKey, entity.getKey());
		}
		if(StringUtils.isNotEmpty(entity.getUr())){
			update = update.set(OperatinEventConfigEntity::getUr, entity.getUr());
		}
		if(entity.getThirdConfigInfoId() != null){
			update = update.set(OperatinEventConfigEntity::getThirdConfigInfoId, entity.getThirdConfigInfoId());
		}
		return update.where(OperatinEventConfigEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverOperatinEventConfigById(OperatinEventConfigEntity entity){
		ReactiveUpdate<OperatinEventConfigEntity> update = createUpdate()
                .set(OperatinEventConfigEntity::getModifyTime, new Date());
		update = update.set(OperatinEventConfigEntity::getIsDelete, entity.getIsDelete());
		update = update.set(OperatinEventConfigEntity::getType, entity.getType());
		update = update.set(OperatinEventConfigEntity::getKey, entity.getKey());
		update = update.set(OperatinEventConfigEntity::getUr, entity.getUr());
		update = update.set(OperatinEventConfigEntity::getThirdConfigInfoId, entity.getThirdConfigInfoId());
		return update.where(OperatinEventConfigEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
