Commit 1799a5cd by zhouhaibao

feat(dailyReport): 新增理想品牌门店产值日报表导出功能

- 新增理想品牌门店产值数据Excel下载接口及数据存在性判断接口
- 注入merchantRemote2实现根据理想品牌ID动态查询门店列表
- 新增理想品牌专用Excel导出方法,支持延保/轻改小计及总计行高亮样式
- 实现文件同步发送至大数据端的逻辑
- 添加理想品牌配置项解析及完整的异常处理机制
1 parent b02bbe31
......@@ -291,4 +291,114 @@ public class StaffDailyReportService {
return staffDailyReportRemote.boutiquePermeabilityDailyReportPageExport(pagination);
}
public <T> String exportExcelNoDateFormatLx(List<T> dataList, String fileName, HashMap<String, String> dateMap, InputStream template, int startRow){
long startTime = System.currentTimeMillis();
try {
File savePath = new File(env.getProperty("gop.excel.tem.path"));
String name = fileName + ExcelTypeEnum.XLSX.getValue();
if (!savePath.exists()) {
savePath.mkdir();
}
File file = new File(savePath, name);
List<LinkedHashMap<String, ExcelMergeEntity>> list = new ArrayList();
EasyExcelUtil.convertMergeData(dataList, list, (String)null, -1);
EasyExcelUtil.handler(list);
ExcelWriter excelWriter = ((ExcelWriterBuilder)((ExcelWriterBuilder) EasyExcel.write(file)
.registerWriteHandler(new MergeRowStrategy(list, startRow)))
.registerWriteHandler(new AbstractCellWriteHandler() {
private Set<Integer> hejiRowSet = new HashSet<>();
private int colIndex = 0;
private Set<Integer> hejiFinalRowSet = new HashSet<>();
private int finalColIndex = 0;
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder,
WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head,
Integer relativeRowIndex, Boolean isHead) {
if(StringUtils.isNotBlank(cell.getStringCellValue()) && !hejiRowSet.contains(relativeRowIndex) &&
(cell.getStringCellValue().equals("延保总计") || cell.getStringCellValue().contains("延保小计")
|| cell.getStringCellValue().contains("轻改总计"))
|| cell.getStringCellValue().contains("小计")){
hejiRowSet.add(relativeRowIndex);
colIndex = 0;
}
if(hejiRowSet.contains(relativeRowIndex)) {
Workbook workbook = writeSheetHolder.getParentWriteWorkbookHolder().getWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setWrapText(true);
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 11);// 字体大小
font.setBold(true);
Row row = writeSheetHolder.getSheet().getRow(cell.getRowIndex());
for(int i = colIndex;i <= cell.getColumnIndex();i++) {
if(i == 2){
cellStyle.setAlignment(HorizontalAlignment.LEFT);
cellStyle.setIndention((short)1);//小缩进
font.setFontHeightInPoints((short) 10);
}
cellStyle.setFont(font);
row.getCell(i).setCellStyle(cellStyle);
}
colIndex = cell.getColumnIndex() + 1;
}
if(StringUtils.isNotBlank(cell.getStringCellValue()) && !hejiFinalRowSet.contains(relativeRowIndex) &&
(cell.getStringCellValue().equals("延保总计")
|| cell.getStringCellValue().equals("区域合计")
|| cell.getStringCellValue().contains("轻改总计"))) {
hejiFinalRowSet.add(relativeRowIndex);
finalColIndex = 0;
}
if(hejiFinalRowSet.contains(relativeRowIndex)) {
Workbook workbook = writeSheetHolder.getParentWriteWorkbookHolder().getWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(IndexedColors.GOLD.getIndex());
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setWrapText(true);
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 11);// 字体大小
font.setBold(true);
Row row = writeSheetHolder.getSheet().getRow(cell.getRowIndex());
for(int i = finalColIndex;i <= cell.getColumnIndex();i++) {
if(i == 2){
cellStyle.setAlignment(HorizontalAlignment.LEFT);
cellStyle.setIndention((short)1);//小缩进
font.setFontHeightInPoints((short) 10);
}
cellStyle.setFont(font);
row.getCell(i).setCellStyle(cellStyle);
}
finalColIndex = cell.getColumnIndex() + 1;
}
}
})
.registerConverter(new ExcelMergeEntityToStringConvert())).withTemplate(template).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.FALSE).build();
excelWriter.fill(list, fillConfig, writeSheet);
excelWriter.fill(dateMap, writeSheet);
excelWriter.finish();
return env.getProperty("gop.excel.tem.url") + name;
}finally{
log.info("export {} excel use {}ms",fileName,System.currentTimeMillis() - startTime);
}
}
}
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(仅限 Top 3)

    1. 资源泄露与 POI 样式溢出风险(安全性与健壮性)

      • 问题描述:
        1. template 作为 InputStream 传入 exportExcelNoDateFormatLx 方法后未显式关闭,容易引发文件句柄/内存泄露。
        2. 在 AbstractCellWriteHandler.afterCellDispose 回调中,对匹配命中的每一行单元格均重复调用 workbook.createCellStyle() 与 workbook.createFont()。Excel 针对单个 Workbook 的 CellStyle 上限为 64,000,在大数据量或频繁调用场景下极易触发 IllegalStateException: The maximum number of cell styles was exceeded,导致报表导出崩溃并耗尽堆内存。
        3. 直接调用 cell.getStringCellValue() 未校验单元格类型,若单元格为数字、公式或空类型时,将抛出 IllegalStateException。
      • 优化建议:
        • 在调用的最外层使用 try-with-resources 确保 InputStream 安全关闭。
        • 将 CellStyle 与 Font 提取至外部作为复用对象(或在初次进入时初始化并缓存),禁止在遍历单元格循环中频繁 createCellStyle。
        • 获取单元格值前判断 cell.getCellType(),或统一使用 POI 的 DataFormatter 转为字符串处理。
    2. 异常处理吞没异常与日期空指针风险(功能正确性)

      • 问题描述:
        • 控制层中捕获 ParseException 时仅记录 log.error("时间转换异常"),未打印异常堆栈,未中断流程也未返回前端错误。此时 startDate 和 endDate 为 null,后续 RPC 远程查询将携带空条件执行全量检索,甚至引发下游接口 NPE。
        • DateUtil.parse(request.getEndDate()) 未校验 endDate 是否为空及格式合法性,可能直接导致运行时崩溃。
      • 优化建议:
        • 捕获 ParseException 后,应打印完整堆栈信息 log.error("时间转换异常: ", e),并直接阻断流程返回参数错误响应:return RopResponse.error("日期格式错误");。
        • 对入参 request.getStartDate() 和 getEndDate() 严格进行非空与合法性校验。
    3. 代码冗余度极高且缺乏分层架构设计(最佳实践与可维护性)

      • 问题描述:
        • downloadMerchantDailyReportExcelLx 与 existMerchantDailyReportExcelDataLx 包含近 60 行完全相同的代码(包括配置读取、逗号分割解析、RPC 调用及商户提取校验),属于典型的 Copy-Paste 反模式,违背 DRY 原则。
        • Controller 承担了过多下沉业务逻辑(如读取环境变量、解析配置字符串、调用外部 RPC 获取门店并装配参数、组装文件物理路径推送大数据等),严重违反阿里巴巴规约中“Controller 仅负责参数校验、业务转发及数据渲染”的分层原则。
      • 优化建议:
        • 将根据品牌配置获取门店 ID 列表(getLixiangMerchantIds)的公共逻辑抽取为 Service 层私有方法或组件方法复用。
        • 将业务流转逻辑(报表生成、路径拼接、大数据推送等)整体下沉至 StaffDailyReportService,Controller 仅保留单薄的请求映射和转发。

    二、评分明细

    • 功能实现的正确性与健壮性:30 / 40 分(存在 ParseException 吞没导致下游参数为空、单元格非 String 读取异常、样式数量溢出等隐患)
    • 安全性与潜在风险:24 / 30 分(InputStream 存在资源泄露隐患,大量创建 CellStyle 容易触发 OOM/DoS)
    • 最佳实践与可维护性:12 / 20 分(Controller 层代码冗余严重、缺乏分层抽象、大段重复逻辑)
    • 性能与资源利用:3 / 5 分(循环内无节制新建 POI 样式对象与字体,性能损耗大)
    • 提交信息质量:5 / 5 分(符合 Angular 提交规范,详细说明了改动要点)

    总分:74分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!