Commit 835aa4c1 by 崔彦峰

feat(clues): 添加线索异步导出功能

- 新增 CluesExportDTO 数据传输对象用于线索导出
- 在 CluesRemote 接口中添加 exportAsync 异步导出方法
- 在 CluesController 中实现异步导出接口并注入 ExcelAsyncExportService
- 在 CluesInfoService 中实现 export 方法并添加线索导出转换逻辑
- 在前端控制器中添加异步导出接口支持并配置最大页面大小参数
1 parent 5fa36c37
......@@ -21,6 +21,7 @@ import com.gsc4cloud.gsc.vpc.api.req.web.clues.CluesWebPageReq;
import com.gsc4cloud.gsc.vpc.api.req.web.clues.CluesOperationLogListReq;
import com.gsc4cloud.gsc.vpc.api.req.web.clues.CluesMerchantPageReq;
import com.gsc4cloud.gsc.vpc.service.clues.CluesInfoService;
import com.gsc4cloud.gsc.vpc.service.common.ExcelAsyncExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
......@@ -33,6 +34,9 @@ public class CluesController {
@Autowired
private CluesInfoService cluesInfoService;
@Autowired
private ExcelAsyncExportService excelAsyncExportService;
@PostMapping("/add")
public R<AddCluesResp> addClues(@RequestBody AddCluesReq req) {
AddCluesResp resp = cluesInfoService.addClues(req);
......@@ -82,6 +86,14 @@ public class CluesController {
return R.ok(page);
}
@PostMapping("/exportAsync")
public R<String> exportAsync(@RequestBody PageRequest<CluesWebPageReq> req) {
String createdBy = req == null ? null : req.getUserName();
String fileUUID = excelAsyncExportService.exportAsync("exportClues", createdBy,
() -> cluesInfoService.export(req));
return R.ok(fileUUID);
}
@PostMapping("/merchantPage")
public R<Page<CluesMerchantPageResp>> merchantPage(
@RequestBody PageRequest<CluesMerchantPageReq> req) {
......
......@@ -3,6 +3,7 @@ package com.gsc4cloud.gsc.vpc.service.clues;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gsc4cloud.gsc.vpc.api.dto.web.resdto.clues.CluesExportDTO;
import com.gsc4cloud.gsc.vpc.api.dto.pad.resdto.clues.AddCluesResp;
import com.gsc4cloud.gsc.vpc.api.dto.pad.resdto.clues.CluesDetailResp;
import com.gsc4cloud.gsc.vpc.api.dto.pad.resdto.clues.CluesFollowRecordListResp;
......@@ -38,6 +39,7 @@ import com.gsc4cloud.gsc.vpc.mapper.clues.CluesFollowRecordMapper;
import com.gsc4cloud.gsc.vpc.mapper.clues.CluesInfoMapper;
import com.gsc4cloud.gsc.vpc.mapper.clues.CluesOperationLogMapper;
import com.gsc4cloud.gsc.vpc.utils.EvalNoGenerator;
import com.gsc4cloud.gsc.vpc.utils.excel.EasyExcelUtil;
import com.gsc4cloud.gsc.vpc.api.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
......@@ -51,6 +53,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.stream.Collectors;
@Service
......@@ -312,6 +315,19 @@ public class CluesInfoService extends ServiceImpl<CluesInfoMapper, CluesInfo> {
return result;
}
public String export(PageRequest<CluesWebPageReq> req) {
PageRequest<CluesWebPageReq> pageRequest = req == null ? new PageRequest<>() : req;
CluesWebPageReq queryReq = pageRequest.getT() == null ? new CluesWebPageReq() : pageRequest.getT();
Page<CluesPageResp> page = new Page<>(pageRequest.getCurrentPage(), pageRequest.getPageSize());
Page<CluesPageResp> result = baseMapper.page(page, toWebPageQuery(queryReq));
fillFollowInfo(result.getRecords());
List<CluesExportDTO> exportList = result.getRecords().stream()
.map(this::toCluesExportDTO)
.collect(Collectors.toList());
return EasyExcelUtil.exportExcel(exportList, "线索导出", CluesExportDTO.class);
}
public Page<CluesMerchantPageResp> merchantPage(PageRequest<CluesMerchantPageReq> req) {
PageRequest<CluesMerchantPageReq> pageRequest = req == null ? new PageRequest<>() : req;
CluesMerchantPageReq queryReq = pageRequest.getT() == null
......@@ -399,6 +415,47 @@ public class CluesInfoService extends ServiceImpl<CluesInfoMapper, CluesInfo> {
return resp;
}
private CluesExportDTO toCluesExportDTO(CluesPageResp clues) {
CluesExportDTO exportDTO = new CluesExportDTO();
BeanUtils.copyProperties(clues, exportDTO);
exportDTO.setFollowRecordList(formatFollowRecordList(clues.getFollowRecordList()));
return exportDTO;
}
private String formatFollowRecordList(List<FollowRecordItemResp> followRecordList) {
if (followRecordList == null || followRecordList.isEmpty()) {
return "";
}
return followRecordList.stream()
.map(this::formatFollowRecord)
.collect(Collectors.joining("\n"));
}
private String formatFollowRecord(FollowRecordItemResp followRecord) {
StringJoiner joiner = new StringJoiner(";");
joiner.add("跟进人:" + defaultText(followRecord.getFollowUserName()));
joiner.add("跟进方式:" + getFollowStyleDesc(followRecord.getFollowStyle()));
joiner.add("跟进结果:" + getFollowResultDesc(followRecord.getFollowResult()));
joiner.add("跟进内容:" + defaultText(followRecord.getFollowRemark()));
joiner.add("跟进时间:" + (followRecord.getCreatedDate() == null
? "" : DateUtil.formatDateTime(followRecord.getCreatedDate())));
return joiner.toString();
}
private String getFollowStyleDesc(Integer code) {
FollowStyleEnum followStyle = FollowStyleEnum.getByCode(code);
return followStyle == null ? defaultText(code) : followStyle.getDesc();
}
private String getFollowResultDesc(Integer code) {
FollowResultEnum followResult = FollowResultEnum.getByCode(code);
return followResult == null ? defaultText(code) : followResult.getDesc();
}
private String defaultText(Object value) {
return value == null ? "" : String.valueOf(value);
}
private CluesFollowRecordListResp toFollowRecordListResp(CluesFollowRecord followRecord) {
CluesFollowRecordListResp resp = new CluesFollowRecordListResp();
BeanUtils.copyProperties(followRecord, resp);
......
  • @wangxw1
    commented
    Master

    这是一份针对您提交代码的专业审查报告。本次审查重点关注了异步导出功能的逻辑正确性、内存安全性以及性能瓶颈。

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

    1. 导出功能的内存溢出(OOM)风险与逻辑缺陷

    • 问题描述:在 CluesInfoService.export 方法中,使用了标准的分页查询 baseMapper.page(page, ...) 来获取导出数据。结合提交信息中的“前端...配置最大页面大小参数”,说明目前的导出方案是通过前端传入极大的 pageSize(如 100000)来一次性查出所有数据。
    • 影响分析:这是典型的导出功能反模式。将数万或数十万条记录一次性加载到内存中,并进行 DTO 转换和 fillFollowInfo 组装,极易导致 JVM 堆内存耗尽,触发 OutOfMemoryError (OOM),从而导致服务崩溃(可用性风险)。
    • 优化建议:对于异步导出,严禁一次性加载全量数据。应使用 EasyExcel 的分页写入(多次写入同一个 Sheet)或 MyBatis 的游标(Cursor)/ ResultHandler 进行流式读取。 代码示例(基于 EasyExcel 分页写入的伪代码):

      public String export(PageRequest<CluesWebPageReq> req) {
        // 1. 初始化 EasyExcel 写出器
        // String fileName = ...;
        // ExcelWriter excelWriter = EasyExcel.write(fileName, CluesExportDTO.class).build();
        // WriteSheet writeSheet = EasyExcel.writerSheet("线索导出").build();
      
        int currentPage = 1;
        int batchSize = 1000; // 固定安全批次大小
        PageRequest<CluesWebPageReq> pageRequest = req == null ? new PageRequest<>() : req;
        CluesWebPageReq queryReq = pageRequest.getT() == null ? new CluesWebPageReq() : pageRequest.getT();
      
        while (true) {
            Page<CluesPageResp> page = new Page<>(currentPage, batchSize);
            Page<CluesPageResp> result = baseMapper.page(page, toWebPageQuery(queryReq));
      
            if (result.getRecords().isEmpty()) {
                break;
            }
      
            fillFollowInfo(result.getRecords());
            List<CluesExportDTO> exportList = result.getRecords().stream()
                    .map(this::toCluesExportDTO).collect(Collectors.toList());
      
            // 分批写入 Excel
            // excelWriter.write(exportList, writeSheet);
      
            if (result.getRecords().size() < batchSize) {
                break; // 最后一页
            }
            currentPage++;
        }
        // excelWriter.finish();
        // return fileUUID;
      }
      

    2. 导出场景下的潜在 N+1 查询性能问题

    • 问题描述:export 方法中调用了 fillFollowInfo(result.getRecords())。在常规分页(10条/页)时,该方法如果存在循环查库可能不会明显暴露问题;但在导出场景下(数据量巨大),该方法的执行成本会被无限放大。
    • 影响分析:如果 fillFollowInfo 内部存在 for 循环查询数据库,或者即使用了 IN 语句但传入了超大集合,会导致数据库连接池被迅速占满,CPU 飙升,甚至引发慢 SQL 和数据库阻塞。
    • 优化建议:审查 fillFollowInfo 的内部实现,确保其在处理大批量数据时使用的是高效的批量查询(如分批次 IN 查询),并在内存中基于 Map 进行数据拼装。如果业务允许,建议为导出功能编写专用的连表 SQL,直接在数据库层完成数据聚合,避免在内存中二次组装。

    3. 冗余的空指针检查与框架特性利用不足

    • 问题描述:在 CluesController.exportAsync 和 CluesInfoService.export 中,存在对 req 的判空逻辑(如 req == null ? null : req.getUserName())。
    • 影响分析:Spring MVC 的 @RequestBody 注解默认是 required = true 的。如果前端不传 body,Spring 会直接抛出 HttpMessageNotReadableException(400错误),根本不会进入 Controller 方法。因此,这些判空是无效的冗余代码,降低了代码可读性。
    • 优化建议:移除不必要的 req == null 判断。如果需要校验 req 内部的字段(如 userName 是否为空),应使用 Spring Validation(在参数前加 @Validated,并在 DTO 字段上加 @NotBlank 等注解)来优雅地处理参数校验。

    二、评分明细

    • 功能实现的正确性与健壮性 (25/40分) 理由:业务逻辑基本连通,但依赖前端控制 pageSize 来实现导出全量数据是严重的设计缺陷,在生产环境中面对大数据量时必然失败。
    • 安全性与潜在风险 (22/30分) 理由:无明显 SQL 注入或越权漏洞,但内存溢出(OOM)风险极高,在大并发或大数据量导出时容易被恶意或无意触发,导致服务拒绝服务(DoS)。
    • 最佳实践与可维护性 (16/20分) 理由:代码结构清晰,合理使用了 StringJoiner 和枚举类提取描述,但存在冗余的防御性编程(无效判空),未充分利用框架的参数校验能力。
    • 性能与资源利用 (2/5分) 理由:一次性加载全量数据到内存且可能伴随批量数据的循环处理,对 JVM 内存和数据库资源的利用极不合理。
    • 提交信息质量 (4/5分) 理由:Commit 格式规范,清晰列出了修改项和涉及的层次,但暴露了前端采用“配置最大页面大小参数”这种不合理的妥协方案。

    总分:69分

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