task-116: 后台管理新增/导入表单弹窗化 + 分组管理独立菜单与UI优化 + 轻量进度端点

- 后台管理页(admin)所有面板的新增/导入表单改为弹窗操作,原有字段 ID 全部保留、提交逻辑不变;导入删除入口不再触发二次确认拦截
- 分组管理升级为独立菜单(V102 + schema initializer),移除 5 个面板内的管理分组按钮;分组列表改为蓝白主题、增加权限分组横幅
- Python 侧 group-manage 权限守卫(_ensure_backend_menu_access 补充 group-manage)
- 引入 V101(biz_task_file_job 复合索引)+ 新增 TaskProgressLight/TaskFileJob 轻量端点与进度聚合作
- 前端 progress-light / page-separated-loads / dispatch-guard 共享模块及单元测试
This commit is contained in:
2026-09-01 12:54:13 +08:00
parent 652ec1bbc9
commit b148dc57eb
139 changed files with 10540 additions and 8834 deletions
@@ -31,6 +31,8 @@ import org.springframework.web.server.ResponseStatusException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import com.nanri.aiimage.modules.task.model.dto.TaskProgressLightRequest;
import com.nanri.aiimage.modules.task.model.vo.TaskProgressLightBatchVo;
@RestController
@RequiredArgsConstructor
@@ -98,6 +100,14 @@ public class AppearancePatentController {
return ApiResponse.success(service.progressBatch(request.getTaskIds()));
}
@PostMapping("/tasks/progress/light")
@Operation(summary = "批量查询任务轻量进度(前端轮询专用)",
description = "与 progress/batch 同输入,只返回轻量字段(taskId/status/statusCode/fileStatus/fileError/fileReady/updatedAt),"
+ "不查明细/result 行,响应体更小;旧 progress/batch 端点保留不动。")
public ApiResponse<TaskProgressLightBatchVo> progressLight(@Valid @RequestBody TaskProgressLightRequest request) {
return ApiResponse.success(service.progressLight(request.getTaskIds()));
}
@PostMapping("/tasks/{taskId}/activate")
@Operation(summary = "激活任务", description = "前端手动推送 Python 队列成功后调用,将任务从 PENDING 改为 RUNNING,并记录后端内部活跃时间。后续活跃时间由 Python 回传结果接口自动刷新,不需要单独心跳接口。")
public ApiResponse<Void> activate(
@@ -24,6 +24,7 @@ import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentSour
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentSubmitResultRequest;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentDashboardVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryItemVo;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentHistoryAssembler;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedGroupVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedRowVo;
@@ -31,6 +32,7 @@ import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParse
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentTaskBatchVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentTaskDetailVo;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentTaskItemVo;
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentSheetBuilder;
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
import com.nanri.aiimage.modules.task.mapper.FileResultMapper;
@@ -88,6 +90,8 @@ import java.util.UUID;
import java.util.function.Supplier;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.nanri.aiimage.modules.task.model.vo.TaskProgressLightBatchVo;
import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
@Service
@RequiredArgsConstructor
@@ -95,6 +99,10 @@ import java.util.zip.ZipOutputStream;
public class AppearancePatentTaskService {
public static final String MODULE_TYPE = "APPEARANCE_PATENT";
public TaskProgressLightBatchVo progressLight(List<Long> taskIds) {
return taskProgressLightAssembler.assemble(MODULE_TYPE, null, taskIds);
}
private static final String STATUS_PENDING = "PENDING";
private static final String STATUS_RUNNING = "RUNNING";
private static final String STATUS_SUCCESS = "SUCCESS";
@@ -107,18 +115,6 @@ public class AppearancePatentTaskService {
private static final Duration TASK_LOCK_TTL = Duration.ofMinutes(5);
private static final long TASK_LOCK_WAIT_MILLIS = 10000L;
private static final long TASK_LOCK_RETRY_DELAY_MILLIS = 200L;
private static final List<String> RESULT_HEADERS = List.of(
"id",
"asin",
"国家",
"卖家名称",
"品牌",
"价格",
"标题维度(商标)",
"外观维度(外观设计专利)",
"结论",
"状态"
);
private final LocalFileStorageService localFileStorageService;
private final OssStorageService ossStorageService;
@@ -138,6 +134,7 @@ public class AppearancePatentTaskService {
private final DistributedJobLockService distributedJobLockService;
private final TaskDistributedLockService taskDistributedLockService;
private final InstanceMetadata instanceMetadata;
private final TaskProgressLightAssembler taskProgressLightAssembler;
public AppearancePatentParseVo parseAndCreateTask(AppearancePatentParseRequest request) {
long startedAt = System.nanoTime();
@@ -2199,44 +2196,8 @@ public class AppearancePatentTaskService {
List<AppearancePatentParsedRowVo> receivedRows,
Map<String, AppearancePatentResultRowDto> resultMap) {
try (SXSSFWorkbook workbook = new SXSSFWorkbook(200); FileOutputStream fos = new FileOutputStream(xlsx)) {
Sheet sheet = workbook.createSheet("外观专利检测结果");
CellStyle headerStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
headerStyle.setFont(font);
List<String> resultHeaders = new ArrayList<>(RESULT_HEADERS);
resultHeaders.add(6, "标题");
resultHeaders.add(7, "图片链接");
resultHeaders.add(8, "sku");
Row header = sheet.createRow(0);
for (int i = 0; i < resultHeaders.size(); i++) {
Cell cell = header.createCell(i);
cell.setCellValue(resultHeaders.get(i));
cell.setCellStyle(headerStyle);
}
int rowIndex = 1;
List<AppearancePatentParsedRowVo> rowsToWrite = receivedRows == null ? List.of() : receivedRows;
for (AppearancePatentParsedRowVo parsedRow : rowsToWrite) {
AppearancePatentResultRowDto resultRow = findResultRow(parsedRow, resultMap);
Row row = sheet.createRow(rowIndex++);
int col = 0;
row.createCell(col++).setCellValue(firstNonBlank(parsedRow.getDisplayId(), parsedRow.getSourceId()));
row.createCell(col++).setCellValue(firstNonBlank(parsedRow.getAsin(), ""));
row.createCell(col++).setCellValue(firstNonBlank(parsedRow.getCountry(), ""));
row.createCell(col++).setCellValue(readValueByHeader(parsedRow, "卖家名称", "卖家名", "卖家", "店铺名称", "店铺名", "seller name", "seller_name", "seller-name", "sellername", "store name", "shop name"));
row.createCell(col++).setCellValue(resolveBrand(resultRow, parsedRow));
row.createCell(col++).setCellValue(resolvePrice(resultRow));
row.createCell(col++).setCellValue(resultRow == null ? firstNonBlank(parsedRow.getTitle(), "") : firstNonBlank(resultRow.getTitle(), parsedRow.getTitle()));
row.createCell(col++).setCellValue(resultRow == null ? firstNonBlank(parsedRow.getUrl(), "") : firstNonBlank(resultRow.getUrl(), parsedRow.getUrl()));
row.createCell(col++).setCellValue(resultRow == null ? firstNonBlank(parsedRow.getSku(), "") : firstNonBlank(resultRow.getSku(), parsedRow.getSku()));
row.createCell(col++).setCellValue(resultRow == null ? "" : userFacingLlmCellValue(resultRow, resultRow.getTitleRisk()));
row.createCell(col++).setCellValue(resultRow == null ? "" : userFacingLlmCellValue(resultRow, resultRow.getAppearanceRisk()));
row.createCell(col++).setCellValue(resultRow == null ? "" : userFacingConclusion(resultRow));
row.createCell(col).setCellValue(resultRow == null ? "" : userFacingStatus(resultRow));
}
writeReasonSheet(workbook, headerStyle, rowsToWrite, resultMap);
AppearancePatentSheetBuilder.buildResultSheet(workbook, receivedRows, resultMap,
parsedRow -> findResultRow(parsedRow, resultMap));
workbook.write(fos);
workbook.dispose();
} catch (Exception ex) {
@@ -2244,36 +2205,6 @@ public class AppearancePatentTaskService {
}
}
private void writeReasonSheet(SXSSFWorkbook workbook,
CellStyle headerStyle,
List<AppearancePatentParsedRowVo> rowsToWrite,
Map<String, AppearancePatentResultRowDto> resultMap) {
Sheet sheet = workbook.createSheet("原因");
Row header = sheet.createRow(0);
List<String> headers = List.of("ASIN", "外观原因", "专利原因", "标题原因");
for (int i = 0; i < headers.size(); i++) {
Cell cell = header.createCell(i);
cell.setCellValue(headers.get(i));
cell.setCellStyle(headerStyle);
}
Set<String> writtenAsins = new LinkedHashSet<>();
int rowIndex = 1;
for (AppearancePatentParsedRowVo parsedRow : rowsToWrite == null ? List.<AppearancePatentParsedRowVo>of() : rowsToWrite) {
String asin = normalize(parsedRow.getAsin()).toUpperCase(Locale.ROOT);
if (asin.isBlank() || !writtenAsins.add(asin)) {
continue;
}
AppearancePatentResultRowDto resultRow = findResultRow(parsedRow, resultMap);
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(asin);
row.createCell(1).setCellValue(resultRow == null ? "" : firstNonBlank(resultRow.getAppearanceReason(), ""));
row.createCell(2).setCellValue(resultRow == null ? "" : firstNonBlank(resultRow.getPatentReason(), ""));
row.createCell(3).setCellValue(resultRow == null ? "" : firstNonBlank(resultRow.getTitleReason(), ""));
rowIndex++;
}
}
private AppearancePatentResultRowDto findResultRowByAsin(String asin, Map<String, AppearancePatentResultRowDto> resultMap) {
String normalizedAsin = normalize(asin).toUpperCase(Locale.ROOT);
if (normalizedAsin.isBlank() || resultMap == null || resultMap.isEmpty()) {
@@ -2496,7 +2427,7 @@ public class AppearancePatentTaskService {
private boolean hasUsableLlmField(String value) {
String normalized = normalize(value);
return !normalized.isBlank() && !isTechnicalLlmFailure(normalized);
return !normalized.isBlank() && !AppearancePatentSheetBuilder.isTechnicalLlmFailure(normalized);
}
private Map<String, Integer> buildHeaderMap(Row header, DataFormatter formatter) {
@@ -2641,119 +2572,33 @@ public class AppearancePatentTaskService {
return vo;
}
private AppearancePatentHistoryAssembler historyAssembler() {
return new AppearancePatentHistoryAssembler(
taskProgressSnapshotService,
this::calculateDisplayProgressPercent,
this::extractSnapshotDisplayPercent);
}
private AppearancePatentHistoryItemVo toHistoryItem(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
AppearancePatentHistoryItemVo vo = new AppearancePatentHistoryItemVo();
vo.setResultId(row.getId());
vo.setTaskId(row.getTaskId());
vo.setSourceFilename(row.getSourceFilename());
vo.setResultFilename(row.getResultFilename());
vo.setDownloadUrl(null);
attachFileJobState(vo, row, job);
vo.setTaskStatus(task == null ? null : task.getStatus());
vo.setSuccess(row.getResultFileUrl() != null && !row.getResultFileUrl().isBlank()
|| row.getSuccess() != null && row.getSuccess() == 1);
vo.setError(row.getErrorMessage());
vo.setRowCount(row.getRowCount());
vo.setCreatedAt(fmt(row.getCreatedAt()));
// 任务开始时间复用 biz_file_task.created_at;缺 task 时回退到 result.createdAt 兜底
vo.setStartedAt(fmt(task == null ? row.getCreatedAt() : task.getCreatedAt()));
vo.setFinishedAt(fmt(task == null ? null : task.getFinishedAt()));
return vo;
return historyAssembler().toHistoryItem(row, task, job);
}
private int historyPriority(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
String taskStatus = task == null ? null : task.getStatus();
if (STATUS_PENDING.equals(taskStatus) || STATUS_RUNNING.equals(taskStatus) || isHistoryFileBuilding(row, taskStatus, job)) {
return 0;
}
return 1;
return historyAssembler().historyPriority(row, task, job);
}
private LocalDateTime historyActivityTime(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
LocalDateTime latest = latestTime(
task == null ? null : task.getUpdatedAt(),
job == null ? null : job.getUpdatedAt(),
task == null ? null : task.getFinishedAt(),
row == null ? null : row.getCreatedAt(),
task == null ? null : task.getCreatedAt());
return latest == null && row != null ? row.getCreatedAt() : latest;
return historyAssembler().historyActivityTime(row, task, job);
}
private boolean isHistoryFileBuilding(FileResultEntity row, String taskStatus, TaskFileJobEntity job) {
if (!STATUS_SUCCESS.equals(taskStatus)) {
return false;
}
boolean fileReady = row != null && row.getResultFileUrl() != null && !row.getResultFileUrl().isBlank();
if (fileReady) {
return false;
}
String fileStatus = job == null ? null : job.getStatus();
return !STATUS_SUCCESS.equals(fileStatus) && !STATUS_FAILED.equals(fileStatus);
}
private void attachFileJobState(AppearancePatentHistoryItemVo vo, FileResultEntity row, TaskFileJobEntity job) {
vo.setFileReady(row.getResultFileUrl() != null && !row.getResultFileUrl().isBlank());
if (job == null) {
vo.setFileStatus(Boolean.TRUE.equals(vo.getFileReady()) ? "SUCCESS" : null);
attachFileProgress(vo, row, job);
return;
}
vo.setFileJobId(job.getId());
vo.setFileStatus(job.getStatus());
vo.setFileError(STATUS_FAILED.equals(job.getStatus()) ? firstNonBlank(job.getErrorMessage(), null) : null);
attachFileProgress(vo, row, job);
}
private void attachFileProgress(AppearancePatentHistoryItemVo vo, FileResultEntity row, TaskFileJobEntity job) {
if (row == null || row.getTaskId() == null) {
return;
}
if (Boolean.TRUE.equals(vo.getFileReady())) {
vo.setFileProgressCurrent(1);
vo.setFileProgressTotal(1);
vo.setFileProgressPercent(100);
vo.setFileProgressMessage("结果文件已生成");
return;
}
TaskProgressSnapshotEntity snapshot = taskProgressSnapshotService.find(row.getTaskId(), MODULE_TYPE);
if (snapshot == null) {
return;
}
int total = snapshot.getTotalCount() == null ? 0 : snapshot.getTotalCount();
int current = snapshot.getSuccessCount() == null ? 0 : snapshot.getSuccessCount();
if (total <= 0) {
return;
}
LocalDateTime baseTime = snapshot.getUpdatedAt() != null ? snapshot.getUpdatedAt() : (job == null ? null : job.getUpdatedAt());
int percent = calculateDisplayProgressPercent(current, total, job, baseTime);
percent = Math.max(percent, extractSnapshotDisplayPercent(snapshot));
percent = Boolean.TRUE.equals(vo.getFileReady()) ? 100 : Math.min(99, percent);
vo.setFileProgressCurrent(current);
vo.setFileProgressTotal(total);
vo.setFileProgressPercent(percent);
vo.setFileProgressMessage(snapshot.getMessage());
return historyAssembler().isHistoryFileBuilding(row, taskStatus, job);
}
private String fmt(LocalDateTime t) {
return t == null ? null : t.toString();
}
private LocalDateTime latestTime(LocalDateTime... values) {
LocalDateTime latest = null;
if (values == null) {
return null;
}
for (LocalDateTime value : values) {
if (value == null) {
continue;
}
if (latest == null || value.isAfter(latest)) {
latest = value;
}
}
return latest;
}
private String firstNonBlank(String preferred, String fallback) {
return preferred == null || preferred.isBlank() ? fallback : preferred.trim();
}
@@ -3004,43 +2849,6 @@ public class AppearancePatentTaskService {
return normalize(id) + "::" + normalize(asin).toUpperCase(Locale.ROOT) + "::" + normalize(country);
}
private String readValueByHeader(AppearancePatentParsedRowVo row, String... candidates) {
if (row == null || row.getValues() == null || row.getValues().isEmpty() || candidates == null) {
return "";
}
for (Map.Entry<String, String> entry : row.getValues().entrySet()) {
String header = normalize(entry.getKey()).toLowerCase(Locale.ROOT);
for (String candidate : candidates) {
String expected = normalize(candidate).toLowerCase(Locale.ROOT);
if (!expected.isBlank() && header.contains(expected)) {
return entry.getValue() == null ? "" : entry.getValue();
}
}
}
return "";
}
static String resolveBrand(AppearancePatentResultRowDto resultRow, AppearancePatentParsedRowVo parsedRow) {
String pythonBrand = resultRow == null ? "" : resultRow.getBrand();
if (pythonBrand != null && !pythonBrand.isBlank()) {
return pythonBrand.trim();
}
if (parsedRow == null || parsedRow.getValues() == null) {
return "";
}
for (Map.Entry<String, String> entry : parsedRow.getValues().entrySet()) {
String header = entry.getKey() == null ? "" : entry.getKey().trim().toLowerCase(Locale.ROOT);
if (header.contains("品牌") || header.contains("brand")) {
return entry.getValue() == null ? "" : entry.getValue();
}
}
return "";
}
static String resolvePrice(AppearancePatentResultRowDto resultRow) {
return resultRow == null || resultRow.getPrice() == null ? "" : resultRow.getPrice().trim();
}
static String resolveTaskExecutionStatus(boolean waitingForAssemble, boolean executionFailed) {
if (waitingForAssemble) {
return STATUS_RUNNING;
@@ -3048,57 +2856,6 @@ public class AppearancePatentTaskService {
return executionFailed ? STATUS_FAILED : STATUS_SUCCESS;
}
private String userFacingLlmCellValue(AppearancePatentResultRowDto row, String value) {
String normalizedValue = normalize(value);
if (!normalizedValue.isBlank() && !isTechnicalLlmFailure(normalizedValue)) {
return value;
}
// llm 技术性失败:有错误信息则放入错误信息,没有则留空
if (row != null && isTechnicalLlmFailure(row.getError())) {
return firstNonBlank(row.getError(), "");
}
return firstNonBlank(value, "");
}
private String userFacingConclusion(AppearancePatentResultRowDto row) {
if (row == null) {
return "";
}
String conclusion = normalize(row.getConclusion());
if (!conclusion.isBlank() && !isTechnicalLlmFailure(conclusion)) {
return row.getConclusion();
}
// llm 技术性失败:有错误信息则放入错误信息,没有则留空
if (isTechnicalLlmFailure(row.getError())) {
return firstNonBlank(row.getError(), "");
}
return firstNonBlank(row.getConclusion(), "");
}
private String userFacingStatus(AppearancePatentResultRowDto row) {
if (row == null) {
return "";
}
String conclusion = userFacingConclusion(row);
return resolveResultStatus(conclusion);
}
static String resolveResultStatus(String conclusion) {
String normalized = conclusion == null ? "" : conclusion.trim();
return normalized.isBlank() ? "\u5931\u8d25" : "\u6210\u529f";
}
private boolean isTechnicalLlmFailure(String value) {
String normalized = normalize(value).toLowerCase(Locale.ROOT);
return normalized.contains("coze") || normalized.contains("llm")
|| normalized.contains("结果不完整")
|| normalized.contains("工作流节点执行超限")
|| normalized.contains("调用超时")
|| normalized.contains("timeout");
}
private String safeFileStem(String filename) {
String name = filename == null || filename.isBlank() ? "appearance-patent" : filename;
int idx = name.lastIndexOf('.');
@@ -0,0 +1,225 @@
package com.nanri.aiimage.modules.appearancepatent.service.support;
import com.nanri.aiimage.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
/**
* 任务 93:AppearancePatentExcelParser 行解析器。
* POI 读 Excel 行 → 中间行对象(原始单元格值)。语义与 AppearancePatentTaskService.parseWorkbook
* 中对应段落一致:cell 归一化(BOM/全角空格/trim/连续空白折叠)、表头别名匹配(contains + 精确别名)、
* 空行跳过、必填表头缺失抛错。注意:appearancepatent 无 2000 字段截断、无错误值转空(与 similarasin 不同)。
*/
@Slf4j
public class AppearancePatentExcelParser {
public ParsedSheet parse(File input) {
if (input == null) {
throw new IllegalArgumentException("input must not be null");
}
try (FileInputStream fis = new FileInputStream(input);
Workbook workbook = WorkbookFactory.create(fis)) {
return parseWorkbook(workbook);
} catch (BusinessException ex) {
throw ex;
} catch (Exception ex) {
log.warn("[appearance-patent] parse failed file={} err={}", input.getName(), ex.getMessage());
throw new BusinessException("解析 Excel 失败");
}
}
public ParsedSheet parse(InputStream input) {
if (input == null) {
throw new IllegalArgumentException("input must not be null");
}
try (Workbook workbook = WorkbookFactory.create(input)) {
return parseWorkbook(workbook);
} catch (BusinessException ex) {
throw ex;
} catch (Exception ex) {
log.warn("[appearance-patent] parse failed input err={}", ex.getMessage());
throw new BusinessException("解析 Excel 失败");
}
}
private ParsedSheet parseWorkbook(Workbook workbook) {
DataFormatter formatter = new DataFormatter();
Sheet sheet = workbook.getSheetAt(0);
Row header = sheet.getRow(0);
if (header == null) {
throw new BusinessException("Excel 表头为空");
}
Map<String, Integer> headerMap = buildHeaderMap(header, formatter);
List<String> headers = readHeaders(header, formatter);
int idCol = findRequiredHeader(headerMap, "id");
int asinCol = findRequiredHeader(headerMap, "asin");
int countryCol = findRequiredHeader(headerMap, "国家", "country");
int priceCol = findOptionalHeaderExact(headerMap, "价格", "price");
int skuCol = findOptionalHeaderExact(headerMap,
"sku", "seller sku", "seller_sku", "merchant sku", "merchant_sku", "商品sku", "商品 sku", "库存sku");
int urlCol = findOptionalHeaderExact(headerMap,
"url", "rul", "link", "image", "img", "pic", "picture",
"链接", "商品链接", "图片", "商品图片", "主图", "商品主图", "图片链接", "主图链接");
int titleCol = findOptionalHeaderExact(headerMap,
"标题", "title", "listing title", "product title", "商品标题", "商品名称", "产品名称");
List<AppearanceExcelRow> rows = new ArrayList<>();
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
if (row == null) {
continue;
}
String id = cell(row, idCol, formatter);
String asin = cell(row, asinCol, formatter).toUpperCase(Locale.ROOT);
String country = cell(row, countryCol, formatter);
if (id.isBlank() && asin.isBlank() && country.isBlank()) {
continue;
}
rows.add(new AppearanceExcelRow(
i + 1,
id,
asin,
country,
priceCol >= 0 ? cell(row, priceCol, formatter) : "",
skuCol >= 0 ? cell(row, skuCol, formatter) : "",
urlCol >= 0 ? cell(row, urlCol, formatter) : "",
titleCol >= 0 ? cell(row, titleCol, formatter) : "",
readRowValues(row, headers, formatter)));
}
return new ParsedSheet(headers, rows);
}
private Map<String, Integer> buildHeaderMap(Row header, DataFormatter formatter) {
Map<String, Integer> map = new LinkedHashMap<>();
for (int i = 0; i < header.getLastCellNum(); i++) {
String val = normalize(formatter.formatCellValue(header.getCell(i)));
if (!val.isBlank()) {
map.putIfAbsent(val.toLowerCase(Locale.ROOT), i);
}
}
return map;
}
private List<String> readHeaders(Row header, DataFormatter formatter) {
List<String> headers = new ArrayList<>();
for (int i = 0; i < header.getLastCellNum(); i++) {
String val = normalize(formatter.formatCellValue(header.getCell(i)));
headers.add(val.isBlank() ? "列" + (i + 1) : val);
}
return headers;
}
private Map<String, String> readRowValues(Row row, List<String> headers, DataFormatter formatter) {
Map<String, String> values = new LinkedHashMap<>();
for (int i = 0; i < headers.size(); i++) {
values.put(headers.get(i), cell(row, i, formatter));
}
return values;
}
private int findRequiredHeader(Map<String, Integer> map, String... names) {
int idx = findOptionalHeader(map, names);
if (idx < 0) {
throw new BusinessException("缺少必要表头: " + String.join("/", names));
}
return idx;
}
private int findOptionalHeader(Map<String, Integer> map, String... names) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
for (String name : names) {
if (entry.getKey().contains(name.toLowerCase(Locale.ROOT))) {
return entry.getValue();
}
}
}
return -1;
}
private int findOptionalHeaderExact(Map<String, Integer> map, String... names) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String normalizedHeader = normalizeHeaderAlias(entry.getKey());
for (String name : names) {
if (normalizedHeader.equals(normalizeHeaderAlias(name))) {
return entry.getValue();
}
}
}
return -1;
}
private String normalizeHeaderAlias(String value) {
String normalized = normalize(value).toLowerCase(Locale.ROOT);
return normalized.replaceAll("[\\s_\\-()()\\[\\]{}::/\\\\]+", "");
}
private String cell(Row row, int col, DataFormatter formatter) {
return col < 0 ? "" : normalize(formatter.formatCellValue(row.getCell(col)));
}
private String normalize(String val) {
return val == null ? "" : val.replace(String.valueOf((char) 0xFEFF), "").replace((char) 0x3000, ' ').trim().replaceAll("\\s+", " ");
}
public record ParsedSheet(List<String> headers, List<AppearanceExcelRow> rows) {
public ParsedSheet {
headers = headers == null ? List.of() : new ArrayList<>(headers);
rows = rows == null ? List.of() : new ArrayList<>(rows);
}
@Override
public List<String> headers() {
return java.util.Collections.unmodifiableList(headers);
}
@Override
public List<AppearanceExcelRow> rows() {
return java.util.Collections.unmodifiableList(rows);
}
}
public record AppearanceExcelRow(int rowIndex, String id, String asin, String country,
String price, String sku, String url, String title,
Map<String, String> values) {
public AppearanceExcelRow {
values = values == null ? Map.of() : new LinkedHashMap<>(values);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof AppearanceExcelRow other)) {
return false;
}
return rowIndex == other.rowIndex
&& Objects.equals(id, other.id)
&& Objects.equals(asin, other.asin)
&& Objects.equals(country, other.country)
&& Objects.equals(price, other.price)
&& Objects.equals(sku, other.sku)
&& Objects.equals(url, other.url)
&& Objects.equals(title, other.title)
&& values.equals(other.values);
}
@Override
public int hashCode() {
return Objects.hash(rowIndex, id, asin, country, price, sku, url, title, values);
}
}
}
@@ -0,0 +1,161 @@
package com.nanri.aiimage.modules.appearancepatent.service.support;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryItemVo;
import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentTaskService;
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskProgressSnapshotEntity;
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
import java.time.LocalDateTime;
import java.util.function.Function;
/**
* 任务 97:AppearancePatentHistoryAssembler 历史查询组装器。
* 历史列表 VO 拼装(toHistoryItem + 排序辅助 historyPriority/historyActivityTime +
* 文件进度链)从 AppearancePatentTaskService 原样搬移;只读不落库;输出与现状逐字段一致。
* 进度百分比计算(calculateDisplayProgressPercent/extractSnapshotDisplayPercent)仍在
* 服务侧(与 saveFileBuildProgress 实时路径共享),以函数注入。
*/
public class AppearancePatentHistoryAssembler {
private static final String STATUS_PENDING = "PENDING";
private static final String STATUS_RUNNING = "RUNNING";
private static final String STATUS_SUCCESS = "SUCCESS";
private static final String STATUS_FAILED = "FAILED";
@FunctionalInterface
public interface ProgressPercentCalculator {
int calculate(int current, int total, TaskFileJobEntity job, LocalDateTime baseTime);
}
private final TaskProgressSnapshotService taskProgressSnapshotService;
private final ProgressPercentCalculator calculateDisplayProgressPercent;
private final Function<TaskProgressSnapshotEntity, Integer> extractSnapshotDisplayPercent;
public AppearancePatentHistoryAssembler(TaskProgressSnapshotService taskProgressSnapshotService,
ProgressPercentCalculator calculateDisplayProgressPercent,
Function<TaskProgressSnapshotEntity, Integer> extractSnapshotDisplayPercent) {
this.taskProgressSnapshotService = taskProgressSnapshotService;
this.calculateDisplayProgressPercent = calculateDisplayProgressPercent;
this.extractSnapshotDisplayPercent = extractSnapshotDisplayPercent;
}
public AppearancePatentHistoryItemVo toHistoryItem(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
AppearancePatentHistoryItemVo vo = new AppearancePatentHistoryItemVo();
vo.setResultId(row.getId());
vo.setTaskId(row.getTaskId());
vo.setSourceFilename(row.getSourceFilename());
vo.setResultFilename(row.getResultFilename());
vo.setDownloadUrl(null);
attachFileJobState(vo, row, job);
vo.setTaskStatus(task == null ? null : task.getStatus());
vo.setSuccess(row.getResultFileUrl() != null && !row.getResultFileUrl().isBlank()
|| row.getSuccess() != null && row.getSuccess() == 1);
vo.setError(row.getErrorMessage());
vo.setRowCount(row.getRowCount());
vo.setCreatedAt(fmt(row.getCreatedAt()));
// 任务开始时间复用 biz_file_task.created_at;缺 task 时回退到 result.createdAt 兜底
vo.setStartedAt(fmt(task == null ? row.getCreatedAt() : task.getCreatedAt()));
vo.setFinishedAt(fmt(task == null ? null : task.getFinishedAt()));
return vo;
}
public int historyPriority(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
String taskStatus = task == null ? null : task.getStatus();
if (STATUS_PENDING.equals(taskStatus) || STATUS_RUNNING.equals(taskStatus) || isHistoryFileBuilding(row, taskStatus, job)) {
return 0;
}
return 1;
}
public LocalDateTime historyActivityTime(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
LocalDateTime latest = latestTime(
task == null ? null : task.getUpdatedAt(),
job == null ? null : job.getUpdatedAt(),
task == null ? null : task.getFinishedAt(),
row == null ? null : row.getCreatedAt(),
task == null ? null : task.getCreatedAt());
return latest == null && row != null ? row.getCreatedAt() : latest;
}
public boolean isHistoryFileBuilding(FileResultEntity row, String taskStatus, TaskFileJobEntity job) {
if (!STATUS_SUCCESS.equals(taskStatus)) {
return false;
}
boolean fileReady = row != null && row.getResultFileUrl() != null && !row.getResultFileUrl().isBlank();
if (fileReady) {
return false;
}
String fileStatus = job == null ? null : job.getStatus();
return !STATUS_SUCCESS.equals(fileStatus) && !STATUS_FAILED.equals(fileStatus);
}
private void attachFileJobState(AppearancePatentHistoryItemVo vo, FileResultEntity row, TaskFileJobEntity job) {
vo.setFileReady(row.getResultFileUrl() != null && !row.getResultFileUrl().isBlank());
if (job == null) {
vo.setFileStatus(Boolean.TRUE.equals(vo.getFileReady()) ? "SUCCESS" : null);
attachFileProgress(vo, row, job);
return;
}
vo.setFileJobId(job.getId());
vo.setFileStatus(job.getStatus());
vo.setFileError(STATUS_FAILED.equals(job.getStatus()) ? firstNonBlank(job.getErrorMessage(), null) : null);
attachFileProgress(vo, row, job);
}
private void attachFileProgress(AppearancePatentHistoryItemVo vo, FileResultEntity row, TaskFileJobEntity job) {
if (row == null || row.getTaskId() == null) {
return;
}
if (Boolean.TRUE.equals(vo.getFileReady())) {
vo.setFileProgressCurrent(1);
vo.setFileProgressTotal(1);
vo.setFileProgressPercent(100);
vo.setFileProgressMessage("结果文件已生成");
return;
}
TaskProgressSnapshotEntity snapshot = taskProgressSnapshotService.find(row.getTaskId(), AppearancePatentTaskService.MODULE_TYPE);
if (snapshot == null) {
return;
}
int total = snapshot.getTotalCount() == null ? 0 : snapshot.getTotalCount();
int current = snapshot.getSuccessCount() == null ? 0 : snapshot.getSuccessCount();
if (total <= 0) {
return;
}
LocalDateTime baseTime = snapshot.getUpdatedAt() != null ? snapshot.getUpdatedAt() : (job == null ? null : job.getUpdatedAt());
int percent = calculateDisplayProgressPercent.calculate(current, total, job, baseTime);
percent = Math.max(percent, extractSnapshotDisplayPercent.apply(snapshot));
percent = Boolean.TRUE.equals(vo.getFileReady()) ? 100 : Math.min(99, percent);
vo.setFileProgressCurrent(current);
vo.setFileProgressTotal(total);
vo.setFileProgressPercent(percent);
vo.setFileProgressMessage(snapshot.getMessage());
}
private String fmt(LocalDateTime t) {
return t == null ? null : t.toString();
}
private LocalDateTime latestTime(LocalDateTime... values) {
LocalDateTime latest = null;
if (values == null) {
return null;
}
for (LocalDateTime value : values) {
if (value == null) {
continue;
}
if (latest == null || value.isAfter(latest)) {
latest = value;
}
}
return latest;
}
private String firstNonBlank(String preferred, String fallback) {
return preferred == null || preferred.isBlank() ? fallback : preferred.trim();
}
}
@@ -0,0 +1,31 @@
package com.nanri.aiimage.modules.appearancepatent.service.support;
/**
* 任务 93:AppearancePatentRowNormalizer 字段归一化器。
* 规则与 AppearancePatentTaskService.normalize / firstNonBlank / baseId / normalizeDisplayId
* 现状逐字节一致(BOM 剥离、全角空格转半角、trim、连续空白折叠为单空格;
* firstNonBlank 取首非空并 trim;baseId 取下划线前块基 id;displayId 仅 trim)。纯函数无状态。
*/
public final class AppearancePatentRowNormalizer {
private AppearancePatentRowNormalizer() {
}
public static String normalize(String val) {
return val == null ? "" : val.replace(String.valueOf((char) 0xFEFF), "").replace((char) 0x3000, ' ').trim().replaceAll("\\s+", " ");
}
public static String firstNonBlank(String preferred, String fallback) {
return preferred == null || preferred.isBlank() ? fallback : preferred.trim();
}
public static String baseId(String id) {
String s = normalize(id);
int idx = s.indexOf('_');
return idx > 0 ? s.substring(0, idx) : s;
}
public static String normalizeDisplayId(String id) {
return id == null ? "" : id.trim();
}
}
@@ -0,0 +1,265 @@
package com.nanri.aiimage.modules.appearancepatent.service.support;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedRowVo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
/**
* 任务 94:AppearancePatentSheetBuilder Sheet 构造器。
* 结果 Workbook/Sheet 构造辅助(表头、列顺序、样式、行值派生)。
* 与现状 writeResultWorkbook / writeReasonSheet 语义一致:主 sheet 名"外观专利检测结果"、
* 13 列表头(RESULT_HEADERS 10 列 + 第 6/7/8 位插入标题/图片链接/sku)、加粗表头、
* 数据行从第 1 行;"原因"sheet 4 列按 ASIN 去重;LLM 技术性失败展示错误信息。
* 不落库、无 IO 依赖;rowKey/行值解析/LLM 失败判定均为静态纯函数。
*/
public final class AppearancePatentSheetBuilder {
public static final List<String> RESULT_HEADERS = List.of(
"id",
"asin",
"国家",
"卖家名称",
"品牌",
"价格",
"标题维度(商标)",
"外观维度(外观设计专利)",
"结论",
"状态"
);
private AppearancePatentSheetBuilder() {
}
public static void buildResultSheet(Workbook workbook, List<AppearancePatentParsedRowVo> rows,
Map<String, AppearancePatentResultRowDto> resultMap) {
buildResultSheet(workbook, rows, resultMap, null);
}
public static void buildResultSheet(Workbook workbook, List<AppearancePatentParsedRowVo> rows,
Map<String, AppearancePatentResultRowDto> resultMap,
java.util.function.Function<AppearancePatentParsedRowVo, AppearancePatentResultRowDto> findResultRowFn) {
java.util.function.Function<AppearancePatentParsedRowVo, AppearancePatentResultRowDto> finder =
findResultRowFn == null ? row -> findResultRow(row, resultMap) : findResultRowFn;
Sheet sheet = workbook.createSheet("外观专利检测结果");
CellStyle headerStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
headerStyle.setFont(font);
List<String> resultHeaders = new ArrayList<>(RESULT_HEADERS);
resultHeaders.add(6, "标题");
resultHeaders.add(7, "图片链接");
resultHeaders.add(8, "sku");
Row header = sheet.createRow(0);
for (int i = 0; i < resultHeaders.size(); i++) {
Cell cell = header.createCell(i);
cell.setCellValue(resultHeaders.get(i));
cell.setCellStyle(headerStyle);
}
int rowIndex = 1;
List<AppearancePatentParsedRowVo> rowsToWrite = rows == null ? List.of() : rows;
for (AppearancePatentParsedRowVo parsedRow : rowsToWrite) {
if (parsedRow == null) {
continue;
}
AppearancePatentResultRowDto resultRow = finder.apply(parsedRow);
Row row = sheet.createRow(rowIndex++);
int col = 0;
row.createCell(col++).setCellValue(firstNonBlank(parsedRow.getDisplayId(), parsedRow.getSourceId()));
row.createCell(col++).setCellValue(firstNonBlank(parsedRow.getAsin(), ""));
row.createCell(col++).setCellValue(firstNonBlank(parsedRow.getCountry(), ""));
row.createCell(col++).setCellValue(readValueByHeader(parsedRow, "卖家名称", "卖家名", "卖家", "店铺名称", "店铺名", "seller name", "seller_name", "seller-name", "sellername", "store name", "shop name"));
row.createCell(col++).setCellValue(resolveBrand(resultRow, parsedRow));
row.createCell(col++).setCellValue(resolvePrice(resultRow));
row.createCell(col++).setCellValue(resultRow == null ? firstNonBlank(parsedRow.getTitle(), "") : firstNonBlank(resultRow.getTitle(), parsedRow.getTitle()));
row.createCell(col++).setCellValue(resultRow == null ? firstNonBlank(parsedRow.getUrl(), "") : firstNonBlank(resultRow.getUrl(), parsedRow.getUrl()));
row.createCell(col++).setCellValue(resultRow == null ? firstNonBlank(parsedRow.getSku(), "") : firstNonBlank(resultRow.getSku(), parsedRow.getSku()));
row.createCell(col++).setCellValue(resultRow == null ? "" : userFacingLlmCellValue(resultRow, resultRow.getTitleRisk()));
row.createCell(col++).setCellValue(resultRow == null ? "" : userFacingLlmCellValue(resultRow, resultRow.getAppearanceRisk()));
row.createCell(col++).setCellValue(resultRow == null ? "" : userFacingConclusion(resultRow));
row.createCell(col).setCellValue(resultRow == null ? "" : userFacingStatus(resultRow));
}
writeReasonSheet(workbook, headerStyle, rowsToWrite, resultMap, finder);
}
private static void writeReasonSheet(Workbook workbook, CellStyle headerStyle,
List<AppearancePatentParsedRowVo> rowsToWrite,
Map<String, AppearancePatentResultRowDto> resultMap,
java.util.function.Function<AppearancePatentParsedRowVo, AppearancePatentResultRowDto> finder) {
Sheet sheet = workbook.createSheet("原因");
Row header = sheet.createRow(0);
List<String> headers = List.of("ASIN", "外观原因", "专利原因", "标题原因");
for (int i = 0; i < headers.size(); i++) {
Cell cell = header.createCell(i);
cell.setCellValue(headers.get(i));
cell.setCellStyle(headerStyle);
}
Set<String> writtenAsins = new LinkedHashSet<>();
int rowIndex = 1;
for (AppearancePatentParsedRowVo parsedRow : rowsToWrite == null ? List.<AppearancePatentParsedRowVo>of() : rowsToWrite) {
if (parsedRow == null) {
continue;
}
String asin = normalize(parsedRow.getAsin()).toUpperCase(Locale.ROOT);
if (asin.isBlank() || !writtenAsins.add(asin)) {
continue;
}
AppearancePatentResultRowDto resultRow = finder.apply(parsedRow);
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(asin);
row.createCell(1).setCellValue(resultRow == null ? "" : firstNonBlank(resultRow.getAppearanceReason(), ""));
row.createCell(2).setCellValue(resultRow == null ? "" : firstNonBlank(resultRow.getPatentReason(), ""));
row.createCell(3).setCellValue(resultRow == null ? "" : firstNonBlank(resultRow.getTitleReason(), ""));
rowIndex++;
}
}
public static AppearancePatentResultRowDto findResultRow(AppearancePatentParsedRowVo parsedRow,
Map<String, AppearancePatentResultRowDto> resultMap) {
if (parsedRow == null || resultMap == null || resultMap.isEmpty()) {
return null;
}
AppearancePatentResultRowDto resultRow = resultMap.get(rowKey(parsedRow));
if (resultRow != null) {
return resultRow;
}
String legacyKey = legacyRowKey(parsedRow);
if (legacyKey.isBlank()) {
return null;
}
return resultMap.get(legacyKey);
}
public static String legacyRowKey(AppearancePatentParsedRowVo row) {
if (row == null) {
return "";
}
return rowKey(row.getDisplayId(), row.getAsin(), row.getCountry());
}
public static String rowKey(AppearancePatentParsedRowVo row) {
if (row == null) {
return "";
}
return rowKey(row.getDisplayId(), row.getAsin(), row.getCountry());
}
public static String rowKey(AppearancePatentResultRowDto row) {
if (row == null) {
return "";
}
return rowKey(row.getId(), row.getAsin(), row.getCountry());
}
public static String rowKey(String id, String asin, String country) {
return normalize(id) + "::" + normalize(asin).toUpperCase(Locale.ROOT) + "::" + normalize(country);
}
public static String readValueByHeader(AppearancePatentParsedRowVo row, String... candidates) {
if (row == null || row.getValues() == null || row.getValues().isEmpty() || candidates == null) {
return "";
}
for (Map.Entry<String, String> entry : row.getValues().entrySet()) {
String header = normalize(entry.getKey()).toLowerCase(Locale.ROOT);
for (String candidate : candidates) {
String expected = normalize(candidate).toLowerCase(Locale.ROOT);
if (!expected.isBlank() && header.contains(expected)) {
return entry.getValue() == null ? "" : entry.getValue();
}
}
}
return "";
}
public static String resolveBrand(AppearancePatentResultRowDto resultRow, AppearancePatentParsedRowVo parsedRow) {
String pythonBrand = resultRow == null ? "" : resultRow.getBrand();
if (pythonBrand != null && !pythonBrand.isBlank()) {
return pythonBrand.trim();
}
if (parsedRow == null || parsedRow.getValues() == null) {
return "";
}
for (Map.Entry<String, String> entry : parsedRow.getValues().entrySet()) {
String header = entry.getKey() == null ? "" : entry.getKey().trim().toLowerCase(Locale.ROOT);
if (header.contains("品牌") || header.contains("brand")) {
return entry.getValue() == null ? "" : entry.getValue();
}
}
return "";
}
public static String resolvePrice(AppearancePatentResultRowDto resultRow) {
return resultRow == null || resultRow.getPrice() == null ? "" : resultRow.getPrice().trim();
}
public static String userFacingLlmCellValue(AppearancePatentResultRowDto row, String value) {
String normalizedValue = normalize(value);
if (!normalizedValue.isBlank() && !isTechnicalLlmFailure(normalizedValue)) {
return value;
}
// llm 技术性失败:有错误信息则放入错误信息,没有则留空
if (row != null && isTechnicalLlmFailure(row.getError())) {
return firstNonBlank(row.getError(), "");
}
return firstNonBlank(value, "");
}
public static String userFacingConclusion(AppearancePatentResultRowDto row) {
if (row == null) {
return "";
}
String conclusion = normalize(row.getConclusion());
if (!conclusion.isBlank() && !isTechnicalLlmFailure(conclusion)) {
return row.getConclusion();
}
// llm 技术性失败:有错误信息则放入错误信息,没有则留空
if (isTechnicalLlmFailure(row.getError())) {
return firstNonBlank(row.getError(), "");
}
return firstNonBlank(row.getConclusion(), "");
}
public static String userFacingStatus(AppearancePatentResultRowDto row) {
if (row == null) {
return "";
}
String conclusion = userFacingConclusion(row);
return resolveResultStatus(conclusion);
}
public static String resolveResultStatus(String conclusion) {
String normalized = conclusion == null ? "" : conclusion.trim();
return normalized.isBlank() ? "失败" : "成功";
}
public static boolean isTechnicalLlmFailure(String value) {
String normalized = normalize(value).toLowerCase(Locale.ROOT);
return normalized.contains("coze") || normalized.contains("llm")
|| normalized.contains("结果不完整")
|| normalized.contains("工作流节点执行超限")
|| normalized.contains("调用超时")
|| normalized.contains("timeout");
}
private static String firstNonBlank(String preferred, String fallback) {
return preferred == null || preferred.isBlank() ? fallback : preferred.trim();
}
private static String normalize(String val) {
return val == null ? "" : val.replace(String.valueOf((char) 0xFEFF), "").replace((char) 0x3000, ' ').trim().replaceAll("\\s+", " ");
}
}