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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.system.SystemTalkBindDeviceEntity;
import com.qiuguo.iot.data.entity.system.SystemTalkBindDeviceEntity;
import com.qiuguo.iot.data.request.system.SystemTalkBindDeviceRequest;
import com.qiuguo.iot.data.request.system.SystemTalkBindDeviceRequest;
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-25
*/

@Service
@Slf4j
public class SystemTalkBindDeviceService extends GenericReactiveCrudService<SystemTalkBindDeviceEntity, Long> {


	public Mono<SystemTalkBindDeviceEntity> selectSystemTalkBindDeviceByRequest(SystemTalkBindDeviceRequest request){
		ReactiveQuery<SystemTalkBindDeviceEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getSystemTalkId() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getSystemTalkId, request.getSystemTalkId());
		}
		if(request.getUserHandlingId() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getUserHandlingId, request.getUserHandlingId());
		}
		if(StringUtils.isNotEmpty(request.getCategoryCode())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getCategoryCode, request.getCategoryCode());
		}
		if(StringUtils.isNotEmpty(request.getAskCommon())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getAskCommon, request.getAskCommon());
		}
		if(StringUtils.isNotEmpty(request.getAnswerValue())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getAnswerValue, request.getAnswerValue());
		}
		if(StringUtils.isNotEmpty(request.getAnswerValueFaild())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getAnswerValueFaild, request.getAnswerValueFaild());
		}
		if(StringUtils.isNotEmpty(request.getAnswerAction())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getAnswerAction, request.getAnswerAction());
		}
		if(StringUtils.isNotEmpty(request.getAnswerActionFaild())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getAnswerActionFaild, request.getAnswerActionFaild());
		}
		if(StringUtils.isNotEmpty(request.getAnswerBackSound())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getAnswerBackSound, request.getAnswerBackSound());
		}
		if(StringUtils.isNotEmpty(request.getAnswerBackImg())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getAnswerBackImg, request.getAnswerBackImg());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::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<SystemTalkBindDeviceEntity>> selectSystemTalkBindDevicesByRequest(SystemTalkBindDeviceRequest request){
		ReactiveQuery<SystemTalkBindDeviceEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(SystemTalkBindDeviceRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(SystemTalkBindDeviceRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(SystemTalkBindDeviceRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(SystemTalkBindDeviceRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getSystemTalkId() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getSystemTalkId, request.getSystemTalkId());
		}
		if(request.getUserHandlingId() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getUserHandlingId, request.getUserHandlingId());
		}
		if(StringUtils.isNotEmpty(request.getCategoryCode())){
			reactiveQuery = reactiveQuery.$like$(SystemTalkBindDeviceRequest::getCategoryCode, request.getCategoryCode());
		}
		if(StringUtils.isNotEmpty(request.getAskCommon())){
			reactiveQuery = reactiveQuery.$like$(SystemTalkBindDeviceRequest::getAskCommon, request.getAskCommon());
		}
		if(StringUtils.isNotEmpty(request.getAnswerValue())){
			reactiveQuery = reactiveQuery.$like$(SystemTalkBindDeviceRequest::getAnswerValue, request.getAnswerValue());
		}
		if(StringUtils.isNotEmpty(request.getAnswerValueFaild())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getAnswerValueFaild, request.getAnswerValueFaild());
		}
		if(StringUtils.isNotEmpty(request.getAnswerAction())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getAnswerAction, request.getAnswerAction());
		}
		if(StringUtils.isNotEmpty(request.getAnswerActionFaild())){
			reactiveQuery = reactiveQuery.and(SystemTalkBindDeviceRequest::getAnswerActionFaild, request.getAnswerActionFaild());
		}
		if(StringUtils.isNotEmpty(request.getAnswerBackSound())){
			reactiveQuery = reactiveQuery.$like$(SystemTalkBindDeviceRequest::getAnswerBackSound, request.getAnswerBackSound());
		}
		if(StringUtils.isNotEmpty(request.getAnswerBackImg())){
			reactiveQuery = reactiveQuery.$like$(SystemTalkBindDeviceRequest::getAnswerBackImg, request.getAnswerBackImg());
		}
		if(StringUtils.isNotEmpty(request.getRemark())){
			reactiveQuery = reactiveQuery.$like$(SystemTalkBindDeviceRequest::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<SystemTalkBindDeviceEntity> selectSystemTalkBindDeviceById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateSystemTalkBindDeviceById(SystemTalkBindDeviceEntity entity){
		ReactiveUpdate<SystemTalkBindDeviceEntity> update = createUpdate()
                .set(SystemTalkBindDeviceEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(SystemTalkBindDeviceEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getSystemTalkId() != null){
			update = update.set(SystemTalkBindDeviceEntity::getSystemTalkId, entity.getSystemTalkId());
		}
		if(entity.getUserHandlingId() != null){
			update = update.set(SystemTalkBindDeviceEntity::getUserHandlingId, entity.getUserHandlingId());
		}
		if(StringUtils.isNotEmpty(entity.getCategoryCode())){
			update = update.set(SystemTalkBindDeviceEntity::getCategoryCode, entity.getCategoryCode());
		}
		if(StringUtils.isNotEmpty(entity.getAskCommon())){
			update = update.set(SystemTalkBindDeviceEntity::getAskCommon, entity.getAskCommon());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerValue())){
			update = update.set(SystemTalkBindDeviceEntity::getAnswerValue, entity.getAnswerValue());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerAction())){
			update = update.set(SystemTalkBindDeviceEntity::getAnswerAction, entity.getAnswerAction());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerValueFaild())){
			update = update.set(SystemTalkBindDeviceEntity::getAnswerValueFaild, entity.getAnswerValueFaild());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerActionFaild())){
			update = update.set(SystemTalkBindDeviceEntity::getAnswerActionFaild, entity.getAnswerActionFaild());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerBackSound())){
			update = update.set(SystemTalkBindDeviceEntity::getAnswerBackSound, entity.getAnswerBackSound());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerBackImg())){
			update = update.set(SystemTalkBindDeviceEntity::getAnswerBackImg, entity.getAnswerBackImg());
		}
		if(StringUtils.isNotEmpty(entity.getRemark())){
			update = update.set(SystemTalkBindDeviceEntity::getRemark, entity.getRemark());
		}
		return update.where(SystemTalkBindDeviceEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverSystemTalkBindDeviceById(SystemTalkBindDeviceEntity entity){
		ReactiveUpdate<SystemTalkBindDeviceEntity> update = createUpdate()
                .set(SystemTalkBindDeviceEntity::getModifyTime, new Date());
		update = update.set(SystemTalkBindDeviceEntity::getIsDelete, entity.getIsDelete());
		update = update.set(SystemTalkBindDeviceEntity::getSystemTalkId, entity.getSystemTalkId());
		update = update.set(SystemTalkBindDeviceEntity::getUserHandlingId, entity.getUserHandlingId());
		update = update.set(SystemTalkBindDeviceEntity::getCategoryCode, entity.getCategoryCode());
		update = update.set(SystemTalkBindDeviceEntity::getAskCommon, entity.getAskCommon());
		update = update.set(SystemTalkBindDeviceEntity::getAnswerValue, entity.getAnswerValue());
		update = update.set(SystemTalkBindDeviceEntity::getAnswerAction, entity.getAnswerAction());
		update = update.set(SystemTalkBindDeviceEntity::getAnswerValueFaild, entity.getAnswerValueFaild());
		update = update.set(SystemTalkBindDeviceEntity::getAnswerActionFaild, entity.getAnswerActionFaild());
		update = update.set(SystemTalkBindDeviceEntity::getAnswerBackSound, entity.getAnswerBackSound());
		update = update.set(SystemTalkBindDeviceEntity::getAnswerBackImg, entity.getAnswerBackImg());
		update = update.set(SystemTalkBindDeviceEntity::getRemark, entity.getRemark());
		return update.where(SystemTalkBindDeviceEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
