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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.system.SystemSameTalkEntity;
import com.qiuguo.iot.data.request.system.SystemSameTalkRequest;
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-28
*/

@Service
@Slf4j
public class SystemSameTalkService extends GenericReactiveCrudService<SystemSameTalkEntity, Long> {


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



	public Mono<Integer> insertSystemSameTalk(SystemSameTalkEntity entity){
		entity.setId(null);
		entity.setCreateTime(null);
		entity.setModifyTime(null);
		return insert(entity);
	}



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



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



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



}
