Browse Source

根据任务id导出数据excel表格

tags/V1.0.1^2
chengwang 1 year ago
parent
commit
11aab0266d
10 changed files with 193 additions and 15 deletions
  1. +5
    -10
      pom.xml
  2. +7
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/constant/SystemConstant.java
  3. +13
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/AirDataController.java
  4. +44
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/entity/request/VoteTitleRequest.java
  5. +2
    -2
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/entity/vo/airData/AirDataExcelVo.java
  6. +25
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/enums/AirDataEnum.java
  7. +19
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/airData/AirDataServiceImpl.java
  8. +2
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/airData/IAirDataService.java
  9. +73
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/airData/query/QueryAirDataListService.java
  10. +3
    -3
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/inspection/add/AddInspectionService.java

+ 5
- 10
pom.xml View File

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

+ 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
*/

+ 13
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/AirDataController.java View File

@@ -34,4 +34,17 @@ public class AirDataController {
return airDataService.getAirDataList(id);
}

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

/**
* 下载表格
*/


}

+ 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));
}
}
}

}
}

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

@@ -21,13 +21,13 @@ public class AirDataExcelVo {
/**
* 采样点时间
*/
@ExcelProperty(value = {"项目名称: ","采样点时间"},index = 0)
@ExcelProperty(value = {"采样点时间"},index = 0)
private String cUtcTime;

/**
* 相对高度
*/
@ExcelProperty(value = {"项目名称: 监测单位:","相对高度m"},index = 1)
@ExcelProperty(value = {"相对高度m"},index = 1)
private String altitude;

/**

+ 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;
}

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

@@ -1,14 +1,22 @@
package com.tuoheng.admin.service.airData;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.domain.AirData;
import com.tuoheng.admin.enums.AirDataEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.AirDataMapper;
import com.tuoheng.admin.service.airData.query.QueryAirDataListService;
import com.tuoheng.common.core.common.BaseServiceImpl;
import com.tuoheng.common.core.enums.ServiceExceptionEnum;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.rowset.serial.SerialException;
import java.util.List;

/**
@@ -32,4 +40,15 @@ public class AirDataServiceImpl extends BaseServiceImpl<AirDataMapper, AirData>
public List<AirData> getAirDataList(String id) {
return queryAirDataListService.list(id);
}

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


}

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

@@ -13,4 +13,6 @@ import java.util.List;
public interface IAirDataService extends IBaseService<AirData> {

List<AirData> getAirDataList(String id);

JsonResult generateTable(String id);
}

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

@@ -1,15 +1,28 @@
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
@@ -22,6 +35,9 @@ 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()
@@ -30,4 +46,61 @@ public class QueryAirDataListService {

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();
}
}

+ 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());

Loading…
Cancel
Save