package com.ruoyi.project.listener.yearSemsApprove;


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.thread.sems.SendEmailThead;
import com.ruoyi.common.utils.SpringContextUtils;
import com.ruoyi.project.sems.domain.TApprove;
import com.ruoyi.project.sems.service.ITApproveService;
import com.ruoyi.project.system.service.ISysUserService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;


@Service("auditorTaskCreateListener")
public class AuditorTaskCreateListener implements Serializable, TaskListener {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    private static final long serialVersionUID = -375905405805540186L;

    @Override
    public void notify(DelegateTask delegateTask) {
        Long auditor = (Long) delegateTask.getVariables().get("auditor");
        String id = delegateTask.getExecution().getProcessInstanceBusinessKey();
        ITApproveService tApproveService = (ITApproveService) SpringContextUtils.getBean("tApproveService");
        ISysUserService sysUserService = (ISysUserService) SpringContextUtils.getBean("sysUserService");
        IMailService mailService = (IMailService) SpringContextUtils.getBean("mailService");
        TApprove approveEntity = tApproveService.getById(id);
        logger.info("approveEntity:" + approveEntity);
        //发送邮件
        try {
            String email = sysUserService.selectUserById(auditor).getEmail();
            String apNo = approveEntity.getApNo();
            String username = sysUserService.selectUserById(auditor).getNickName();
            String usernameEN = PinyinHelper.convertToPinyinString(username, " ", PinyinFormat.WITHOUT_TONE);
            logger.info("email:" + email);
            logger.info("username:" + username);
            //发送邮件
            String sendType = "year";
            SendEmailThead sendEmailThead = new SendEmailThead(email,apNo, username,usernameEN, mailService, approveEntity, sendType);
            Thread t = new Thread(sendEmailThead);
            t.start();
        }catch (Exception e) {
            logger.error("邮件发送失败" + JSON.toJSONString(e));
        }
    }

    public void generate(HttpServletRequest request, HttpServletResponse response) {

    }
}
