package com.ruoyi.framework.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.sendEmail.IMailService;
import com.ruoyi.common.thread.sems.*;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.sems.domain.*;
import com.ruoyi.project.sems.service.*;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 特种设备预警标识定时任务
 */

@Component("tSpecdevAllTask")
public class TSpecdevAllTask extends BaseController {
    //注入发送邮件的接口
    @Autowired
    private IMailService mailService;
    //注入叉车台账接口
    @Autowired
    private ITSpecdevCcService specdevCcService;
    //注入电梯台账接口
    @Autowired
    private ITSpecdevDtService specdevDtService;
    //注入锅炉台账接口
    @Autowired
    private ITSpecdevGlService specdevGlService;
    //注入起重机械台账接口
    @Autowired
    private ITSpecdevDzsbService specdevDzsbService;
    //注入压力管道台账接口
    @Autowired
    private ITSpecdevYlgdService specdevYlgdService;
    //注入压力容器台账接口
    @Autowired
    private ITSpecdevYlrqService specdevYlrqService;
    //注入特种设备预警接口
    @Autowired
    private ITAlarmtypeService tAlarmtypeService;
    //注入预警管理负责人接口
    @Autowired
    private ITAlarmPrincipalService tAlarmPrincipalService;
    //注入用户接口
    @Autowired
    private ISysUserService sysUserService;
    //注入报警记录管理的接口
    @Autowired
    private ITAlarmhistoryService alarmhistoryService;
    @Autowired
    private ISysPlantService sysPlantService;
    //系统基础配置
    @Autowired
    private RuoYiConfig ruoyiConfig;

    private final String glAlarmType = "特种设备-锅炉-在用";
    private final String glUrlSuffix = "sems/devgl/specGl";
    private final String ccAlarmType = "特种设备-叉车-在用";
    private final String ccUrlSuffix = "sems/decvcc/specCc";
    private final String dtAlarmType = "特种设备-电梯-在用";
    private final String dtUrlSuffix = "sems/devdt/specDt";
    private final String dzsbAlarmType = "特种设备-起重机械-在用";
    private final String dzsbUrlSuffix = "sems/devdzsb/specDzsb";
    private final String ylgdAlarmType = "特种设备-压力管道-在用";
    private final String ylgdUrlSuffix = "sems/devylgd/specYlgd";
    private final String ylrqAlarmType = "特种设备-压力容器-在用";
    private final String ylrqUrlSuffix = "sems/devylrq/specYlrq";

    private final String firstWarningLevel = "1级预警";
    private final String secondWarningLevel = "2级预警";
    private final String thirdWarningLevel = "3级预警";

    private final Long alarmtype = 1080L;//特种设备预警ID

    private final String contentFormat = "（装置名称，单元，位号，下次检验日期）";
    private final String ccContentFormat = "（装置名称，车牌号，位号，下次检验日期）";

    /**
     * 检查预警标识状态
     * 3天检查一次
     */
    public void checkWarnFlag() {
        //获取动态处理预警级别
        TAlarmtype tAlarmtype = this.tAlarmtypeService.selectTAlarmtypeById(this.alarmtype);
        if (tAlarmtype.getIsOpen() == 0) {
            logger.info("特种设备预警标识定时任务未启用");
            return;
        }
        Long firstlevel = tAlarmtype.getFirstlevel();//1级
        Long secondlevel = tAlarmtype.getSecondlevel();//2级
        Long thirdlevel = tAlarmtype.getThirdlevel();//3级
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        this.checkWarnFlagForGl(firstlevel, secondlevel, thirdlevel, sdf);
        this.checkWarnFlagForCc(firstlevel, secondlevel, thirdlevel, sdf);
        this.checkWarnFlagForDt(firstlevel, secondlevel, thirdlevel, sdf);
        this.checkWarnFlagForDzsb(firstlevel, secondlevel, thirdlevel, sdf);
        this.checkWarnFlagForYlgd(firstlevel, secondlevel, thirdlevel, sdf);
        this.checkWarnFlagForYlrq(firstlevel, secondlevel, thirdlevel, sdf);
        //
        SpecOverLifeThead specOverLifeThead = new SpecOverLifeThead(specdevYlrqService);
        Thread t0 = new Thread(specOverLifeThead);
        t0.start();
        //
        SpecDevYlgdThread specDevYlgdThread = new SpecDevYlgdThread(specdevYlgdService);
        Thread t = new Thread(specDevYlgdThread);
        t.start();
        SpecDevYlrqThread specDevYlgrThread = new SpecDevYlrqThread(specdevYlrqService);
        Thread t2 = new Thread(specDevYlgrThread);
        t2.start();
        SpecDevCcThread specDevCcThread = new SpecDevCcThread(specdevCcService);
        Thread t3 = new Thread(specDevCcThread);
        t3.start();
        SpecDevDtThread specDevDtThread = new SpecDevDtThread(specdevDtService);
        Thread t4 = new Thread(specDevDtThread);
        t4.start();
        SpecDevGlThread specDevGlThread = new SpecDevGlThread(specdevGlService);
        Thread t5 = new Thread(specDevGlThread);
        t5.start();
        SpecDevDzsbThread specDevDzsbThread = new SpecDevDzsbThread(specdevDzsbService);
        Thread t6 = new Thread(specDevDzsbThread);
        t6.start();
    }

