更新处理这个外观部分

This commit is contained in:
super
2026-05-01 00:09:14 +08:00
parent 668226a99d
commit 70658041a5
9 changed files with 411 additions and 69 deletions

View File

@@ -60,14 +60,24 @@ public class AppearancePatentCozeClient {
log.warn("[appearance-patent] coze batch fallback split size={} left={} right={} err={}",
rows.size(), middle, rows.size() - middle, failureMessage(ex));
List<AppearancePatentResultRowDto> merged = new ArrayList<>(rows.size());
merged.addAll(inspectWithFallback(rows.subList(0, middle), prompt));
merged.addAll(inspectWithFallback(rows.subList(middle, rows.size()), prompt));
merged.addAll(inspectPartitionWithFailureFallback(rows.subList(0, middle), prompt));
merged.addAll(inspectPartitionWithFailureFallback(rows.subList(middle, rows.size()), prompt));
return merged;
}
throw propagate(ex);
}
}
private List<AppearancePatentResultRowDto> inspectPartitionWithFailureFallback(List<AppearancePatentResultRowDto> rows, String prompt) {
try {
return inspectWithFallback(rows, prompt);
} catch (Exception ex) {
String failureMessage = failureMessage(ex);
log.warn("[appearance-patent] coze partition failed size={} err={}", rows.size(), failureMessage);
return rows.stream().map(this::copy).map(row -> markFailed(row, failureMessage)).toList();
}
}
private List<AppearancePatentResultRowDto> inspectSingleRowWithRetry(List<AppearancePatentResultRowDto> rows, String prompt) throws Exception {
AppearancePatentResultRowDto row = rows.getFirst();
PartialCozeResultException lastFailure = null;
@@ -154,6 +164,9 @@ public class AppearancePatentCozeClient {
body.put("workflow_id", properties.getCozeWorkflowId());
body.put("parameters", parameters);
body.put("is_async", Boolean.TRUE);
log.info("[appearance-patent] coze request url={} body={}",
joinUrl(properties.getCozeBaseUrl(), properties.getCozeWorkflowPath()),
writeJson(body));
RestClient.RequestBodySpec request = restClient().post()
.uri(joinUrl(properties.getCozeBaseUrl(), properties.getCozeWorkflowPath()))
@@ -164,7 +177,11 @@ public class AppearancePatentCozeClient {
request.body(body);
return request.exchange((clientRequest, clientResponse) -> {
byte[] responseBytes = StreamUtils.copyToByteArray(clientResponse.getBody());
return responseBytes.length == 0 ? "" : new String(responseBytes, StandardCharsets.UTF_8);
String responseText = responseBytes.length == 0 ? "" : new String(responseBytes, StandardCharsets.UTF_8);
log.info("[appearance-patent] coze submit response status={} body={}",
clientResponse.getStatusCode(),
responseText);
return responseText;
});
}
@@ -180,7 +197,12 @@ public class AppearancePatentCozeClient {
})
.exchange((clientRequest, clientResponse) -> {
byte[] responseBytes = StreamUtils.copyToByteArray(clientResponse.getBody());
return responseBytes.length == 0 ? "" : new String(responseBytes, StandardCharsets.UTF_8);
String responseText = responseBytes.length == 0 ? "" : new String(responseBytes, StandardCharsets.UTF_8);
log.info("[appearance-patent] coze history response executeId={} status={} body={}",
executeId,
clientResponse.getStatusCode(),
responseText);
return responseText;
});
}
@@ -245,10 +267,26 @@ public class AppearancePatentCozeClient {
text(firstNonNull(
firstNonNull(node.get("country"), node.get("site")),
firstNonNull(itemNode.get("country"), itemNode.get("site")))),
text(node.get("title")),
text(node.get("appearance")),
text(firstNonNull(node.get("patent"), node.get("patent "))),
text(node.get("result"))
text(firstNonNull(node.get("title"), firstNonNull(itemNode.get("title"), itemNode.get("title_risk")))),
text(firstNonNull(node.get("appearance"),
firstNonNull(node.get("appearance_risk"),
firstNonNull(itemNode.get("appearance"), itemNode.get("appearance_risk"))))),
text(firstNonNull(firstNonNull(node.get("patent"), node.get("patent ")),
firstNonNull(node.get("patent_risk"),
firstNonNull(firstNonNull(itemNode.get("patent"), itemNode.get("patent ")), itemNode.get("patent_risk"))))),
text(firstNonNull(node.get("result"),
firstNonNull(node.get("conclusion"),
firstNonNull(itemNode.get("result"), itemNode.get("conclusion"))))),
text(firstNonNull(node.get("title_reason"),
firstNonNull(node.get("titleReason"),
firstNonNull(itemNode.get("title_reason"), itemNode.get("titleReason"))))),
text(firstNonNull(node.get("appearance_reason"),
firstNonNull(node.get("appearanceReason"),
firstNonNull(itemNode.get("appearance_reason"), itemNode.get("appearanceReason"))))),
text(firstNonNull(node.get("patent_reason"),
firstNonNull(firstNonNull(node.get("patentReason"), node.get("patent reason")),
firstNonNull(itemNode.get("patent_reason"),
firstNonNull(itemNode.get("patentReason"), itemNode.get("patent reason"))))))
));
}
}
@@ -349,6 +387,9 @@ public class AppearancePatentCozeClient {
row.setAppearanceRisk(result.appearance());
row.setPatentRisk(result.patent());
row.setConclusion(result.result());
row.setTitleReason(result.titleReason());
row.setAppearanceReason(result.appearanceReason());
row.setPatentReason(result.patentReason());
}
private RestClient restClient() {
@@ -375,6 +416,9 @@ public class AppearancePatentCozeClient {
row.setAppearanceRisk(source.getAppearanceRisk());
row.setPatentRisk(source.getPatentRisk());
row.setConclusion(source.getConclusion());
row.setTitleReason(source.getTitleReason());
row.setAppearanceReason(source.getAppearanceReason());
row.setPatentReason(source.getPatentReason());
return row;
}
@@ -507,13 +551,32 @@ public class AppearancePatentCozeClient {
return false;
}
for (JsonNode item : array) {
if (item.has("appearance") || item.has("patent") || item.has("patent ") || item.has("result")) {
JsonNode itemNode = resultItemNode(item);
if (hasResultFields(item) || hasResultFields(itemNode)) {
return true;
}
}
return false;
}
private boolean hasResultFields(JsonNode item) {
return item != null
&& (item.has("appearance")
|| item.has("appearance_risk")
|| item.has("patent")
|| item.has("patent ")
|| item.has("patent_risk")
|| item.has("result")
|| item.has("conclusion")
|| item.has("title_reason")
|| item.has("titleReason")
|| item.has("appearance_reason")
|| item.has("appearanceReason")
|| item.has("patent_reason")
|| item.has("patentReason")
|| item.has("patent reason"));
}
private JsonNode parseJsonOrMissing(String value) {
String normalized = normalize(value);
if (!(normalized.startsWith("{") || normalized.startsWith("["))) {
@@ -705,7 +768,10 @@ public class AppearancePatentCozeClient {
String title,
String appearance,
String patent,
String result
String result,
String titleReason,
String appearanceReason,
String patentReason
) {
}

View File

@@ -57,8 +57,10 @@ public class AppearancePatentController {
@Operation(summary = "查询外观专利检测历史", description = "查询当前用户最近的外观专利检测历史记录,包含源文件名、任务状态、行数、错误信息和最终 xlsx 下载地址。")
public ApiResponse<AppearancePatentHistoryVo> history(
@Parameter(description = "当前用户 ID用于查询该用户自己的历史记录。", required = true, example = "1")
@RequestParam("user_id") Long userId) {
return ApiResponse.success(service.history(userId));
@RequestParam("user_id") Long userId,
@Parameter(description = "history limit, default 50, max 100", example = "50")
@RequestParam(value = "limit", required = false, defaultValue = "50") Integer limit) {
return ApiResponse.success(service.history(userId, limit));
}
@PostMapping("/tasks/progress/batch")

View File

@@ -52,4 +52,16 @@ public class AppearancePatentResultRowDto {
@Schema(description = "Java 调用 Coze 后生成的最终结论,对应最终 xlsx 的“结论”列。Python 回传请求中不要传该字段。", example = "未发现明显侵权风险", accessMode = Schema.AccessMode.READ_ONLY)
private String conclusion;
@JsonAlias({"title_reason", "titleReason"})
@Schema(description = "Coze title reason", accessMode = Schema.AccessMode.READ_ONLY)
private String titleReason;
@JsonAlias({"appearance_reason", "appearanceReason"})
@Schema(description = "Coze appearance reason", accessMode = Schema.AccessMode.READ_ONLY)
private String appearanceReason;
@JsonAlias({"patent_reason", "patentReason", "patent reason"})
@Schema(description = "Coze patent reason", accessMode = Schema.AccessMode.READ_ONLY)
private String patentReason;
}

View File

@@ -238,22 +238,42 @@ public class AppearancePatentTaskService {
return vo;
}
public AppearancePatentHistoryVo history(Long userId) {
public AppearancePatentHistoryVo history(Long userId, Integer limit) {
AppearancePatentHistoryVo vo = new AppearancePatentHistoryVo();
int safeLimit = Math.max(1, Math.min(limit == null ? 50 : limit, 100));
List<FileResultEntity> rows = fileResultMapper.selectList(new LambdaQueryWrapper<FileResultEntity>()
.select(FileResultEntity::getId,
FileResultEntity::getTaskId,
FileResultEntity::getSourceFilename,
FileResultEntity::getResultFilename,
FileResultEntity::getResultFileUrl,
FileResultEntity::getRowCount,
FileResultEntity::getSuccess,
FileResultEntity::getErrorMessage,
FileResultEntity::getCreatedAt)
.eq(FileResultEntity::getModuleType, MODULE_TYPE)
.eq(FileResultEntity::getUserId, userId)
.orderByDesc(FileResultEntity::getCreatedAt)
.last("limit 100"));
.last("limit " + safeLimit));
Map<Long, String> statusMap = new LinkedHashMap<>();
List<Long> taskIds = rows.stream().map(FileResultEntity::getTaskId).filter(Objects::nonNull).distinct().toList();
if (!taskIds.isEmpty()) {
for (FileTaskEntity task : fileTaskMapper.selectBatchIds(taskIds)) {
for (FileTaskEntity task : fileTaskMapper.selectList(new LambdaQueryWrapper<FileTaskEntity>()
.select(FileTaskEntity::getId, FileTaskEntity::getStatus)
.in(FileTaskEntity::getId, taskIds))) {
statusMap.put(task.getId(), task.getStatus());
}
}
Map<Long, TaskFileJobEntity> jobMap = taskFileJobService.findAssembleJobsByResultIds(MODULE_TYPE, rows.stream()
.map(FileResultEntity::getId)
.filter(Objects::nonNull)
.toList());
for (FileResultEntity row : rows) {
vo.getItems().add(toHistoryItem(row, statusMap.get(row.getTaskId())));
String taskStatus = statusMap.get(row.getTaskId());
if (STATUS_PENDING.equals(taskStatus)) {
continue;
}
vo.getItems().add(toHistoryItem(row, taskStatus, jobMap.get(row.getId())));
}
return vo;
}
@@ -268,11 +288,43 @@ public class AppearancePatentTaskService {
return vo;
}
Map<Long, FileTaskEntity> taskMap = new LinkedHashMap<>();
for (FileTaskEntity task : fileTaskMapper.selectBatchIds(normalizedIds)) {
for (FileTaskEntity task : fileTaskMapper.selectList(new LambdaQueryWrapper<FileTaskEntity>()
.select(FileTaskEntity::getId,
FileTaskEntity::getTaskNo,
FileTaskEntity::getModuleType,
FileTaskEntity::getStatus,
FileTaskEntity::getErrorMessage,
FileTaskEntity::getCreatedAt,
FileTaskEntity::getUpdatedAt,
FileTaskEntity::getFinishedAt)
.in(FileTaskEntity::getId, normalizedIds))) {
if (task != null && MODULE_TYPE.equals(task.getModuleType())) {
taskMap.put(task.getId(), task);
}
}
List<FileResultEntity> resultRows = fileResultMapper.selectList(new LambdaQueryWrapper<FileResultEntity>()
.select(FileResultEntity::getId,
FileResultEntity::getTaskId,
FileResultEntity::getSourceFilename,
FileResultEntity::getResultFilename,
FileResultEntity::getResultFileUrl,
FileResultEntity::getRowCount,
FileResultEntity::getSuccess,
FileResultEntity::getErrorMessage,
FileResultEntity::getCreatedAt)
.eq(FileResultEntity::getModuleType, MODULE_TYPE)
.in(FileResultEntity::getTaskId, normalizedIds)
.orderByDesc(FileResultEntity::getCreatedAt));
Map<Long, FileResultEntity> resultByTaskId = new LinkedHashMap<>();
for (FileResultEntity row : resultRows) {
if (row.getTaskId() != null) {
resultByTaskId.putIfAbsent(row.getTaskId(), row);
}
}
Map<Long, TaskFileJobEntity> jobMap = taskFileJobService.findAssembleJobsByResultIds(MODULE_TYPE, resultRows.stream()
.map(FileResultEntity::getId)
.filter(Objects::nonNull)
.toList());
for (Long taskId : normalizedIds) {
FileTaskEntity task = taskMap.get(taskId);
if (task == null) {
@@ -281,6 +333,10 @@ public class AppearancePatentTaskService {
}
AppearancePatentTaskDetailVo detail = new AppearancePatentTaskDetailVo();
detail.setTask(toTaskItem(task));
FileResultEntity resultRow = resultByTaskId.get(taskId);
if (resultRow != null) {
detail.getItems().add(toHistoryItem(resultRow, task.getStatus(), jobMap.get(resultRow.getId())));
}
vo.getItems().add(detail);
}
return vo;
@@ -890,6 +946,9 @@ public class AppearancePatentTaskService {
row.setAppearanceRisk(representative.getAppearanceRisk());
row.setPatentRisk(representative.getPatentRisk());
row.setConclusion(representative.getConclusion());
row.setTitleReason(representative.getTitleReason());
row.setAppearanceReason(representative.getAppearanceReason());
row.setPatentReason(representative.getPatentReason());
row.setDone(representative.getDone());
return row;
}
@@ -1057,6 +1116,7 @@ public class AppearancePatentTaskService {
row.createCell(col++).setCellValue(resultRow == null ? missingReason : userFacingCozeCellValue(resultRow, resultRow.getPatentRisk()));
row.createCell(col).setCellValue(resultRow == null ? "未送检" : userFacingConclusion(resultRow));
}
writeReasonSheet(workbook, headerStyle, parsed, resultMap);
workbook.write(fos);
workbook.dispose();
} catch (Exception ex) {
@@ -1064,6 +1124,52 @@ public class AppearancePatentTaskService {
}
}
private void writeReasonSheet(SXSSFWorkbook workbook,
CellStyle headerStyle,
AppearancePatentParsedPayloadDto parsed,
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 : parsed.getAllItems()) {
String asin = normalize(parsedRow.getAsin()).toUpperCase(Locale.ROOT);
if (asin.isBlank() || !writtenAsins.add(asin)) {
continue;
}
AppearancePatentResultRowDto resultRow = findResultRow(parsedRow, resultMap);
if (resultRow == null) {
resultRow = findResultRowByAsin(asin, 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()) {
return null;
}
for (AppearancePatentResultRowDto row : resultMap.values()) {
if (row != null && normalizedAsin.equals(normalize(row.getAsin()).toUpperCase(Locale.ROOT))) {
return row;
}
}
return null;
}
private ParsedWorkbook parseWorkbook(File input, AppearancePatentSourceFileDto source) {
DataFormatter formatter = new DataFormatter();
try (FileInputStream fis = new FileInputStream(input); Workbook workbook = WorkbookFactory.create(fis)) {
@@ -1342,14 +1448,14 @@ public class AppearancePatentTaskService {
return vo;
}
private AppearancePatentHistoryItemVo toHistoryItem(FileResultEntity row, String taskStatus) {
private AppearancePatentHistoryItemVo toHistoryItem(FileResultEntity row, String taskStatus, 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);
attachFileJobState(vo, row, job);
vo.setTaskStatus(taskStatus);
vo.setSuccess(row.getSuccess() != null && row.getSuccess() == 1);
vo.setError(row.getErrorMessage());
@@ -1358,8 +1464,7 @@ public class AppearancePatentTaskService {
return vo;
}
private void attachFileJobState(AppearancePatentHistoryItemVo vo, FileResultEntity row) {
TaskFileJobEntity job = taskFileJobService.findAssembleJob(row.getTaskId(), MODULE_TYPE, row.getId());
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);

View File

@@ -12,7 +12,9 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Service
@RequiredArgsConstructor
@@ -190,6 +192,27 @@ public class TaskFileJobService {
return findJob(taskId, moduleType, resultId, JOB_TYPE_ASSEMBLE_RESULT);
}
public Map<Long, TaskFileJobEntity> findAssembleJobsByResultIds(String moduleType, List<Long> resultIds) {
List<Long> normalizedIds = resultIds == null ? List.of() : resultIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (normalizedIds.isEmpty() || moduleType == null || moduleType.isBlank()) {
return Map.of();
}
List<TaskFileJobEntity> jobs = taskFileJobMapper.selectList(new LambdaQueryWrapper<TaskFileJobEntity>()
.eq(TaskFileJobEntity::getModuleType, moduleType)
.eq(TaskFileJobEntity::getJobType, JOB_TYPE_ASSEMBLE_RESULT)
.in(TaskFileJobEntity::getResultId, normalizedIds));
Map<Long, TaskFileJobEntity> jobMap = new LinkedHashMap<>();
for (TaskFileJobEntity job : jobs) {
if (job != null && job.getResultId() != null) {
jobMap.putIfAbsent(job.getResultId(), job);
}
}
return jobMap;
}
public long countUnfinishedAssembleJobs(Long taskId, String moduleType) {
if (taskId == null || taskId <= 0 || moduleType == null || moduleType.isBlank()) {
return 0L;

View File

@@ -0,0 +1,16 @@
SET @db_name = DATABASE();
SET @idx_exists := (
SELECT COUNT(*)
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = @db_name
AND TABLE_NAME = 'biz_task_file_job'
AND INDEX_NAME = 'idx_file_job_module_type_result'
);
SET @sql := IF(@idx_exists = 0,
'ALTER TABLE biz_task_file_job ADD INDEX idx_file_job_module_type_result (module_type, job_type, result_id)',
'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;