15928045575 2 лет назад
Родитель
Сommit
8eda94bf3e

+ 65 - 5
slope-common/slope-common-excel/src/main/java/com/sckw/excel/utils/ExcelUtil.java

@@ -38,10 +38,7 @@ import java.io.*;
 import java.lang.reflect.Field;
 import java.lang.reflect.Field;
 import java.net.URLEncoder;
 import java.net.URLEncoder;
 import java.time.LocalDateTime;
 import java.time.LocalDateTime;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 
 
 
 /**
 /**
@@ -225,7 +222,7 @@ public class ExcelUtil {
      */
      */
     public static String exportByExcelUploadingToOss(HttpServletResponse response, List<?> data, String fileName, Class<?> excelClass) throws IOException {
     public static String exportByExcelUploadingToOss(HttpServletResponse response, List<?> data, String fileName, Class<?> excelClass) throws IOException {
         @Cleanup ByteArrayOutputStream os = new ByteArrayOutputStream();
         @Cleanup ByteArrayOutputStream os = new ByteArrayOutputStream();
-        log.info("报表导出Size: " + data.size() + "条。");
+        //log.info("报表导出Size: " + data.size() + "条。");
         String sheetName = excelClass.getAnnotation(ExcelContext.class).sheetName();
         String sheetName = excelClass.getAnnotation(ExcelContext.class).sheetName();
         // 把查询到的数据按设置的sheet的容量进行切割
         // 把查询到的数据按设置的sheet的容量进行切割
         List<? extends List<?>> lists = com.sckw.core.utils.StringUtils.splitList(data, PAGE_SIZE);
         List<? extends List<?>> lists = com.sckw.core.utils.StringUtils.splitList(data, PAGE_SIZE);
@@ -523,5 +520,68 @@ public class ExcelUtil {
         return file;
         return file;
     }
     }
 
 
+    /**
+     * 导出报表(使用注解方式设置Excel头数据)
+     * 用于生成excel文件流并上传oss
+     *
+     * @param data       报表数据
+     * @param fileName   文件名字
+     * @param excelClass 报表实体类的Class(根据该Class的属性来设置Excel的头属性)
+     */
+    public static String taskExportByExcelUploadingToOss(Map<String,Object> listData,List<?> data, String fileName, Class<?> excelClass) throws IOException {
+        @Cleanup ByteArrayOutputStream os = new ByteArrayOutputStream();
+
+        ExcelWriter excelWriter = com.alibaba.excel.EasyExcel.write(os, excelClass)
+                .autoCloseStream(Boolean.FALSE)
+                .registerConverter(new LongStringConverter())
+                //设置导出excel的单元格格式为文本
+                .registerWriteHandler(new RowWriteHandlerImpl())
+                // 自定义列宽、行高
+                .registerWriteHandler(new CustomCellWeightWeightConfig())
+//                .registerWriteHandler(formatExcel())
+                .registerWriteHandler(new ExcelUtil.ExcelWidthStyleStrategy()).build();
+
+        for (String key : listData.keySet()) {
+            String sheetName = key;
+            Object getData = listData.get(key);
+            //List<Object> list = Arrays.asList(getData);
+            List<Object> list = new ArrayList<Object>();
+            if (getData instanceof ArrayList<?>) {
+                for (Object o : (List<?>) getData) {
+                    list.add(o);
+                }
+            }
+            // 把查询到的数据按设置的sheet的容量进行切割
+            List<? extends List<?>> lists = com.sckw.core.utils.StringUtils.splitList(list, PAGE_SIZE);
+            ExcelWriterSheetBuilder excelWriterSheetBuilder;
+            WriteSheet writeSheet;
+            //for (int i = 1; i <= lists.size(); ++i) {
+                excelWriterSheetBuilder = new ExcelWriterSheetBuilder(excelWriter);
+                excelWriterSheetBuilder.sheetName(sheetName);
+                writeSheet = excelWriterSheetBuilder.build();
+                excelWriter.write(list, writeSheet);
+            //}
+
+        }
+        // 必须要finish才会写入,不finish只会创建empty的文件
+        excelWriter.finish();
+        //String sheetName = excelClass.getAnnotation(ExcelContext.class).sheetName();
+
+
+        byte[] content = os.toByteArray();
+        @Cleanup InputStream is = new ByteArrayInputStream(content);
+
+
+
+        // 文件上传到OSS
+        Map<String, String> map = FileUtils.uploadFileByInfo(is, fileName, FileEnum.FILE_STORE_TYPE_ALIYUN);
+        System.out.println(com.alibaba.fastjson2.JSONObject.toJSONString(map));
+        String ossAddressPrefix = FileUtils.getOSSAddressPrefix();
+        if (map != null && (map.get("filePath") != null)) {
+            ossAddressPrefix = ossAddressPrefix + map.get("filePath");
+        }
+        return ossAddressPrefix;
+    }
+
 
 
 }
 }

