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);
|
}
|
}
|
}
|
}
|
}
|