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

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.EquipmentStatusEnum;
import org.springblade.enums.OrderFromEnum;
import org.springblade.enums.WarningStatusEnum;
import org.springblade.etl.entity.EtlRainDataEntity;
import org.springblade.etl.entity.EtlRsvrDataEntity;
import org.springblade.etl.entity.EtlWarningInfoEntity;
import org.springblade.etl.service.*;
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.rtumanage.entity.RtuStatusEntity;
import org.springblade.modules.business.rtumanage.service.IRtuManageService;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.service.IDictBizService;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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 TestDataTask implements Callable<Integer> {

	private RedisTemplate redisTemplate;
	private SimpMessagingTemplate messagingTemplate;
	private KafkaTemplate<String, String> kafkaTemplate;

	private IRtuWarningService rtuWarningService;
	private IRtuBaseInfoService rtuBaseInfoService;
	private IProjectBaseInfoService projectBaseInfoService;
	private IRtuDataGroundService dataGroundService;
	private IRtuDataRainService dataRainService;
	private IRtuDataRsvrService dataRsvrService;
	private IRtuDataRiverService dataRiverService;
	private IDictBizService dictBizService;
	private IRtuManageService manageService;

	private IEtlWarningService etlWarningService;
	private IEtlRainDataService etlRainDataService;
	private IEtlRiverDataService etlRiverDataService;
	private IEtlRsvrDataService etlRsvrDataService;
	private IEtlGroundDataService etlGroundDataService;


	public TestDataTask(RedisTemplate redisTemplate, KafkaTemplate<String, String> kafkaTemplate, SimpMessagingTemplate messagingTemplate, IEtlWarningService etlWarningService, IRtuWarningService rtuWarningService, IRtuBaseInfoService rtuBaseInfoService, IProjectBaseInfoService projectBaseInfoService, IRtuDataRainService dataRainService, IRtuDataRiverService dataRiverService, IRtuDataRsvrService dataRsvrService, IRtuDataGroundService dataGroundService, IDictBizService dictBizService, IRtuManageService manageService, IEtlRainDataService etlRainDataService, IEtlRiverDataService etlRiverDataService, IEtlRsvrDataService etlRsvrDataService, IEtlGroundDataService etlGroundDataService) {
		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.dictBizService = dictBizService;
		this.manageService = manageService;
		this.etlRainDataService = etlRainDataService;
		this.etlRiverDataService = etlRiverDataService;
		this.etlRsvrDataService = etlRsvrDataService;
		this.etlGroundDataService = etlGroundDataService;
	}

	@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) {
					List<DictBiz> kindDict = dictBizService.getList("rtu_kind");
					//List<DictBiz> kindDict = DictBizCache.getList("rtu_kind");
					Calendar calendar = Calendar.getInstance();
					calendar.set(Calendar.MINUTE, 0);
					calendar.set(Calendar.SECOND, 0);
					//生成业务数据
					for (RtuInfoEntity rtuInfoEntity : rtuInfoEntityList) {
						LambdaQueryWrapper<RtuStatusEntity> statusWrapper = Wrappers.<RtuStatusEntity>query().lambda();
						statusWrapper.eq(RtuStatusEntity::getRtuCode, rtuInfoEntity.getRtuCode());
						RtuStatusEntity statusEntity = manageService.getOne(statusWrapper);
						if (null == statusEntity || statusEntity.getRunStatusId() == EquipmentStatusEnum.STATUS_RUN.getCode()) {


							String rtuKinds = rtuInfoEntity.getRtuKinds();
							String[] kinds = rtuKinds.split(",");
							for (String k : kinds) {
								for (DictBiz biz : kindDict) {
									if (biz.getId() == Func.toLong(k)) {
										if (biz.getDictKey().equals("rain")) {
											LambdaQueryWrapper<RtuWarningInfoEntity> warnWrapper = Wrappers.<RtuWarningInfoEntity>query().lambda();
											warnWrapper.eq(RtuWarningInfoEntity::getRtuCode, rtuInfoEntity.getRtuCode());
											warnWrapper.isNull(RtuWarningInfoEntity::getWarningRecoveryTime);
											RtuWarningInfoEntity warningInfoEntity = rtuWarningService.getOne(warnWrapper);


											LambdaQueryWrapper<RtuDataRainEntity> dataWrapper = Wrappers.<RtuDataRainEntity>query().lambda();
											dataWrapper.eq(RtuDataRainEntity::getRtuCode, rtuInfoEntity.getRtuCode());
											RtuDataRainEntity rainEntity = dataRainService.getOne(dataWrapper);
											if (null == rainEntity) {
												rainEntity = new RtuDataRainEntity();
											}

											LambdaQueryWrapper<EtlRainDataEntity> etlWrapper = Wrappers.<EtlRainDataEntity>query().lambda();
											etlWrapper.eq(EtlRainDataEntity::getRtuCode, rtuInfoEntity.getRtuCode());
											etlWrapper.orderByDesc(EtlRainDataEntity::getTm);
											etlWrapper.last("limit 1");
											//java.util.Random random = new java.util.Random();
											//Double drp = random.nextDouble();
											EtlRainDataEntity etlRainDataEntity = etlRainDataService.getOne(etlWrapper);
											if (etlRainDataEntity != null) {
												rainEntity.setDrp(etlRainDataEntity.getDrp());
												rainEntity.setRtuCode(rtuInfoEntity.getRtuCode());
												rainEntity.setTm(calendar.getTime());
												dataRainService.saveOrUpdate(rainEntity);
											}


										} else if (biz.getDictKey().equals("ground")) {
											LambdaQueryWrapper<RtuDataGroundEntity> dataWrapper = Wrappers.<RtuDataGroundEntity>query().lambda();
											dataWrapper.eq(RtuDataGroundEntity::getRtuCode, rtuInfoEntity.getRtuCode());
											RtuDataGroundEntity groundEntity = dataGroundService.getOne(dataWrapper);
											if (null == groundEntity) {
												groundEntity = new RtuDataGroundEntity();
											}


											groundEntity.setRtuCode(rtuInfoEntity.getRtuCode());
											java.util.Random random = new java.util.Random();
											groundEntity.setSrlslm(random.nextDouble());
											groundEntity.setVtavslm(random.nextDouble());
											groundEntity.setSlm10(random.nextDouble());
											groundEntity.setSlm20(random.nextDouble());
											groundEntity.setSlm30(random.nextDouble());
											groundEntity.setSlm40(random.nextDouble());
											groundEntity.setSlm60(random.nextDouble());
											groundEntity.setSlm80(random.nextDouble());
											groundEntity.setSlm100(random.nextDouble());

											groundEntity.setTm(calendar.getTime());
											//	dataGroundService.saveOrUpdate(groundEntity);


										} else if (biz.getDictKey().equals("river")) {
											LambdaQueryWrapper<RtuDataRiverEntity> dataWrapper = Wrappers.<RtuDataRiverEntity>query().lambda();
											dataWrapper.eq(RtuDataRiverEntity::getRtuCode, rtuInfoEntity.getRtuCode());
											RtuDataRiverEntity riverEntity = dataRiverService.getOne(dataWrapper);
											if (null == riverEntity) {
												riverEntity = new RtuDataRiverEntity();
											}


											riverEntity.setRtuCode(rtuInfoEntity.getRtuCode());
											java.util.Random random = new java.util.Random();
											riverEntity.setZ(random.nextDouble());
											riverEntity.setTm(calendar.getTime());
											//	dataRiverService.saveOrUpdate(riverEntity);


										} else if (biz.getDictKey().equals("rsvr")) {
											LambdaQueryWrapper<RtuDataRsvrEntity> dataWrapper = Wrappers.<RtuDataRsvrEntity>query().lambda();
											dataWrapper.eq(RtuDataRsvrEntity::getRtuCode, rtuInfoEntity.getRtuCode());
											RtuDataRsvrEntity rsvrEntity = dataRsvrService.getOne(dataWrapper);
											if (null == rsvrEntity) {
												rsvrEntity = new RtuDataRsvrEntity();
											}

											rsvrEntity.setRtuCode(rtuInfoEntity.getRtuCode());


											//	java.util.Random random = new java.util.Random();
											//	rsvrEntity.setRz(random.nextDouble());

											LambdaQueryWrapper<EtlRsvrDataEntity> etlWrapper = Wrappers.<EtlRsvrDataEntity>query().lambda();
											etlWrapper.eq(EtlRsvrDataEntity::getRtuCode, rtuInfoEntity.getRtuCode());
											etlWrapper.orderByDesc(EtlRsvrDataEntity::getTm);
											etlWrapper.last("limit 1");
											//java.util.Random random = new java.util.Random();
											//Double drp = random.nextDouble();
											EtlRsvrDataEntity etlRsvrDataEntity = etlRsvrDataService.getOne(etlWrapper);
											if (etlRsvrDataEntity != null) {
												rsvrEntity.setRz(etlRsvrDataEntity.getRz());
												rsvrEntity.setTm(calendar.getTime());
												dataRsvrService.saveOrUpdate(rsvrEntity);
											}


										}
										break;
									}
								}
							}

							RtuStatusEntity newStatus = new RtuStatusEntity();
							newStatus.setRunStatusId(EquipmentStatusEnum.STATUS_RUN.getCode());
							newStatus.setRtuCode(rtuInfoEntity.getRtuCode());
							newStatus.setLastUpTime(calendar.getTime());
							kafkaTemplate.send("ywxt.rtu.status", JsonUtil.toJson(newStatus));

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

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