From 2f55cebbad3dea187a5f91d16ec80a9677dab699 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 13 十一月 2024 11:16:53 +0800
Subject: [PATCH] 1

---
 src/main/java/com/yssh/service/XlsReportService.java |   59 ++++++++++++++++++++++++-----------------------------------
 1 files changed, 24 insertions(+), 35 deletions(-)

diff --git a/src/main/java/com/yssh/service/XlsReportService.java b/src/main/java/com/yssh/service/XlsReportService.java
index a194aea..236cd7d 100644
--- a/src/main/java/com/yssh/service/XlsReportService.java
+++ b/src/main/java/com/yssh/service/XlsReportService.java
@@ -10,6 +10,7 @@
 import com.yssh.utils.CalculateUtils;
 import com.yssh.utils.DateUtils;
 import com.yssh.utils.ExcelUtils;
+import com.yssh.utils.WebUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
@@ -17,15 +18,11 @@
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
 import java.io.File;
 import java.io.IOException;
 import java.util.*;
 
-/**
- * Excel瀵煎嚭鏈嶅姟绫�
- * @author WWW
- * @author 2023-08-05
- */
 @Service
 public class XlsReportService {
     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@@ -55,8 +52,11 @@
     private double bjz;
 
     /**
-     * 鑾峰彇瀵煎嚭璺緞
+     * 鎶ヨ鍊�
      */
+    @Value("${report.templates}")
+    private String templates;
+
     private String getExpPath(String type) {
         String path = reportPath + File.separator + type;
 
@@ -68,18 +68,13 @@
         return path;
     }
 
-    /**
-     * 鑾峰彇Excel妯℃澘
-     */
     private String getXslTemplate(String type) throws IOException {
-        ClassPathResource resource = new ClassPathResource(String.format("templates/%s.xlsx", type));
+        //ClassPathResource resource = new ClassPathResource(String.format("templates/%s.xlsx", type));
+        //return resource.exists() ? resource.getFile().getPath() : null;
 
-        return resource.exists() ? resource.getFile().getPath() : null;
+        return String.format(templates + File.separator + type + ".xlsx");
     }
 
-    /**
-     * 鍒涘缓Excel
-     */
     private <T> void createExcel(String source, String target, List<T> list) {
         Map<String, List<T>> map = new HashMap<>();
         map.put("data", list);
@@ -87,25 +82,16 @@
         ExcelUtils.writeToTemplate(source, target, map);
     }
 
-    /**
-     * 鑾峰彇瀛楃涓叉棩鏈�
-     */
     private String getStrDate(String type, Date date) {
         return DateUtils.parseDateToStr("month".equals(type) ? DateUtils.YYYYMM : DateUtils.YYYYMMDD, date);
     }
 
-    /**
-     * 鑾峰彇鐩爣鏂囦欢
-     */
     private String getTarget(String type, Date date) {
         String strData = getStrDate(type, date);
 
         return String.format("%s\\%s.xlsx", getExpPath(type), strData);
     }
 
-    /**
-     * Excel鏄�/鍚﹀瓨鍦�
-     */
     private boolean xlsExists(String type, Date date) {
         String target = getTarget(type, date);
         File f = new File(target);
@@ -113,9 +99,6 @@
         return f.exists() && !f.isDirectory();
     }
 
-    /**
-     * 鍒涘缓Excel
-     */
     private <T> String createExcel(String type, Date date, List<T> list) throws Exception {
         String source = getXslTemplate(type);
         String strData = getStrDate(type, date);
@@ -128,9 +111,6 @@
         return String.format("%s\\%s.xlsx", type, strData);
     }
 
-    /**
-     * 璁板綍鏄�/鍚﹀瓨鍦�
-     */
     private boolean recordExists(String type, Date date) {
         String name = String.format("%s.xlsx", getStrDate(type, date));
         int rows = mapper.reportExists(type, name);
@@ -138,9 +118,6 @@
         return rows > 0;
     }
 
-    /**
-     * 淇濆瓨缁撴灉
-     */
     private <T> void saveResult(String type, Date date, List<T> list) throws Exception {
         String filePath = createExcel(type, date, list);
         if (recordExists(type, date)) return;
@@ -157,9 +134,6 @@
         mapper.insertReport(xls);
     }
 
-    /**
-     * 鑾峰彇鍙楀奖鍝嶅洜绱犲強鍘熷洜
-     */
     public String getYs(List<SuYuan700> suList) {
         if (null == suList || 0 == suList.size()) return "";
 
@@ -343,4 +317,19 @@
             logger.error(ex.getMessage(), ex);
         }
     }
+
+    public void downloadById(Integer id, HttpServletResponse res) {
+        XlsReport xlsReport = mapper.selectById(id);
+        if (null == xlsReport) return;
+
+        String file = reportPath + File.separator + xlsReport.getPath();
+        File f = new File(file);
+        if (!f.exists() || f.isDirectory()) return;
+
+        try {
+            WebUtils.download(file, xlsReport.getName(), false, res);
+        } catch (Exception ex) {
+            logger.error(ex.getMessage(), ex);
+        }
+    }
 }

--
Gitblit v1.9.3