cjl
2024-01-24 3dbc786738cdf005fac8ae5c27023e39a0554a68
screen-job/src/main/java/com/moral/api/controller/PubController.java
@@ -1,6 +1,9 @@
package com.moral.api.controller;
import com.moral.api.service.*;
import com.moral.api.util.DeviceExcelDTO;
import com.moral.api.util.EmailSpringUtil;
import com.moral.api.util.ExcelUtil;
import com.moral.api.util.HttpUtils;
import com.moral.constant.ResultMessage;
import com.moral.util.DateUtils;
@@ -10,13 +13,15 @@
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.mail.MessagingException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
/**
 * @ClassName UserController
@@ -46,6 +51,10 @@
    private HistoryHourlyService historyHourlyService;
    @Autowired
    private HistoryMonthlyService historyMonthlyService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private EmailSpringUtil emailSpringUtil;
    @GetMapping("insertHistoryDaily")
    @ApiOperation(value = "天数据补录", notes = "天数据补录")
@@ -159,5 +168,32 @@
        }
    }
    @GetMapping("excelDevice")
    @ApiOperation(value = "excle", notes = "excle")
    public void  excelDevice() 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 = "909710561@qq.com";
            emailSpringUtil.sendEmail("设备分钟数据",content,false,"909710561@qq.com",
                    toMail,"null",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();
            }
        }
    }
}