package com.sys.bus.jx.service;

import com.jfinal.kit.PropKit;
import com.sys.bus.hr.model.Person;
import com.sys.bus.hr.service.HrService;
import com.sys.bus.model.*;
import com.sys.bus.syscode.service.SysCodeService;
import com.sys.core.utils.Tool;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;

public class MailService {
    Log log = LogFactory.getLog(this.getClass());
    static HrService hrService = new HrService();
    static String mailTo = PropKit.get("mailTo");
    static boolean devDebug = "true".equals(PropKit.get("devDebug"));
    static SysCodeService codeService = new SysCodeService();
    static String enterImage = "";
    static JxCommonService jxCommonService = new JxCommonService();
    //static RatingService ratingService = new RatingService();
//    static{
//        SysCode code = codeService.getSysCodeName("emailimage");
//        if(code!=null &&StringUtils.isNotBlank(code.getSyscodeValue())){
//            enterImage = code.getSyscodeValue();
//        }
//    }
    public String getShrUrl(){
       return  codeService.getSysCodeName("金蝶shr登录路径").getSyscodeValue();
    }
    public String getCurrDate(){
        return  Tool.getCurrDate();
    }
    public String getCurrDateTime(){
        return  Tool.getCurrTime();
    }

    /**
     *
     * @param id
     * @param idType 1 entry  2kpi  3ratingDept  4entryNo
     * @param type 10个人指标编辑提醒 11个人指标编辑催办 20领导确认指标提醒 21领导确认指标催办 30个人自评提醒 31个人自评催办 40评分提醒 41评分催办
     *      *      --50 部门评级提醒  51 部门评级催办  60 中心评级提醒  61 中心评级催办  70 绩效主管评级提醒 80 hr领导评级提醒
     */
    public void sendJxEmail(String id,String idType,String type) {
        //${考核周期}${shrUrl}${currDate}${currDateTime}$(dept)$(center)
//        List<Mailtemplate> mailTemplatList = Mailtemplate.dao.find("select * from ct_jx_mailtemplate where mailtemplate_type=?",type);
//        if(mailTemplatList.size()!=1){
//            throw new RuntimeException("类型："+type+"的邮件模板没配置或配置了多个！本次不发邮件！");
//        }
//        String tempContent = mailTemplatList.get(0).getMailtemplateContent();
//        if(StringUtils.isBlank(tempContent)){
//            throw new RuntimeException("类型："+type+"的邮件模板内容为空！本次不发邮件！");
//        }
//        tempContent = this.getContext(id,idType,tempContent);
//       Person person = this.getMail(id,idType,type);
//       if(person == null){
//           throw new RuntimeException("未获取到人员信息！id:"+id+",idType:"+idType+",type:"+type);
//       }
//       String mail = person.getEmail();
//        if(StringUtils.isBlank(mail)){
//            throw new RuntimeException(person.getName()+"的邮箱在shr里未配置！本次不发邮件！");
//        }
//       this.sendMail(mail,person.getName(),mailTemplatList.get(0).getMailtemplateTitle(),tempContent);
    }
    private Person getMail(String id,String idType,String type){
        String personId = null;
        if(("10".equals(type)||"11".equals(type)) && "1".equals(idType)){
            Entry entry = jxCommonService.getJxentryById(id);
            if(entry!=null)personId =  entry.getEntryKhpersonid();
        }else if(("20".equals(type)||"21".equals(type)) && "1".equals(idType)){
            Entry entry = jxCommonService.getJxentryById(id);
            if(entry!=null)personId =  entry.getEntrySuperId();
        }else if(("30".equals(type)||"31".equals(type)) && "1".equals(idType)){
            Entry entry = jxCommonService.getJxentryById(id);
            if(entry!=null)personId =  entry.getEntryKhpersonid();
        }else if(("40".equals(type)||"41".equals(type)) && "2".equals(idType)){
            Kpi kpi = Kpi.dao.findById(id);
           // if(kpi!=null)personId =  kpi.getKpiGraderid();
        }else if(("50".equals(type)||"51".equals(type)) && "3".equals(idType)){
//            RatingDept ratingDept = ratingService.getRatingDeptById(id);
//            personId =  ratingDept.getRatingdeptManagerid();
        }else if(("60".equals(type)||"61".equals(type)) && "3".equals(idType)){
//            RatingDept ratingDept = ratingService.getRatingDeptById(id);
//            personId =  ratingDept.getRatingdeptManagerid();
        }else if("70".equals(type) && "4".equals(idType)){
            EntryNo entryNo = jxCommonService.getEntryNoById(id);
            if(entryNo!=null)personId = entryNo.getEntrynoJxpersonid();
        }else if("80".equals(type) && "4".equals(idType)){
            EntryNo entryNo = jxCommonService.getEntryNoById(id);
            if(entryNo!=null)personId = entryNo.getEntrynoHrmanagerid();
        }
        if(StringUtils.isNotBlank(personId)) {
           return  hrService.getPersonById(personId);
        }
        return null;
    }

    /**
     *
     * @param id
     * @param idType   1 entry  2kpi  3ratingDept  4entryNo
     * @param tempContent
     * @return
     */
    private String getContext(String id,String idType,String tempContent){
        if(tempContent.indexOf("${shrUrl}")>=0){
            tempContent = tempContent.replace("${shrUrl}",getShrUrl());

        }
        if(tempContent.indexOf("${currDate}")>=0){
            tempContent = tempContent.replace("${currDate}",getCurrDate());

        }
        if(tempContent.indexOf("${currDateTime}")>=0){
            tempContent = tempContent.replace("${currDateTime}",getCurrDateTime());

        }
        EntryNo entryNo = null;
        if("1".equals(idType)){
            entryNo = jxCommonService.getEntryNoByEntryId(id);
        }else if("2".equals(idType)){
            entryNo = jxCommonService.getEntryNoByKpiId(id);
        }else if("3".equals(idType)){
            entryNo = jxCommonService.getEntryNoByRatingDeptId(id);
        }else if("4".equals(idType)){
            entryNo = jxCommonService.getEntryNoById(id);
        }

        if(tempContent.indexOf("${考核周期}")>=0){
            tempContent = tempContent.replace("${考核周期}",entryNo.getEntrynoCyclename());
        }

        if(tempContent.indexOf("${dept}")>=0){
            tempContent = tempContent.replace("${dept}",entryNo.getStr("ratingdept_name"));
        }
        if(tempContent.indexOf("${center}")>=0){
            tempContent = tempContent.replace("${center}",entryNo.getStr("ratingdept_name"));
        }

        return tempContent;
    }

    public void sendMail(String to,String toPerson, String title, String context) {

        if (StringUtils.isNotBlank(enterImage)) {
           // context = context.replaceFirst("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", "&nbsp;<span style='font-size: 10.5pt; line-height: 1.5; background-color: window;'>如下图所示：</span><br><img style=\"CURSOR: hand\" onload=\"javascript:if(this.width>screen.width-333)this.width=screen.width-333\" src=\"" + enterImage + "\">");
        }
        if (StringUtils.isBlank(mailTo) && devDebug) {
            return;
        }
        if (StringUtils.isNotBlank(mailTo)) {
            to = mailTo;
        }

        if (StringUtils.isBlank(to)) {
            return;
        }
        Email email = new Email();
        email.setEmailId(Tool.uuid());
        email.setEmailStatus("0");
        email.setEmailText(context);
        email.setEmailTime(Tool.getCurrTime());
        email.setEmailTitle(title);
        email.setEmailTo(to);
        email.setEmailToPerson(toPerson);
        email.save();
    }
}