    private void checkWarnFlagForGl(Long firstlevel, Long secondlevel, Long thirdlevel, SimpleDateFormat sdf) {
        //获取需要发送邮件的人员信息
        TAlarmPrincipal tAlarmPrincipal = new TAlarmPrincipal();
        tAlarmPrincipal.setTypeId(this.alarmtype);
        List<TAlarmPrincipal> userList = this.tAlarmPrincipalService.selectList(tAlarmPrincipal);
        for (TAlarmPrincipal t : userList) {
            SysUser user = this.sysUserService.selectUserByStaffId(t.getStaffid());
            if (user != null) {
                logger.info("发送人"+user.getNickName() + "--发送邮箱：" + user.getEmail());
                t.setPrincipalName(user.getNickName());
                t.setPrincipalEmail(user.getEmail());
            }
        }
        for (TAlarmPrincipal o : userList) {
            //根据装置权限获取数据
            List<SysPlant> plantList = sysPlantService.selectSysPlantByDeptId(o.getDeptId());
            logger.info("部门id"+o.getDeptId() + "--部门：" + plantList.toString());
            List<String> plants = new ArrayList<>();
            for (SysPlant s : plantList
            ) {
                plants.add(s.getName());
            }
            List<TSpecdevGl> list = this.specdevGlService.list(new QueryWrapper<TSpecdevGl>()
                    .eq("del_flag", 0).eq("status", "1")
                    .in("plant_code", plants)
            );
            //获取动态处理预警级别
            TAlarmtype tAlarmtype = this.tAlarmtypeService.selectTAlarmtypeById(this.alarmtype);
            List<TSpecdevGl> firstList = new ArrayList<>();
            List<TSpecdevGl> secondList = new ArrayList<>();
            List<TSpecdevGl> thirdList = new ArrayList<>();
            for (TSpecdevGl tSpecdevGl : list) {
                if (tSpecdevGl.getNextWarnDate() != null) {
                    long now = System.currentTimeMillis();
                    long diff = tSpecdevGl.getNextWarnDate().getTime() - now;
                    long nd = 1000 * 24 * 60 * 60;
                    long day = diff / nd;
                    // 半年内 预警标识为2级
                    if (day > 0 && day <= secondlevel) {
                        this.updateForGl(2L, tSpecdevGl);
                        //只在当天发送邮件
                        if (day == secondlevel) {
                            firstList.add(tSpecdevGl);
                        }
                        continue;
                    }
                    // 半年-一年 预警标识为1级
                    if (day > secondlevel && day <= thirdlevel) {
                        this.updateForGl(1L, tSpecdevGl);
                        //只在当天发送邮件
                        if (day == thirdlevel) {
                            secondList.add(tSpecdevGl);
                        }
                        continue;
                    }
                    // 超时 预警标识为3级
                    if (day <= firstlevel) {
                        this.updateForGl(3L, tSpecdevGl);
                        //只在当天发送邮件
                        if (day == firstlevel) {
                            thirdList.add(tSpecdevGl);
                        }
                        continue;
                    }
                    if (day > thirdlevel) {
                        this.updateForGl(0l, tSpecdevGl);
                        continue;
                    }
                }
            }
            glSendEmail(sdf, firstList, o, this.glWarningContent(firstList, sdf), this.firstWarningLevel);
            glSendEmail(sdf, secondList, o, this.glWarningContent(secondList, sdf), this.secondWarningLevel);
            glSendEmail(sdf, thirdList, o, this.glWarningContent(thirdList, sdf), this.thirdWarningLevel);
        }
    }

