李宇
2021-01-28 2f52e8c752122625f189ae7657e621db0d6d253c
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
package com.nanometer.smartlab.email;
 
import com.nanometer.smartlab.entity.EmailStatus;
import com.nanometer.smartlab.entity.enumtype.EmailSendStatus;
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.service.*;
import com.nanometer.smartlab.util.EmailSend;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.List;
 
/**
 * Created by wjd on 18/6/18.
 */
@Component
public class EmailSendSchedule {
    @Lazy
    @Resource
    private OpeOrderService opeOrderService;
 
    private static Logger logger = Logger.getLogger(EmailSendSchedule.class);
 
    @Scheduled(fixedDelayString = "${emailSend.schedule.delay}")
    public void EmailSendSchedule(){
 
        List<EmailStatus> list = null;
        try {
            int count = this.opeOrderService.getUnsendEmailCount();
            if (count > 0) {
                list = opeOrderService.getUnsendEmailList();
            }
        } catch (Exception e) {
            logger.error(e);
        }
 
        if(list != null){
 
            for (EmailStatus email: list) {
                try {
                    EmailSend.send(email.getEmailAddress(), email.getApplyCode());
                    //  发送成功,更新邮件发送状态为:已发送
                    email.setStatus(EmailSendStatus.SEND_SUCCESS);
                    this.opeOrderService.updateEmailStatus(email);
                } catch (BusinessException e) {
                    //  发送失败,更新邮件发送状态为:发送失败
                    email.setStatus(EmailSendStatus.SEND_FAIL);
                    this.opeOrderService.updateEmailStatus(email);
                    logger.warn("操作失败。", e);
                } catch (Exception e) {
                    logger.error(e);
                }
            }
        }
    }
}