package com.ruoyi.framework.task;

import com.alibaba.fastjson.JSON;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.ruoyi.common.sendEmail.IMailService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SpringContextUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.TAlarmPrincipal;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.system.service.ITAlarmPrincipalService;
import com.ruoyi.project.training.domain.TTrainingRegular;
import com.ruoyi.project.training.service.ITTrainingRegularService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component("trainingRegularTask")
public class TrainingRegularTask extends BaseController {

    @Autowired
    private ITTrainingRegularService tTrainingRegularService;

    @Autowired
    private ISysUserService sysUserService;
    private static Long AlarmtypeId = 1300l;
    /**
     * 注入报警管理负责人的接口
     */
    @Autowired
    private ITAlarmPrincipalService alarmPrincipalService;

    public void execute(Long deptId) {
        try {
            TTrainingRegular tTrainingRegular = new TTrainingRegular();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            tTrainingRegular.setPlanDate(sdf.format(new Date()));
            tTrainingRegular.setDeptId(deptId);
            List<TTrainingRegular> list = tTrainingRegularService.selectTTrainingRegularByPlanDate(tTrainingRegular);
            IMailService mailService = (IMailService) SpringContextUtils.getBean("mailService");
            logger.info("=====================本次装置培训计划清单：{}", list);

            TAlarmPrincipal alarmPrincipal = new TAlarmPrincipal();
            //预警类型对应负责人
            alarmPrincipal.setTypeId(AlarmtypeId);
            List<TAlarmPrincipal> alarmPrincipals = alarmPrincipalService.selectList(alarmPrincipal);
            System.out.println(JSON.toJSONString(alarmPrincipals));
            for (TAlarmPrincipal t : alarmPrincipals) {
                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 (TTrainingRegular regular : list) {
                for (String staffId : regular.getLecturer().split(",")) {
                    try {
                        SysUser user = sysUserService.selectUserByStaffId(staffId);
                        String username = user.getNickName();
                        String loginName = user.getUserName();
                        String usernameEN = PinyinHelper.convertToPinyinString(username, " ", PinyinFormat.WITHOUT_TONE);
                        String email = user.getEmail();
                        logger.info("=====================本次装置培训计划邮件发送给授课人：{}", username);
                        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); \">Lecture reminder</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\">" +
                                "Dear usernameEN(loginName)</font></font></h2>" +
                                "<p>您本月有装置培训计划。" +"<br>" +
                                "You have a device training plan for this month." +"<br>" +
                                "培训名称：" +regular.getItem()+ "<br>" +
                                "Training name:" +regular.getItem()+ "<br>" +
                                "请登录<a href=\"https://cpms.basf-ypc.net.cn/cpms/index.html#/training/regular\">装置培训计划</a>查看。<br>" +
                                "Please log in the <a href=\"https://cpms.basf-ypc.net.cn/cpms/index.html#/training/regular\">Device Training Plan</a> to handle it.</p>" +
                                "<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 = center.replaceFirst("username", username);
                        String two = one.replaceFirst("usernameEN", usernameEN);
                        String three = two.replace("loginName", loginName);
                        String result = three.replaceFirst("date", DateUtils.getDate());
                        String end = "</tbody></table></div></body></html>";
                        //拼接html
                        String html = start + result + end;
                        logger.info("html:" + html);
                        if (deptId == 10010) {
                            String emailCc = alarmPrincipals.get(0).getPrincipalEmail();
                            mailService.sendHtmlMail(email, "您本月有装置培训计划 You have a device training plan for this month", html,emailCc);
                        }else if (deptId == 1003){
                            mailService.sendHtmlMail(email, "您本月有装置培训计划 You have a device training plan for this month", html);
                        }
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
