Browse Source

提交导出报告代码

tags/V1.0.1^2
wanjing 1 year ago
parent
commit
df9b341b8f
15 changed files with 503 additions and 18 deletions
  1. +5
    -5
      pom.xml
  2. +112
    -0
      tuoheng-common/tuoheng-common-core/src/main/java/com/tuoheng/common/core/annotation/Excel.java
  3. +18
    -0
      tuoheng-common/tuoheng-common-core/src/main/java/com/tuoheng/common/core/annotation/Excels.java
  4. +7
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/constant/SystemConstant.java
  5. +26
    -3
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/AirDataController.java
  6. +5
    -5
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/entity/domain/AirData.java
  7. +44
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/entity/request/VoteTitleRequest.java
  8. +119
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/entity/vo/airData/AirDataExcelVo.java
  9. +25
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/enums/AirDataEnum.java
  10. +26
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/airData/AirDataServiceImpl.java
  11. +5
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/airData/IAirDataService.java
  12. +106
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/airData/query/QueryAirDataListService.java
  13. +1
    -1
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/airData/report/ExportReportService.java
  14. +3
    -3
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/inspection/add/AddInspectionService.java
  15. +1
    -1
      tuoheng-service/tuoheng-admin/src/test/java/com/tuoheng/admin/service/ExportReportServiceTest.java

+ 5
- 5
pom.xml View File

@@ -70,11 +70,11 @@
<scope>import</scope>
</dependency>
<!-- Excel工具 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.poi</groupId>-->
<!-- <artifactId>poi-ooxml</artifactId>-->
<!-- <version>${poi.version}</version>-->
<!-- </dependency>-->
<!-- JSON 解析器和生成器 -->
<dependency>
<groupId>com.alibaba</groupId>

+ 112
- 0
tuoheng-common/tuoheng-common-core/src/main/java/com/tuoheng/common/core/annotation/Excel.java View File

@@ -0,0 +1,112 @@
package com.tuoheng.common.core.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 自定义导出Excel
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Excel {

/**
* 导出到Excel中的名字.
*/
String name() default "";

/**
* 日期格式, 如: yyyy-MM-dd
*/
String dateFormat() default "";

/**
* 读取内容转表达式 (如: 0=男,1=女,2=未知)
*/
String readConverterExp() default "";

/**
* 导出类型(0数字 1字符串)
*/
ColumnType cellType() default ColumnType.STRING;

/**
* 导出时在excel中每个列的高度 单位为字符
*/
double height() default 14;

/**
* 导出时在excel中每个列的宽 单位为字符
*/
double width() default 16;

/**
* 文字后缀,如% 90 变成90%
*/
String suffix() default "";

/**
* 当值为空时,字段的默认值
*/
String defaultValue() default "";

/**
* 提示信息
*/
String prompt() default "";

/**
* 设置只能选择不能输入的列内容.
*/
String[] combo() default {};

/**
* 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
*/
boolean isExport() default true;

/**
* 另一个类中的属性名称,支持多级获取,以小数点隔开
*/
String targetAttr() default "";

/**
* 字段类型(0:导出导入;1:仅导出;2:仅导入)
*/
Type type() default Type.ALL;

/**
* 导出类型枚举
*/
enum Type {
ALL(0), EXPORT(1), IMPORT(2);
private final int value;

Type(int value) {
this.value = value;
}

public int value() {
return this.value;
}
}

/**
* 列类型枚举
*/
enum ColumnType {
NUMERIC(0), STRING(1);
private final int value;

ColumnType(int value) {
this.value = value;
}

public int value() {
return this.value;
}
}

}

+ 18
- 0
tuoheng-common/tuoheng-common-core/src/main/java/com/tuoheng/common/core/annotation/Excels.java View File

@@ -0,0 +1,18 @@
package com.tuoheng.common.core.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 自定义导出Excel注解集
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Excels {

// Excel数组
Excel[] value();

}

+ 7
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/constant/SystemConstant.java View File