    private void checkWarnFlagForCc(Long firstlevel, Long secondlevel, Long thirdlevel, SimpleDateFormat sdf) {

        //获取需要发送邮件的人员信息
        TAlarmPrincipal tAlarmPrincipal = new TAlarmPrincipal();
        tAlarmPrincipal.setTypeId(this.alarmtype);
        List<TAlarmPrincipal> userList = this.tAlarmPrincipalService.selectList(tAlarmPrincipal);
        for (TAlarmPrincipal t : userList) {
            SysUser user = this.sysUserService.selectUserByStaffId(t.getStaffid());
            if (user != null) {
                logger.info("发送人"+user.getNickName() + "--发送邮箱：" + user.getEmail());
                t.setPrincipalName(user.getNickName());
                t.setPrincipalEmail(user.getEmail());
            }
        }
        for (TAlarmPrincipal o : userList) {
            //根据装置权限获取数据
            List<SysPlant> plantList = sysPlantService.selectSysPlantByDeptId(o.getDeptId());
            List<String> plants = new ArrayList<>();
            for (SysPlant s : plantList
            ) {
                plants.add(s.getName());
            }
            List<TSpecdevCc> list = this.specdevCcService.list(new QueryWrapper<TSpecdevCc>()
                    .eq("del_flag", 0).eq("status", "1").in("plant_code", plants));
            List<TSpecdevCc> firstList = new ArrayList<>();
            List<TSpecdevCc> secondList = new ArrayList<>();
            List<TSpecdevCc> thirdList = new ArrayList<>();
            for (TSpecdevCc tSpecdevCc : list) {
                if (tSpecdevCc.getNextWarnDate() != null) {
                    long now = System.currentTimeMillis();
                    long diff = tSpecdevCc.getNextWarnDate().getTime() - now;
                    long nd = 1000 * 24 * 60 * 60;
                    long day = diff / nd;
                    // 半年内 预警标识为2级
                    if (day > 0 && day <= secondlevel) {
                        this.updateForCc(2L, tSpecdevCc);
                        //只在当天发送邮件
                        if (day == secondlevel) {
                            firstList.add(tSpecdevCc);
                        }
                        continue;
                    }
                    // 半年-一年 预警标识为1级
                    if (day > secondlevel && day <= thirdlevel) {
                        this.updateForCc(1L, tSpecdevCc);
                        //只在当天发送邮件
                        if (day == thirdlevel) {
                            secondList.add(tSpecdevCc);
                        }
                        continue;
                    }
                    // 超时 预警标识为3级
                    if (day <= firstlevel) {
                        this.updateForCc(3L, tSpecdevCc);
                        //只在当天发送邮件
                        if (day == firstlevel) {
                            thirdList.add(tSpecdevCc);
                        }
                        continue;
                    }
                    if (day > thirdlevel) {
                        this.updateForCc(0L, tSpecdevCc);
                        continue;
                    }
                }
            }
            ccSendEmail(sdf, firstList, o, this.ccWarningContent(firstList, sdf), this.firstWarningLevel);
            ccSendEmail(sdf, secondList, o, this.ccWarningContent(secondList, sdf), this.secondWarningLevel);
            ccSendEmail(sdf, thirdList, o, this.ccWarningContent(thirdList, sdf), this.thirdWarningLevel);
        }
    }

    private void checkWarnFlagForDzsb(Long firstlevel, Long secondlevel, Long thirdlevel, SimpleDateFormat sdf) {

        //获取需要发送邮件的人员信息
        TAlarmPrincipal tAlarmPrincipal = new TAlarmPrincipal();
        tAlarmPrincipal.setTypeId(this.alarmtype);
        List<TAlarmPrincipal> userList = this.tAlarmPrincipalService.selectList(tAlarmPrincipal);
        for (TAlarmPrincipal t : userList) {
            SysUser user = this.sysUserService.selectUserByStaffId(t.getStaffid());
            if (user != null) {
                logger.info("发送人"+user.getNickName() + "--发送邮箱：" + user.getEmail());
                t.setPrincipalName(user.getNickName());
                t.setPrincipalEmail(user.getEmail());
            }
        }
        for (TAlarmPrincipal o : userList) {
            //根据装置权限获取数据
            List<SysPlant> plantList = sysPlantService.selectSysPlantByDeptId(o.getDeptId());
            List<String> plants = new ArrayList<>();
            for (SysPlant s : plantList
            ) {
                plants.add(s.getName());
            }
            List<TSpecdevDzsb> list = this.specdevDzsbService.list(new QueryWrapper<TSpecdevDzsb>()
                    .eq("del_flag", 0).eq("status", "1").in("plant_code", plants));
            List<TSpecdevDzsb> firstList = new ArrayList<>();
            List<TSpecdevDzsb> secondList = new ArrayList<>();
            List<TSpecdevDzsb> thirdList = new ArrayList<>();
            for (TSpecdevDzsb tSpecdevDzsb : list) {
                if (tSpecdevDzsb.getNextWarnDate() != null) {
                    long now = System.currentTimeMillis();
                    long diff = tSpecdevDzsb.getNextWarnDate().getTime() - now;
                    long nd = 1000 * 24 * 60 * 60;
                    long day = diff / nd;
                    // 半年内 预警标识为2级
                    if (day > 0 && day <= secondlevel) {
                        this.updateForDzsb(2L, tSpecdevDzsb);
                        //只在当天发送邮件
                        if (day == secondlevel) {
                            firstList.add(tSpecdevDzsb);
                        }
                        continue;
                    }
                    // 半年-一年 预警标识为1级
                    if (day > secondlevel && day <= thirdlevel) {
                        this.updateForDzsb(1L, tSpecdevDzsb);
                        //只在当天发送邮件
                        if (day == thirdlevel) {
                            secondList.add(tSpecdevDzsb);
                        }
                        continue;
                    }
                    // 超时 预警标识为3级
                    if (day <= firstlevel) {
                        this.updateForDzsb(3L, tSpecdevDzsb);
                        //只在当天发送邮件
                        if (day == firstlevel) {
                            thirdList.add(tSpecdevDzsb);
                        }
                        continue;
                    }
                    if (day > thirdlevel ) {
                        this.updateForDzsb(0L, tSpecdevDzsb);
                        continue;
                    }
                }
            }
            dzsbSendEmail(sdf, firstList, o, this.dzsbWarningContent(firstList, sdf), this.firstWarningLevel);
            dzsbSendEmail(sdf, secondList, o, this.dzsbWarningContent(secondList, sdf), this.secondWarningLevel);
            dzsbSendEmail(sdf, thirdList, o, this.dzsbWarningContent(thirdList, sdf), this.thirdWarningLevel);
        }
    }

