/**
 * Copyright
 * All right reserved.
 * 项目名称:
 * 创建日期:2022/5/22
 */
package org.springblade.etl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.cache.DictBizCache;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.enums.WarningStatusEnum;
import org.springblade.etl.service.IEtlWarningService;
import org.springblade.modules.business.project.base.entity.ProjectInfoEntity;
import org.springblade.modules.business.project.base.service.IProjectBaseInfoService;
import org.springblade.modules.business.rtu.base.entity.RtuInfoEntity;
import org.springblade.modules.business.rtu.base.service.IRtuBaseInfoService;
import org.springblade.modules.business.rtu.data.entity.RtuDataGroundEntity;
import org.springblade.modules.business.rtu.data.entity.RtuDataRainEntity;
import org.springblade.modules.business.rtu.data.entity.RtuDataRiverEntity;
import org.springblade.modules.business.rtu.data.entity.RtuDataRsvrEntity;
import org.springblade.modules.business.rtu.data.service.IRtuDataGroundService;
import org.springblade.modules.business.rtu.data.service.IRtuDataRainService;
import org.springblade.modules.business.rtu.data.service.IRtuDataRiverService;
import org.springblade.modules.business.rtu.data.service.IRtuDataRsvrService;
import org.springblade.modules.business.warning.entity.OriginalWarningInfoEntity;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.service.IOriginalWarningService;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springblade.modules.system.entity.DictBiz;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

