Commit c85952d3 authored by 刘林's avatar 刘林

fix:(大屏):综合查询导出优化

parent 8cc27be1
...@@ -2,6 +2,10 @@ package com.yeejoin.amos.boot.module.statistcs.biz.service.impl; ...@@ -2,6 +2,10 @@ package com.yeejoin.amos.boot.module.statistcs.biz.service.impl;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions; import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
...@@ -11,6 +15,7 @@ import com.fasterxml.jackson.databind.JsonNode; ...@@ -11,6 +15,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.yeejoin.amos.boot.biz.common.annotation.TechnicalParameter; import com.yeejoin.amos.boot.biz.common.annotation.TechnicalParameter;
import com.yeejoin.amos.boot.biz.common.dao.mapper.DataDictionaryMapper; import com.yeejoin.amos.boot.biz.common.dao.mapper.DataDictionaryMapper;
import com.yeejoin.amos.boot.biz.common.dto.ByteArrayMultipartFile;
import com.yeejoin.amos.boot.biz.common.entity.BaseEntity; import com.yeejoin.amos.boot.biz.common.entity.BaseEntity;
import com.yeejoin.amos.boot.biz.common.entity.DataDictionary; import com.yeejoin.amos.boot.biz.common.entity.DataDictionary;
import com.yeejoin.amos.boot.biz.common.excel.ExcelUtil; import com.yeejoin.amos.boot.biz.common.excel.ExcelUtil;
...@@ -80,9 +85,11 @@ import org.typroject.tyboot.core.foundation.context.RequestContext; ...@@ -80,9 +85,11 @@ import org.typroject.tyboot.core.foundation.context.RequestContext;
import org.typroject.tyboot.core.foundation.exception.BaseException; import org.typroject.tyboot.core.foundation.exception.BaseException;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil; import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
...@@ -94,7 +101,6 @@ import java.util.concurrent.Executors; ...@@ -94,7 +101,6 @@ import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.yeejoin.amos.boot.module.common.biz.service.impl.TZSCommonServiceImpl.*; import static com.yeejoin.amos.boot.module.common.biz.service.impl.TZSCommonServiceImpl.*;
import static org.elasticsearch.index.query.QueryBuilders.existsQuery; import static org.elasticsearch.index.query.QueryBuilders.existsQuery;
...@@ -2697,51 +2703,109 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -2697,51 +2703,109 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
} }
private static MultipartFile getEquipMultipartFile(JSONArray records, String fileName, String sheetName) { private static MultipartFile getEquipMultipartFile(JSONArray records, String fileName, String sheetName) {
MultipartFile templateExcelFile; // 临时文件
List<EquipInfoVo> exportData = new ArrayList<>(); File tempFile = new File(System.getProperty("java.io.tmpdir"), fileName + ".xlsx");
records.forEach(obj -> {
JSONObject equip = JSONObject.parseObject(JSON.toJSONString(obj)); ExcelWriter excelWriter = null;
EquipInfoVo equipInfoVo = new EquipInfoVo(); try {
BeanUtil.copyProperties(equip, equipInfoVo, CopyOptions.create().setIgnoreNullValue(true)); excelWriter = EasyExcel.write(tempFile, EquipInfoVo.class)
// 最新一条检验信息 .excelType(ExcelTypeEnum.XLSX)
if (!equip.getJSONArray("inspections").isEmpty()) { .build();
JSONObject inspection = equip.getJSONArray("inspections").getJSONObject(0);
BeanUtil.copyProperties(inspection, equipInfoVo, CopyOptions.create().setIgnoreNullValue(true)); WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
}
// 最新一条维保信息 List<EquipInfoVo> batchData = new ArrayList<>(1000); // 每批 1000 条
if (!equip.getJSONArray("maintenances").isEmpty()) {
JSONObject maintenances = equip.getJSONArray("maintenances").getJSONObject(0); for (Object record : records) {
BeanUtil.copyProperties(maintenances, equipInfoVo, CopyOptions.create().setIgnoreNullValue(true)); JSONObject equip = (JSONObject) record;
} Map<String, Object> combinedMap = new HashMap<>();
Optional.of(equip)
.filter(e -> "3000".equals(String.valueOf(e.get("EQU_LIST_CODE")))) // 基础字段
.filter(e -> String.valueOf(e.get("DATA_SOURCE")).contains("jg_his_xa")) equip.forEach((k, v) -> combinedMap.put(k, v != null ? String.valueOf(v) : null));
.map(e -> String.valueOf(e.get("SEQUENCE_NBR")))
.ifPresent(equipInfoVo::setRECORD); // 最新检验
JSONArray inspections = equip.getJSONArray("inspections");
if (inspections != null && !inspections.isEmpty()) {
JSONObject inspection = inspections.getJSONObject(0);
inspection.forEach((k, v) -> combinedMap.put(k, v != null ? String.valueOf(v) : null));
}
// 最新维保
JSONArray maintenances = equip.getJSONArray("maintenances");
if (maintenances != null && !maintenances.isEmpty()) {
JSONObject maintenance = maintenances.getJSONObject(0);
maintenance.forEach((k, v) -> combinedMap.put(k, v != null ? String.valueOf(v) : null));
}
// RECORD 特殊逻辑
if ("3000".equals(String.valueOf(equip.get("EQU_LIST_CODE"))) &&
String.valueOf(equip.get("DATA_SOURCE")).contains("jg_his_xa")) {
combinedMap.put("RECORD", String.valueOf(equip.get("SEQUENCE_NBR")));
}
// 技术参数 // 技术参数
JSONArray techParams = equip.getJSONArray("techParams"); JSONArray techParams = equip.getJSONArray("techParams");
if (techParams != null && !techParams.isEmpty()) { if (techParams != null && !techParams.isEmpty()) {
JSONObject paramsJson = new JSONObject(); for (int k = 0; k < techParams.size(); k++) {
equip.getJSONArray("techParams").forEach(techParam -> { JSONObject param = techParams.getJSONObject(k);
ESEquipmentInfo.TechParam techParamItem = JSONObject.parseObject(JSON.toJSONString(techParam), ESEquipmentInfo.TechParam.class); String key = param.getString("paramKey");
Optional.ofNullable(techParamItem.getParamKey()).ifPresent(key -> Stream.of( if (key == null) continue;
Optional.ofNullable(techParamItem.getBoolValue()),
Optional.ofNullable(techParamItem.getDateValue()), Object value = param.getOrDefault("strValue",
Optional.ofNullable(techParamItem.getLongValue()), param.getOrDefault("boolValue",
Optional.ofNullable(techParamItem.getStrValue()), param.getOrDefault("dateValue",
Optional.ofNullable(techParamItem.getDoubleValue()) param.getOrDefault("longValue",
) param.get("doubleValue")))));
.filter(Optional::isPresent)
.findFirst() if (value != null) {
.ifPresent(value -> paramsJson.put(key, value.get()))); combinedMap.put(key, String.valueOf(value));
}); }
BeanUtil.copyProperties(paramsJson, equipInfoVo, CopyOptions.create().setIgnoreNullValue(true));
} }
exportData.add(equipInfoVo); }
// Map → VO,并过滤非法日期
EquipInfoVo vo = new EquipInfoVo();
combinedMap.replaceAll((k, v) -> {
if (v != null && v.toString().startsWith("-0001")) return null; // 过滤非法日期
return v;
}); });
templateExcelFile = ExcelUtil.createTemplateExcelFile(fileName, sheetName, exportData, EquipInfoVo.class, null, false); BeanUtil.copyProperties(combinedMap, vo, CopyOptions.create().setIgnoreNullValue(true));
return templateExcelFile;
batchData.add(vo);
// 批量写入
if (batchData.size() >= 1000) {
excelWriter.write(batchData, writeSheet);
batchData.clear();
}
}
// 写入剩余数据
if (!batchData.isEmpty()) {
excelWriter.write(batchData, writeSheet);
}
} catch (Exception e) {
throw new RuntimeException("生成Excel失败", e);
} finally {
if (excelWriter != null) {
excelWriter.finish();
}
}
// 返回 MultipartFile
try {
return new ByteArrayMultipartFile(
fileName,
fileName + ".xlsx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
Files.readAllBytes(tempFile.toPath())
);
} catch (IOException e) {
throw new RuntimeException("Excel文件读取失败", e);
} finally {
tempFile.delete();
}
} }
private static MultipartFile getCompanyMultipartFile(JSONArray records, String fileName, String sheetName) { private static MultipartFile getCompanyMultipartFile(JSONArray records, String fileName, String sheetName) {
...@@ -2751,7 +2815,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl { ...@@ -2751,7 +2815,7 @@ public class ComprehensiveStatisticalAnalysisServiceImpl {
String jsonString = JSON.toJSONString(company); String jsonString = JSON.toJSONString(company);
CompanyInfoVo companyInfoVo = JSONObject.parseObject(jsonString, CompanyInfoVo.class); CompanyInfoVo companyInfoVo = JSONObject.parseObject(jsonString, CompanyInfoVo.class);
JSONArray licenses = JSON.parseArray(JSON.toJSONString(JSON.parseObject(jsonString).get("licenses"))); JSONArray licenses = JSON.parseArray(JSON.toJSONString(JSON.parseObject(jsonString).get("licenses")));
if (!licenses.isEmpty()) { if (!ObjectUtils.isEmpty(licenses)) {
licenses.forEach(lic -> { licenses.forEach(lic -> {
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(lic)); JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(lic));
CompanyInfoVo temp = jsonObject.toJavaObject(CompanyInfoVo.class); CompanyInfoVo temp = jsonObject.toJavaObject(CompanyInfoVo.class);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment