whycxzp
2025-05-23 44c257f55f2cce190762742e75573ab9f0961ce7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.whyc.schedule;
 
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.Software;
import com.whyc.service.SoftwareService;
import com.whyc.service.UserService;
import com.whyc.util.DateUtil;
import com.whyc.util.MailUtil;
import com.whyc.util.ThreadLocalUtil;
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 javax.mail.MessagingException;
import java.util.Arrays;
import java.util.List;
 
/**
 * 软件升级申请邮件推送定时任务
 */
@EnableScheduling
@Component
public class SoftwareMailSchedule {
 
    @Autowired
    private MailUtil mailUtil;
 
    @Autowired
    private SoftwareService softwareService;
 
    @Autowired
    private UserService userService;
 
    /**数据4秒钟获取一次*/
    @Scheduled(cron = "0/4 * * * * ? ")
    public void sendMail(){
        //查询需要发送邮件
        List<Software> list = softwareService.getSendList();
        //查询研发角色的所有用户邮箱
        List<String> toArr = userService.getEmailList();
        //String[] toArrStr = new String[]{"perryhsu@163.com"};
        //List toArr = Arrays.asList(toArrStr);
 
        for (int i = 0; i < list.size(); i++){
            Software software = list.get(i);
            ThreadPoolExecutorFactory.getPoolExecutor().execute(() -> {
                // 发送邮件
                try {
                    mailUtil.sendMailBatch(toArr, "长城汽车-电池系统软件升级申请", "SN码:"+software.getSnCode()+"\n软件序列号:"+software.getSerialNumber()+ "\n物料编码:"+software.getMaterialCode()+"\n版本:"+software.getVersion()+"\n申请时间:"+ ThreadLocalUtil.format(software.getApplyTime(),1));
                    //更新邮件发送状态
                    softwareService.updateMailStatus(software.getId());
                } catch (MessagingException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    }
 
 
}