    private void checkWarnFlagForYlrq(Long firstlevel, Long secondlevel, Long thirdlevel, SimpleDateFormat sdf) {

        //获取需要发送邮件的人员信息
        TAlarmPrincipal tAlarmPrincipal = new TAlarmPrincipal();
        tAlarmPrincipal.setTypeId(this.alarmtype);
        List<TAlarmPrincipal> userList = this.tAlarmPrincipalService.selectList(tAlarmPrincipal);
        for (TAlarmPrincipal t : userList) {
            SysUser user = this.sysUserService.selectUserByStaffId(t.getStaffid());
            if (user != null) {
                logger.info("发送人"+user.getNickName() + "--发送邮箱：" + user.getEmail());
                t.setPrincipalName(user.getNickName());
                t.setPrincipalEmail(user.getEmail());
            }
        }
        for (TAlarmPrincipal o : userList) {
            //根据装置权限获取数据
            List<SysPlant> plantList = sysPlantService.selectSysPlantByDeptId(o.getDeptId());
            List<String> plants = new ArrayList<>();
            for (SysPlant s : plantList
            ) {
                plants.add(s.getName());
            }
            List<TSpecdevYlrq> list = this.specdevYlrqService.list(new QueryWrapper<TSpecdevYlrq>()
                    .eq("del_flag", 0).eq("status", "1").in("plant_code", plants));
            List<TSpecdevYlrq> firstList = new ArrayList<>();
            List<TSpecdevYlrq> secondList = new ArrayList<>();
            List<TSpecdevYlrq> thirdList = new ArrayList<>();
            for (TSpecdevYlrq tSpecdevYlrq : list) {
                if (tSpecdevYlrq.getNextWarnDate() != null) {
                    long now = System.currentTimeMillis();
                    long diff = tSpecdevYlrq.getNextWarnDate().getTime() - now;
                    long nd = 1000 * 24 * 60 * 60;
                    long day = diff / nd;
                    // 半年内 预警标识为2级
                    if (day > 0 && day <= secondlevel) {
                        this.updateForYlrq(2L, tSpecdevYlrq);
                        //只在当天发送邮件
                        if (day == secondlevel) {
                            firstList.add(tSpecdevYlrq);
                        }
                        continue;
                    }
                    // 半年-一年 预警标识为1级
                    if (day > secondlevel && day <= thirdlevel) {
                        this.updateForYlrq(1L, tSpecdevYlrq);
                        //只在当天发送邮件
                        if (day == thirdlevel) {
                            secondList.add(tSpecdevYlrq);
                        }
                        continue;
                    }
                    // 超时 预警标识为3级
                    if (day <= firstlevel) {
                        this.updateForYlrq(3L, tSpecdevYlrq);
                        //只在当天发送邮件
                        if (day == firstlevel) {
                            thirdList.add(tSpecdevYlrq);
                        }
                        continue;
                    }
                    if (day > thirdlevel ) {
                        this.updateForYlrq(0L, tSpecdevYlrq);
                        continue;
                    }
                }
            }
            ylrqSendEmail(sdf, firstList, o, this.ylrqWarningContent(firstList, sdf), this.firstWarningLevel);
            ylrqSendEmail(sdf, secondList, o, this.ylrqWarningContent(secondList, sdf), this.secondWarningLevel);
            ylrqSendEmail(sdf, thirdList, o, this.ylrqWarningContent(thirdList, sdf), this.thirdWarningLevel);
        }
    }

