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



import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.device.DeviceUserTalkRecordEntity;
import com.qiuguo.iot.data.request.device.DeviceUserTalkRecordRequest;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.core.param.Sort;
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.Arrays;
import java.util.Date;
import java.util.List;

/**
* <p>
* 设备信息对话记录服务类
* </p>
*
* @author wulin
* @since 2023-08-08
*/

@Service
@Slf4j
public class DeviceUserTalkRecordService extends GenericReactiveCrudService<DeviceUserTalkRecordEntity, Long> {


	public Mono<DeviceUserTalkRecordEntity> selectDeviceUserTalkRecordByRequest(DeviceUserTalkRecordRequest request){
		ReactiveQuery<DeviceUserTalkRecordEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getAskNumber() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getAskNumber, request.getAskNumber());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getUserId, request.getUserId());
		}
		if(request.getDeviceId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getDeviceId, request.getDeviceId());
		}
		if(StringUtils.isNotEmpty(request.getAskValue())){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getAskValue, request.getAskValue());
		}
		if(request.getAskType() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getAskType, request.getAskType());
		}
		if(StringUtils.isNotEmpty(request.getAskKey())){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getAskKey, request.getAskKey());
		}
		if(StringUtils.isNotEmpty(request.getAnswerValue())){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getAnswerValue, request.getAnswerValue());
		}
		if(request.getAnswerThirdId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getAnswerThirdId, request.getAnswerThirdId());
		}
		if(StringUtils.isNotEmpty(request.getAnswerThirdValue())){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getAnswerThirdValue, request.getAnswerThirdValue());
		}
		if(request.getIsReturn() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getIsReturn, request.getIsReturn());
		}
		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<DeviceUserTalkRecordEntity>> selectDeviceUserTalkRecordsByRequest(DeviceUserTalkRecordRequest request){
		ReactiveQuery<DeviceUserTalkRecordEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceUserTalkRecordRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceUserTalkRecordRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(DeviceUserTalkRecordRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(DeviceUserTalkRecordRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getAskNumber() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getAskNumber, request.getAskNumber());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getUserId, request.getUserId());
		}
		if(request.getDeviceId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getDeviceId, request.getDeviceId());
		}
		if(StringUtils.isNotEmpty(request.getAskValue())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserTalkRecordRequest::getAskValue, request.getAskValue());
		}
		if(request.getAskType() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getAskType, request.getAskType());
		}
		if(StringUtils.isNotEmpty(request.getAskKey())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserTalkRecordRequest::getAskKey, request.getAskKey());
		}
		if(StringUtils.isNotEmpty(request.getAnswerValue())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserTalkRecordRequest::getAnswerValue, request.getAnswerValue());
		}
		if(request.getAnswerThirdId() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getAnswerThirdId, request.getAnswerThirdId());
		}
		if(StringUtils.isNotEmpty(request.getAnswerThirdValue())){
			reactiveQuery = reactiveQuery.$like$(DeviceUserTalkRecordRequest::getAnswerThirdValue, request.getAnswerThirdValue());
		}
		if(request.getIsReturn() != null){
			reactiveQuery = reactiveQuery.and(DeviceUserTalkRecordRequest::getIsReturn, request.getIsReturn());
		}
		SortOrder sortOrder = null;
		QueryParamEntity param = QueryParamEntity.of(reactiveQuery.getParam());
		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());
			}
			//sortOrder.setColumn(request.getOrder());
			reactiveQuery = reactiveQuery.orderBy(sortOrder);
			Sort sort = new Sort();
			sort.setName(request.getOrder());
			sort.desc();
			param.setSorts(Arrays.asList(sort));
			//List<Sort> sort = new Sort();
		}

		param.setPageIndex(request.getCurrPage());
		param.setPageSize(request.getPageSize());
		param.setPaging(true);
		//param.setSorts();
		param.setFirstPageIndex(1);

		return queryPager(param);
	}



	public Mono<DeviceUserTalkRecordEntity> selectDeviceUserTalkRecordById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateDeviceUserTalkRecordById(DeviceUserTalkRecordEntity entity){
		ReactiveUpdate<DeviceUserTalkRecordEntity> update = createUpdate()
                .set(DeviceUserTalkRecordEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(DeviceUserTalkRecordEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getAskNumber() != null){
			update = update.set(DeviceUserTalkRecordEntity::getAskNumber, entity.getAskNumber());
		}
		if(entity.getUserId() != null){
			update = update.set(DeviceUserTalkRecordEntity::getUserId, entity.getUserId());
		}
		if(entity.getDeviceId() != null){
			update = update.set(DeviceUserTalkRecordEntity::getDeviceId, entity.getDeviceId());
		}
		if(StringUtils.isNotEmpty(entity.getAskValue())){
			update = update.set(DeviceUserTalkRecordEntity::getAskValue, entity.getAskValue());
		}
		if(entity.getAskType() != null){
			update = update.set(DeviceUserTalkRecordEntity::getAskType, entity.getAskType());
		}
		if(StringUtils.isNotEmpty(entity.getAskKey())){
			update = update.set(DeviceUserTalkRecordEntity::getAskKey, entity.getAskKey());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerValue())){
			update = update.set(DeviceUserTalkRecordEntity::getAnswerValue, entity.getAnswerValue());
		}
		if(entity.getAnswerThirdId() != null){
			update = update.set(DeviceUserTalkRecordEntity::getAnswerThirdId, entity.getAnswerThirdId());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerThirdValue())){
			update = update.set(DeviceUserTalkRecordEntity::getAnswerThirdValue, entity.getAnswerThirdValue());
		}
		if(entity.getIsReturn() != null){
			update = update.set(DeviceUserTalkRecordEntity::getIsReturn, entity.getIsReturn());
		}
		return update.where(DeviceUserTalkRecordEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverDeviceUserTalkRecordById(DeviceUserTalkRecordEntity entity){
		ReactiveUpdate<DeviceUserTalkRecordEntity> update = createUpdate()
                .set(DeviceUserTalkRecordEntity::getModifyTime, new Date());
		update = update.set(DeviceUserTalkRecordEntity::getIsDelete, entity.getIsDelete());
		update = update.set(DeviceUserTalkRecordEntity::getAskNumber, entity.getAskNumber());
		update = update.set(DeviceUserTalkRecordEntity::getUserId, entity.getUserId());
		update = update.set(DeviceUserTalkRecordEntity::getDeviceId, entity.getDeviceId());
		update = update.set(DeviceUserTalkRecordEntity::getAskValue, entity.getAskValue());
		update = update.set(DeviceUserTalkRecordEntity::getAskType, entity.getAskType());
		update = update.set(DeviceUserTalkRecordEntity::getAskKey, entity.getAskKey());
		update = update.set(DeviceUserTalkRecordEntity::getAnswerValue, entity.getAnswerValue());
		update = update.set(DeviceUserTalkRecordEntity::getAnswerThirdId, entity.getAnswerThirdId());
		update = update.set(DeviceUserTalkRecordEntity::getAnswerThirdValue, entity.getAnswerThirdValue());
		update = update.set(DeviceUserTalkRecordEntity::getIsReturn, entity.getIsReturn());
		return update.where(DeviceUserTalkRecordEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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



}
