package org.ts.ddcs.worker;

import org.ts.ddcs.entity.WarningInfo;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.tool.MailHelp;
import org.ts.ddcs.utils.GBConstants;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Callable;


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

    private WarningInfo warningInfo;

    WarningNotifyTask(WarningInfo warningInfo) {
        this.warningInfo = warningInfo;
    }

    @Override
    public Integer call() throws Exception {
        try {
            SimpleDateFormat timeFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //LogHelper.info("rtu warning notify : " + timeFormat2.format(new Date()));
            //预警信息通知，邮件等
            RedisTemplate redisTemplate = DataProcessHolder.newInstance().dataProcess.redisTemplate;
            HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
            Map<String, String> rtuLastTime = new HashMap<>();
            String rtuCode = warningInfo.getRtuCode();
            String projectId = opsHash.get(rtuCode, "projectId");
            if (null != projectId && projectId.length() > 0) {
                String redis_key = "project_" + projectId;
                if (redisTemplate.hasKey(redis_key)) {
                    String gb_warning_active = opsHash.get(redis_key, GBConstants.WARNING_PROJECT_SETTING_GB_ACTIVE_KEY);
                    if (null != gb_warning_active && gb_warning_active.length() > 0 && gb_warning_active.equals(GBConstants.WARNING_ACTIVE_ON)) {//判断预警总开关
                        String mail_warning_active = opsHash.get(redis_key, GBConstants.WARNING_PROJECT_SETTING_MAIL_ACTIVE_KEY);
                        if (null != mail_warning_active && mail_warning_active.length() > 0 && mail_warning_active.equals(GBConstants.WARNING_ACTIVE_ON)) {//判断邮件预警开关
                            String toMail = opsHash.get(redis_key, GBConstants.WARNING_PROJECT_SETTING_MAIL_KEY);
                            if (null != toMail && toMail.length() > 0) {
                                String rtuName = opsHash.get(rtuCode, "rtuName");
                                String projectName = opsHash.get(rtuCode, "projectName");
                                String orgName = opsHash.get(rtuCode, "orgName");
                                String title = "";
                                if (null != rtuName) {
                                    title += rtuName;
                                }
                                if (warningInfo.getWarningStatus().equals(GBConstants.WARNING_STATUS_HAPPEN)) {
                                    title += "<故障预警>";
                                } else {
                                    title += "<故障恢复>";
                                }
                                String mailText = "<div>设备名称：";
                                if (null != rtuName) {
                                    mailText += "<span>" + rtuName + "</span>";
                                }
                                mailText += "</div>";
                                mailText += "<div>项目名称：";
                                if (null != projectName) {
                                    mailText += projectName;
                                }
                                mailText += "</div>";
                                mailText += "<div>机构名称：";
                                if (null != orgName) {
                                    mailText += orgName;
                                }
                                mailText += "</div>";
                                mailText += "<div>预警信息：" + warningInfo.getWarningInfo();
                                mailText += "</div>";
                                MailHelp.sendMail(toMail, title, mailText);
                                LogHelper.info("send mail " + rtuCode + " " + toMail + " " + title);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return 0;
    }
}