    private void checkWarnFlagForYlgd(Long firstlevel, Long secondlevel, Long thirdlevel, SimpleDateFormat sdf) {

        //获取需要发送邮件的人员信息
        TAlarmPrincipal tAlarmPrincipal = new TAlarmPrincipal();
        tAlarmPrincipal.setTypeId(this.alarmtype);
        List<TAlarmPrincipal> userList = this.tAlarmPrincipalService.selectList(tAlarmPrincipal);
        for (TAlarmPrincipal t : userList) {
            SysUser user = this.sysUserService.selectUserByStaffId(t.getStaffid());
            if (user != null) {
                logger.info("发送人"+user.getNickName() + "--发送邮箱：" + user.getEmail());
                t.setPrincipalName(user.getNickName());
                t.setPrincipalEmail(user.getEmail());
            }
        }
        for (TAlarmPrincipal o : userList) {
            //根据装置权限获取数据
            List<SysPlant> plantList = sysPlantService.selectSysPlantByDeptId(o.getDeptId());
            List<String> plants = new ArrayList<>();
            for (SysPlant s : plantList
            ) {
                plants.add(s.getName());
            }
            List<TSpecdevYlgd> list = this.specdevYlgdService.list(new QueryWrapper<TSpecdevYlgd>()
                    .eq("del_flag", 0).eq("status", "1").in("plant_code", plants));
            List<TSpecdevYlgd> firstList = new ArrayList<>();
            List<TSpecdevYlgd> secondList = new ArrayList<>();
            List<TSpecdevYlgd> thirdList = new ArrayList<>();
            for (TSpecdevYlgd tSpecdevYlgd : list) {
                if (tSpecdevYlgd.getNextWarnDate() != null) {
                    long now = System.currentTimeMillis();
                    long diff = tSpecdevYlgd.getNextWarnDate().getTime() - now;
                    long nd = 1000 * 24 * 60 * 60;
                    long day = diff / nd;
                    // 半年内 预警标识为2级
                    if (day > 0 && day <= secondlevel) {
                        this.updateForYlgd(2L, tSpecdevYlgd);
                        //只在当天发送邮件
                        if (day == secondlevel) {
                            firstList.add(tSpecdevYlgd);
                        }
                        continue;
                    }
                    // 半年-一年 预警标识为1级
                    if (day > secondlevel && day <= thirdlevel) {
                        this.updateForYlgd(1L, tSpecdevYlgd);
                        //只在当天发送邮件
                        if (day == thirdlevel) {
                            secondList.add(tSpecdevYlgd);
                        }
                        continue;
                    }
                    // 超时 预警标识为3级
                    if (day <= firstlevel) {
                        this.updateForYlgd(3L, tSpecdevYlgd);
                        //只在当天发送邮件
                        if (day == firstlevel) {
                            thirdList.add(tSpecdevYlgd);
                        }
                        continue;
                    }
                    // 半年-一年 预警标识为1级
                    if (day > thirdlevel ) {
                        this.updateForYlgd(0L, tSpecdevYlgd);
                        continue;
                    }
                }
            }
            ylgdSendEmail(sdf, firstList, o, this.ylgdWarningContent(firstList, sdf), this.firstWarningLevel);
            ylgdSendEmail(sdf, secondList, o, this.ylgdWarningContent(secondList, sdf), this.secondWarningLevel);
            ylgdSendEmail(sdf, thirdList, o, this.ylgdWarningContent(thirdList, sdf), this.thirdWarningLevel);
        }
    }

    private void checkWarnFlagForDt(Long firstlevel, Long secondlevel, Long thirdlevel, SimpleDateFormat sdf) {
        //获取需要发送邮件的人员信息
        TAlarmPrincipal tAlarmPrincipal = new TAlarmPrincipal();
        tAlarmPrincipal.setTypeId(this.alarmtype);
        List<TAlarmPrincipal> userList = this.tAlarmPrincipalService.selectList(tAlarmPrincipal);
        for (TAlarmPrincipal t : userList) {
            SysUser user = this.sysUserService.selectUserByStaffId(t.getStaffid());
            if (user != null) {
                t.setPrincipalName(user.getNickName());
                t.setPrincipalEmail(user.getEmail());
            }
        }
        for (TAlarmPrincipal o : userList) {
            //根据装置权限获取数据
            List<SysPlant> plantList = sysPlantService.selectSysPlantByDeptId(o.getDeptId());
            List<String> plants = new ArrayList<>();
            for (SysPlant s : plantList
            ) {
                plants.add(s.getName());
            }
            List<TSpecdevDt> list = this.specdevDtService.list(new QueryWrapper<TSpecdevDt>()
                    .eq("del_flag", 0).eq("status", "1").in("plant_code", plants));
            List<TSpecdevDt> firstList = new ArrayList<>();
            List<TSpecdevDt> secondList = new ArrayList<>();
            List<TSpecdevDt> thirdList = new ArrayList<>();
            for (TSpecdevDt tSpecdevDt : list) {
                if (tSpecdevDt.getNextWarnDate() != null) {
                    long now = System.currentTimeMillis();
                    long diff = tSpecdevDt.getNextWarnDate().getTime() - now;
                    long nd = 1000 * 24 * 60 * 60;
                    long day = diff / nd;
                    // 半年内 预警标识为2级
                    if (day > 0 && day <= secondlevel) {
                        this.updateForDt(2L, tSpecdevDt);
                        //只在当天发送邮件
                        if (day == secondlevel) {
                            firstList.add(tSpecdevDt);
                        }
                        continue;
                    }
                    // 半年-一年 预警标识为1级
                    if (day > secondlevel && day <= thirdlevel) {
                        this.updateForDt(1L, tSpecdevDt);
                        //只在当天发送邮件
                        if (day == thirdlevel) {
                            secondList.add(tSpecdevDt);
                        }
                        continue;
                    }
                    // 超时 预警标识为3级
                    if (day <= firstlevel) {
                        this.updateForDt(3L, tSpecdevDt);
                        //只在当天发送邮件
                        if (day == firstlevel) {
                            thirdList.add(tSpecdevDt);
                        }
                        continue;
                    }
                    if (day > thirdlevel ) {
                        this.updateForDt(0L, tSpecdevDt);
                        continue;
                    }
                }
            }
            dtSendEmail(sdf, firstList, o, this.dtWarningContent(firstList, sdf), this.firstWarningLevel);
            dtSendEmail(sdf, secondList, o, this.dtWarningContent(secondList, sdf), this.secondWarningLevel);
            dtSendEmail(sdf, thirdList, o, this.dtWarningContent(thirdList, sdf), this.thirdWarningLevel);
        }
    }