+ 6 - 0
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/model/vo/DeviceVo.java

@@ -41,6 +41,11 @@ public class DeviceVo implements Serializable {
      */
      */
     private String snCode;
     private String snCode;
 
 
+    /**
+     * sn码
+     */
+    private String snCodeFullName;
+
     /**
     /**
      * 设备型号id
      * 设备型号id
      */
      */
@@ -96,6 +101,7 @@ public class DeviceVo implements Serializable {
     /**
     /**
      * 创建时间
      * 创建时间
      */
      */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     private LocalDateTime createTime;
     private LocalDateTime createTime;
 
 
     /**
     /**

+ 40 - 0
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/model/vo/excel/TaskReportExportVO.java

@@ -0,0 +1,40 @@
+package com.sckw.slope.detection.model.vo.excel;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.sckw.excel.annotation.ExcelContext;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+/**
+ * @author lfdc
+ * @description 报表模板输出
+ * @date 2023-11-13 10:11:57
+ */
+@Data
+@ExcelContext(fileName = "报表模板输出", sheetName = "报表模板输出")
+public class TaskReportExportVO implements Serializable {
+
+    /**
+     * 推送时间
+     */
+    @ExcelProperty("推送时间")
+    private String ts;
+
+    /**
+     * lng
+     */
+    @ExcelProperty("lng")
+    private String lng;
+
+    /**
+     * lat
+     */
+    @ExcelProperty("lat")
+    private String lat;
+
+
+    private static final long serialVersionUID = 1L;
+}

+ 21 - 9
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/service/task/ReportTemplateTaskService.java

@@ -3,12 +3,18 @@ package com.sckw.slope.detection.service.task;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.date.DateUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.sckw.core.model.enums.ReportTypeEnum;
 import com.sckw.core.model.enums.ReportTypeEnum;
+import com.sckw.excel.annotation.ExcelContext;
+import com.sckw.excel.utils.ExcelUtil;
 import com.sckw.slope.detection.dao.mysql.KwsReportTemplateMapper;
 import com.sckw.slope.detection.dao.mysql.KwsReportTemplateMapper;
 import com.sckw.slope.detection.model.dos.mysql.KwsReportTemplate;
 import com.sckw.slope.detection.model.dos.mysql.KwsReportTemplate;
 import com.sckw.slope.detection.model.dos.mysql.KwsThreshold;
 import com.sckw.slope.detection.model.dos.mysql.KwsThreshold;
 import com.sckw.slope.detection.model.dos.tdengine.SlopeData;
 import com.sckw.slope.detection.model.dos.tdengine.SlopeData;
 import com.sckw.slope.detection.model.vo.DeviceVo;
 import com.sckw.slope.detection.model.vo.DeviceVo;
+import com.sckw.slope.detection.model.vo.excel.KwsAlarmExportVO;
+import com.sckw.slope.detection.model.vo.excel.TaskReportExportVO;
 import jakarta.annotation.Resource;
 import jakarta.annotation.Resource;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.EnableScheduling;
@@ -17,6 +23,7 @@ import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.CollectionUtils;
 import com.sckw.slope.detection.dao.tdengine.SlopeDataMapper;
 import com.sckw.slope.detection.dao.tdengine.SlopeDataMapper;
 
 
+import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.time.ZoneId;
@@ -44,8 +51,8 @@ public class ReportTemplateTaskService {
     /**
     /**
      * 报表生成定时任务-天
      * 报表生成定时任务-天
      */
      */
-    //@Scheduled(cron = "* * * * * *")
-    public void templateTaskDay() {
+    @Scheduled(cron = "* * * * * *")
+    public void templateTaskDay() throws IOException {
         List<KwsReportTemplate> kwsReportTemplates = reportTemplateMapper.selectList(new LambdaQueryWrapper<KwsReportTemplate>()
         List<KwsReportTemplate> kwsReportTemplates = reportTemplateMapper.selectList(new LambdaQueryWrapper<KwsReportTemplate>()
                 .eq(KwsReportTemplate::getStatus, 0)
                 .eq(KwsReportTemplate::getStatus, 0)
                 .eq(KwsReportTemplate::getDelFlag, 0));
                 .eq(KwsReportTemplate::getDelFlag, 0));
@@ -67,7 +74,10 @@ public class ReportTemplateTaskService {
                     //首先查询模板中的要素关联的设备-项目中的设备
                     //首先查询模板中的要素关联的设备-项目中的设备
                     List<DeviceVo> devices = reportTemplateMapper.selectDeviceByProjectId(projectId,list);
                     List<DeviceVo> devices = reportTemplateMapper.selectDeviceByProjectId(projectId,list);
                     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                    List<String> deviceName = new ArrayList<>();
+                    Map<String,Object> deviceData = new HashMap<>();
                     for (DeviceVo vo : devices){
                     for (DeviceVo vo : devices){
+                        //deviceName.add(vo.getName());
                         List<SlopeData> selectedData = slopeDataMapper.selectLineListByList(vo.getSnCode(), list, "2022-1-1 00:00:00", date2);
                         List<SlopeData> selectedData = slopeDataMapper.selectLineListByList(vo.getSnCode(), list, "2022-1-1 00:00:00", date2);
                         if(Objects.nonNull(selectedData)){
                         if(Objects.nonNull(selectedData)){
                             Map<String,List<SlopeData>> map = selectedData.stream().collect(Collectors.groupingBy(SlopeData::getMsgId));
                             Map<String,List<SlopeData>> map = selectedData.stream().collect(Collectors.groupingBy(SlopeData::getMsgId));
@@ -76,19 +86,21 @@ public class ReportTemplateTaskService {
                             map.forEach((key,value) -> {
                             map.forEach((key,value) -> {
                                 Map<Object,Object> mapdata = new HashMap<>();
                                 Map<Object,Object> mapdata = new HashMap<>();
                                 value.forEach(valuetemp->{
                                 value.forEach(valuetemp->{
-                                    mapdata.put("ts",sdf.format(valuetemp.getTs()));
+                                    mapdata.put("ts",valuetemp.getMsgId());
                                     for (int i = 0;i< list.size();i++){
                                     for (int i = 0;i< list.size();i++){
-                                       // if(valuetemp.getLine().equals(list[i].get("line"))){
-                                            mapdata.put(valuetemp.getLine(),valuetemp.getVal());
-                                        //}
+                                        mapdata.put(valuetemp.getLine(),valuetemp.getVal());
                                     }
                                     }
                                 });
                                 });
-                                list1.add(map);
+                                list1.add(mapdata);
                             });
                             });
-                            vo.setData(list1);
+                            deviceData.put(vo.getName(),list1);
+                            //deviceData.add(list1);
                         }
                         }
                     }
                     }
-                    log.info("aaa:{}",devices);
+                    ExcelContext excelContext = TaskReportExportVO.class.getAnnotation(ExcelContext.class);
+                    if(Objects.nonNull(devices)) {
+                        String filePath = ExcelUtil.taskExportByExcelUploadingToOss(deviceData,devices, excelContext.fileName() + ".xlsx", TaskReportExportVO.class);
+                    }
                 }
                 }
             }
             }
         }
         }

+ 1 - 1
slope-modules/slope-detection/src/main/resources/mapper/mysql/KwsReportTemplateMapper.xml

@@ -221,7 +221,7 @@
     </select>
     </select>
 
 
     <select id="selectDeviceByProjectId" resultType="com.sckw.slope.detection.model.vo.DeviceVo">
     <select id="selectDeviceByProjectId" resultType="com.sckw.slope.detection.model.vo.DeviceVo">
-        select DISTINCT d.id,d.sn_code from kws_device_reference as r
+        select DISTINCT d.id,d.sn_code,d.name from kws_device_reference as r
             inner join kws_device as d on r.device_id=d.id
             inner join kws_device as d on r.device_id=d.id
             inner join kws_project_device as p on p.device_id=d.id
             inner join kws_project_device as p on p.device_id=d.id
         where p.del_flag=0 and d.del_flag=0 and r.del_flag=0
         where p.del_flag=0 and d.del_flag=0 and r.del_flag=0