From 52f98f1a31e682f725f09b4a37bcd316332d3131 Mon Sep 17 00:00:00 2001
From: whycxzp <glperry@163.com>
Date: 星期四, 01 八月 2024 20:06:15 +0800
Subject: [PATCH] 优化word报告生成

---
 src/main/java/com/whyc/util/CommonUtil.java             |   57 +++++++++
 src/main/resources/config/application-dev.yml           |   10 -
 src/main/java/com/whyc/service/ReportService.java       |   51 ++++++++
 src/main/java/com/whyc/service/BattRTStateService.java  |   10 +
 src/main/java/com/whyc/controller/ReportController.java |   38 -----
 src/main/java/com/whyc/pojo/BattRTState.java            |   90 +++++++++++++++
 src/main/java/com/whyc/constant/YamlProperties.java     |   31 +++++
 src/main/resources/config/application-prod.yml          |   17 --
 8 files changed, 248 insertions(+), 56 deletions(-)

diff --git a/src/main/java/com/whyc/constant/YamlProperties.java b/src/main/java/com/whyc/constant/YamlProperties.java
new file mode 100644
index 0000000..ba53167
--- /dev/null
+++ b/src/main/java/com/whyc/constant/YamlProperties.java
@@ -0,0 +1,31 @@
+package com.whyc.constant;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+/**
+ * Yaml閰嶇疆鍙傛暟 瀹炰綋绫�
+ */
+@Component
+public class YamlProperties {
+
+    /**
+     * 杩愯妯″紡:dev,prod
+     */
+    public static String profileType;
+
+    /**绯荤粺杩愯妯″紡*/
+    public static Integer runModel;
+
+    @Value("${spring.profiles.active}")
+    public void setProfileType(String profileType) {
+        YamlProperties.profileType = profileType;
+    }
+
+    @Value("${configFile.type}")
+    public void setRunModel(Integer runModel) {
+        YamlProperties.runModel = runModel;
+    }
+
+
+}
diff --git a/src/main/java/com/whyc/controller/ReportController.java b/src/main/java/com/whyc/controller/ReportController.java
index 302bec9..b3fba47 100644
--- a/src/main/java/com/whyc/controller/ReportController.java
+++ b/src/main/java/com/whyc/controller/ReportController.java
@@ -1,7 +1,9 @@
 package com.whyc.controller;
 
+import com.whyc.dto.Response;
 import com.whyc.service.ReportService;
 import org.docx4j.Docx4J;
+import org.docx4j.openpackaging.exceptions.Docx4JException;
 import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
 import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -9,11 +11,10 @@
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.xml.bind.JAXBException;
 import java.io.File;
 import java.text.SimpleDateFormat;
 import java.util.HashMap;