    /**
     * 锅炉发送邮件公共方法
     *
     * @param sdf            日期转换
     * @param gl             锅炉集合
     * @param o              负责人信息
     * @param warningContent 预警内容详情
     * @param level          预警等级
     */
    private void glSendEmail(SimpleDateFormat sdf, List<TSpecdevGl> gl, TAlarmPrincipal o, String warningContent, String level) {
        if (gl.size() > 0) {
            this.sendEmail(o.getPrincipalName(), o.getPrincipalEmail(), this.glAlarmType, level,
                    this.contentFormat, this.glWarningContent(gl, sdf), this.glUrlSuffix);
            insertHistory(o, level.substring(0, 1), warningContent, this.alarmtype + "", this.glAlarmType);
        }
    }

    /**
     * 电梯发送邮件公共方法
     *
     * @param sdf            日期转换
     * @param dt             电梯集合
     * @param o              负责人信息
     * @param warningContent 预警内容详情
     * @param level          预警等级
     */
    private void dtSendEmail(SimpleDateFormat sdf, List<TSpecdevDt> dt, TAlarmPrincipal o, String warningContent, String level) {
        if (dt.size() > 0) {
            this.sendEmail(o.getPrincipalName(), o.getPrincipalEmail(), this.dtAlarmType, level,
                    this.contentFormat, this.dtWarningContent(dt, sdf), this.dtUrlSuffix);
            insertHistory(o, level.substring(0, 1), warningContent, this.alarmtype + "", this.dtAlarmType);
        }
    }

    /**
     * 叉车发送邮件公共方法
     *
     * @param sdf            日期转换
     * @param cc             叉车集合
     * @param o              负责人信息
     * @param warningContent 预警内容详情
     * @param level          预警等级
     */
    private void ccSendEmail(SimpleDateFormat sdf, List<TSpecdevCc> cc, TAlarmPrincipal o, String warningContent, String level) {
        if (cc.size() > 0) {
            this.sendEmail(o.getPrincipalName(), o.getPrincipalEmail(), this.ccAlarmType, level,
                    this.ccContentFormat, this.ccWarningContent(cc, sdf), this.ccUrlSuffix);
            insertHistory(o, level.substring(0, 1), warningContent, this.alarmtype + "", this.ccAlarmType);
        }
    }

    /**
     * 吊装设备发送邮件公共方法
     *
     * @param sdf            日期转换
     * @param dzsb           吊装设备集合
     * @param o              负责人信息
     * @param warningContent 预警内容详情
     * @param level          预警等级
     */
    private void dzsbSendEmail(SimpleDateFormat sdf, List<TSpecdevDzsb> dzsb, TAlarmPrincipal o, String warningContent, String level) {
        if (dzsb.size() > 0) {
            this.sendEmail(o.getPrincipalName(), o.getPrincipalEmail(), this.dzsbAlarmType, level,
                    this.contentFormat, this.dzsbWarningContent(dzsb, sdf), this.dzsbUrlSuffix);
            insertHistory(o, level.substring(0, 1), warningContent, this.alarmtype + "", this.dzsbAlarmType);
        }
    }

    /**
     * 压力管道发送邮件公共方法
     *
     * @param sdf            日期转换
     * @param ylgd           压力管道集合
     * @param o              负责人信息
     * @param warningContent 预警内容详情
     * @param level          预警等级
     */
    private void ylgdSendEmail(SimpleDateFormat sdf, List<TSpecdevYlgd> ylgd, TAlarmPrincipal o, String warningContent, String level) {
        if (ylgd.size() > 0) {
            this.sendEmail(o.getPrincipalName(), o.getPrincipalEmail(), this.ylgdAlarmType, level,
                    this.contentFormat, this.ylgdWarningContent(ylgd, sdf), this.ylgdUrlSuffix);
            insertHistory(o, level.substring(0, 1), warningContent, this.alarmtype + "", this.ylgdAlarmType);
        }
    }

