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


import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.system.SystemTalkAnswerConfigEntity;
import com.qiuguo.iot.data.request.system.SystemTalkAnswerConfigRequest;
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 org.springframework.web.bind.annotation.InitBinder;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

/**
* <p>
* 问答配置（问答关键字匹配，该中未匹配到的才会调用第三方语言模型）服务类
* </p>
*
* @author wulin
* @since 2023-08-08
*/

@Service
@Slf4j
public class SystemTalkAnswerConfigService extends GenericReactiveCrudService<SystemTalkAnswerConfigEntity, Long> {

	private static Integer MAX_COUT = 1000;
	//内存中缓存这些数据，正常这些数据不会很多
	private static ConcurrentHashMap<String, SystemTalkAnswerConfigEntity> group = new ConcurrentHashMap<>();

	@PostConstruct
	public void initGroup() {
		log.info("初始化自定义回答缓存数据");
		group.clear();
		ReactiveQuery<SystemTalkAnswerConfigEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);

		reactiveQuery.paging(0, MAX_COUT.intValue()).fetch().map(item ->{
			group.put(item.getAskKey(), item);
			return item;
		}).doFinally(signalType -> {
			log.info("配置最多读取{}条，实际读取了{}条自定义回答指令", MAX_COUT, group.size());
		}).subscribeOn(Schedulers.single()).subscribe();

	}


	public Mono<SystemTalkAnswerConfigEntity> selectSystemTalkAnswerConfigByRequest(SystemTalkAnswerConfigRequest request){
		ReactiveQuery<SystemTalkAnswerConfigEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTime() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getCreateTime, request.getCreateTime());
		}
		if(request.getModifyTime() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getModifyTime, request.getModifyTime());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getUserId, request.getUserId());
		}
		if(StringUtils.isNotEmpty(request.getAskKey())){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAskKey, request.getAskKey());
		}
		if(StringUtils.isNotEmpty(request.getAnswerValue())){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAnswerValue, request.getAnswerValue());
		}
		if(StringUtils.isNotEmpty(request.getAnswerValueFaild())){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAnswerValueFaild, request.getAnswerValueFaild());
		}
		if(StringUtils.isNotEmpty(request.getAnswerAction())){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAnswerAction, request.getAnswerAction());
		}
		if(StringUtils.isNotEmpty(request.getAnswerActionFaild())){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAnswerActionFaild, request.getAnswerActionFaild());
		}
		if(StringUtils.isNotEmpty(request.getAnswerBackSound())){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAnswerBackSound, request.getAnswerBackSound());
		}
		if(StringUtils.isNotEmpty(request.getAnswerBackImg())){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAnswerBackImg, request.getAnswerBackImg());
		}
		if(request.getKeyOrder() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getKeyOrder, request.getKeyOrder());
		}
		if(request.getAnswerType() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAnswerType, request.getAnswerType());
		}
		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<SystemTalkAnswerConfigEntity>> selectSystemTalkAnswerConfigsByRequest(SystemTalkAnswerConfigRequest request){
		ReactiveQuery<SystemTalkAnswerConfigEntity> reactiveQuery = createQuery();
		reactiveQuery = reactiveQuery.and("is_delete", 0);
		if(request.getId() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getId, request.getId());
		}
		if(request.getIsDelete() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getIsDelete, request.getIsDelete());
		}
		if(request.getCreateTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(SystemTalkAnswerConfigRequest::getCreateTime, request.getCreateTimeStart());
		}
		if(request.getCreateTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(SystemTalkAnswerConfigRequest::getCreateTime, request.getCreateTimeEnd());
		}
		if(request.getModifyTimeStart() != null){
			reactiveQuery = reactiveQuery.gte(SystemTalkAnswerConfigRequest::getModifyTime, request.getModifyTimeStart());
		}
		if(request.getModifyTimeEnd() != null){
			reactiveQuery = reactiveQuery.lte(SystemTalkAnswerConfigRequest::getModifyTime, request.getModifyTimeEnd());
		}
		if(request.getUserId() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getUserId, request.getUserId());
		}
		if(StringUtils.isNotEmpty(request.getAskKey())){
			reactiveQuery = reactiveQuery.$like$(SystemTalkAnswerConfigRequest::getAskKey, request.getAskKey());
		}
		if(StringUtils.isNotEmpty(request.getAnswerValue())){
			reactiveQuery = reactiveQuery.$like$(SystemTalkAnswerConfigRequest::getAnswerValue, request.getAnswerValue());
		}
		if(StringUtils.isNotEmpty(request.getAnswerValueFaild())){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAnswerValueFaild, request.getAnswerValueFaild());
		}
		if(StringUtils.isNotEmpty(request.getAnswerAction())){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAnswerAction, request.getAnswerAction());
		}
		if(StringUtils.isNotEmpty(request.getAnswerActionFaild())){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAnswerActionFaild, request.getAnswerActionFaild());
		}
		if(StringUtils.isNotEmpty(request.getAnswerBackSound())){
			reactiveQuery = reactiveQuery.$like$(SystemTalkAnswerConfigRequest::getAnswerBackSound, request.getAnswerBackSound());
		}
		if(StringUtils.isNotEmpty(request.getAnswerBackImg())){
			reactiveQuery = reactiveQuery.$like$(SystemTalkAnswerConfigRequest::getAnswerBackImg, request.getAnswerBackImg());
		}
		if(request.getAnswerType() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getAnswerType, request.getAnswerType());
		}
		if(request.getKeyOrder() != null){
			reactiveQuery = reactiveQuery.and(SystemTalkAnswerConfigRequest::getKeyOrder, request.getKeyOrder());
		}
		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<SystemTalkAnswerConfigEntity> selectSystemTalkAnswerConfigById(Long id){
		return createQuery()
				.and("is_delete", 0)
				.and("id", id)
				.fetchOne();
	}



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



	public Mono<Integer> updateSystemTalkAnswerConfigById(SystemTalkAnswerConfigEntity entity){
		ReactiveUpdate<SystemTalkAnswerConfigEntity> update = createUpdate()
                .set(SystemTalkAnswerConfigEntity::getModifyTime, new Date());
		if(entity.getIsDelete() != null){
			update = update.set(SystemTalkAnswerConfigEntity::getIsDelete, entity.getIsDelete());
		}
		if(entity.getUserId() != null){
			update = update.set(SystemTalkAnswerConfigEntity::getUserId, entity.getUserId());
		}
		if(StringUtils.isNotEmpty(entity.getAskKey())){
			update = update.set(SystemTalkAnswerConfigEntity::getAskKey, entity.getAskKey());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerValue())){
			update = update.set(SystemTalkAnswerConfigEntity::getAnswerValue, entity.getAnswerValue());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerAction())){
			update = update.set(SystemTalkAnswerConfigEntity::getAnswerAction, entity.getAnswerAction());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerValueFaild())){
			update = update.set(SystemTalkAnswerConfigEntity::getAnswerValueFaild, entity.getAnswerValueFaild());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerActionFaild())){
			update = update.set(SystemTalkAnswerConfigEntity::getAnswerActionFaild, entity.getAnswerActionFaild());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerBackSound())){
			update = update.set(SystemTalkAnswerConfigEntity::getAnswerBackSound, entity.getAnswerBackSound());
		}
		if(StringUtils.isNotEmpty(entity.getAnswerBackImg())){
			update = update.set(SystemTalkAnswerConfigEntity::getAnswerBackImg, entity.getAnswerBackImg());
		}
		if(entity.getKeyOrder() != null){
			update = update.set(SystemTalkAnswerConfigEntity::getKeyOrder, entity.getKeyOrder());
		}
		if(entity.getAnswerType() != null){
			update = update.set(SystemTalkAnswerConfigEntity::getAnswerType, entity.getAnswerType());
		}
		return update.where(SystemTalkAnswerConfigEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



	public Mono<Integer> updateCoverSystemTalkAnswerConfigById(SystemTalkAnswerConfigEntity entity){
		ReactiveUpdate<SystemTalkAnswerConfigEntity> update = createUpdate()
                .set(SystemTalkAnswerConfigEntity::getModifyTime, new Date());
		update = update.set(SystemTalkAnswerConfigEntity::getIsDelete, entity.getIsDelete());
		update = update.set(SystemTalkAnswerConfigEntity::getUserId, entity.getUserId());
		update = update.set(SystemTalkAnswerConfigEntity::getAskKey, entity.getAskKey());
		update = update.set(SystemTalkAnswerConfigEntity::getAnswerValue, entity.getAnswerValue());
		update = update.set(SystemTalkAnswerConfigEntity::getAnswerAction, entity.getAnswerAction());
		update = update.set(SystemTalkAnswerConfigEntity::getAnswerValueFaild, entity.getAnswerValueFaild());
		update = update.set(SystemTalkAnswerConfigEntity::getAnswerActionFaild, entity.getAnswerActionFaild());
		update = update.set(SystemTalkAnswerConfigEntity::getAnswerBackSound, entity.getAnswerBackSound());
		update = update.set(SystemTalkAnswerConfigEntity::getAnswerBackImg, entity.getAnswerBackImg());
		update = update.set(SystemTalkAnswerConfigEntity::getKeyOrder, entity.getKeyOrder());
		update = update.set(SystemTalkAnswerConfigEntity::getAnswerType, entity.getAnswerType());
		return update.where(SystemTalkAnswerConfigEntity::getId, entity.getId()).and("is_delete", 0).execute();
	}



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


    public SystemTalkAnswerConfigEntity getSystemTalkWithKey(String key) {
		if(group.containsKey(key)){
			return group.get(key);
		}
		return null;

    }
}