-
-import static org.docx4j.Docx4J.*;
 
 /**
  * 鎶ュ憡鐢熸垚
@@ -26,37 +27,8 @@
     private ReportService service;
 
     @GetMapping("generateDocument")
-    public void generateDocument(){
-        try {
-            //鑾峰彇classpath涓嬬殑妯℃澘
-            String templatePath = this.getClass().getClassLoader().getResource("").getPath()+"resources\\template\\template_01.docx";
-            //鍔犺浇妯℃澘
-            WordprocessingMLPackage wordprocessingMLPackage = WordprocessingMLPackage.load(new File(templatePath));
-            MainDocumentPart documentPart =  wordprocessingMLPackage.getMainDocumentPart();
-
-            //鏌ヨ宸℃鏃跺�欑殑鐢垫祦鐢靛帇娓╁害鏁版嵁
-            float current = 1.0f;
-            float voltage = 2.0f;
-            float temperature = 3.0f;
-            String reporter = "whyc";
-            String inspectionTime = "2020-01-01 00:00:00";
-            HashMap<String,String> map = new HashMap<>();
-            map.put("current",current+"");
-            map.put("voltage",voltage+"");
-            map.put("temperature",temperature+"");
-            map.put("reporter",reporter);
-            map.put("inspectionTime",inspectionTime);
-            //鏇挎崲鍗犱綅绗�
-            documentPart.variableReplace(map);
-            //杈撳嚭鏂囦欢鍚嶇О
-            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
-            String fileName = "鎶ュ憡_01_"+format.format(System.currentTimeMillis())+".docx";
-            Docx4J.save(wordprocessingMLPackage, new File("C:\\code\\web\\InspectionSystem\\src\\main\\resources\\template\\"+fileName));
-
-        }catch(Exception e){
-            e.printStackTrace();
-        }
-
+    public Response<String> generateDocument() throws JAXBException, Docx4JException {
+        return service.generateDocument();
     }
 
 }
diff --git a/src/main/java/com/whyc/pojo/BattRTState.java b/src/main/java/com/whyc/pojo/BattRTState.java
new file mode 100644
index 0000000..ccfe2e1
--- /dev/null
+++ b/src/main/java/com/whyc/pojo/BattRTState.java
@@ -0,0 +1,90 @@
+package com.whyc.pojo;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.ToString;
+
+import java.io.Serializable;
+import java.util.Date;
+
+
+@TableName(schema = "db_ram_db",value = "tb_batt_rtstate")
+public class BattRTState{
+
+    private Long num;
+
+    @TableField("BattGroupId")
+    private Integer battGroupId;
+
+    private Integer battCount;
+
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private Date recDatetime;
+
+    private Float onlineVol;
+
+    private Float groupVol;
+
+    private Float groupTmp;
+
+    private Float groupCurr;
+
+    /**
+     *  鏈煡       0
+     *  娴厖       1
+     *  鍏呯數       2
+     *  鏀剧數       3
+     *  鍧囧厖       4
+     *  鍐呴樆娴嬭瘯   5
+     */
+    private Integer battState;
+
+    private Integer battTestType;
+
+    private Date battTestStarttime;
+
+    private Date battTestRecordtime;
+
+    private Integer battTestTlong;
+
+    private Float battTestCap;
+
+    private Float battRealCap;
+
+    private Float battRestCap;
+
+    private Integer battRestPower1Time;
+
+    private Integer battRestPower2Time;
+
+    private Integer testLoadertype;
+
+    private Integer testRecordnum;
+
+    private Integer a059Num;
+
+    @TableField("dev_testPower")
+    private Float devTestPower;
+
+    @TableField("batt_alm_state")
+    @ApiModelProperty(value = "鐢垫睜缁勫憡璀︾姸鎬�")
+    private Integer battAlmState;
+
+
+   /*9800鐢熸晥*/
+    @TableField("dev_temperature")
+    @ApiModelProperty(value = "娓╁害浼犳劅鍣ㄦ俯搴�")
+    private Float devTemperature;
+
+    @TableField("dev_humidity")
+    @ApiModelProperty(value = "娓╁害浼犳劅鍣ㄦ箍搴�")
+    private Float devHumidity;
+
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/whyc/service/BattRTStateService.java b/src/main/java/com/whyc/service/BattRTStateService.java
new file mode 100644
index 0000000..7af6eea
--- /dev/null
+++ b/src/main/java/com/whyc/service/BattRTStateService.java
@@ -0,0 +1,10 @@
+package com.whyc.service;
+
+import com.whyc.dto.Response;
+import org.springframework.stereotype.Service;
+
+@Service
+public class BattRTStateService {
+
+
+}
diff --git a/src/main/java/com/whyc/service/ReportService.java b/src/main/java/com/whyc/service/ReportService.java
index f8b990a..a018113 100644
--- a/src/main/java/com/whyc/service/ReportService.java
+++ b/src/main/java/com/whyc/service/ReportService.java
@@ -1,9 +1,20 @@
 package com.whyc.service;
 
+import com.whyc.dto.Response;
 import com.whyc.mapper.ReportMapper;
+import com.whyc.util.CommonUtil;
+import org.docx4j.Docx4J;
+import org.docx4j.openpackaging.exceptions.Docx4JException;
+import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
+import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import javax.xml.bind.JAXBException;
+import java.io.File;
+import java.text.SimpleDateFormat;
+import java.util.HashMap;
 
 @Service
 public class ReportService {
@@ -11,4 +22,44 @@
     @Resource
     private ReportMapper mapper;
 
+    @Autowired
+    private BattRTStateService battRTStateService;
+
+    public Response<String> generateDocument() throws JAXBException, Docx4JException {
+        Response<String> response = new Response<>();
+        //鑾峰彇classpath涓嬬殑妯℃澘
+        String templatePath = this.getClass().getClassLoader().getResource("").getPath()+"\\template\\template_01.docx";
+        //鍔犺浇妯℃澘
+        WordprocessingMLPackage wordprocessingMLPackage = WordprocessingMLPackage.load(new File(templatePath));
+        MainDocumentPart documentPart =  wordprocessingMLPackage.getMainDocumentPart();
+
+        //鏌ヨ宸℃鏃跺�欑殑鐢垫祦鐢靛帇娓╁害鏁版嵁
+        float current = 1.0f;
+        float voltage = 2.0f;
+        float temperature = 3.0f;
+        String reporter = "whyc";
+        String inspectionTime = "2020-01-01 00:00:00";
+        HashMap<String,String> map = new HashMap<>();
+        map.put("current",current+"");
+        map.put("voltage",voltage+"");
+        map.put("temperature",temperature+"");
+        map.put("reporter",reporter);
+        map.put("inspectionTime",inspectionTime);
+        //鏇挎崲鍗犱綅绗�
+        documentPart.variableReplace(map);
+        //杈撳嚭鏂囦欢鍚嶇О
+        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
+        String fileName = "鎶ュ憡_01_"+format.format(System.currentTimeMillis())+".docx";
+        String fileDir = CommonUtil.getRootFile()+"鎶ュ憡";
+        File dirFile = new File(fileDir);
+        if(!dirFile.exists()){
+            dirFile.mkdirs();
+        }
+        String filePath = fileDir + File.separator + fileName;
+        Docx4J.save(wordprocessingMLPackage, new File(filePath));
+        response.setCode(1);
+        response.setData("\\inspectionSystem_file\\鎶ュ憡\\"+fileName);
+        return response;
+
+    }
 }
