package com.ruoyi.project.training.spec.controller.scheduler;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.ruoyi.common.sendEmail.IMailService;
import com.ruoyi.common.thread.Trainning.MeetingFeedbackMailThread;
import com.ruoyi.project.system.domain.SysMessage;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysMessageService;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.training.spec.domain.TStFeedback;
import com.ruoyi.project.training.spec.service.ITStFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专项培训反馈定时任务
 *
 * @author Wang Zi Wen
 * @email wangggziwen@163.com
 * @date 2022/05/24 13:52:00
 */
@Component
@EnableScheduling
public class TStFeedbackScheduler {

    @Autowired
    private ISysMessageService sysMessageService;

    @Autowired
    private ITStFeedbackService tStFeedbackService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IMailService mailService;

    /**
     * 会议时间一周后给学员发送邮件
     * 定时任务触发条件：每日8：00
     */
    @Scheduled(cron = "0 0 8 * * ?")
    private void mailSuccessorAfterMeeting() throws PinyinException {
        // 获取所有反馈记录
        List<TStFeedback> tStFeedbackList = tStFeedbackService.selectTStFeedbackList(null);
        // 过滤出所有会议日期不为空的反馈记录
        tStFeedbackList = tStFeedbackList.stream().filter(tStFeedback -> tStFeedback.getMeetingDate()!=null).collect(Collectors.toList());
        // 过滤出会议日期为七日前的反馈记录
        tStFeedbackList = tStFeedbackList.stream().filter(tStFeedback -> {
            // 当前日期毫秒数
            Long dateMillis = new Date().getTime();
            // 会议日期毫秒数
            Long meetingDateMillis = tStFeedback.getMeetingDate().getTime();
            // 是否发送邮件
            boolean sendMail = false;
            // 当前日期和会议日期毫秒差
            long diff = dateMillis - meetingDateMillis;
            long minMillis = 3600 * 1000 * 24 * 7;
            long maxMillis = 3600 * 1000 * 24 * 8;
            // 7<=当前日期和会议日期毫秒差<8天
            if (diff>=minMillis && diff<maxMillis) {
                sendMail = true;
            }
            return sendMail;
        }).collect(Collectors.toList());
        // 发送邮件
        for (TStFeedback feedback : tStFeedbackList) {
            // 根据学员staffId获取用户对象
            SysUser successor = sysUserService.selectUserByStaffId(feedback.getSuccessorId());
            String email = successor.getEmail();
            String username = successor.getNickName();
            String usernameEN = PinyinHelper.convertToPinyinString(username, " ", PinyinFormat.WITHOUT_TONE);
            String feedbackYear = feedback.getFeedbackYear();
            String feedbackSeason = feedback.getFeedbackSeason();
            Date meetingDate = feedback.getMeetingDate();
            String meetingDateString = "20" + (meetingDate.getYear() + "年" + (meetingDate.getMonth() + 1 + "月") + meetingDate.getDate() + "日").substring(1);
            // 根据导师staffId获取用户对象
            SysUser mentor = sysUserService.selectUserByStaffId(feedback.getMentorId());
            // 参会导师名单
            String mentorNamesString = mentor.getNickName();
            TStFeedback queryObj = new TStFeedback();
            queryObj.setParentId(feedback.getId());
            List<TStFeedback> invitedMentorsFeedback = tStFeedbackService.selectTStFeedbackList(queryObj);
            for (TStFeedback tStFeedback : invitedMentorsFeedback) {
                // 根据受邀导师staffId获取用户对象
                SysUser invitedMentor = sysUserService.selectUserByStaffId(tStFeedback.getMentorId());
                mentorNamesString += "，";
                mentorNamesString += invitedMentor.getNickName();
            }
            // 系统通知
            SysMessage message = new SysMessage();
            message.setUserId(successor.getUserId());
            message.setMsgTitle("您有一条系统消息");
            message.setMsgContent("您有一条季度汇报消息待查看");
            // 邮件通知
            MeetingFeedbackMailThread meetingFeedbackMailThread = new MeetingFeedbackMailThread(
                    mailService, email, username, usernameEN, feedbackYear, feedbackSeason, meetingDateString,
                    mentorNamesString, sysMessageService, message);
            Thread thread = new Thread(meetingFeedbackMailThread);
            thread.start();
        }
    }

}

