New file |
| | |
| | | package com.moral.api.task; |
| | | |
| | | import com.moral.api.service.DeviceService; |
| | | import com.moral.api.util.DeviceExcelDTO; |
| | | import com.moral.api.util.EmailSpringUtil; |
| | | import com.moral.api.util.ExcelUtil; |
| | | import com.moral.util.DateUtils; |
| | | import com.xxl.job.core.handler.annotation.XxlJob; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.core.io.ByteArrayResource; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.mail.MessagingException; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * @ClassName EmaleTask |
| | | * @Description TODO |
| | | * @Author @cjl |
| | | * @Date 2024-01-24 15:33 |
| | | * @Version 1.0 |
| | | */ |
| | | @Component |
| | | @Slf4j |
| | | public class EmilInsetTask { |
| | | @Autowired |
| | | private DeviceService deviceService; |
| | | private final EmailSpringUtil emailSpringUtil; |
| | | |
| | | public EmilInsetTask(EmailSpringUtil emailSpringUtil) { |
| | | this.emailSpringUtil = emailSpringUtil; |
| | | } |
| | | |
| | | @XxlJob("emilTask") |
| | | public void emilTask()throws IOException { |
| | | String startTime = DateUtils.dateToDateString(DateUtils.addDays(new Date(),-1),DateUtils.yyyy_MM_dd_EN); |
| | | List<DeviceExcelDTO> list = deviceService.ListDeviceExcel(startTime,null,72); |
| | | String path = System.getProperty("user.dir")+"/"; |
| | | String fileName = String.format("%s疾控中心数据-%s.xlsx",path,startTime); |
| | | ByteArrayOutputStream out = null; |
| | | try { |
| | | // 生成excel文件 |
| | | out = ExcelUtil.generateExcel(list, DeviceExcelDTO.class); |
| | | // 发送邮件 |
| | | String content = startTime+"疾控中心设备统计数据"; |
| | | String toMail = "lili@nieh.chinacdc.cn"; |
| | | emailSpringUtil.sendEmail("设备分钟数据",content,false,"909710561@qq.com", |
| | | toMail,"909710561@qq.com",null, fileName, new ByteArrayResource(out.toByteArray())); |
| | | } catch (IOException e) { |
| | | log.error(String.format("生成excel失败,原因:%s",e)); |
| | | e.printStackTrace(); |
| | | } catch (MessagingException e) { |
| | | log.error(String.format("邮件发送失败,原因:%s",e)); |
| | | e.printStackTrace(); |
| | | }finally { |
| | | if(out != null){ |
| | | out.close(); |
| | | } |
| | | } |
| | | log.info("邮件发送成功"); |
| | | } |
| | | } |