Browse Source

导出优化

zhc
review512jwy@163.com 3 weeks ago
parent
commit
5d4d03ba1d
  1. 2
      dongjian-dashboard-back-common/src/main/java/com/dongjian/dashboard/back/common/Constants.java
  2. 72
      dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/controller/DeviceDataAccumulateController.java
  3. 61
      dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/controller/DeviceDataAlarmController.java
  4. 65
      dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/controller/DeviceDataBaStatusController.java
  5. 65
      dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/controller/DeviceDataMeasureController.java

2
dongjian-dashboard-back-common/src/main/java/com/dongjian/dashboard/back/common/Constants.java

@ -12,6 +12,8 @@ public class Constants {
public static final ZoneId ZONE_TOKYO = ZoneId.of("Asia/Tokyo");
public static final DateTimeFormatter FORMATTER_YMDHMS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static final int EXPORT_PAGE_SIZE = 600;
//这个很重要,不要随便动
public static final String DES_SALT = "ci3b512jwy199511";

72
dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/controller/DeviceDataAccumulateController.java

@ -2,7 +2,10 @@ package com.dongjian.dashboard.back.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.dongjian.dashboard.back.common.Constants;
import com.dongjian.dashboard.back.common.exception.BusinessException;
import com.dongjian.dashboard.back.common.response.PageInfo;
import com.dongjian.dashboard.back.common.response.PageResponse;
@ -19,11 +22,13 @@ import com.dongjian.dashboard.back.service.common.CommonOpt;
import com.dongjian.dashboard.back.util.CommonUtil;
import com.dongjian.dashboard.back.vo.data.DeviceAccumulateData;
import com.dongjian.dashboard.back.vo.device.LineData;
import com.github.pagehelper.PageHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
@ -98,25 +103,58 @@ public class DeviceDataAccumulateController {
String fileName = URLEncoder.encode("積算データ"+CommonUtil.generateExcelSuffix(), StandardCharsets.UTF_8).replace("+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
PageInfo<DeviceAccumulateData> pageData = deviceDataAccumulateService.getDataList(pageSearchParam, CompanyId, UserId, LanguageType);
List<DeviceAccumulateData> list = pageData.getList();
// ===== 固定导出分页大小=====
pageSearchParam.setPageSize(Constants.EXPORT_PAGE_SIZE);
List<ExportDeviceAccumulateDataDTO> exportList = list.stream().map(item -> {
ExportDeviceAccumulateDataDTO dto = new ExportDeviceAccumulateDataDTO();
BeanUtils.copyProperties(item, dto);
dto.setLastYearRatio(CommonUtil.ratio(item.getCumulativeValue(), item.getLastYearValue()));
dto.setYesterdayRatio(CommonUtil.ratio(item.getCumulativeValue(), item.getYesterdayValue()));
dto.setStatus111(commonOpt.status111Mapping(item.getStatus111()));
return dto;
}).toList();
// 构造 Excel header:每列三语组合为字符串 "中文||English||日本語"
// ===== 构建 Excel =====
List<List<String>> head = LanguageDynamicHeaderAdapter.buildHead(ExportDeviceAccumulateDataDTO.class, LanguageType);
EasyExcel.write(response.getOutputStream())
.head(head)
.sheet("sheet1")
.doWrite(exportList);
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).head(head).build();
WriteSheet sheet = EasyExcel.writerSheet("sheet1").build();
// ===== 循环分页查询并写 Excel =====
int pageNum = 1;
try {
while (true) {
pageSearchParam.setPageNum(pageNum);
PageInfo<DeviceAccumulateData> pageData = deviceDataAccumulateService.getDataList(pageSearchParam, CompanyId, UserId, LanguageType);
List<DeviceAccumulateData> list = pageData.getList();
if (CollectionUtils.isEmpty(list)) {
break; // 没数据了,结束
}
for (DeviceAccumulateData item : list) {
ExportDeviceAccumulateDataDTO dto = new ExportDeviceAccumulateDataDTO();
BeanUtils.copyProperties(item, dto);
dto.setLastYearRatio(CommonUtil.ratio(item.getCumulativeValue(), item.getLastYearValue()));
dto.setYesterdayRatio(CommonUtil.ratio(item.getCumulativeValue(), item.getYesterdayValue()));
dto.setStatus111(commonOpt.status111Mapping(item.getStatus111()));
excelWriter.write(List.of(dto), sheet);
// 手动置空,提醒 GC 回收
dto = null;
item = null;
}
// 手动清理分页数据和缓存
list.clear();
PageHelper.clearPage();
// 最后一页判断(加这个更安全)
if (pageNum >= pageData.getPages()) {
break;
}
pageNum++;
}
} catch (Exception e) {
logger.error("export error", e);
throw e;
} finally {
excelWriter.finish();
PageHelper.clearPage();
}
}

61
dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/controller/DeviceDataAlarmController.java

@ -2,6 +2,9 @@ package com.dongjian.dashboard.back.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.dongjian.dashboard.back.common.Constants;
import com.dongjian.dashboard.back.common.exception.BusinessException;
import com.dongjian.dashboard.back.common.response.PageInfo;
import com.dongjian.dashboard.back.common.response.PageResponse;
@ -21,11 +24,13 @@ import com.dongjian.dashboard.back.util.CommonUtil;
import com.dongjian.dashboard.back.vo.data.DeviceAlarmData;
import com.dongjian.dashboard.back.vo.data.HandleHistoryDataVO;
import com.dongjian.dashboard.back.vo.device.WindowAlertVO;
import com.github.pagehelper.PageHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
@ -98,22 +103,52 @@ public class DeviceDataAlarmController {
String fileName = URLEncoder.encode("警報データ"+ CommonUtil.generateExcelSuffix(), StandardCharsets.UTF_8).replace("+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
PageInfo<DeviceAlarmData> pageData = deviceDataAlarmService.getDataList(pageSearchParam, CompanyId, UserId, LanguageType);
List<DeviceAlarmData> list = pageData.getList();
// ===== 固定导出分页大小 =====
pageSearchParam.setPageSize(Constants.EXPORT_PAGE_SIZE);
List<ExportDeviceAlarmDataDTO> exportList = list.stream().map(item -> {
ExportDeviceAlarmDataDTO dto = new ExportDeviceAlarmDataDTO();
BeanUtils.copyProperties(item, dto);
return dto;
}).toList();
// 构造 Excel header:每列三语组合为字符串 "中文||English||日本語"
// ===== 构建 Excel =====
List<List<String>> head = LanguageDynamicHeaderAdapter.buildHead(ExportDeviceAlarmDataDTO.class, LanguageType);
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).head(head).build();
WriteSheet sheet = EasyExcel.writerSheet("sheet1").build();
EasyExcel.write(response.getOutputStream())
.head(head)
.sheet("sheet1")
.doWrite(exportList);
int pageNum = 1;
try {
while (true) {
pageSearchParam.setPageNum(pageNum);
PageInfo<DeviceAlarmData> pageData = deviceDataAlarmService.getDataList(pageSearchParam, CompanyId, UserId, LanguageType);
List<DeviceAlarmData> list = pageData.getList();
if (CollectionUtils.isEmpty(list)) {
break; // 没数据了,结束
}
for (DeviceAlarmData item : list) {
ExportDeviceAlarmDataDTO dto = new ExportDeviceAlarmDataDTO();
BeanUtils.copyProperties(item, dto);
excelWriter.write(List.of(dto), sheet);
// 手动置空,提醒 GC 回收
dto = null;
item = null;
}
// 手动清理分页数据和缓存
list.clear();
PageHelper.clearPage();
if (pageNum >= pageData.getPages()) {
break;
}
pageNum++;
}
} catch (Exception e) {
logger.error("export alarm error", e);
throw e;
} finally {
excelWriter.finish();
PageHelper.clearPage();
}
}
@OperationLog(operation = "handleAlarm", remark = "")

65
dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/controller/DeviceDataBaStatusController.java

@ -2,6 +2,9 @@ package com.dongjian.dashboard.back.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.dongjian.dashboard.back.common.Constants;
import com.dongjian.dashboard.back.common.exception.BusinessException;
import com.dongjian.dashboard.back.common.response.PageInfo;
import com.dongjian.dashboard.back.common.response.PageResponse;
@ -18,11 +21,13 @@ import com.dongjian.dashboard.back.service.common.CommonOpt;
import com.dongjian.dashboard.back.util.CommonUtil;
import com.dongjian.dashboard.back.vo.data.DeviceBaStatusData;
import com.dongjian.dashboard.back.vo.device.LineData;
import com.github.pagehelper.PageHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
@ -97,23 +102,53 @@ public class DeviceDataBaStatusController {
String fileName = URLEncoder.encode("稼働設備"+ CommonUtil.generateExcelSuffix(), StandardCharsets.UTF_8).replace("+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
PageInfo<DeviceBaStatusData> pageData = deviceDataBaStatusService.getDataList(pageSearchParam, CompanyId, UserId, LanguageType);
List<DeviceBaStatusData> list = pageData.getList();
// ===== 固定导出分页大小 =====
pageSearchParam.setPageSize(Constants.EXPORT_PAGE_SIZE);
List<ExportDeviceBaStatusDataDTO> exportList = list.stream().map(item -> {
ExportDeviceBaStatusDataDTO dto = new ExportDeviceBaStatusDataDTO();
BeanUtils.copyProperties(item, dto);
dto.setStatus111(commonOpt.status111Mapping(item.getStatus111()));
return dto;
}).toList();
// 构造 Excel header:每列三语组合为字符串 "中文||English||日本語"
// ===== 构建 Excel =====
List<List<String>> head = LanguageDynamicHeaderAdapter.buildHead(ExportDeviceBaStatusDataDTO.class, LanguageType);
EasyExcel.write(response.getOutputStream())
.head(head)
.sheet("sheet1")
.doWrite(exportList);
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).head(head).build();
WriteSheet sheet = EasyExcel.writerSheet("sheet1").build();
int pageNum = 1;
try {
while (true) {
pageSearchParam.setPageNum(pageNum);
PageInfo<DeviceBaStatusData> pageData = deviceDataBaStatusService.getDataList(pageSearchParam, CompanyId, UserId, LanguageType);
List<DeviceBaStatusData> list = pageData.getList();
if (CollectionUtils.isEmpty(list)) {
break; // 没数据了,结束
}
for (DeviceBaStatusData item : list) {
ExportDeviceBaStatusDataDTO dto = new ExportDeviceBaStatusDataDTO();
BeanUtils.copyProperties(item, dto);
dto.setStatus111(commonOpt.status111Mapping(item.getStatus111()));
excelWriter.write(List.of(dto), sheet);
// 手动置空,提醒 GC 回收
dto = null;
item = null;
}
// 手动清理分页数据和缓存
list.clear();
PageHelper.clearPage();
if (pageNum >= pageData.getPages()) {
break;
}
pageNum++;
}
} catch (Exception e) {
logger.error("export BA status error", e);
throw e;
} finally {
excelWriter.finish();
PageHelper.clearPage();
}
}
@OperationLog(operation = "getLineData", remark = "")

65
dongjian-dashboard-back-controller/src/main/java/com/dongjian/dashboard/back/controller/DeviceDataMeasureController.java

@ -2,6 +2,9 @@ package com.dongjian.dashboard.back.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.dongjian.dashboard.back.common.Constants;
import com.dongjian.dashboard.back.common.exception.BusinessException;
import com.dongjian.dashboard.back.common.response.PageInfo;
import com.dongjian.dashboard.back.common.response.PageResponse;
@ -18,11 +21,13 @@ import com.dongjian.dashboard.back.service.common.CommonOpt;
import com.dongjian.dashboard.back.util.CommonUtil;
import com.dongjian.dashboard.back.vo.data.DeviceMeasureData;
import com.dongjian.dashboard.back.vo.device.LineData;
import com.github.pagehelper.PageHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
@ -97,23 +102,53 @@ public class DeviceDataMeasureController {
String fileName = URLEncoder.encode("計測データ"+ CommonUtil.generateExcelSuffix(), StandardCharsets.UTF_8).replace("+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
PageInfo<DeviceMeasureData> pageData = deviceDataMeasureService.getDataList(pageSearchParam, CompanyId, UserId, LanguageType);
List<DeviceMeasureData> list = pageData.getList();
// ===== 固定导出分页大小 =====
pageSearchParam.setPageSize(Constants.EXPORT_PAGE_SIZE);
List<ExportDeviceMeasureDataDTO> exportList = list.stream().map(item -> {
ExportDeviceMeasureDataDTO dto = new ExportDeviceMeasureDataDTO();
BeanUtils.copyProperties(item, dto);
dto.setStatus111(commonOpt.status111Mapping(item.getStatus111()));
return dto;
}).toList();
// 构造 Excel header:每列三语组合为字符串 "中文||English||日本語"
// ===== 构建 Excel =====
List<List<String>> head = LanguageDynamicHeaderAdapter.buildHead(ExportDeviceMeasureDataDTO.class, LanguageType);
EasyExcel.write(response.getOutputStream())
.head(head)
.sheet("sheet1")
.doWrite(exportList);
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).head(head).build();
WriteSheet sheet = EasyExcel.writerSheet("sheet1").build();
int pageNum = 1;
try {
while (true) {
pageSearchParam.setPageNum(pageNum);
PageInfo<DeviceMeasureData> pageData = deviceDataMeasureService.getDataList(pageSearchParam, CompanyId, UserId, LanguageType);
List<DeviceMeasureData> list = pageData.getList();
if (CollectionUtils.isEmpty(list)) {
break; // 没数据了,结束
}
for (DeviceMeasureData item : list) {
ExportDeviceMeasureDataDTO dto = new ExportDeviceMeasureDataDTO();
BeanUtils.copyProperties(item, dto);
dto.setStatus111(commonOpt.status111Mapping(item.getStatus111()));
excelWriter.write(List.of(dto), sheet);
// 手动置空,提醒 GC 回收
dto = null;
item = null;
}
// 手动清理分页数据和缓存
list.clear();
PageHelper.clearPage();
if (pageNum >= pageData.getPages()) {
break;
}
pageNum++;
}
} catch (Exception e) {
logger.error("export measure data error", e);
throw e;
} finally {
excelWriter.finish();
PageHelper.clearPage();
}
}
@OperationLog(operation = "getLineData", remark = "")

Loading…
Cancel
Save