/***
 * Date:2022/5/22
 * Title: 环境辐射与气象监测系统(KRS) 模块
 * Description：MODBUS TCP 协议传输 ,读取任务
 * @author swp
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class TestWarnTask implements Callable<Integer> {

	private RedisTemplate redisTemplate;
	private SimpMessagingTemplate messagingTemplate;
	private KafkaTemplate<String, String> kafkaTemplate;
	private IEtlWarningService etlWarningService;
	private IRtuWarningService rtuWarningService;
	private IRtuBaseInfoService rtuBaseInfoService;
	private IProjectBaseInfoService projectBaseInfoService;
	private IRtuDataGroundService dataGroundService;
	private IRtuDataRainService dataRainService;
	private IRtuDataRsvrService dataRsvrService;
	private IRtuDataRiverService dataRiverService;
	private IOriginalWarningService originalWarningService;

	public TestWarnTask(RedisTemplate redisTemplate, KafkaTemplate<String, String> kafkaTemplate, SimpMessagingTemplate messagingTemplate, IEtlWarningService etlWarningService, IRtuWarningService rtuWarningService, IRtuBaseInfoService rtuBaseInfoService, IProjectBaseInfoService projectBaseInfoService, IRtuDataRainService dataRainService, IRtuDataRiverService dataRiverService, IRtuDataRsvrService dataRsvrService, IRtuDataGroundService dataGroundService,IOriginalWarningService originalWarningService) {
		this.redisTemplate = redisTemplate;
		this.kafkaTemplate = kafkaTemplate;
		this.messagingTemplate = messagingTemplate;
		this.etlWarningService = etlWarningService;
		this.rtuWarningService = rtuWarningService;
		this.rtuBaseInfoService = rtuBaseInfoService;
		this.projectBaseInfoService = projectBaseInfoService;
		this.dataGroundService = dataGroundService;
		this.dataRainService = dataRainService;
		this.dataRiverService = dataRiverService;
		this.dataRsvrService = dataRsvrService;
		this.originalWarningService=originalWarningService;
	}

	@Override
	public Integer call() {
		try {
			LambdaQueryWrapper<ProjectInfoEntity> wrapper = Wrappers.<ProjectInfoEntity>query().lambda();
			wrapper.eq(ProjectInfoEntity::getIsDeleted, 0);
			List<ProjectInfoEntity> projectInfoEntityList = projectBaseInfoService.list(wrapper);
			for (ProjectInfoEntity projectInfoEntity : projectInfoEntityList) {
				LambdaQueryWrapper<RtuInfoEntity> rtuWrapper = Wrappers.<RtuInfoEntity>query().lambda();
				rtuWrapper.eq(RtuInfoEntity::getProjectId, projectInfoEntity.getId());
				rtuWrapper.orderByAsc(RtuInfoEntity::getRtuCode);
				List<RtuInfoEntity> rtuInfoEntityList = rtuBaseInfoService.list(rtuWrapper);
				if (null != rtuInfoEntityList && rtuInfoEntityList.size() > 0) {
					//生成预警信息
					while (true) {
						java.util.Random random = new java.util.Random();
						int index = random.nextInt(rtuInfoEntityList.size());
						RtuInfoEntity toWarnRtuEntity = rtuInfoEntityList.get(index);

						LambdaQueryWrapper<RtuWarningInfoEntity> warnWrapper = Wrappers.<RtuWarningInfoEntity>query().lambda();
						warnWrapper.eq(RtuWarningInfoEntity::getRtuCode, toWarnRtuEntity.getRtuCode());
						warnWrapper.eq(RtuWarningInfoEntity::getIsDeleted, 0);
						warnWrapper.isNull(RtuWarningInfoEntity::getWarningRecoveryTime);

						RtuWarningInfoEntity entity = rtuWarningService.getOne(warnWrapper);
						if (null == entity) {

							OriginalWarningInfoEntity originalWarningInfoEntity=new OriginalWarningInfoEntity();
							originalWarningInfoEntity.setRtuCode(toWarnRtuEntity.getRtuCode());
							originalWarningInfoEntity.setWarnName(toWarnRtuEntity.getRtuName()+"-预警");
							originalWarningInfoEntity.setWarnTime(new Date());
							originalWarningInfoEntity.setWarnLevelCode("严重");
							originalWarningInfoEntity.setWarnInfo("降水量超限预警");
							originalWarningInfoEntity.setWarnStatus(5);
							originalWarningInfoEntity.setReviewStatus("2");
							originalWarningInfoEntity.setType("1");
							originalWarningInfoEntity.setRainWarnRate(1);
							originalWarningInfoEntity.setWarnSort("监测雨量");
							originalWarningInfoEntity.setWarnLocation("全市");
							originalWarningInfoEntity.setWarnAdcd("全市");
							originalWarningInfoEntity.setUserAdcd("全市");
							originalWarningInfoEntity.setWarnLgtd("116.727371");
							originalWarningInfoEntity.setWarnLttd("39.906744");
							originalWarningInfoEntity.setIntv("1小时");




							originalWarningInfoEntity.setDrp(random.nextDouble());
							originalWarningInfoEntity.setZ(random.nextDouble());
							originalWarningInfoEntity.setQ(random.nextDouble());
							originalWarningInfoEntity.setWptn("水势");
							originalWarningInfoEntity.setRz(random.nextDouble());

							originalWarningInfoEntity.setRwptn("水势");

							originalWarningInfoEntity.setInq(random.nextDouble());

							originalWarningService.save(originalWarningInfoEntity);

							RtuWarningInfoEntity rtuWarningInfoEntity = new RtuWarningInfoEntity();
							rtuWarningInfoEntity.setWarningKind("监测雨量");
							rtuWarningInfoEntity.setWarningDesc("降水量超限预警");
							rtuWarningInfoEntity.setRtuCode(toWarnRtuEntity.getRtuCode());
							rtuWarningInfoEntity.setWarningHappenTime(new Date());
							rtuWarningInfoEntity.setDataFromId(originalWarningInfoEntity.getId());
							rtuWarningInfoEntity.setWarningStatus(WarningStatusEnum.STATUS_HAPPEN.getCode());

							rtuWarningService.save(rtuWarningInfoEntity);

							kafkaTemplate.send("ywxt.warning", JsonUtil.toJson(rtuWarningInfoEntity));


							break;
						}
					}

				}

			}
			String timeStartText = DateUtil.format(new Date(), "yyyy年MM月dd日 HH时mm分ss秒");
			log.info("模似预警生成{}", timeStartText);

		} catch (Exception e) {
			log.error("模似预警务异常 {}", e.getMessage());
		}
		return 0;
	}
}
