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

import cn.hutool.core.date.DateUtil;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.rtu.base.service.IRtuBaseInfoService;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;


import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
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 HomeInfoCountTask implements Callable<Integer> {

	private RedisTemplate redisTemplate;
	private IRtuBaseInfoService rtuBaseInfoService;
	private IRtuWarningService rtuWarningService;
	private SimpMessagingTemplate messagingTemplate;
	public HomeInfoCountTask(RedisTemplate redisTemplate, IRtuBaseInfoService rtuBaseInfoService, IRtuWarningService rtuWarningService, SimpMessagingTemplate messagingTemplate){
		this.redisTemplate=redisTemplate;
		this.rtuBaseInfoService=rtuBaseInfoService;
		this.rtuWarningService=rtuWarningService;
		this.messagingTemplate=messagingTemplate;
	}

	@Override
	public Integer call() throws Exception {
		try {
			Date startTm = new Date();
			String timeStartText = DateUtil.format(startTm, "yyyy年MM月dd日 HH时mm分ss秒");


			HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();

			long rtus =rtuBaseInfoService.count();

			opsHash.put("real_data_info","rtus",Long.toString(rtus));


			LambdaUpdateWrapper<RtuWarningInfoEntity> updateWrapper = Wrappers.<RtuWarningInfoEntity>update().lambda();
			updateWrapper.eq(RtuWarningInfoEntity::getIsDeleted, 0);
			updateWrapper.eq(RtuWarningInfoEntity::getWarningStatus, 0);
			long warningRtus=rtuWarningService.count(updateWrapper);

			opsHash.put("real_data_info","warningRtus",Long.toString(warningRtus));

			opsHash.put("real_data_info","delayOrders","1");
			opsHash.put("real_data_info","equipmentInspectionCount","0");


			JSONObject info = new JSONObject();
			info.put("rtus",rtus);
			info.put("warningRtus",warningRtus);
			info.put("delayOrders",0);
			info.put("equipmentInspectionCount",0);

		//	messagingTemplate.convertAndSend("real_data_info", info.toJSONString());

			Date endTime = new Date();
			log.info("实时数据统计任务结束 用时：{} 毫秒", (endTime.getTime() - startTm.getTime()));
		} catch (Exception e) {
			log.error("实时数据统计任务异常 {}", e.getMessage());
		}
		return 0;
	}
}
