Explorar o código

修改模型准确率导出,改为后台导出

fanxiaoyu hai 7 meses
pai
achega
035c06108c

+ 94 - 9
cpp-admin/src/main/java/com/cpp/web/controller/accuracy/AccuracyPassRateController.java

@@ -4,34 +4,119 @@ import com.cpp.common.core.domain.R;
 import com.cpp.web.domain.accuracy.AccuracyPassRate;
 import com.cpp.web.domain.enums.DataSourcesEnum;
 import com.cpp.web.domain.enums.ForecastTypeEnum;
+import com.cpp.web.dto.TableColumn;
 import com.cpp.web.service.accuracy.AccuracyPassRateService;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.*;
 
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
+import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.text.SimpleDateFormat;
+import java.util.*;
 
 @RestController
 @RequiredArgsConstructor
+@Slf4j
 @RequestMapping("accuracyPassRate")
 public class AccuracyPassRateController {
     private final AccuracyPassRateService accuracyPassRateService;
 
+    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
+
     @GetMapping("/getByTimeBetweenAndForecastTypeAndDataSourcesAndForecastModelAndStationCode")
-    public R getByTimeBetweenAndForecastTypeAndDataSourcesAndForecastModelAndStationCode(Long startTime, Long endTime, ForecastTypeEnum forecastType, DataSourcesEnum dataSources,Integer ago, String forecastModel, String stationCode){
-        List<AccuracyPassRate> accuracyPassRateList = accuracyPassRateService.findByTimeBetweenAndForecastTypeAndDataSourcesAndAgoAndForecastModelAndStationCode(new Date(startTime),new Date(endTime),forecastType,dataSources,ago,forecastModel,stationCode);
+    public R getByTimeBetweenAndForecastTypeAndDataSourcesAndForecastModelAndStationCode(Long startTime, Long endTime, ForecastTypeEnum forecastType, DataSourcesEnum dataSources, Integer ago, String forecastModel, String stationCode) {
+        List<AccuracyPassRate> accuracyPassRateList = accuracyPassRateService.findByTimeBetweenAndForecastTypeAndDataSourcesAndAgoAndForecastModelAndStationCode(new Date(startTime), new Date(endTime), forecastType, dataSources, ago, forecastModel, stationCode);
         return R.ok(accuracyPassRateList);
     }
 
     @GetMapping("/getByMonthBetweenAndForecastTypeAndStationCode")
-    public R getByMonthBetweenAndForecastTypeAndStationCode(Long startTime,Long endTime,ForecastTypeEnum forecastType, DataSourcesEnum dataSources, String stationCode){
-        Map<String, Double> map = accuracyPassRateService.findByMonthBetweenAndForecastTypeAndStationCode(startTime,endTime,forecastType,dataSources,stationCode);
+    public R getByMonthBetweenAndForecastTypeAndStationCode(Long startTime, Long endTime, ForecastTypeEnum forecastType, DataSourcesEnum dataSources, String stationCode) {
+        Map<String, Double> map = accuracyPassRateService.findByMonthBetweenAndForecastTypeAndStationCode(startTime, endTime, forecastType, dataSources, stationCode);
         return R.ok(map);
     }
+
     @GetMapping("/getBySingleMonthBetweenAndForecastTypeAndStationCode")
-    public R getBySingleMonthBetweenAndForecastTypeAndStationCode(Long startTime,Long endTime,ForecastTypeEnum forecastType, DataSourcesEnum dataSources, String stationCode,String stationName,String uploadDataSources){
-        List<Map<String,String>> accuracyPassRateList = accuracyPassRateService.finfBySingleMonthBetweenAndForecastTypeAndStationCode(startTime,endTime,forecastType,dataSources,stationCode,stationName,uploadDataSources);
+    public R getBySingleMonthBetweenAndForecastTypeAndStationCode(Long startTime, Long endTime, ForecastTypeEnum forecastType, DataSourcesEnum dataSources, String stationCode, String stationName, String uploadDataSources) {
+        List<Map<String, String>> accuracyPassRateList = accuracyPassRateService.finfBySingleMonthBetweenAndForecastTypeAndStationCode(startTime, endTime, forecastType, dataSources, stationCode, stationName, uploadDataSources);
         return R.ok(accuracyPassRateList);
     }
+
+    @GetMapping("/export")
+    public void export(Long startTime, Long endTime, ForecastTypeEnum forecastType, DataSourcesEnum dataSources, String stationCode, String stationName, String uploadDataSources, HttpServletResponse response) {
+
+        BufferedOutputStream bos = null;
+        List<Map<String, String>> mapList = new ArrayList<>();
+        try {
+
+            StringBuilder templateContent = new StringBuilder();
+            response.setCharacterEncoding("UTF-8");
+            mapList = accuracyPassRateService.finfBySingleMonthBetweenAndForecastTypeAndStationCode(startTime, endTime, forecastType, dataSources, stationCode, stationName, uploadDataSources);
+
+            List<TableColumn> tableColumns = getTableColumn();
+
+            String header = "";
+            StringBuilder content = new StringBuilder();
+
+            for (TableColumn tableColumn : tableColumns) {
+                header += "\"" + tableColumn.getTitle() + "\",";
+            }
+            for (Map<String, String> tableData : mapList) {
+                for (TableColumn tableColumn : tableColumns) {
+                    if (tableColumn.getField().equals("month")){
+                        content.append("\t"+tableData.get(tableColumn.getField()) + ",");
+                    }else {
+                        content.append(tableData.get(tableColumn.getField()) + ",");
+                    }
+
+                }
+                content.append("\r\n");
+            }
+
+            header += "\r\n";
+            templateContent.append(header);
+
+            templateContent.append(content.toString());
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+
+            String fileName = sdf.format(startTime) + "模型准确率" + ".csv";
+            response.setContentType("application/x-msdownload;charset=UTF-8");
+            response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
+            byte[] templateContentBytes = templateContent.toString().getBytes("UTF-8");
+            bos = new BufferedOutputStream(response.getOutputStream());
+            bos.write(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
+            bos.write(templateContentBytes);
+            response.flushBuffer();
+
+        } catch (Exception e) {
+            log.error("系统错误:" + e.getMessage(), e);
+            throw new RuntimeException(e);
+        } finally {
+            if (bos != null) {
+                try {
+                    bos.close();
+                } catch (IOException e) {
+                    log.error("系统错误:" + e.getMessage(), e);
+                }
+            }
+        }
+    }
+
+    /**
+     *  获取表头
+     * @return
+     */
+    public List<TableColumn> getTableColumn() {
+        List<TableColumn> tableColumns = new ArrayList<>();
+        tableColumns.add(new TableColumn("station", "场站"));
+        tableColumns.add(new TableColumn("month", "月份"));
+        tableColumns.add(new TableColumn("preModels", "预测模型"));
+        tableColumns.add(new TableColumn("shortAccuracy", "短期准确率(%)"));
+        tableColumns.add(new TableColumn("uploadShortAccuracy", "上报文件短期准确率(%)"));
+        tableColumns.add(new TableColumn("accuracyRanking", "准确率排名"));
+        return tableColumns;
+    }
+
 }

+ 0 - 2
cpp-admin/src/main/java/com/cpp/web/controller/reportData/ReportDataController.java

@@ -140,8 +140,6 @@ public class ReportDataController {
                     resultList.add(collectRecords(fileType, rCdqKey, stationCode, item));
                 }
             }
-            resultList = resultList.stream().filter(f -> f.getFileType().name().equals(FileTypeEnum.valueOf(fileType))).collect(Collectors.toList());
-
         }
 
         return resultList;

+ 36 - 0
cpp-admin/src/main/java/com/cpp/web/dto/TableColumn.java

@@ -0,0 +1,36 @@
+package com.cpp.web.dto;
+
+import lombok.Data;
+
+/**
+ * 返回前端的表格列
+ *
+ * @author tl
+ * @version 3.0
+ */
+@Data
+public class TableColumn {
+
+  //字段
+  private String field;
+
+  //标题
+  private String title;
+
+  //最小宽度,默认为60
+  private String minWidth = "60";
+
+  public TableColumn(String field, String title, String minWidth) {
+    this.field = field;
+    this.title = title;
+    this.minWidth = minWidth;
+  }
+
+  public TableColumn() {
+  }
+
+  public TableColumn(String field, String title) {
+    this.field = field;
+    this.title = title;
+  }
+}

+ 23 - 25
cpp-ui/src/views/regulation/modelAccuracyStatistics/index.vue

@@ -154,30 +154,29 @@ export default {
       this.forecastType = tab.name
       this.dataQuery()
     },
-    exportFile(){
-      const modifyData = this.tableData.map(item=>({
-        ...item,
-        month:item.month.substring(0,4)+"年"+item.month.substring(5,item.month.length)+"月"
-      }))
-      let startMonth = new Date(this.dateTime[0]).getFullYear() + '-' +(new Date(this.dateTime[0]).getMonth()+1)+'月'
-      let endMonth = new Date(this.dateTime[1]).getFullYear() + '-' +(new Date(this.dateTime[1]).getMonth()+1)+'月'
-      if (this.activeName == 'dq'){
-        this.$refs.shortTermTable.exportData({
-          filename: startMonth==endMonth?startMonth+'短期模型准确率统计':startMonth+'至'+endMonth+'短期模型准确率统计',
-          type: 'csv',
-          isHeader: true,
-          isFooter: true,
-          data:modifyData
-        })
-      }else if (this.activeName == 'cdq'){
-        this.$refs.ultraShortTermTable.exportData({
-          filename: startMonth==endMonth?startMonth+'超短期模型准确率统计':startMonth+'至'+endMonth+'超短期模型准确率统计',
-          type: 'csv',
-          isHeader: true,
-          isFooter: true,
-          data:modifyData
-        })
+    exportFile() {
+      this.loading = true
+      var find = this.stationList.find(s=>s.value == this.stationCode);
+      this.stationName = find.label
+      let startTime = Math.round(this.dateTime[0])
+      let endTime = Math.round(this.dateTime[1])
+      let queryParams = {
+        "stationCode": this.stationCode,
+        "startTime": startTime,
+        "endTime": endTime,
+        "forecastType": this.forecastType,
+        "dataSources": 'E2',
+        "uploadDataSources": 'E4',
+        "stationName":this.stationName
       }
+      this.$axios.get("/accuracyPassRate/export/", {params: queryParams},{
+        responseType: 'blob'// 用于解决中文乱码
+      }).then((response) => {
+        this.loading = false
+      }).catch((error) => {
+        this.loading = false
+        this.$message.error('导出失败' + error)
+      })
     },
     dataQuery(){
       var find = this.stationList.find(s=>s.value == this.stationCode);
@@ -230,8 +229,7 @@ export default {
           text: '综合准确率',
           textStyle: {
             color:'#fff',
-            fontWeight: 'normal',
-            fontSize: 16,
+             fontSize: 16,
           },
           left: 'center'
         },