Explorar el Código

年月日定时任务分开

LLL hace 4 meses
padre
commit
1c11ba4ab2

+ 176 - 0
jeecg-module-interlock/src/main/java/org/jeecg/modules/history/quartz/ReportJobDay.java

@@ -0,0 +1,176 @@
+package org.jeecg.modules.history.quartz;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.jeecg.modules.history.dto.InterlockHistoryQueryDTO;
+import org.jeecg.modules.history.mapper.InterlockSummaryHistoryMapper;
+import org.jeecg.modules.history.service.IInterlockDetailHistoryService;
+import org.jeecg.modules.history.vo.InterlockHistoryDistinctZZXTVO;
+import org.jeecg.modules.history.vo.InterlockSummaryHistoryVO;
+import org.jeecg.modules.iotedgeCollectData.service.RestClientService;
+import org.jeecg.modules.report.entity.InterlockDataReport;
+import org.jeecg.modules.report.service.IInterlockDataReportService;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * 功能描述
+ *
+ * @author: nn
+ * @date: 2024年07月11日 11:27
+ */
+@Slf4j
+public class ReportJobDay implements Job {
+
+    @Autowired
+    private IInterlockDetailHistoryService interlockDetailHistoryService;
+
+    @Autowired
+    private IInterlockDataReportService reportService;
+    @Value("${jeecg.path.upload}")
+    private String upLoadPath;
+
+    @Autowired
+    private RestClientService restClientService;
+
+    @Autowired
+    @SuppressWarnings("all")
+    private InterlockSummaryHistoryMapper summaryHistoryMapper;
+
+
+    /**
+     * 若参数变量名
+     */
+    private String parameter;
+
+    public void setParameter(String parameter) {
+        this.parameter = parameter;
+    }
+
+    @Override
+    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+        log.info(" Job Execution key:"+jobExecutionContext.getJobDetail().getKey());
+
+        String separator = "/";
+        String filePrex = this.parameter;
+        String reportType = "";
+
+        InterlockHistoryQueryDTO dto = new InterlockHistoryQueryDTO();
+        Calendar start = Calendar.getInstance();
+        start.set(Calendar.HOUR_OF_DAY, 0);
+        start.set(Calendar.MINUTE, 0);
+        start.set(Calendar.SECOND, 0);
+
+        Calendar end = Calendar.getInstance();
+        end.set(Calendar.HOUR_OF_DAY, 23);
+        end.set(Calendar.MINUTE, 59);
+        end.set(Calendar.SECOND, 59);
+
+        if(this.parameter.equals("day")) {
+            // 昨天凌晨00:00:00
+            start.add(Calendar.DAY_OF_MONTH, -1);
+            // 昨天晚上23:59:59
+            end.add(Calendar.DAY_OF_MONTH, -1);
+
+            reportType = "0";
+
+        }else if(this.parameter.equals("month")){
+            // 上个月的第一天凌晨00:00:00
+            start.add(Calendar.MONTH, -1);
+            start.set(Calendar.DAY_OF_MONTH, 1);
+            // 上个月的最后一天晚上23:59:59
+            end.set(Calendar.DAY_OF_MONTH, 1);
+            end.add(Calendar.DAY_OF_MONTH, -1);
+
+            reportType = "1";
+
+        }else if(this.parameter.equals("year")){
+            // 去年的第一天凌晨00:00:00
+            start.add(Calendar.YEAR, -1);
+            start.set(Calendar.MONTH, 0);
+            start.set(Calendar.DAY_OF_MONTH, 1);
+            // 去年的最后一天晚上23:59:59
+            end.set(Calendar.MONTH, 0);
+            end.set(Calendar.DAY_OF_MONTH, 1);
+            end.add(Calendar.DAY_OF_MONTH, -1);
+
+            reportType = "2";
+        }
+
+        Date beginTime = start.getTime();
+        Date endTime = end.getTime();
+
+        log.info(String.format(" **********导出" + filePrex + "历史数据**********  时间:" + beginTime + "——" + endTime));
+
+        dto.setBeginTime(beginTime);
+        dto.setEndTime(endTime);
+
+        // 文件路径,如"D:/ttt/opt/upFiles/"
+        String fileUrlPrex = upLoadPath + separator + filePrex + separator;
+        File saveFile = new File(fileUrlPrex);
+        if (!saveFile.exists()) {
+            saveFile.mkdirs();
+        }
+
+        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
+        String timestamp = dateFormat.format(new Date());
+        String fileName = String.format("联锁历史数据_" + filePrex + "报表_%s.xlsx", timestamp);
+
+        String filePath = fileUrlPrex  + fileName;
+
+        List<InterlockHistoryDistinctZZXTVO> zzxtvoList = summaryHistoryMapper.getDistinctZZXTSummaryList(dto);
+
+        if(zzxtvoList == null || zzxtvoList.size()==0) return;
+        else {
+            Workbook workbook = new XSSFWorkbook();
+            try {
+                workbook = interlockDetailHistoryService.exportXlsToFile(dto, zzxtvoList, InterlockSummaryHistoryVO.class);
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+
+            try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
+                workbook.write(fileOut);
+            } catch (IOException e) {
+                // 处理异常
+                e.printStackTrace();
+            } finally {
+                try {
+                    if (workbook != null) {
+                        workbook.close();
+                    }
+                } catch (IOException e) {
+                    // 处理关闭 workbook 时可能发生的异常
+                    e.printStackTrace();
+                }
+            }
+        }
+
+        //新增报表记录
+        InterlockDataReport report = new InterlockDataReport();
+        report.setReportName(fileName);
+        report.setReportType(reportType); // 0日报表1月报表2年报表
+        report.setReportUrl(filePath);
+        reportService.save(report);
+
+        // IoTEdge-发送邮件
+        try {
+            restClientService.sendEmali(fileName,fileUrlPrex);
+            log.info(String.format(" **********发送" + filePrex + "历史数据导出邮件通知**********  "));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 176 - 0
jeecg-module-interlock/src/main/java/org/jeecg/modules/history/quartz/ReportJobMonth.java

@@ -0,0 +1,176 @@
+package org.jeecg.modules.history.quartz;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.jeecg.modules.history.dto.InterlockHistoryQueryDTO;
+import org.jeecg.modules.history.mapper.InterlockSummaryHistoryMapper;
+import org.jeecg.modules.history.service.IInterlockDetailHistoryService;
+import org.jeecg.modules.history.vo.InterlockHistoryDistinctZZXTVO;
+import org.jeecg.modules.history.vo.InterlockSummaryHistoryVO;
+import org.jeecg.modules.iotedgeCollectData.service.RestClientService;
+import org.jeecg.modules.report.entity.InterlockDataReport;
+import org.jeecg.modules.report.service.IInterlockDataReportService;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * 功能描述
+ *
+ * @author: nn
+ * @date: 2024年07月11日 11:25
+ */
+@Slf4j
+public class ReportJobMonth implements Job {
+
+    @Autowired
+    private IInterlockDetailHistoryService interlockDetailHistoryService;
+
+    @Autowired
+    private IInterlockDataReportService reportService;
+    @Value("${jeecg.path.upload}")
+    private String upLoadPath;
+
+    @Autowired
+    private RestClientService restClientService;
+
+    @Autowired
+    @SuppressWarnings("all")
+    private InterlockSummaryHistoryMapper summaryHistoryMapper;
+
+
+    /**
+     * 若参数变量名
+     */
+    private String parameter;
+
+    public void setParameter(String parameter) {
+        this.parameter = parameter;
+    }
+
+    @Override
+    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+        log.info(" Job Execution key:"+jobExecutionContext.getJobDetail().getKey());
+
+        String separator = "/";
+        String filePrex = this.parameter;
+        String reportType = "";
+
+        InterlockHistoryQueryDTO dto = new InterlockHistoryQueryDTO();
+        Calendar start = Calendar.getInstance();
+        start.set(Calendar.HOUR_OF_DAY, 0);
+        start.set(Calendar.MINUTE, 0);
+        start.set(Calendar.SECOND, 0);
+
+        Calendar end = Calendar.getInstance();
+        end.set(Calendar.HOUR_OF_DAY, 23);
+        end.set(Calendar.MINUTE, 59);
+        end.set(Calendar.SECOND, 59);
+
+        if(this.parameter.equals("day")) {
+            // 昨天凌晨00:00:00
+            start.add(Calendar.DAY_OF_MONTH, -1);
+            // 昨天晚上23:59:59
+            end.add(Calendar.DAY_OF_MONTH, -1);
+
+            reportType = "0";
+
+        }else if(this.parameter.equals("month")){
+            // 上个月的第一天凌晨00:00:00
+            start.add(Calendar.MONTH, -1);
+            start.set(Calendar.DAY_OF_MONTH, 1);
+            // 上个月的最后一天晚上23:59:59
+            end.set(Calendar.DAY_OF_MONTH, 1);
+            end.add(Calendar.DAY_OF_MONTH, -1);
+
+            reportType = "1";
+
+        }else if(this.parameter.equals("year")){
+            // 去年的第一天凌晨00:00:00
+            start.add(Calendar.YEAR, -1);
+            start.set(Calendar.MONTH, 0);
+            start.set(Calendar.DAY_OF_MONTH, 1);
+            // 去年的最后一天晚上23:59:59
+            end.set(Calendar.MONTH, 0);
+            end.set(Calendar.DAY_OF_MONTH, 1);
+            end.add(Calendar.DAY_OF_MONTH, -1);
+
+            reportType = "2";
+        }
+
+        Date beginTime = start.getTime();
+        Date endTime = end.getTime();
+
+        log.info(String.format(" **********导出" + filePrex + "历史数据**********  时间:" + beginTime + "——" + endTime));
+
+        dto.setBeginTime(beginTime);
+        dto.setEndTime(endTime);
+
+        // 文件路径,如"D:/ttt/opt/upFiles/"
+        String fileUrlPrex = upLoadPath + separator + filePrex + separator;
+        File saveFile = new File(fileUrlPrex);
+        if (!saveFile.exists()) {
+            saveFile.mkdirs();
+        }
+
+        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
+        String timestamp = dateFormat.format(new Date());
+        String fileName = String.format("联锁历史数据_" + filePrex + "报表_%s.xlsx", timestamp);
+
+        String filePath = fileUrlPrex  + fileName;
+
+        List< InterlockHistoryDistinctZZXTVO > zzxtvoList = summaryHistoryMapper.getDistinctZZXTSummaryList(dto);
+
+        if(zzxtvoList == null || zzxtvoList.size()==0) return;
+        else {
+            Workbook workbook = new XSSFWorkbook();
+            try {
+                workbook = interlockDetailHistoryService.exportXlsToFile(dto, zzxtvoList, InterlockSummaryHistoryVO.class);
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+
+            try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
+                workbook.write(fileOut);
+            } catch (IOException e) {
+                // 处理异常
+                e.printStackTrace();
+            } finally {
+                try {
+                    if (workbook != null) {
+                        workbook.close();
+                    }
+                } catch (IOException e) {
+                    // 处理关闭 workbook 时可能发生的异常
+                    e.printStackTrace();
+                }
+            }
+        }
+
+        //新增报表记录
+        InterlockDataReport report = new InterlockDataReport();
+        report.setReportName(fileName);
+        report.setReportType(reportType); // 0日报表1月报表2年报表
+        report.setReportUrl(filePath);
+        reportService.save(report);
+
+        // IoTEdge-发送邮件
+        try {
+            restClientService.sendEmali(fileName,fileUrlPrex);
+            log.info(String.format(" **********发送" + filePrex + "历史数据导出邮件通知**********  "));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 176 - 0
jeecg-module-interlock/src/main/java/org/jeecg/modules/history/quartz/ReportJobYear.java

@@ -0,0 +1,176 @@
+package org.jeecg.modules.history.quartz;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.jeecg.modules.history.dto.InterlockHistoryQueryDTO;
+import org.jeecg.modules.history.mapper.InterlockSummaryHistoryMapper;
+import org.jeecg.modules.history.service.IInterlockDetailHistoryService;
+import org.jeecg.modules.history.vo.InterlockHistoryDistinctZZXTVO;
+import org.jeecg.modules.history.vo.InterlockSummaryHistoryVO;
+import org.jeecg.modules.iotedgeCollectData.service.RestClientService;
+import org.jeecg.modules.report.entity.InterlockDataReport;
+import org.jeecg.modules.report.service.IInterlockDataReportService;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * 功能描述
+ *
+ * @author: nn
+ * @date: 2024年07月11日 11:27
+ */
+@Slf4j
+public class ReportJobYear implements Job {
+
+    @Autowired
+    private IInterlockDetailHistoryService interlockDetailHistoryService;
+
+    @Autowired
+    private IInterlockDataReportService reportService;
+    @Value("${jeecg.path.upload}")
+    private String upLoadPath;
+
+    @Autowired
+    private RestClientService restClientService;
+
+    @Autowired
+    @SuppressWarnings("all")
+    private InterlockSummaryHistoryMapper summaryHistoryMapper;
+
+
+    /**
+     * 若参数变量名
+     */
+    private String parameter;
+
+    public void setParameter(String parameter) {
+        this.parameter = parameter;
+    }
+
+    @Override
+    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+        log.info(" Job Execution key:"+jobExecutionContext.getJobDetail().getKey());
+
+        String separator = "/";
+        String filePrex = this.parameter;
+        String reportType = "";
+
+        InterlockHistoryQueryDTO dto = new InterlockHistoryQueryDTO();
+        Calendar start = Calendar.getInstance();
+        start.set(Calendar.HOUR_OF_DAY, 0);
+        start.set(Calendar.MINUTE, 0);
+        start.set(Calendar.SECOND, 0);
+
+        Calendar end = Calendar.getInstance();
+        end.set(Calendar.HOUR_OF_DAY, 23);
+        end.set(Calendar.MINUTE, 59);
+        end.set(Calendar.SECOND, 59);
+
+        if(this.parameter.equals("day")) {
+            // 昨天凌晨00:00:00
+            start.add(Calendar.DAY_OF_MONTH, -1);
+            // 昨天晚上23:59:59
+            end.add(Calendar.DAY_OF_MONTH, -1);
+
+            reportType = "0";
+
+        }else if(this.parameter.equals("month")){
+            // 上个月的第一天凌晨00:00:00
+            start.add(Calendar.MONTH, -1);
+            start.set(Calendar.DAY_OF_MONTH, 1);
+            // 上个月的最后一天晚上23:59:59
+            end.set(Calendar.DAY_OF_MONTH, 1);
+            end.add(Calendar.DAY_OF_MONTH, -1);
+
+            reportType = "1";
+
+        }else if(this.parameter.equals("year")){
+            // 去年的第一天凌晨00:00:00
+            start.add(Calendar.YEAR, -1);
+            start.set(Calendar.MONTH, 0);
+            start.set(Calendar.DAY_OF_MONTH, 1);
+            // 去年的最后一天晚上23:59:59
+            end.set(Calendar.MONTH, 0);
+            end.set(Calendar.DAY_OF_MONTH, 1);
+            end.add(Calendar.DAY_OF_MONTH, -1);
+
+            reportType = "2";
+        }
+
+        Date beginTime = start.getTime();
+        Date endTime = end.getTime();
+
+        log.info(String.format(" **********导出" + filePrex + "历史数据**********  时间:" + beginTime + "——" + endTime));
+
+        dto.setBeginTime(beginTime);
+        dto.setEndTime(endTime);
+
+        // 文件路径,如"D:/ttt/opt/upFiles/"
+        String fileUrlPrex = upLoadPath + separator + filePrex + separator;
+        File saveFile = new File(fileUrlPrex);
+        if (!saveFile.exists()) {
+            saveFile.mkdirs();
+        }
+
+        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
+        String timestamp = dateFormat.format(new Date());
+        String fileName = String.format("联锁历史数据_" + filePrex + "报表_%s.xlsx", timestamp);
+
+        String filePath = fileUrlPrex  + fileName;
+
+        List<InterlockHistoryDistinctZZXTVO> zzxtvoList = summaryHistoryMapper.getDistinctZZXTSummaryList(dto);
+
+        if(zzxtvoList == null || zzxtvoList.size()==0) return;
+        else {
+            Workbook workbook = new XSSFWorkbook();
+            try {
+                workbook = interlockDetailHistoryService.exportXlsToFile(dto, zzxtvoList, InterlockSummaryHistoryVO.class);
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+
+            try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
+                workbook.write(fileOut);
+            } catch (IOException e) {
+                // 处理异常
+                e.printStackTrace();
+            } finally {
+                try {
+                    if (workbook != null) {
+                        workbook.close();
+                    }
+                } catch (IOException e) {
+                    // 处理关闭 workbook 时可能发生的异常
+                    e.printStackTrace();
+                }
+            }
+        }
+
+        //新增报表记录
+        InterlockDataReport report = new InterlockDataReport();
+        report.setReportName(fileName);
+        report.setReportType(reportType); // 0日报表1月报表2年报表
+        report.setReportUrl(filePath);
+        reportService.save(report);
+
+        // IoTEdge-发送邮件
+        try {
+            restClientService.sendEmali(fileName,fileUrlPrex);
+            log.info(String.format(" **********发送" + filePrex + "历史数据导出邮件通知**********  "));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}