    /**
     * 压力容器发送邮件公共方法
     *
     * @param sdf            日期转换
     * @param ylrq           压力容器集合
     * @param o              负责人信息
     * @param warningContent 预警内容详情
     * @param level          预警等级
     */
    private void ylrqSendEmail(SimpleDateFormat sdf, List<TSpecdevYlrq> ylrq, TAlarmPrincipal o, String warningContent, String level) {
        if (ylrq.size() > 0) {
            this.sendEmail(o.getPrincipalName(), o.getPrincipalEmail(), this.ylrqAlarmType, level,
                    this.contentFormat, this.ylrqWarningContent(ylrq, sdf), this.ylrqUrlSuffix);
            insertHistory(o, level.substring(0, 1), warningContent, this.alarmtype + "", this.ylrqAlarmType);
        }
    }

    /**
     * 处理电梯预警内容详情
     *
     * @param list 电梯数据集合
     * @param sdf  日期转换
     * @return
     */
    private String dtWarningContent(List<TSpecdevDt> list, SimpleDateFormat sdf) {
        String str = "";
        for (TSpecdevDt t : list) {
            str += "<br>" + t.getPlantCode() + "，" + t.getUnit() + "，" + t.getDevno() + "，" + sdf.format(t.getNextWarnDate());
        }
        return str;
    }

    /**
     * 处理吊装设备预警内容详情
     *
     * @param list 吊装设备数据集合
     * @param sdf  日期转换
     * @return
     */
    private String dzsbWarningContent(List<TSpecdevDzsb> list, SimpleDateFormat sdf) {
        String str = "";
        for (TSpecdevDzsb t : list) {
            str += "<br>" + t.getPlantCode() + "，" + t.getUnit() + "，" + t.getDevno() + "，" + sdf.format(t.getNextWarnDate());
        }
        return str;
    }

    /**
     * 处理叉车预警内容详情
     *
     * @param list 叉车数据集合
     * @param sdf  日期转换
     * @return
     */
    private String ccWarningContent(List<TSpecdevCc> list, SimpleDateFormat sdf) {
        String str = "";
        for (TSpecdevCc t : list) {
            str += "<br>" + t.getPlantCode() + "，" + t.getCarNo() + "，" + t.getDevno() + "，" + sdf.format(t.getNextWarnDate());
        }
        return str;
    }

    /**
     * 处理压力管道预警内容详情
     *
     * @param list 压力管道数据集合
     * @param sdf  日期转换
     * @return
     */
    private String ylgdWarningContent(List<TSpecdevYlgd> list, SimpleDateFormat sdf) {
        String str = "";
        for (TSpecdevYlgd t : list) {
            str += "<br>" + t.getPlantCode() + "，" + t.getUnit() + "，" + t.getDevno() + "，" + sdf.format(t.getNextWarnDate());
        }
        return str;
    }

    /**
     * 处理压力容器预警内容详情
     *
     * @param list 压力容器数据集合
     * @param sdf  日期转换
     * @return
     */
    private String ylrqWarningContent(List<TSpecdevYlrq> list, SimpleDateFormat sdf) {
        String str = "";
        for (TSpecdevYlrq t : list) {
            str += "<br>" + t.getPlantCode() + "，" + t.getUnit() + "，" + t.getDevno() + "，" + sdf.format(t.getNextWarnDate());
        }
        return str;
    }

    /**
     * 处理锅炉预警内容详情
     *
     * @param list 锅炉数据集合
     * @param sdf  日期转换
     * @return
     */
    private String glWarningContent(List<TSpecdevGl> list, SimpleDateFormat sdf) {
        String str = "";
        for (TSpecdevGl t : list) {
            str += "<br>" + t.getPlantCode() + "，" + t.getUnit() + "，" + t.getDevno() + "，" + sdf.format(t.getNextWarnDate());
        }
        return str;
    }

    private void updateForCc(Long flag, TSpecdevCc tSpecdevCc) {
        tSpecdevCc.setWarnFlag(flag);
        this.specdevCcService.updateTSpecdevCc(tSpecdevCc);
    }

    private void updateForDt(Long flag, TSpecdevDt tSpecdevDt) {
        tSpecdevDt.setWarnFlag(flag);
        this.specdevDtService.updateTSpecdevDt(tSpecdevDt);
    }

    private void updateForGl(Long flag, TSpecdevGl tSpecdevGl) {
        tSpecdevGl.setWarnFlag(flag);
        this.specdevGlService.updateTSpecdevGl(tSpecdevGl);
    }

