package com.ruoyi.framework.task;

import com.ruoyi.common.sendEmail.IMailService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SpringContextUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.patrol.patrol.domain.TPatrolMain;
import com.ruoyi.project.patrol.patrol.domain.TPatrolPlan;
import com.ruoyi.project.patrol.patrol.service.ITPatrolMainService;
import com.ruoyi.project.patrol.patrol.service.ITPatrolPlanService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component("planTask")
public class PlanTask {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITPatrolMainService tPatrolMainService;

    @Autowired
    private ITPatrolPlanService tPatrolPlanService;

    @Autowired
    private ISysUserService sysUserService;


    public void generatePlan() {
        TPatrolPlan tPatrolPlan = new TPatrolPlan();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        tPatrolPlan.setPlanQuarter((long) (calendar.get(Calendar.MONTH) + 1));
        tPatrolPlan.setPlanYear((long) (calendar.get(Calendar.YEAR)));
        TPatrolMain tPatrolMain = new TPatrolMain();
        tPatrolMain.setYear(String.valueOf(calendar.get(Calendar.YEAR)));
        if (calendar.get(Calendar.DATE) <= 15) {
            // 每三个月一次的
            if (calendar.get(Calendar.MONTH) == Calendar.FEBRUARY || calendar.get(Calendar.MONTH) == Calendar.MAY || calendar.get(Calendar.MONTH) == Calendar.AUGUST || calendar.get(Calendar.MONTH) == Calendar.NOVEMBER) {
                // 每三个月一次巡检的设备，中间月创建巡检计划
                tPatrolMain.setCheckCycle("3");
                List<TPatrolMain> tPatrolMains = tPatrolMainService.selectTPatrolMainList(tPatrolMain);
                for (TPatrolMain patrolMain : tPatrolMains) {
                    tPatrolPlan.setPatrolType(patrolMain.getPatrolType());
                    tPatrolPlan.setPlanName(calendar.get(Calendar.YEAR) + "年-" + (calendar.get(Calendar.MONTH) + 1) + "月" + "-固定计划-" + patrolMain.getDevName());
                    tPatrolPlanService.add(tPatrolPlan, "1");
                }
            }
            //每半年一次的
            if (calendar.get(Calendar.MONTH) == Calendar.JANUARY || calendar.get(Calendar.MONTH) == Calendar.JULY) {
                // 每半年一次巡检的设备，1、7月创建巡检计划
                tPatrolMain.setCheckCycle("4");
                List<TPatrolMain> tPatrolMains = tPatrolMainService.selectTPatrolMainList(tPatrolMain);
                String val = "";
                if (calendar.get(Calendar.MONTH) == Calendar.JANUARY)
                    val = "-上半年-";
                else
                    val = "-下半年-";
                for (TPatrolMain patrolMain : tPatrolMains) {
                    tPatrolPlan.setPatrolType(patrolMain.getPatrolType());
                    tPatrolPlan.setPlanName(calendar.get(Calendar.YEAR) + "年-" + val + "-固定计划-" + patrolMain.getDevName());
                    tPatrolPlanService.add(tPatrolPlan, "1");
                }
            }
            //每年一次的
            if (calendar.get(Calendar.MONTH) == Calendar.JANUARY) {
                // 每年一次巡检的设备，1月创建巡检计划
                tPatrolMain.setCheckCycle("5");
                List<TPatrolMain> tPatrolMains = tPatrolMainService.selectTPatrolMainList(tPatrolMain);
                for (TPatrolMain patrolMain : tPatrolMains) {
                    tPatrolPlan.setPatrolType(patrolMain.getPatrolType());
                    tPatrolPlan.setPlanName(calendar.get(Calendar.YEAR) + "年-" + "-固定计划-" + patrolMain.getDevName());
                    tPatrolPlanService.add(tPatrolPlan, "1");
                }
            }
            tPatrolMain.setCheckCycle("2");
            List<TPatrolMain> tPatrolMains = tPatrolMainService.selectTPatrolMainList(tPatrolMain);
            for (TPatrolMain patrolMain : tPatrolMains) {
                tPatrolPlan.setPatrolType(patrolMain.getPatrolType());
                tPatrolPlan.setPlanName(calendar.get(Calendar.YEAR) + "年-" + (calendar.get(Calendar.MONTH) + 1) + "月" + "-固定计划-" + patrolMain.getDevName());
                tPatrolPlanService.add(tPatrolPlan, "1");
            }
        }
        tPatrolMain.setCheckCycle("1");
        List<TPatrolMain> tPatrolMains = tPatrolMainService.selectTPatrolMainList(tPatrolMain);
        for (TPatrolMain patrolMain : tPatrolMains) {
            String val = "";
            if (patrolMain.getPatrolType().equals("1")) {
                if (calendar.get(Calendar.DATE) <= 15) {
                    val = "-上半月-";
                    tPatrolPlan.setDetectionFrequency("1");
                } else {
                    tPatrolPlan.setDetectionFrequency("2");
                    val = "-下半月-";
                }
            }
            tPatrolPlan.setPatrolType(patrolMain.getPatrolType());
            tPatrolPlan.setPlanName(calendar.get(Calendar.YEAR) + "年-" + (calendar.get(Calendar.MONTH) + 1) + "月" + val + "-固定计划-" + patrolMain.getDevName());
            tPatrolPlanService.add(tPatrolPlan, "1");
        }
    }

