package org.ts.ddcs.worker;

import com.alibaba.fastjson.JSON;
import org.ts.ddcs.entity.ProjectWarningSettingInfo;
import org.ts.ddcs.entity.RtuStatusEntity;
import org.ts.ddcs.entity.RtuWarningSettingInfo;
import org.ts.ddcs.entity.WarningInfo;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.tool.WarningHelper;
import org.ts.ddcs.utils.GBConstants;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Callable;


/**
 * @author dylan
 */
public class RtuStatusCheckTask implements Callable<Integer> {

    private String jsonBody;

    RtuStatusCheckTask(String jsonBody) {
        this.jsonBody = jsonBody;
    }

    @Override
    public Integer call() throws Exception {
        try {
            RedisTemplate redisTemplate = DataProcessHolder.newInstance().dataProcess.redisTemplate;
            HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
            Date dt = new Date();
            SimpleDateFormat timeFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           // LogHelper.info("rtu static check: " + timeFormat2.format(dt.getTime()));
            LogHelper.info(jsonBody);
            RtuStatusEntity rtuStatusEntity = JSON.parseObject(jsonBody, RtuStatusEntity.class);
            String rtuCode = rtuStatusEntity.getStcd();
            if (null == rtuCode) {
                return 0;
            }
            String projectId = opsHash.get(rtuCode, "projectId");
            if (null == projectId || projectId.equals("")) {//没有配置项目的测站不进行检测
                return 0;
            }
            /**检查所属项目配置,规则：项目预警开关配置需要显式配置为开，才会进行预警检查，没有配置或配置成关，不进行预警检查**/
            boolean warningActive = false;
            ProjectWarningSettingInfo projectWarningSettingInfo = WarningHelper.getProjectWarningSetting(projectId, redisTemplate);
            if (null != projectWarningSettingInfo) {
                String globalSwitch = projectWarningSettingInfo.getGlobalSwitch();
                if (null != globalSwitch && globalSwitch.equals(GBConstants.WARNING_ACTIVE_ON)) {
                    warningActive = true;
                }
            }
            if (!warningActive) {
                return 0;
            }
            /**检查RTU配置,规则：默认为开，只有显示配置为关时，不进行预警检查，其它情况需要进行预警检查**/
            RtuWarningSettingInfo rtuWarningSettingInfo = WarningHelper.getRtuWarningSetting(rtuCode, redisTemplate);
            boolean rtuWarningActive = true;
            if (null != rtuWarningSettingInfo) {
                String globalSwitch = rtuWarningSettingInfo.getGlobalSwitch();
                if (null != globalSwitch && globalSwitch.equals(GBConstants.WARNING_ACTIVE_OFF)) {
                    rtuWarningActive = false;
                }
            }
            if (!rtuWarningActive) {
                return 0;
            }
            /**检查RTU发包是否阻塞 规则：当前包数不等于总包数，RTU发送故障**/
            String netBlockingSwitch = projectWarningSettingInfo.getRtuNetBlockingSwitch();
            if (null != netBlockingSwitch && netBlockingSwitch.equals(GBConstants.WARNING_ACTIVE_ON)) {
                List<Integer> datagramIndex = rtuStatusEntity.getSendChancelIndex();
                if (null != datagramIndex && datagramIndex.size() > 0) {
                    int totalPackageSize = datagramIndex.get(0);
                    String warningMessage = "";
                    boolean warningHappen = false;
                    for (int i = 1; i < datagramIndex.size() - 1; i++) {
                        int packageSize = datagramIndex.get(i);
                        if (packageSize != -1) {
                            if (packageSize != totalPackageSize) {
                                warningHappen = true;
                                int delayPackageSize = 0;
                                if (totalPackageSize > packageSize) {
                                    delayPackageSize = totalPackageSize - packageSize;
                                } else {
                                    int realTotalPackageSize = totalPackageSize + 4095;
                                    delayPackageSize = realTotalPackageSize - packageSize;
                                }
                                if (warningMessage.length() == 0) {
                                    warningMessage = "第 " + String.format("%d", i) + " 路延迟 " + String.format("%d", delayPackageSize) + " 包";
                                } else {
                                    warningMessage += " ; " + "第 " + String.format("%d", i) + " 路延迟 " + String.format("%d", delayPackageSize) + " 包";
                                }
                                break;
                            }
                        }
                    }
                    if (warningHappen) {
                        WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_NET_BLOCKING, GBConstants.WARNING_STATUS_HAPPEN, dt, warningMessage, null);
                        //保存预警信息
                        DataProcessHolder.newInstance().dataProcess.warningService.saveWarning(warningInfo);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_NET_BLOCKING)) {
                            String warning = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_NET_BLOCKING);
                            if (null == warning || warning.equals(GBConstants.WARNING_STATUS_RECOVERY)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_NET_BLOCKING, GBConstants.WARNING_STATUS_HAPPEN);
                                //推送预警信息
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        } else {
                            //更新缓存
                            opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_NET_BLOCKING, GBConstants.WARNING_STATUS_HAPPEN);
                            //推送预警信息
                            DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                        }
                    } else {
                        WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_NET_BLOCKING, GBConstants.WARNING_STATUS_RECOVERY, dt, "RTU网络堵塞已恢复正常！", null);
                        //保存预警信息
                        DataProcessHolder.newInstance().dataProcess.warningService.warningRecovery(warningInfo);
                        //判断是否已预警
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_NET_BLOCKING)) {
                            String warning = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_NET_BLOCKING);
                            if (null == warning || warning.equals(GBConstants.WARNING_STATUS_HAPPEN)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_NET_BLOCKING, GBConstants.WARNING_STATUS_RECOVERY);
                                //推送预警信息
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        } else {
                            //更新缓存
                            opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_NET_BLOCKING, GBConstants.WARNING_STATUS_RECOVERY);
                        }
                    }
                }
            }
            /**检查交流电是否发生断电故障 规则：显示配置并设置为开时进行预警检查**/
            String faultAcSwitch = projectWarningSettingInfo.getFaultAcSwitch();
            if (null != faultAcSwitch && faultAcSwitch.equals(GBConstants.WARNING_ACTIVE_ON)) {
                String status_ac = rtuStatusEntity.getAcZt();
                if (null != status_ac && status_ac.length() > 0) {
                    if (status_ac.equals("0")) {
                        //交流电供电正常
                        DataProcessHolder.newInstance().dataProcess.warningService.RtuAcWarnningRecovery(rtuStatusEntity);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_AC)) {
                            String warnning_status_power_ac = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_AC);
                            if (null != warnning_status_power_ac && warnning_status_power_ac.equals(GBConstants.WARNING_STATUS_HAPPEN)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_AC, GBConstants.WARNING_STATUS_RECOVERY);
                                //推送预警信息
                                WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_FAULT_AC, GBConstants.WARNING_STATUS_RECOVERY, dt, "交流电恢复供电", null);
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        }
                    } else if (status_ac.equals("1")) {
                        //故障报警
                        DataProcessHolder.newInstance().dataProcess.warningService.RtuAcWarnningHappen(rtuStatusEntity);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_AC)) {
                            String warnning_status_power_ac = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_AC);
                            if (null == warnning_status_power_ac || warnning_status_power_ac.equals(GBConstants.WARNING_STATUS_RECOVERY)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_AC, GBConstants.WARNING_STATUS_HAPPEN);
                                //推送预警信息
                                WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_FAULT_AC, GBConstants.WARNING_STATUS_HAPPEN, dt, "交流电断电故障", null);
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        } else {
                            //更新缓存
                            opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_AC, GBConstants.WARNING_STATUS_HAPPEN);
                            //推送预警信息
                            WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_FAULT_AC, GBConstants.WARNING_STATUS_HAPPEN, dt, "交流电断电故障", null);
                            DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                        }
                    }
                }
            }
            /**水位计状态检测 规则：显示配置并设置为开时进行预警检查**/
            String faultSeSwitch = projectWarningSettingInfo.getFaultSeSwitch();
            if (null != faultSeSwitch && faultSeSwitch.equals(GBConstants.WARNING_ACTIVE_ON)) {
                String se = rtuStatusEntity.getWaterSe();
                if (null != se && se.length() > 0) {
                    if (se.equals("0")) {//水位计设备正常
                        DataProcessHolder.newInstance().dataProcess.warningService.RtuSeWarnningRecovery(rtuStatusEntity);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_SE)) {
                            String warnning_status_water_se = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_SE);
                            if (null != warnning_status_water_se && warnning_status_water_se.equals(GBConstants.WARNING_STATUS_HAPPEN)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_SE, GBConstants.WARNING_STATUS_RECOVERY);
                                //推送预警信息
                                WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_FAULT_SE, GBConstants.WARNING_STATUS_RECOVERY, dt, "水位计设备故障恢复", null);
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        }
                    } else if (se.equals("1")) {//故障报警,只报一次
                        DataProcessHolder.newInstance().dataProcess.warningService.RtuSeWarnningHappen(rtuStatusEntity);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_SE)) {
                            String warnning_status_water_se = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_SE);
                            if (null == warnning_status_water_se || warnning_status_water_se.equals(GBConstants.WARNING_STATUS_RECOVERY)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_SE, GBConstants.WARNING_STATUS_HAPPEN);
                                //推送预警信息
                                WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_FAULT_SE, GBConstants.WARNING_STATUS_RECOVERY, dt, "水位计设备故障", null);
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        } else {
                            //更新缓存
                            opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_SE, GBConstants.WARNING_STATUS_HAPPEN);
                            //推送预警信息
                            WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_FAULT_SE, GBConstants.WARNING_STATUS_RECOVERY, dt, "水位计设备故障", null);
                            DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                        }
                    }
                }
            }
            /**判断SD卡是否损坏**/
            String sd_damage_switch = projectWarningSettingInfo.getSdDamageSwitch();
            if (null != sd_damage_switch && sd_damage_switch.equals(GBConstants.WARNING_ACTIVE_ON)) {
                Integer sd = rtuStatusEntity.getStatus();
                if (null != sd) {
                    if (sd.intValue() == 1) { //SD卡已损坏
                        WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_SD_DAMAGE, GBConstants.WARNING_STATUS_HAPPEN, dt, "SD卡已损坏", null);
                        //保存预警信息
                        DataProcessHolder.newInstance().dataProcess.warningService.saveWarning(warningInfo);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SD_DAMAGE)) {
                            String warning = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SD_DAMAGE);
                            if (null == warning || warning.equals(GBConstants.WARNING_STATUS_RECOVERY)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SD_DAMAGE, GBConstants.WARNING_STATUS_HAPPEN);
                                //推送预警信息
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        } else {
                            //更新缓存
                            opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SD_DAMAGE, GBConstants.WARNING_STATUS_HAPPEN);
                            //推送预警信息
                            DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                        }
                    } else if (sd.intValue() == 0) {
                        WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_SD_DAMAGE, GBConstants.WARNING_STATUS_RECOVERY, dt, "SD卡已恢复", null);
                        //保存预警信息
                        DataProcessHolder.newInstance().dataProcess.warningService.warningRecovery(warningInfo);
                        //故障恢复，已预警时需要处理
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SD_DAMAGE)) {
                            String warning = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SD_DAMAGE);
                            if (null != warning && warning.equals(GBConstants.WARNING_STATUS_HAPPEN)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SD_DAMAGE, GBConstants.WARNING_STATUS_RECOVERY);
                                //推送预警信息
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        }
                    }
                }
            }
            /**电池电压 规则：显示配置并设置为开时进行预警检查**/
            String faultBvSwitch = projectWarningSettingInfo.getFaultBvSwitch();
            if (null != faultBvSwitch && faultBvSwitch.equals(GBConstants.WARNING_ACTIVE_ON)) {
                String bt = rtuStatusEntity.getBatteryVoltage();
                if (null != bt && bt.length() > 0) {
                    if (bt.equals("0")) {//电池电压正常
                        DataProcessHolder.newInstance().dataProcess.warningService.RtuBvWarnningRecovery(rtuStatusEntity);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_BV)) {
                            String warnning_status_bt_v = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_BV);
                            if (null != warnning_status_bt_v && warnning_status_bt_v.equals(GBConstants.WARNING_STATUS_HAPPEN)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_BV, GBConstants.WARNING_STATUS_RECOVERY);
                                //推送预警信息
                                WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_FAULT_BV, GBConstants.WARNING_STATUS_RECOVERY, dt, "电池电压故障已恢复", null);
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        }
                    } else if (bt.equals("1")) {//故障报警
                        WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_FAULT_BV, GBConstants.WARNING_STATUS_HAPPEN, dt, "电池电压故障", null);
                        DataProcessHolder.newInstance().dataProcess.warningService.RtuBvWarnningHappen(rtuStatusEntity);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_BV)) {
                            String warnning_status_bt_v = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_BV);
                            if (null == warnning_status_bt_v || warnning_status_bt_v.equals(GBConstants.WARNING_STATUS_RECOVERY)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_BV, GBConstants.WARNING_STATUS_HAPPEN);
                                //推送预警信息
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        } else {
                            //更新缓存
                            opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_FAULT_BV, GBConstants.WARNING_STATUS_HAPPEN);
                            //推送预警信息
                            DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                        }
                    }
                }
            }
            /**设备报文上报延时检测 规则：显示配置并设置为开时进行预警检查**/
            String datagramDelaySwitch = projectWarningSettingInfo.getDatagramDelaySwitch();
            if (null != datagramDelaySwitch && datagramDelaySwitch.equals(GBConstants.WARNING_ACTIVE_ON)) {
                Timestamp collectTm = rtuStatusEntity.getCollectTime();
                if (null != collectTm) {
                    int minute = 30;//默认30分延时报警
                    if (opsHash.hasKey(rtuCode, "projectId")) {
                        String project_key = "project_" + projectId;
                        if (redisTemplate.hasKey(project_key)) {
                            if (opsHash.hasKey(project_key, "warning_delay_time")) {
                                String delayTime = opsHash.get(project_key, "warning_delay_time");
                                if (null != delayTime && delayTime.length() > 0) {
                                    minute = Integer.parseInt(delayTime);
                                }
                            }
                        }
                    }
                    if (opsHash.hasKey(rtuCode, "warning_delay_time")) {
                        String delayTime = opsHash.get(rtuCode, "warning_delay_time");
                        if (null != delayTime && delayTime.length() > 0) {
                            minute = Integer.parseInt(delayTime);
                        }
                    }

                    Date now_tm = new Date();
                    long goingTm = now_tm.getTime() - collectTm.getTime();
                    if (goingTm > 0) {
                        goingTm = goingTm / (60 * 1000);
                    } else {
                        goingTm = 0;
                    }
                    if (goingTm <= minute) {//正常
                        WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_DATAGRAM_DELAY, GBConstants.WARNING_STATUS_RECOVERY, dt, "上报延时预警恢复", null);
                        //保存预警信息
                        DataProcessHolder.newInstance().dataProcess.warningService.warningRecovery(warningInfo);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_DATAGRAM_DELAY)) {
                            String warningStatus = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_DATAGRAM_DELAY);
                            if (warningStatus != null && warningStatus.equals(GBConstants.WARNING_STATUS_HAPPEN)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_DATAGRAM_DELAY, GBConstants.WARNING_STATUS_RECOVERY);
                                //推送预警信息
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        }
                    } else {//故障报警
                        WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_DATAGRAM_DELAY, GBConstants.WARNING_STATUS_HAPPEN, dt, "上报延时故障预警", null);
                        //保存预警信息
                        DataProcessHolder.newInstance().dataProcess.warningService.saveWarning(warningInfo);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_DATAGRAM_DELAY)) {
                            String warnning_status_bt_v = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_DATAGRAM_DELAY);
                            if (null == warnning_status_bt_v || warnning_status_bt_v.equals(GBConstants.WARNING_STATUS_RECOVERY)) {//只在第一次预警时处理
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_DATAGRAM_DELAY, GBConstants.WARNING_STATUS_HAPPEN);
                                //推送预警信息
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        } else {
                            //更新缓存
                            opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_DATAGRAM_DELAY, GBConstants.WARNING_STATUS_HAPPEN);
                            //推送预警信息
                            DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                        }
                    }
                }
            }
            /**检测报文采集时间错误 规则：显示配置并设置为开时进行预警检查**/
            String collectTimeErrorSwitch = projectWarningSettingInfo.getCollectTimeErrorSwitch();
            if (null != collectTimeErrorSwitch && collectTimeErrorSwitch.equals(GBConstants.WARNING_ACTIVE_ON)) {
                Timestamp datagram_dt = rtuStatusEntity.getSendDatagramTime();//发报时间
                Timestamp datagram_pt = rtuStatusEntity.getPickTime();//收报时间
                if (null != datagram_pt && null != datagram_dt) {
                    boolean warningHappen = false;
                    long net_time = (datagram_pt.getTime() - datagram_dt.getTime()) / (60 * 1000);//时间差，可能为负
                    long clock_error = DataProcessHolder.newInstance().dataProcess.config.getClockError();
                    if (Math.abs(net_time) > clock_error) {
                        warningHappen = true;
                    }
                    if (warningHappen) {
                        WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_COLLECT_TIME_ERROR, GBConstants.WARNING_STATUS_HAPPEN, dt, "RTU采集时间错误故障预警", null);
                        //保存预警信息
                        DataProcessHolder.newInstance().dataProcess.warningService.saveWarning(warningInfo);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_COLLECT_TIME_ERROR)) {
                            String warnning_status_bt_v = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_COLLECT_TIME_ERROR);
                            if (null == warnning_status_bt_v || warnning_status_bt_v.equals(GBConstants.WARNING_STATUS_RECOVERY)) {//只在第一次预警时处理
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_COLLECT_TIME_ERROR, GBConstants.WARNING_STATUS_HAPPEN);
                                //推送预警信息
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        } else {
                            //更新缓存
                            opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_COLLECT_TIME_ERROR, GBConstants.WARNING_STATUS_HAPPEN);
                            //推送预警信息
                            DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                        }
                    } else {
                        WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_RTU_COLLECT_TIME_ERROR, GBConstants.WARNING_STATUS_RECOVERY, dt, "RTU采集时间错误恢复", null);
                        //保存预警信息
                        DataProcessHolder.newInstance().dataProcess.warningService.warningRecovery(warningInfo);
                        if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_COLLECT_TIME_ERROR)) {
                            String warningStatus = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_COLLECT_TIME_ERROR);
                            if (null != warningStatus && warningStatus.equals(GBConstants.WARNING_STATUS_HAPPEN)) {
                                //更新缓存
                                opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_RTU_COLLECT_TIME_ERROR, GBConstants.WARNING_STATUS_RECOVERY);
                                //推送预警信息
                                DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                            }
                        }
                    }
                }
            }
            /**太阳能充电电压状态检测,规则：根据配置的规则进行检测，格式为开始时间：结束时间：电压最低阈值**/
            String warningCheck = projectWarningSettingInfo.getSunVoltageSwitch();
            if (null != warningCheck && warningCheck.equals(GBConstants.WARNING_ACTIVE_ON)) {
                String key = "threshold_value_rule:" + projectId;
                String rule = opsHash.get(key, "rule_" + GBConstants.WARNING_TYPE_SUN_VOLTAGE);
                Timestamp datagram_dt = rtuStatusEntity.getSendDatagramTime();//发报时间
                if (null != rule && null != datagram_dt) {
                    boolean warningHappen = false;
                    String[] rules = rule.split(":");
                    int rule_begin_hour = Integer.parseInt(rules[0]);
                    int rule_end_hour = Integer.parseInt(rules[1]);
                    int thresholdValue = Integer.parseInt(rules[2]);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date(datagram_dt.getTime()));
                    int hour = calendar.get(Calendar.HOUR_OF_DAY);
                    if (hour >= rule_begin_hour && hour <= rule_end_hour) {
                        BigDecimal voltage = rtuStatusEntity.getSunPowerVoltage();
                        if (null != voltage) {
                            if (voltage.doubleValue() < (double) thresholdValue) {
                                warningHappen = true;
                            }
                            if (warningHappen) {
                                WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_SUN_VOLTAGE, GBConstants.WARNING_STATUS_HAPPEN, dt, "太阳能板电压异常故障预警", null);
                                //保存预警信息
                                DataProcessHolder.newInstance().dataProcess.warningService.saveWarning(warningInfo);
                                if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SUN_VOLTAGE)) {
                                    String warnning_status_bt_v = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SUN_VOLTAGE);
                                    if (null == warnning_status_bt_v || warnning_status_bt_v.equals(GBConstants.WARNING_STATUS_RECOVERY)) {//只在第一次预警时处理
                                        //更新缓存
                                        opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SUN_VOLTAGE, GBConstants.WARNING_STATUS_HAPPEN);
                                        //推送预警信息
                                        DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                                    }
                                } else {
                                    //更新缓存
                                    opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SUN_VOLTAGE, GBConstants.WARNING_STATUS_HAPPEN);
                                    //推送预警信息
                                    DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                                }
                            } else {
                                WarningInfo warningInfo = WarningHelper.toWarningInfo(rtuCode, GBConstants.WARNING_TYPE_SUN_VOLTAGE, GBConstants.WARNING_STATUS_RECOVERY, dt, "太阳能板电压异常故障恢复", null);
                                //保存预警信息
                                DataProcessHolder.newInstance().dataProcess.warningService.warningRecovery(warningInfo);
                                if (opsHash.hasKey(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SUN_VOLTAGE)) {
                                    String warningStatus = opsHash.get(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SUN_VOLTAGE);
                                    if (null != warningStatus && warningStatus.equals(GBConstants.WARNING_STATUS_HAPPEN)) {
                                        //更新缓存
                                        opsHash.put(rtuCode, "warning_" + GBConstants.WARNING_TYPE_SUN_VOLTAGE, GBConstants.WARNING_STATUS_RECOVERY);
                                        //推送预警信息
                                        DataProcessHolder.newInstance().dataProcess.submitWarningNotifyTask(warningInfo);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return 0;
    }
}