diff --git a/src/main/java/com/whyc/util/CommonUtil.java b/src/main/java/com/whyc/util/CommonUtil.java
new file mode 100644
index 0000000..37ce0de
--- /dev/null
+++ b/src/main/java/com/whyc/util/CommonUtil.java
@@ -0,0 +1,57 @@
+package com.whyc.util;
+
+import com.whyc.constant.YamlProperties;
+import org.springframework.boot.system.ApplicationHome;
+import org.springframework.stereotype.Component;
+
+import java.io.File;
+
+/**
+ * 閫氱敤宸ュ叿鍒�
+ */
+@Component
+public class CommonUtil {
+
+    public static String classesPath(){
+        ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
+        File jarFile = applicationHome.getDir();
+        return jarFile.toString();
+    }
+
+    /**
+     * 鑾峰彇瀛樻斁鏂囦欢鐨勬牴璺緞
+     * @return 杩斿洖瀛樻斁鏂囦欢鐨勬牴璺緞
+     */
+    public static String getRootFile(){
+        ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
+        File jarFile = applicationHome.getDir();
+        String baseDirPath;
+        if(YamlProperties.profileType.equalsIgnoreCase("dev")) {
+            //寮�鍙戣矾寰�
+            baseDirPath = jarFile.getParentFile().toString()+File.separator+"inspectionSystem_file"+File.separator;
+        }else {
+            //鎵撳寘璺緞
+            baseDirPath = jarFile.toString()+File.separator+"inspectionSystem_file"+File.separator;
+        }
+        return baseDirPath;
+    }
+
+    /**
+     * 鑾峰彇椤圭洰鎵�鍦ㄦ枃浠跺す璺緞
+     * @return 鑾峰彇椤圭洰鎵�鍦ㄦ枃浠跺す璺緞
+     */
+    public static String getProjectDir(){
+        ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
+        File jarFile = applicationHome.getDir();
+        String baseDirPath;
+        if(YamlProperties.profileType.equalsIgnoreCase("dev")) {
+            //寮�鍙戣矾寰�
+            baseDirPath = jarFile.getParentFile().toString();
+        }else {
+            //鎵撳寘璺緞
+            baseDirPath = jarFile.toString();
+        }
+        return baseDirPath;
+    }
+
+}
diff --git a/src/main/resources/config/application-dev.yml b/src/main/resources/config/application-dev.yml
index fb86b5e..51b61ed 100644
--- a/src/main/resources/config/application-dev.yml
+++ b/src/main/resources/config/application-dev.yml
@@ -1,6 +1,3 @@
-#绯荤粺绫诲瀷:1(鏅��,鏃犻檺鍒�),2(涓ユ牸-gw鏍囧噯),3(涓ユ牸-绛惧悕)
-system:
-  type: 1
 #鏈嶅姟绔彛鍙�
 server:
   port: 8081
@@ -59,10 +56,3 @@
   enable: true
 configFile:
   type: 1 #1:鏈湴娴嬭瘯锛�2锛氭墦鍖卝ar
-snmp:
-  version: 1
-#  address: 10.34.245.2/161
-  address: 192.168.10.80/161
-  community: public
-  retries: 1
-  timeout: 5000
diff --git a/src/main/resources/config/application-prod.yml b/src/main/resources/config/application-prod.yml
index 51d5fc7..b801158 100644
--- a/src/main/resources/config/application-prod.yml
+++ b/src/main/resources/config/application-prod.yml
@@ -1,6 +1,3 @@
-#绯荤粺绫诲瀷:1(鏅��,鏃犻檺鍒�),2(涓ユ牸-gw鏍囧噯),3(涓ユ牸-绛惧悕)
-system:
-  type: 1
 #鏈嶅姟绔彛鍙�
 server:
   port: 8081
@@ -19,10 +16,10 @@
 spring:
   datasource:
     type: com.alibaba.druid.pool.DruidDataSource
-    driver-class-name: com.highgo.jdbc.Driver
-    url: jdbc:highgo://localhost:5866/highgo?rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
-    username: sysdba
-    password: Lmx&8688139
+    driver-class-name: com.mysql.cj.jdbc.Driver
+    url: jdbc:mysql://localhost:3360/db_user?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true&rewriteBatchedStatements=true&useSSL=false
+    username: root
+    password: lmx8688139
     maxIdel: 60
     initialPoolSize: 2
     minPoolSize: 5
@@ -56,10 +53,4 @@
   enable: false
 configFile:
   type: 2 #1:鏈湴娴嬭瘯锛�2锛氭墦鍖卝ar
-snmp:
-  version: 1
-  address: 10.34.245.2/161
-  community: public
-  retries: 1
-  timeout: 5000
 

--
Gitblit v1.9.1