    public void generateMain() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        TPatrolMain tPatrolMain = new TPatrolMain();
        tPatrolMain.setYear(String.valueOf(calendar.get(Calendar.YEAR)));
        for (TPatrolMain patrolMain : tPatrolMainService.selectTPatrolMainList(tPatrolMain)) {
            TPatrolMain main = new TPatrolMain();
            main.setPatrolType(patrolMain.getPatrolType());
            main.setYear(String.valueOf(calendar.get(Calendar.YEAR) + 1));
            if (CollectionUtils.isNotEmpty(tPatrolMainService.selectTPatrolMainList(main))) {
                continue;
            }
            main.setCheckCycle(patrolMain.getCheckCycle());
            main.setDevLink(patrolMain.getDevLink());
            main.setDevName(patrolMain.getDevName());
            main.setDevNum(patrolMain.getDevNum());
            if ("1".equals((patrolMain.getCheckCycle()))) {
                main.setJan("0,0");
                main.setFeb("0,0");
                main.setMar("0,0");
                main.setApr("0,0");
                main.setMay("0,0");
                main.setJun("0,0");
                main.setJul("0,0");
                main.setAug("0,0");
                main.setSep("0,0");
                main.setOct("0,0");
                main.setNov("0,0");
                main.setDec("0,0");
            } else if ("2".equals((patrolMain.getCheckCycle()))) {
                main.setJan("0");
                main.setFeb("0");
                main.setMar("0");
                main.setApr("0");
                main.setMay("0");
                main.setJun("0");
                main.setJul("0");
                main.setAug("0");
                main.setSep("0");
                main.setOct("0");
                main.setNov("0");
                main.setDec("0");
            } else if ("3".equals((patrolMain.getCheckCycle()))) {
                main.setJan("-");
                main.setFeb("0");
                main.setMar("-");
                main.setApr("-");
                main.setMay("0");
                main.setJun("-");
                main.setJul("-");
                main.setAug("0");
                main.setSep("-");
                main.setOct("-");
                main.setNov("0");
                main.setDec("-");
            } else if ("4".equals((patrolMain.getCheckCycle()))) {
                main.setJan("0");
                main.setFeb("-");
                main.setMar("-");
                main.setApr("-");
                main.setMay("-");
                main.setJun("-");
                main.setJul("0");
                main.setAug("-");
                main.setSep("-");
                main.setOct("-");
                main.setNov("-");
                main.setDec("-");
            } else if ("5".equals((patrolMain.getCheckCycle()))) {
                main.setJan("0");
                main.setFeb("-");
                main.setMar("-");
                main.setApr("-");
                main.setMay("-");
                main.setJun("-");
                main.setJul("-");
                main.setAug("-");
                main.setSep("-");
                main.setOct("-");
                main.setNov("-");
                main.setDec("-");
            }
            main.setPersonLiable(patrolMain.getPersonLiable());
            main.setCreaterCode("1");
            main.setCreatedate(new Date());
            tPatrolMainService.insertTPatrolMain(main);
        }

    }

    public void emailReminder() {
        IMailService mailService = (IMailService) SpringContextUtils.getBean("mailService");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        TPatrolPlan tPatrolPlan = new TPatrolPlan();
        tPatrolPlan.setStatus(0L);
        tPatrolPlan.setPlanYear((long) calendar.get(Calendar.YEAR));
        tPatrolPlan.setPlanQuarter((long) calendar.get(Calendar.MONTH) + 1);
        if (calendar.get(Calendar.DATE) < 15) {
            tPatrolPlan.setDetectionFrequency("1");
        }
        List<TPatrolPlan> tPatrolPlans = tPatrolPlanService.selectTPatrolPlanList(tPatrolPlan);
        if (CollectionUtils.isEmpty(tPatrolPlans)) {
            return;
        }
        Set<String> names = new HashSet<>();
        for (TPatrolPlan patrolPlan : tPatrolPlans) {
            TPatrolMain main = new TPatrolMain();
            main.setYear(String.valueOf(calendar.get(Calendar.YEAR)));
            main.setPatrolType(patrolPlan.getPatrolType());
            for (TPatrolMain tPatrolMain : tPatrolMainService.selectTPatrolMainList(main)) {
                if (StringUtils.isNotEmpty(tPatrolMain.getPersonLiable())) {
                    names.addAll(Arrays.asList(tPatrolMain.getPersonLiable().split("、")));
                }
            }
        }
        List<SysUser> users = new ArrayList<>();
        for (String name : names) {
            SysUser sysUser = new SysUser();
            sysUser.setNickName(name);
//            users.addAll(sysUserService.selectAllUser(sysUser));
        }
        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); \">EHS管理助手</font></th></tr>";
        //表html中间内容
        String center = "<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(loginName)</font><br><font style=\"line-height: 22px; \" size=\"4\">" +
                "<p>您有" + tPatrolPlans.size() + "条巡检计划将于三天后截止：<br>" +
                "请登录<a href=\"http://43.143.92.79:93/plan\">检查计划</a>或EHS管理助手APP查看。<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>此为系统邮件，请勿回复</p>" +
                "</div></div></div></td></tr>";
        for (SysUser user : users) {
            String one = center.replaceFirst("username", user.getNickName());
            String two = one.replace("loginName", user.getUserName());
            String result = two.replaceFirst("date", DateUtils.getDate());
            String end = "</tbody></table></div></body></html>";
            //拼接html
            String html = start + result + end;
            logger.info("html:" + html);
            if (StringUtils.isNotEmpty(user.getEmail()))
                mailService.sendHtmlMail(user.getEmail(), "您有巡检任务尚未完成", html);
        }

    }
}
