package com.ruoyi.framework.task.meeting;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.sendEmail.IMailService;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.plant.domain.TMtActionlist;
import com.ruoyi.project.plant.domain.TMtKeymaintenance;
import com.ruoyi.project.plant.domain.TMtMessageattention;
import com.ruoyi.project.plant.domain.TMtdEmail;
import com.ruoyi.project.plant.service.ITMtActionlistService;
import com.ruoyi.project.plant.service.ITMtKeymaintenanceService;
import com.ruoyi.project.plant.service.ITMtMessageattentionService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("weekMeetingTask")
public class WeekMeetingTask extends BaseController
{
    @Autowired
    private ITMtActionlistService tMtActionlistService;
    @Autowired
    private ITMtKeymaintenanceService tMtKeymaintenanceService;
    @Autowired
    private ITMtMessageattentionService tMtMessageattentionService;
    @Autowired
    private ISysUserService sysUserService;
    //系统基础配置
    @Autowired
    private RuoYiConfig ruoyiConfig;

    @Autowired
    private IMailService mailService;

    public void weekMeetingTask()
    {
        //会议执行
        TMtActionlist actionParam = new TMtActionlist();
        actionParam.setIsHis(0l);
        List<TMtActionlist> actionlists = tMtActionlistService.selectTMtActionlistList(actionParam);
        for (TMtActionlist t: actionlists
             ) {
            if ( "1".equals(t.getIsPerson()) && !"3".equals(t.getStatus()) ) {
                long now = System.currentTimeMillis();
                long diff = t.getDeadline().getTime() - now;
                long nd = 1000 * 24 * 60 * 60;
                long day = diff / nd;
                // 60天到53天
                if (day > 53 && day <= 60) {
                    sendContent(t);
                }else if (day <= 30) {
                    sendContent(t);
                }
            }
        }
        //关键维修
        TMtKeymaintenance keyParam = new TMtKeymaintenance();
        keyParam.setIsHis(0l);
        List<TMtKeymaintenance> keylists = tMtKeymaintenanceService.selectTMtKeymaintenanceList(keyParam);
        for (TMtKeymaintenance t: keylists
        ) {
            if ( "1".equals(t.getIsPerson()) && !"3".equals(t.getStatus()) ) {
                long now = System.currentTimeMillis();
                long diff = t.getDeadline().getTime() - now;
                long nd = 1000 * 24 * 60 * 60;
                long day = diff / nd;
                // 60天到53天
                if (day > 53 && day <= 60) {
                    sendContent(t);
                }else if (day <= 30) {
                    sendContent(t);
                }
            }
        }

        //信息
//        TMtMessageattention meParam = new TMtMessageattention();
//        meParam.setIsHis(0l);
//        List<TMtMessageattention> melists = tMtMessageattentionService.selectTMtMessageattentionList(meParam);
//        for (TMtMessageattention t: melists
//        ) {
//            if ( "1".equals(t.getIsPerson()) && !"3".equals(t.getStatus()) ) {
//                long now = System.currentTimeMillis();
//                long diff = t.getDeadline().getTime() - now;
//                long nd = 1000 * 24 * 60 * 60;
//                long day = diff / nd;
//                // 60天到53天
//                if (day > 53 && day <= 60) {
//                    sendContent(t);
//                }else if (day <= 30) {
//                    sendContent(t);
//                }
//            }
//        }

    }

    private void sendContent(TMtKeymaintenance t) {
    }

    private void sendContent(TMtActionlist t) {
        ArrayList<String> emailList = new ArrayList<>();
        if (StringUtils.isNotEmpty(t.getResponsible())) {
            String[] respArr = t.getResponsible().split(",");
            for (int i = 0; i < respArr.length; i++) {
                SysUser user = sysUserService.selectUserByStaffId(respArr[i]);
                if (user != null) {
                   emailList.add(user.getEmail());
                }
            }
        }
        Map<String,String> action = new HashMap<>();

        String pattern = "yyyy年MM月dd日"; // 定义日期格式化模板
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); // 创建日期格式化器
        action.put("action",t.getAction());
        action.put("deadline",dateFormat.format(t.getDeadline()));
        logger.info(JSON.toJSONString(action));
        logger.info(JSON.toJSONString(emailList));
        sendMail(action ,emailList );
    }

    private void sendMail(Map<String, String> action, ArrayList<String> emailList) {
        try {
            //写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>";
            String username = "";

//        email = "735032128@qq.com";
            //表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\">" +
                    "亲爱的 CPMS用户</font><br><font style=\"line-height: 22px; \" size=\"4\">" +
                    "<p>您有一条周会议待完成：<br>" +
                    "行动：<b>action</b><br>" +
                    "截止日期：<b>deadline</b><br>" +
                    "请前往<a href=\"requestJumpPath/plant/meeting/meeting\">周历史会议</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 pattern = "yyyy年MM月dd日"; // 定义日期格式化模板

            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); // 创建日期格式化器



            String one = firstcenter.replaceFirst("action", action.get("action"));
            String two = one.replace("deadline", action.get("deadline"));
            String three = two.replace("requestJumpPath", this.ruoyiConfig.getRequestJumpPath());
            String result = three.replaceFirst("date", dateFormat.format(new Date()));
            prime = prime + result;
            //拼接html
            String html = start + prime + end;
            String[] email = emailList.toArray(new String[emailList.size()]);
            mailService.sendHtmlMail(email, "周会议预警：您有一条周会议行动项待完成", html);

        } catch (Exception e) {

            logger.error("e" , e);
        }finally {

        }
    }





}