@@ -7,6 +7,13 @@ public interface SystemConstant {
*/
String PLATFORM_CODE = "airmonitor";

/**
* 表格表头集
*/
String[] EXCEL_NAME = {"采样点时间","相对高度m","经度",
"纬度","温度°C","湿度%","气压Pa","VOCs ppm","SO2 ppd","CO ppm","NO2 ppb",
"o3+NO2 ppm","PM1.0 ug/m3","PM2.5 ug/m3","PM10 ug/m3","设备序列号"};

/**
* 顶级部门pid,默认为0
*/

+ 26
- 3
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/AirDataController.java View File

@@ -1,5 +1,6 @@
package com.tuoheng.admin.controller;

import com.tuoheng.admin.entity.domain.AirData;
import com.tuoheng.admin.entity.request.report.ExportReportRequest;
import com.tuoheng.admin.service.airData.IAirDataService;
import com.tuoheng.common.core.utils.JsonResult;
@@ -9,9 +10,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @Author ChengWang
@@ -33,6 +32,30 @@ public class AirDataController {
return null;
}

/**
* 根据任务id获取大气数据列表
*
* @param id
* @return
*/
@GetMapping("/list/{id}")
public List<AirData> getList(@PathVariable("id") String id) {
return airDataService.getAirDataList(id);
}

/**
* 生成excel表格
*/
@GetMapping("/generateTable/{id}")
public JsonResult generateTableById(@PathVariable("id") String id) {
return airDataService.generateTable(id);
}

/**
* 下载表格
*/


/**
* 导出报告
*/

+ 5
- 5
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/entity/domain/AirData.java View File

@@ -138,11 +138,6 @@ public class AirData extends BaseEntity {
*/
private String ph3;

/**
* PM1.0(单位ug/m3)
*/
private String pm10;

/**
* PM1.0(单位ug/m3)
*/
@@ -178,6 +173,11 @@ public class AirData extends BaseEntity {
*/
private String wrSo2;

/**
* 二氧化氮(单位ppm)
*/
private String n02;





+ 44
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/entity/request/VoteTitleRequest.java View File

@@ -0,0 +1,44 @@
package com.tuoheng.admin.entity.request;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.util.CollectionUtils;
import org.springframework.util.PropertyPlaceholderHelper;

import java.util.List;
import java.util.Properties;

/**
* @Author ChengWang
* @Date 2023/5/24
*/
public class VoteTitleRequest implements CellWriteHandler {

private String title;

PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("${", "}");

public VoteTitleRequest(String title) {
this.title = title;
}

@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
if(head != null){
List<String> headNameList = head.getHeadNameList();
if(CollectionUtil.isNotEmpty(headNameList)){
Properties properties = new Properties();
properties.setProperty("title", title);
for (int i = 0; i < headNameList.size(); i++) {
headNameList.set(i, placeholderHelper.replacePlaceholders(headNameList.get(i), properties));
}
}
}

}
}

+ 119
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/entity/vo/airData/AirDataExcelVo.java View File

@@ -0,0 +1,119 @@
package com.tuoheng.admin.entity.vo.airData;

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
* @Author ChengWang
* @Date 2023/5/24
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Accessors(chain = true)
public class AirDataExcelVo {

/**
* 采样点时间
*/
@ExcelProperty(value = {"采样点时间"},index = 0)
private String cUtcTime;

/**
* 相对高度
*/
@ExcelProperty(value = {"相对高度m"},index = 1)
private String altitude;

/**
* 经度
*/
@ExcelProperty(value = {"经度"},index = 2)
private String longitude;

/**
* 纬度
*/
@ExcelProperty(value = {"纬度"},index = 3)
private String latitude;

/**
* 温度
*/
@ExcelProperty(value = {"温度°C"},index = 4)
private String temperature;

/**
* 湿度
*/
@ExcelProperty(value = {"湿度%"},index = 5)
private String humidity;

/**
* 大气压
*/
@ExcelProperty(value = {"气压Pa"},index = 6)
private String pressure;

/**
*挥发性有机物(单位ppm)
*/
@ExcelProperty(value = {"VOCs ppm"},index = 7)
private String vocs;

/**
*二氧化硫(单位ppm)
*/
@ExcelProperty(value = {"SO2 ppd"},index = 8)
private String so2;

/**
* 一氧化碳(单位ppm)
*/
@ExcelProperty(value = {"CO ppm"},index = 9)
private String c0;

/**
* 二氧化氮(单位ppm)
*/
@ExcelProperty(value = {"NO2 ppb"},index = 10)
private String n02;

/**
* o3+NO2
*/
@ExcelProperty(value = {"o3+NO2 ppm"},index = 11)
private String ox;

/**
* PM1.0(单位ug/m3)
*/
@ExcelProperty(value = {"PM1.0 ug/m3"},index = 12)
private String pm10;

/**
*PM2.5(单位ug/m3)
*/
@ExcelProperty(value = {"PM2.5 ug/m3"},index = 13)
private String pm_2_5;

/**
*PM10(单位ug/m3)
*/
@ExcelProperty(value = {"PM10 ug/m3"},index = 14)
private String pm_10;

/**
* 设备序列号
*/
@ExcelProperty(value = {"设备序列号"},index = 15)
private String serial;



}

+ 25
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/enums/AirDataEnum.java View File

@@ -0,0 +1,25 @@
package com.tuoheng.admin.enums;

import lombok.Getter;

/**
* @Author ChengWang
* @Date 2023/5/19
*/
public enum AirDataEnum {

//任务状态1待执行 2执行中
INSPECTION_ID_IS_NULL(10001,"任务id为空"),
AIR_DATA_IS_NOT_EXIST(10002,"大气数据为空");

AirDataEnum(int code, String msg){
this.code = code;
this.msg = msg;
}

@Getter
private int code;

@Getter
private String msg;
}

+ 26
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/airData/AirDataServiceImpl.java View File

@@ -3,6 +3,7 @@ package com.tuoheng.admin.service.airData;
import com.tuoheng.admin.entity.domain.AirData;
import com.tuoheng.admin.entity.request.report.ExportReportRequest;
import com.tuoheng.admin.mapper.AirDataMapper;
import com.tuoheng.admin.service.airData.query.QueryAirDataListService;
import com.tuoheng.admin.service.airData.report.ExportReportService;
import com.tuoheng.common.core.common.BaseServiceImpl;
import com.tuoheng.common.core.utils.JsonResult;
@@ -10,6 +11,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Author ChengWang
* @Date 2023/5/23
@@ -21,8 +24,31 @@ public class AirDataServiceImpl extends BaseServiceImpl<AirDataMapper, AirData>
@Autowired
private ExportReportService eportReportService;

private QueryAirDataListService queryAirDataListService;


/**
* 根据任务id获取大气数据列表
*
* @param id
* @return
*/
@Override
public List<AirData> getAirDataList(String id) {
return queryAirDataListService.list(id);
}

/**
* 生成excel表格并下载
*
* @param id
*/
@Override
public JsonResult generateTable(String id) {
return queryAirDataListService.generateTable(id);
}

/**
* 导出报告
*
* @return

+ 5
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/airData/IAirDataService.java View File

@@ -4,6 +4,7 @@ import com.tuoheng.admin.entity.domain.AirData;
import com.tuoheng.admin.entity.request.report.ExportReportRequest;
import com.tuoheng.common.core.common.IBaseService;
import com.tuoheng.common.core.utils.JsonResult;
import java.util.List;

/**
* @Author ChengWang
@@ -11,6 +12,10 @@ import com.tuoheng.common.core.utils.JsonResult;
*/
public interface IAirDataService extends IBaseService<AirData> {

List<AirData> getAirDataList(String id);

JsonResult generateTable(String id);

/**
*
* 导出报告

+ 106
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/airData/query/QueryAirDataListService.java View File

@@ -0,0 +1,106 @@
package com.tuoheng.admin.service.airData.query;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.constant.SystemConstant;
import com.tuoheng.admin.entity.domain.AirData;
import com.tuoheng.admin.entity.domain.Inspection;
import com.tuoheng.admin.entity.vo.airData.AirDataExcelVo;
import com.tuoheng.admin.enums.AirDataEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.AirDataMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
* @Author ChengWang
* @Date 2023/5/24
*/
@Service
@Slf4j
public class QueryAirDataListService {

@Autowired
private AirDataMapper airDataMapper;

@Autowired
private InspectionMapper inspectionMapper;


public List<AirData> list(String id) {
List<AirData> airDataList = airDataMapper.selectList(Wrappers.<AirData>lambdaQuery()
.eq(AirData::getMark, MarkEnum.VALID.getCode())
.eq(AirData::getInspectionId, id));

return airDataList;
}

public JsonResult generateTable(String id) {
//校验
JsonResult jsonResult = this.check(id);
if(0 != jsonResult.getCode()){
log.info("生成excel表格的任务id为空");
return JsonResult.error(jsonResult.getCode(),jsonResult.getMsg());
}
//获取当前任务id下的大气数据
List<AirData> airDataList = airDataMapper.selectList(Wrappers.<AirData>lambdaQuery()
.eq(AirData::getMark, MarkEnum.VALID.getCode())
.eq(AirData::getInspectionId, id));
if(CollectionUtils.isEmpty(airDataList) || airDataList.size() == 0){
return JsonResult.error(AirDataEnum.AIR_DATA_IS_NOT_EXIST.getCode(),AirDataEnum.AIR_DATA_IS_NOT_EXIST.getMsg());
}
List<AirDataExcelVo> airDataExcelVoList = new ArrayList<>();
airDataList.forEach(x->{
AirDataExcelVo vo = new AirDataExcelVo();
BeanUtils.copyProperties(x,vo);
airDataExcelVoList.add(vo);
});
//获取任务名称
Inspection inspection = inspectionMapper.selectById(id);
String name = inspection.getName();
//获取到vo
JsonResult result = easyExcel(airDataExcelVoList,name);

return JsonResult.success("表格上传下载成功");
}

public static JsonResult easyExcel(List<AirDataExcelVo> voArrayList,String inspectionName) {
String fileNameTemp = "G:/测试/"; //文件输出目录(路径自定义)
String temp = UUID.randomUUID().toString()+".xlsx";
String fileName = fileNameTemp+temp;
List<List<String>> heads = new ArrayList<>();
String totalName = "项目名称:"+ inspectionName;
String[] nameString = SystemConstant.EXCEL_NAME;
for (String excelName : nameString) {
heads.add(Arrays.asList(totalName,excelName));
}
//需要写出的表格(对应实体类)
log.info("开始写入表格中");
EasyExcel.write(fileName, AirDataExcelVo.class).sheet("环境大气数据表").head(heads)
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(20)) // 简单的列宽策略,列宽20
//.registerWriteHandler(new SimpleRowHeightStyleStrategy((short)20,(short)20)) // 设置行高策略
.doWrite(voArrayList);//数据源
log.info("写入表格已完成");

return JsonResult.success();
}

private JsonResult check(String id) {
if(StringUtils.isEmpty(id)){
return JsonResult.error(AirDataEnum.INSPECTION_ID_IS_NULL.getCode(),AirDataEnum.INSPECTION_ID_IS_NULL.getMsg());
}
return JsonResult.success();
}
}

+ 1
- 1
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/airData/report/ExportReportService.java View File

@@ -111,7 +111,7 @@ public class ExportReportService {
this.insertAirDataExport(request, inspection, airDataType, ossUrl);

// 删除原文件
// FileUtils.deleteFile(filePath);
FileUtils.deleteFile(filePath);

return JsonResult.success();
}

+ 3
- 3
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/inspection/add/AddInspectionService.java View File

@@ -144,9 +144,9 @@ public class AddInspectionService {
if (StringUtils.isEmpty(addInspectionRequest.getEndLatitude())) {
return JsonResult.error(AddInspectionCodeEnum.END_LATITUDE_IS_NULL.getCode(), AddInspectionCodeEnum.END_LATITUDE_IS_NULL.getMsg());
}
if(StringUtils.isEmpty(addInspectionRequest.getNote())){
return JsonResult.error(AddInspectionCodeEnum.INSPECTION_NOTE_IS_NOT_EXIST.getCode(),AddInspectionCodeEnum.INSPECTION_NOTE_IS_NOT_EXIST.getMsg());
}
// if(StringUtils.isEmpty(addInspectionRequest.getNote())){
// return JsonResult.error(AddInspectionCodeEnum.INSPECTION_NOTE_IS_NOT_EXIST.getCode(),AddInspectionCodeEnum.INSPECTION_NOTE_IS_NOT_EXIST.getMsg());
// }
}
if (null == addInspectionRequest.getInspectionTime()) {
return JsonResult.error(AddInspectionCodeEnum.INSPECTION_TIME_IS_NULL.getCode(), AddInspectionCodeEnum.INSPECTION_TIME_IS_NULL.getMsg());

+ 1
- 1
tuoheng-service/tuoheng-admin/src/test/java/com/tuoheng/admin/service/ExportReportServiceTest.java View File

@@ -35,7 +35,7 @@ public class ExportReportServiceTest {
request.setGridConcentrationMix("16000ug/m3(1187761E318291N)"); // 网格浓度最小值
request.setSinglePointConcentrationMax("25000ug/m3(1187718318252N)2022/04/29 14:06:40"); // 单点浓度最高值
request.setSinglePointConcentrationMix("16000ug/m3(1187753318286 N)2022/04/2914:02:21"); // 单点浓度最低值
request.setGridSize(1);
// exportReportService.exportReport(request);
}


Loading…
Cancel
Save