cjl
2024-01-24 3dbc786738cdf005fac8ae5c27023e39a0554a68
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
62
63
64
65
66
67
68
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.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
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 EmilTask {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private EmailSpringUtil emailSpringUtil;
    @XxlJob("emilTask")
    public ReturnT 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 = Objects.requireNonNull(this.getClass().getClassLoader().getResource("")).getPath();
        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 = "ad@7drlb.com";
            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();
            return new ReturnT(ReturnT.FAIL_CODE, e.getMessage());
        } catch (MessagingException e) {
            log.error(String.format("邮件发送失败,原因:%s",e));
            e.printStackTrace();
            return new ReturnT(ReturnT.FAIL_CODE, e.getMessage());
        }finally {
            if(out != null){
                out.close();
            }
        }
        return ReturnT.SUCCESS;
 
    }
}