    private void updateForDzsb(Long flag, TSpecdevDzsb tSpecdevDzsb) {
        tSpecdevDzsb.setWarnFlag(flag);
        this.specdevDzsbService.updateTSpecdevDzsb(tSpecdevDzsb);
    }

    private void updateForYlgd(Long flag, TSpecdevYlgd tSpecdevYlgd) {
        tSpecdevYlgd.setWarnFlag(flag);
        this.specdevYlgdService.updateTSpecdevYlgd(tSpecdevYlgd);
    }

    private void updateForYlrq(Long flag, TSpecdevYlrq tSpecdevYlrq) {
        tSpecdevYlrq.setWarnFlag(flag);
        this.specdevYlrqService.updateTSpecdevYlrq(tSpecdevYlrq);
    }

    /**
     * 特种设备预警-邮件发送
     *
     * @param username       负责人姓名
     * @param email          负责人邮箱
     * @param alarmType      预警类型
     * @param warningLevel   预警等级
     * @param contenFormat   预警内容
     * @param warningContent 预警内容详细
     * @param urlSuffix      链接跳转对应路径
     */
    private void sendEmail(String username, String email, String alarmType, String warningLevel, String contenFormat, String warningContent, String urlSuffix) {
        //写html开始内容
        String start = "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title></title></head><body><div style=\"background-color:#ECECEC; padding: 35px;\">" +
                "<table cellpadding=\"0\" align=\"center\"" +
                "style=\"width: 600px; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;\">" +
                "<tbody><tr><th valign=\"middle\" style=\"height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #42a3d3; background-color: #49bcff; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;\">" +
                "<font face=\"微软雅黑\" size=\"5\" style=\"color: rgb(255, 255, 255); \">化工装置管理系统 </font><font face=\"微软雅黑\" size=\"3\" style=\"color: rgb(255, 255, 255); \">Chemical Plant Management System(CPMS)</font></th></tr>";
        //写html结尾内容
        String end = "</tbody></table></div></body></html>";
        //表html中间内容
        String prime = "";
        String firstcenter = "<tr><td><div style=\"padding:25px 35px 40px; background-color:#fff;\"><h2 style=\"margin: 5px 0px; \">" +
                "<font color=\"#333333\" style=\"line-height: 20px; \"><font style=\"line-height: 22px; \" size=\"4\">" +
                "亲爱的 username</font><br><font style=\"line-height: 22px; \" size=\"4\">" +
                "<p>您有一条预警信息：<br>" +
                "预警类型：<b>alarmType</b><br>" +
                "预警等级：<b>warningLevel</b><br>" +
                "预警内容contenFormat：<b>warningContent</b><br>" +
                "请前往<a href=\"requestJumpPath/urlSuffix\">alarmType</a>查看。<br>" +
                "<p align=\"right\">date</p>" +
                "<div style=\"width:700px;margin:0 auto;\">" +
                "<div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;\">" +
                "<p>此为系统邮件，请勿回复<br>This e-Mail is an automatic reminder sent by CPMS, please do not reply</p>" +
                "</div></div></div></td></tr>";
        String one = firstcenter.replaceFirst("username", username);
        String two = one.replaceFirst("alarmType", alarmType);
        String three = two.replaceFirst("warningLevel", warningLevel);
        String four = three.replaceFirst("contenFormat", contenFormat);
        String five = four.replaceFirst("warningContent", warningContent);
        String six = five.replaceFirst("urlSuffix", urlSuffix);
        String seven = six.replaceFirst("alarmType", alarmType);
        String eight = seven.replace("requestJumpPath", this.ruoyiConfig.getRequestJumpPath());
        String result = eight.replaceFirst("date", String.valueOf(new Date()));
        prime = prime + result;
        //拼接html
        String html = start + prime + end;
        this.mailService.sendHtmlMail(email, "CPMS：您有一条预警信息", html);
    }

    /**
     * 预警记录管理新增
     */
    public void insertHistory(TAlarmPrincipal a, String warningLevel, String warningContent, String typeId, String alarmType) {
        //预警记录管理新增
        TAlarmhistory alarmhistory = new TAlarmhistory();
        alarmhistory.setPlantCode(a.getPlantCode());
        alarmhistory.setTableName(alarmType);
        alarmhistory.setStaffid(a.getStaffid());
        alarmhistory.setDeptId(a.getDeptId());
        alarmhistory.setTypeId(Long.parseLong(typeId));
        alarmhistory.setAlarmLevel(warningLevel);
        alarmhistory.setAlarmContent(StringUtils.remove(warningContent, "<br>"));
        String[] content = StringUtils.remove(warningContent, "<br>").split(";");
        if (content.length == 0) {
            alarmhistory.setAlarmNum(Long.parseLong(String.valueOf("1")));
        } else {
            alarmhistory.setAlarmNum(Long.parseLong(String.valueOf(content.length)));
        }
        alarmhistory.setSendTime(new Date());
        alarmhistoryService.insertTAlarmhistory(alarmhistory);
    }

}
