whycxzp
2024-08-01 52f98f1a31e682f725f09b4a37bcd316332d3131
优化word报告生成
4个文件已修改
4个文件已添加
304 ■■■■ 已修改文件
src/main/java/com/whyc/constant/YamlProperties.java 31 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/ReportController.java 38 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/pojo/BattRTState.java 90 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/BattRTStateService.java 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/ReportService.java 51 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/util/CommonUtil.java 57 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/config/application-dev.yml 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/config/application-prod.yml 17 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/constant/YamlProperties.java
New file
@@ -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;
    }
}
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();
    }
}
src/main/java/com/whyc/pojo/BattRTState.java
New file
@@ -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;
}
src/main/java/com/whyc/service/BattRTStateService.java
New file
@@ -0,0 +1,10 @@
package com.whyc.service;
import com.whyc.dto.Response;
import org.springframework.stereotype.Service;
@Service
public class BattRTStateService {
}
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;
    }
}
src/main/java/com/whyc/util/CommonUtil.java
New file
@@ -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;
    }
}
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:打包jar
snmp:
  version: 1
#  address: 10.34.245.2/161
  address: 192.168.10.80/161
  community: public
  retries: 1
  timeout: 5000
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:打包jar
snmp:
  version: 1
  address: 10.34.245.2/161
  community: public
  retries: 1
  timeout: 5000