/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称:
 * 创建日期:2023/8/4
 */
package org.springblade.jobtask;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.springblade.etl.entity.EtlGroundDataEntity;
import org.springblade.etl.entity.EtlRainDataEntity;
import org.springblade.etl.entity.EtlRiverDataEntity;
import org.springblade.etl.entity.EtlRsvrDataEntity;
import org.springblade.modules.baseinfo.rtu.dto.RtuInfoDTO;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/***
 * Date:2023/8/4
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
@Component
@EnableScheduling
public class TestDataProcessor {


	private static ThreadFactory testDataThreadFactory = new ThreadFactoryBuilder().setNameFormat("test-data-task-thread-pool-%d").build();
	private static ExecutorService testDataThreadPool = new ThreadPoolExecutor(1, 2,
		0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), testDataThreadFactory, new ThreadPoolExecutor.AbortPolicy());

	@Value("${spring.task-config.test-task}")
	private boolean testTaskRun;

	@Resource
	private KafkaTemplate<String, String> kafkaTemplate;

	@Resource
	private IRtuBaseInfoService rtuBaseInfoService;

	/**
	 * 测试线程，5分钟执行一次
	 */
	@Scheduled(cron = "5 0/5 * * * * ")
	public void testMinTask() {
		try {
			if (testTaskRun) {
//				Calendar calendar = Calendar.getInstance();
//				int min = calendar.get(Calendar.MINUTE);
//				if (min > 0) {
//					TestDataTask warningTask = new TestDataTask(new Date());
//					FutureTask<Integer> futureTask = new FutureTask<>(warningTask);
//					testDataThreadPool.execute(futureTask);
//				}
			}
		} catch (Exception e) {
			log.error("{}", e.getMessage());
		}
	}

	/**
	 * 测试线程，1小时执行一次
	 */
	@Scheduled(cron = "10 0 0/1 * * * ")
	public void testHourTask() {
		try {
			if (testTaskRun) {
//				TestDataHourTask warningTask = new TestDataHourTask(new Date());
//				FutureTask<Integer> futureTask = new FutureTask<>(warningTask);
//				testDataThreadPool.execute(futureTask);
			}
		} catch (Exception e) {
			log.error("{}", e.getMessage());
		}
	}

	private class TestDataTask implements Callable<Integer> {
		private Date dt;

		public TestDataTask(Date dt) {
			this.dt = dt;
		}

		@Override
		public Integer call() throws Exception {
			try {
				LambdaQueryWrapper<RtuInfoEntity> dataWrapper = Wrappers.<RtuInfoEntity>query().lambda();
				dataWrapper.eq(RtuInfoEntity::getIsDeleted, 0);
				List<RtuInfoEntity> list = rtuBaseInfoService.list(dataWrapper);
				for (RtuInfoEntity entity : list) {
					//log.info("test data {}", entity.getRtuCode());
//					if (entity.getRtuCode().equals("01718525") || entity.getRtuCode().equals("01718530") || entity.getRtuCode().equals("01718550") || entity.getRtuCode().equals("01740030")) {
//						continue;
//					}
//					if (entity.getRtuCode().equals("01740040") || entity.getRtuCode().equals("01740110") || entity.getRtuCode().equals("01740150") || entity.getRtuCode().equals("01740160")) {
//						continue;
//					}

					if (entity.getIsRain() != null && entity.getIsRain() == 1) {
						String topic = "topic-etl-rain-data";
						EtlRainDataEntity lastRainDataEntity = new EtlRainDataEntity();
						lastRainDataEntity.setRtuCode(entity.getRtuCode());

						lastRainDataEntity.setDrp(1.5);
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(dt);
						calendar.set(Calendar.SECOND, 0);
						//	lastRainDataEntity.setTm(calendar.getTime());
//						if (entity.getRtuCode().equals("01740330")) {
//							calendar.set(Calendar.SECOND, 1);
//							lastRainDataEntity.setFromDate(calendar.getTime());
//							calendar.set(Calendar.SECOND, 0);
//							calendar.add(Calendar.MINUTE, -15);
//							lastRainDataEntity.setTm(calendar.getTime());
//						} else if (entity.getRtuCode().equals("40517675")) {
//							calendar.set(Calendar.SECOND, 1);
//							lastRainDataEntity.setFromDate(calendar.getTime());
//							calendar.set(Calendar.SECOND, 0);
//							calendar.add(Calendar.MINUTE, -20);
//							lastRainDataEntity.setTm(calendar.getTime());
//						} else if (entity.getRtuCode().equals("01751240")) {
//							calendar.set(Calendar.SECOND, 1);
//							lastRainDataEntity.setFromDate(calendar.getTime());
//							calendar.set(Calendar.SECOND, 0);
//							calendar.add(Calendar.MINUTE, -20);
//							lastRainDataEntity.setTm(calendar.getTime());
//						} else {
						lastRainDataEntity.setTm(calendar.getTime());
						calendar.set(Calendar.SECOND, 1);
						lastRainDataEntity.setFromDate(calendar.getTime());
						//}
//						lastRainDataEntity.setTm(calendar.getTime());
//						calendar.set(Calendar.SECOND, 1);
//						lastRainDataEntity.setFromDate(calendar.getTime());

						lastRainDataEntity.setIntv(0.05);
						//通过KAFKA发送最新一条数据
						String jsonText = JSONObject.toJSONString(lastRainDataEntity);
						kafkaTemplate.send(topic, jsonText);
					}
					if (entity.getIsRes() != null && entity.getIsRes() == 1) {
						String topic = "topic-etl-rsvr-data";
						EtlRsvrDataEntity rsvrDataEntity = new EtlRsvrDataEntity();
						rsvrDataEntity.setRtuCode(entity.getRtuCode());

						rsvrDataEntity.setRz(15.0);
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(dt);
						calendar.set(Calendar.SECOND, 0);


						rsvrDataEntity.setTm(calendar.getTime());
						calendar.set(Calendar.SECOND, 1);
						rsvrDataEntity.setFromDate(calendar.getTime());


						//通过KAFKA发送最新一条数据
						String jsonText = JSONObject.toJSONString(rsvrDataEntity);
						kafkaTemplate.send(topic, jsonText);
					}
					if (entity.getIsRiver() != null && entity.getIsRiver() == 1) {
						String topic = "topic-etl-river-data";
						EtlRiverDataEntity riverDataEntity = new EtlRiverDataEntity();
						riverDataEntity.setRtuCode(entity.getRtuCode());

						riverDataEntity.setZ(1.5);
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(dt);
						calendar.set(Calendar.SECOND, 0);

						;
						riverDataEntity.setTm(calendar.getTime());
						calendar.set(Calendar.SECOND, 1);
						riverDataEntity.setFromDate(calendar.getTime());


						//通过KAFKA发送最新一条数据
						String jsonText = JSONObject.toJSONString(riverDataEntity);
						kafkaTemplate.send(topic, jsonText);
					}
				}

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


	private class TestDataHourTask implements Callable<Integer> {
		private Date dt;

		public TestDataHourTask(Date dt) {
			this.dt = dt;
		}

		@Override
		public Integer call() throws Exception {
			try {
				RtuInfoDTO dto = new RtuInfoDTO();
				List<RtuInfoEntity> list = rtuBaseInfoService.rtuList(dto);
				for (RtuInfoEntity entity : list) {
//					if (entity.getRtuCode().equals("01718525") || entity.getRtuCode().equals("01718530") || entity.getRtuCode().equals("01718550") || entity.getRtuCode().equals("01740030")) {
//						continue;
//					}
//					if (entity.getRtuCode().equals("01740040") || entity.getRtuCode().equals("01740110") || entity.getRtuCode().equals("01740150") || entity.getRtuCode().equals("01740160")) {
//						continue;
//					}
//					if (entity.getRtuCode().equals("01717200")) {
//						log.info("01717200");
//					}
					if (entity.getIsRain() != null && entity.getIsRain() == 1) {
						String topic = "topic-etl-rain-data";
						EtlRainDataEntity lastRainDataEntity = new EtlRainDataEntity();
						lastRainDataEntity.setRtuCode(entity.getRtuCode());
						lastRainDataEntity.setDrp(1.5);
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(dt);
						calendar.set(Calendar.SECOND, 0);
						calendar.set(Calendar.MINUTE, 0);
//						if (entity.getRtuCode().equals("40509005")) {
//							calendar.set(Calendar.SECOND, 1);
//							lastRainDataEntity.setFromDate(calendar.getTime());
//
//
//							calendar.add(Calendar.HOUR_OF_DAY, -1);
//							calendar.add(Calendar.MINUTE, -1);
//							lastRainDataEntity.setTm(calendar.getTime());
//
//						} else {
						lastRainDataEntity.setTm(calendar.getTime());
						calendar.set(Calendar.SECOND, 1);
						lastRainDataEntity.setFromDate(calendar.getTime());
						//}
						lastRainDataEntity.setIntv(1.0);
						//通过KAFKA发送最新一条数据
						String jsonText = JSONObject.toJSONString(lastRainDataEntity);
						kafkaTemplate.send(topic, jsonText);
					}
					if (entity.getIsRes() != null && entity.getIsRes() == 1) {
						String topic = "topic-etl-rsvr-data";
						EtlRsvrDataEntity rsvrDataEntity = new EtlRsvrDataEntity();
						rsvrDataEntity.setRtuCode(entity.getRtuCode());
						rsvrDataEntity.setRz(15.0);
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(dt);
						calendar.set(Calendar.SECOND, 0);
						calendar.set(Calendar.MINUTE, 0);
//						if (entity.getRtuCode().equals("10001001")) {
//							calendar.set(Calendar.SECOND, 1);
//							rsvrDataEntity.setFromDate(calendar.getTime());
//
//							calendar.add(Calendar.HOUR_OF_DAY, -1);
//							calendar.add(Calendar.MINUTE, -1);
//							rsvrDataEntity.setTm(calendar.getTime());
//
//						}
//						 if (entity.getRtuCode().equals("01710140")) {
//							calendar.set(Calendar.SECOND, 1);
//							rsvrDataEntity.setFromDate(calendar.getTime());
//
//							calendar.add(Calendar.HOUR_OF_DAY, -1);
//							calendar.add(Calendar.MINUTE, -1);
//							rsvrDataEntity.setTm(calendar.getTime());
//
//
//						} else {
						rsvrDataEntity.setTm(calendar.getTime());
						calendar.set(Calendar.SECOND, 1);
						rsvrDataEntity.setFromDate(calendar.getTime());
						//}

						//通过KAFKA发送最新一条数据
						String jsonText = JSONObject.toJSONString(rsvrDataEntity);
						kafkaTemplate.send(topic, jsonText);
					}
					if (entity.getIsRiver() != null && entity.getIsRiver() == 1) {
						String topic = "topic-etl-river-data";
						EtlRiverDataEntity riverDataEntity = new EtlRiverDataEntity();
						riverDataEntity.setRtuCode(entity.getRtuCode());
						riverDataEntity.setZ(1.5);
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(dt);
						calendar.set(Calendar.SECOND, 0);
						calendar.set(Calendar.MINUTE, 0);
//						if (entity.getRtuCode().equals("11300640")) {
//							calendar.set(Calendar.SECOND, 1);
//							riverDataEntity.setFromDate(calendar.getTime());
//
//
//							calendar.add(Calendar.HOUR_OF_DAY, -1);
//							calendar.add(Calendar.MINUTE, -1);
//							riverDataEntity.setTm(calendar.getTime());
//
//						} else if (entity.getRtuCode().equals("10215700")) {
//							calendar.set(Calendar.SECOND, 1);
//							riverDataEntity.setFromDate(calendar.getTime());
//
//
//							calendar.add(Calendar.HOUR_OF_DAY, -1);
//							calendar.add(Calendar.MINUTE, -1);
//							riverDataEntity.setTm(calendar.getTime());
//
//						} else {
						riverDataEntity.setTm(calendar.getTime());
						calendar.set(Calendar.SECOND, 1);
						riverDataEntity.setFromDate(calendar.getTime());
						//	}

						//通过KAFKA发送最新一条数据
						String jsonText = JSONObject.toJSONString(riverDataEntity);
						kafkaTemplate.send(topic, jsonText);
					}
					if (entity.getIsGround() != null && entity.getIsGround() == 1) {
						String topic = "topic-etl-ground-data";
						EtlGroundDataEntity groundDataEntity = new EtlGroundDataEntity();
						groundDataEntity.setRtuCode(entity.getRtuCode());
						groundDataEntity.setMoco10cm(23.5);
						groundDataEntity.setMoco20cm(20.5);
						groundDataEntity.setMoco30cm(12.5);
						groundDataEntity.setMoco40cm(11.5);
						groundDataEntity.setMoco50cm(10.5);
						groundDataEntity.setMoco60cm(3.5);
						groundDataEntity.setMoco80cm(3.2);
						groundDataEntity.setMoco100cm(3.0);
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(dt);
						calendar.set(Calendar.SECOND, 0);
						calendar.set(Calendar.MINUTE, 0);
//						if (entity.getRtuCode().equals("102A0020")) {
//							calendar.set(Calendar.SECOND, 1);
//							groundDataEntity.setFromDate(calendar.getTime());
//
//							calendar.add(Calendar.HOUR_OF_DAY, -1);
//							calendar.add(Calendar.MINUTE, -1);
//							groundDataEntity.setTm(calendar.getTime());
//
//						} else {
						groundDataEntity.setTm(calendar.getTime());
						calendar.set(Calendar.SECOND, 1);
						groundDataEntity.setFromDate(calendar.getTime());
						//	}

						//通过KAFKA发送最新一条数据
						String jsonText = JSONObject.toJSONString(groundDataEntity);
						kafkaTemplate.send(topic, jsonText);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.error("测试数据任务异常 {}", e.getMessage());
			}
			return 0;
		}
	}
}
