fix(brand-check): 出表整读源文件展开全量行——同品牌不同 ASIN 不再被删
任务 2735 复盘:品牌检测解析阶段按品牌删行后,同一份行集既当执行方查询清单 又当出表数据源,904 行源文件结果只剩 173 行。现解析器加 dedupeRowsByBrand 开关,payload 仍逐品牌一行(执行方按行提品牌,不重烧代理),组装时用真实源 文件整读展开全量行;不符合/未检测品牌的挪 sheet 语义不变。 新增 BrandTaskServiceResultRowsTest 4 例,Abort 夹具换真实 Excel 并钉双 ASIN 保行。
This commit is contained in:
+32
-13
@@ -770,7 +770,10 @@ public class BrandTaskService {
|
||||
File sourceLocalFile = resolveSourceFile(sourceFile);
|
||||
String originalFilename = resolveOriginalFilename(sourceFile, sourceLocalFile);
|
||||
File outputFile = buildNamedOutputFile(outputDir, buildResultFilename(originalFilename));
|
||||
writeBrandWorkbook(outputFile, strategy, cachedFile, aggregate, undetectedBrands);
|
||||
// 出表整读源文件展开全量行:缓存 rows 是按品牌去重的查询集(执行方靠它逐品牌只查一次),
|
||||
// 同品牌不同 ASIN 是不同商品,结果文件删不得。
|
||||
List<Map<String, Object>> outputRows = parseBrandFile(sourceLocalFile, false).rows();
|
||||
writeBrandWorkbook(outputFile, strategy, cachedFile, aggregate, undetectedBrands, outputRows);
|
||||
outputEntries.add(new OutputEntry(sourceLocalFile, originalFilename, outputFile, originalFilename));
|
||||
}
|
||||
if (outputEntries.isEmpty()) {
|
||||
@@ -1033,7 +1036,16 @@ public class BrandTaskService {
|
||||
return blankToDefault(sourceFile.getOriginalFilename(), inputFile.getName());
|
||||
}
|
||||
|
||||
private ParsedBrandFile parseBrandFile(File inputFile) {
|
||||
ParsedBrandFile parseBrandFile(File inputFile) {
|
||||
return parseBrandFile(inputFile, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dedupeRowsByBrand 真=回传执行方的 payload 形态:每品牌只留首行,保证逐品牌只查一次
|
||||
* (执行方从 rows 提品牌清单,放开会按行重烧按次计费的代理);
|
||||
* 假=出表形态:同品牌不同 ASIN 是不同商品,结果文件必须每行都在。
|
||||
*/
|
||||
ParsedBrandFile parseBrandFile(File inputFile, boolean dedupeRowsByBrand) {
|
||||
// 2026-09 全维度审查 C5:改逐行流式解析(ExcelStreamReader → EasyExcel SAX)。
|
||||
// 此前 WorkbookFactory.create 把整表读成 DOM,品牌源文件(几十万行)会整表驻留堆内存。
|
||||
// 解析结果(columns/rows/uniqueBrands/行号)与错误文案不变,行号沿用 sheet 绝对 0 基行号 +1。
|
||||
@@ -1041,7 +1053,7 @@ public class BrandTaskService {
|
||||
log.warn("[brand] 源文件不是 Excel(疑似 CSV/文本),拒绝解析 file={}", inputFile.getName());
|
||||
throw new BusinessException("读取 Excel 失败");
|
||||
}
|
||||
BrandFileSheetContext context = new BrandFileSheetContext();
|
||||
BrandFileSheetContext context = new BrandFileSheetContext(dedupeRowsByBrand);
|
||||
try {
|
||||
ExcelStreamReader.readFirstSheet(inputFile, context);
|
||||
} catch (BusinessException ex) {
|
||||
@@ -1072,15 +1084,20 @@ public class BrandTaskService {
|
||||
}
|
||||
}
|
||||
|
||||
/** 品牌源文件流式解析上下文:表头解析 + 按品牌去重累积结果行(内存不随整表 DOM 放大)。 */
|
||||
/** 品牌源文件流式解析上下文:表头解析 + 结果行累积(内存不随整表 DOM 放大)。 */
|
||||
private final class BrandFileSheetContext implements ExcelStreamReader.SheetRowHandler {
|
||||
|
||||
private final boolean dedupeRowsByBrand;
|
||||
private final List<Map<String, Object>> rows = new ArrayList<>();
|
||||
private final Set<String> uniqueBrands = new LinkedHashSet<>();
|
||||
private List<String> columns;
|
||||
private Map<String, Integer> headerIndexes;
|
||||
private String sheetName = "";
|
||||
|
||||
BrandFileSheetContext(boolean dedupeRowsByBrand) {
|
||||
this.dedupeRowsByBrand = dedupeRowsByBrand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHeader(String sheetName, Integer sheetNo, Map<Integer, String> headerMap) {
|
||||
this.sheetName = sheetName == null ? "" : sheetName;
|
||||
@@ -1136,7 +1153,8 @@ public class BrandTaskService {
|
||||
rows.add(rowData);
|
||||
return;
|
||||
}
|
||||
if (uniqueBrands.add(brand)) {
|
||||
boolean firstSeen = uniqueBrands.add(brand);
|
||||
if (firstSeen || !dedupeRowsByBrand) {
|
||||
rowData.put("__rowIndex", rowIndex + 1);
|
||||
rows.add(rowData);
|
||||
}
|
||||
@@ -1188,11 +1206,12 @@ public class BrandTaskService {
|
||||
.replaceAll("\\s+", " ");
|
||||
}
|
||||
|
||||
private void writeBrandWorkbook(File outputFile,
|
||||
String strategy,
|
||||
BrandParsedFileCacheDto cachedFile,
|
||||
BrandFileAggregateCacheDto resultFile,
|
||||
List<String> undetectedBrands) throws IOException {
|
||||
void writeBrandWorkbook(File outputFile,
|
||||
String strategy,
|
||||
BrandParsedFileCacheDto cachedFile,
|
||||
BrandFileAggregateCacheDto resultFile,
|
||||
List<String> undetectedBrands,
|
||||
List<Map<String, Object>> outputRows) throws IOException {
|
||||
String actualStrategy = normalizeStrategy(strategy);
|
||||
SXSSFWorkbook workbook = new SXSSFWorkbook(200);
|
||||
workbook.setCompressTempFiles(true);
|
||||
@@ -1210,7 +1229,7 @@ public class BrandTaskService {
|
||||
// 避免用户把「没查过」的行误当成「已通过检测」上架
|
||||
Set<String> undetectedBrandSet = normalizeBrandSet(undetectedBrands);
|
||||
List<Map<String, Object>> undetectedRows = new ArrayList<>();
|
||||
List<Map<String, Object>> sourceRows = cachedFile.getRows() == null ? List.of() : cachedFile.getRows();
|
||||
List<Map<String, Object>> sourceRows = outputRows == null ? List.of() : outputRows;
|
||||
int writeRowIndex = 1;
|
||||
for (Map<String, Object> rowData : sourceRows) {
|
||||
String brand = normalizeCellText(Objects.toString(rowData.getOrDefault("品牌", ""), ""));
|
||||
@@ -1713,8 +1732,8 @@ public class BrandTaskService {
|
||||
brandTaskProgressCacheService.markFailed(taskId, message);
|
||||
}
|
||||
|
||||
private record ParsedBrandFile(String sheetName, List<String> columns, List<Map<String, Object>> rows,
|
||||
List<String> uniqueBrands) {
|
||||
record ParsedBrandFile(String sheetName, List<String> columns, List<Map<String, Object>> rows,
|
||||
List<String> uniqueBrands) {
|
||||
}
|
||||
|
||||
private record OutputEntry(File sourceFile, String zipSourceFilename, File resultFile, String zipResultFilename) {
|
||||
|
||||
Reference in New Issue
Block a user