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