refactor(phase-3): 3A 拆薄第八笔——AP 进度与视图组装出件,服务 2377→2161
G 聚类(看板/历史/批量进度、文件构建进度百分比与快照落库)原样搬进 AppearancePatentProgressSupport;四个公开入口与 saveFileBuildProgress 保留同名薄委托。SourceScan 面数 34→35、support 11→12 并点名新件。
This commit is contained in:
+16
-232
@@ -26,6 +26,7 @@ import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHisto
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentHistoryAssembler;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentLlmBatchPipeline;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentPayloadSupport;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentProgressSupport;
|
||||
import com.nanri.aiimage.modules.appearancepatent.service.support.AppearancePatentResultRowReader;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryVo;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentParsedGroupVo;
|
||||
@@ -100,7 +101,7 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
|
||||
public static final String MODULE_TYPE = "APPEARANCE_PATENT";
|
||||
|
||||
public TaskProgressLightBatchVo progressLight(List<Long> taskIds, Long userId) {
|
||||
return taskProgressLightAssembler.assemble(MODULE_TYPE, userId, taskIds);
|
||||
return progressSupport().progressLight(taskIds, userId);
|
||||
}
|
||||
private static final String STATUS_PENDING = "PENDING";
|
||||
private static final String STATUS_RUNNING = "RUNNING";
|
||||
@@ -329,124 +330,15 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
|
||||
}
|
||||
|
||||
public AppearancePatentDashboardVo dashboard(Long userId) {
|
||||
AppearancePatentDashboardVo vo = new AppearancePatentDashboardVo();
|
||||
vo.setPendingTaskCount(countTask(userId, STATUS_RUNNING));
|
||||
vo.setSuccessTaskCount(countTask(userId, STATUS_SUCCESS));
|
||||
vo.setFailedTaskCount(countTask(userId, STATUS_FAILED));
|
||||
vo.setProcessedTaskCount(vo.getSuccessTaskCount() + vo.getFailedTaskCount());
|
||||
return vo;
|
||||
return progressSupport().dashboard(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 " + safeLimit));
|
||||
Map<Long, FileTaskEntity> taskMap = new LinkedHashMap<>();
|
||||
List<Long> taskIds = rows.stream().map(FileResultEntity::getTaskId).filter(Objects::nonNull).distinct().toList();
|
||||
if (!taskIds.isEmpty()) {
|
||||
for (FileTaskEntity task : fileTaskMapper.selectList(new LambdaQueryWrapper<FileTaskEntity>()
|
||||
.select(FileTaskEntity::getId,
|
||||
FileTaskEntity::getStatus,
|
||||
FileTaskEntity::getCreatedAt,
|
||||
FileTaskEntity::getUpdatedAt,
|
||||
FileTaskEntity::getFinishedAt)
|
||||
.in(FileTaskEntity::getId, taskIds))) {
|
||||
taskMap.put(task.getId(), task);
|
||||
}
|
||||
}
|
||||
Map<Long, TaskFileJobEntity> jobMap = taskFileJobService.findAssembleJobsByResultIds(MODULE_TYPE, rows.stream()
|
||||
.map(FileResultEntity::getId)
|
||||
.filter(Objects::nonNull)
|
||||
.toList());
|
||||
List<FileResultEntity> sortedRows = new ArrayList<>(rows);
|
||||
sortedRows.sort(Comparator
|
||||
.comparingInt((FileResultEntity row) -> historyPriority(row, taskMap.get(row.getTaskId()), jobMap.get(row.getId())))
|
||||
.thenComparing((FileResultEntity row) -> historyActivityTime(row, taskMap.get(row.getTaskId()), jobMap.get(row.getId())),
|
||||
Comparator.nullsLast(Comparator.reverseOrder()))
|
||||
.thenComparing(FileResultEntity::getCreatedAt, Comparator.nullsLast(Comparator.reverseOrder()))
|
||||
.thenComparing(FileResultEntity::getId, Comparator.nullsLast(Comparator.reverseOrder())));
|
||||
for (FileResultEntity row : sortedRows) {
|
||||
FileTaskEntity task = taskMap.get(row.getTaskId());
|
||||
vo.getItems().add(toHistoryItem(row, task, jobMap.get(row.getId())));
|
||||
}
|
||||
return vo;
|
||||
return progressSupport().history(userId, limit);
|
||||
}
|
||||
|
||||
public AppearancePatentTaskBatchVo progressBatch(List<Long> taskIds) {
|
||||
AppearancePatentTaskBatchVo vo = new AppearancePatentTaskBatchVo();
|
||||
List<Long> normalizedIds = taskIds == null ? List.of() : taskIds.stream()
|
||||
.filter(id -> id != null && id > 0)
|
||||
.distinct()
|
||||
.toList();
|
||||
if (normalizedIds.isEmpty()) {
|
||||
return vo;
|
||||
}
|
||||
Map<Long, FileTaskEntity> taskMap = new LinkedHashMap<>();
|
||||
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) {
|
||||
vo.getMissingTaskIds().add(taskId);
|
||||
continue;
|
||||
}
|
||||
AppearancePatentTaskDetailVo detail = new AppearancePatentTaskDetailVo();
|
||||
detail.setTask(toTaskItem(task));
|
||||
FileResultEntity resultRow = resultByTaskId.get(taskId);
|
||||
if (resultRow != null) {
|
||||
detail.getItems().add(toHistoryItem(resultRow, task, jobMap.get(resultRow.getId())));
|
||||
}
|
||||
vo.getItems().add(detail);
|
||||
}
|
||||
return vo;
|
||||
return progressSupport().progressBatch(taskIds);
|
||||
}
|
||||
|
||||
public void submitResult(Long taskId, AppearancePatentSubmitResultRequest request) {
|
||||
@@ -855,6 +747,16 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
|
||||
this::normalize, payloadSupport()::readParsedPayload);
|
||||
}
|
||||
|
||||
/**
|
||||
* G 聚类(进度与视图组装)住在 {@link AppearancePatentProgressSupport}(3A 拆薄第八笔):
|
||||
* 看板/历史/批量进度、文件构建进度百分比与快照落库逐字搬移;四个公开入口保留为薄委托。
|
||||
*/
|
||||
private AppearancePatentProgressSupport progressSupport() {
|
||||
return new AppearancePatentProgressSupport(
|
||||
MODULE_TYPE, taskProgressLightAssembler, fileTaskMapper, fileResultMapper,
|
||||
taskFileJobService, taskProgressSnapshotService, objectMapper);
|
||||
}
|
||||
|
||||
private void submitLlmForSubmittedChunk(SubmitContext context) {
|
||||
if (context == null || context.task() == null || context.task().getId() == null) {
|
||||
return;
|
||||
@@ -1680,83 +1582,7 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
|
||||
int total,
|
||||
int completed,
|
||||
String message) {
|
||||
if (task == null || task.getId() == null || job == null || job.getId() == null) {
|
||||
return;
|
||||
}
|
||||
int safeTotal = Math.max(1, total);
|
||||
int safeCompleted = Math.max(0, Math.min(completed, safeTotal));
|
||||
TaskProgressSnapshotEntity existing = taskProgressSnapshotService.find(task.getId(), MODULE_TYPE);
|
||||
int displayPercent = calculateDisplayProgressPercent(safeCompleted, safeTotal, job, LocalDateTime.now());
|
||||
int previousDisplayPercent = calculateSnapshotDisplayPercent(existing, job);
|
||||
if (previousDisplayPercent > displayPercent && safeCompleted < safeTotal) {
|
||||
displayPercent = previousDisplayPercent;
|
||||
}
|
||||
// 终态必须带任务真实状态落库:save() 只对终态豁免 500ms 节流,收尾三连写(组装/上传/完成)
|
||||
// 都在同一次调用里,若全按 RUNNING 写,只有第一条落地,满格「结果文件已生成」必被吞——
|
||||
// 看板就会永远停在 0/N 或 N-2/N(2026-10-04 生产实测)。对齐货源查询 isTerminalFileBuildProgress。
|
||||
boolean terminalProgress = safeCompleted >= safeTotal
|
||||
&& (STATUS_SUCCESS.equals(task.getStatus()) || STATUS_FAILED.equals(task.getStatus()));
|
||||
if (terminalProgress) {
|
||||
displayPercent = 100;
|
||||
}
|
||||
taskProgressSnapshotService.save(
|
||||
task.getId(),
|
||||
MODULE_TYPE,
|
||||
terminalProgress ? task.getStatus() : STATUS_RUNNING,
|
||||
safeTotal,
|
||||
safeCompleted,
|
||||
0,
|
||||
job.getScopeKey(),
|
||||
message,
|
||||
Map.of("phase", "RESULT_FILE", "jobId", job.getId(), "displayPercent", displayPercent)
|
||||
);
|
||||
}
|
||||
|
||||
private int calculateDisplayProgressPercent(int current,
|
||||
int total,
|
||||
TaskFileJobEntity job,
|
||||
LocalDateTime baseTime) {
|
||||
if (total <= 0) {
|
||||
return 0;
|
||||
}
|
||||
current = Math.max(0, Math.min(current, total));
|
||||
int percent = Math.max(1, Math.min(99, (int) Math.floor(current * 100.0 / total)));
|
||||
if (job != null && STATUS_RUNNING.equals(job.getStatus())) {
|
||||
long elapsedSeconds = baseTime == null ? 0 : Math.max(0, Duration.between(baseTime, LocalDateTime.now()).getSeconds());
|
||||
if (current <= 0) {
|
||||
int firstRealProgressPercent = Math.max(1, Math.min(99, (int) Math.floor(100.0 / total)));
|
||||
int waitingCap = Math.max(8, Math.min(35, firstRealProgressPercent - 1));
|
||||
percent = Math.max(percent, Math.min(waitingCap, 8 + (int) (elapsedSeconds / 6)));
|
||||
} else if (current < total) {
|
||||
percent = Math.max(percent, Math.min(92, percent + (int) (elapsedSeconds / 10)));
|
||||
}
|
||||
}
|
||||
return percent;
|
||||
}
|
||||
|
||||
private int calculateSnapshotDisplayPercent(TaskProgressSnapshotEntity snapshot, TaskFileJobEntity job) {
|
||||
if (snapshot == null) {
|
||||
return 0;
|
||||
}
|
||||
int total = snapshot.getTotalCount() == null ? 0 : snapshot.getTotalCount();
|
||||
int current = snapshot.getSuccessCount() == null ? 0 : snapshot.getSuccessCount();
|
||||
int percent = total <= 0 ? 0 : calculateDisplayProgressPercent(current, total, job, snapshot.getUpdatedAt());
|
||||
return Math.max(percent, extractSnapshotDisplayPercent(snapshot));
|
||||
}
|
||||
|
||||
private int extractSnapshotDisplayPercent(TaskProgressSnapshotEntity snapshot) {
|
||||
if (snapshot == null || snapshot.getSnapshotJson() == null || snapshot.getSnapshotJson().isBlank()) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
JsonNode displayPercent = objectMapper.readTree(snapshot.getSnapshotJson()).path("displayPercent");
|
||||
if (!displayPercent.isNumber()) {
|
||||
return 0;
|
||||
}
|
||||
return Math.max(0, Math.min(100, displayPercent.asInt()));
|
||||
} catch (Exception ignored) {
|
||||
return 0;
|
||||
}
|
||||
progressSupport().saveFileBuildProgress(task, job, total, completed, message);
|
||||
}
|
||||
|
||||
public void cleanupResultFileJob(TaskFileJobEntity job) {
|
||||
@@ -2257,14 +2083,6 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
|
||||
return normalize(sourceFileKey) + "::" + normalize(rowBaseId) + "@" + (rowIndex == null ? 0 : rowIndex);
|
||||
}
|
||||
|
||||
private long countTask(Long userId, String status) {
|
||||
Long count = fileTaskMapper.selectCount(new LambdaQueryWrapper<FileTaskEntity>()
|
||||
.eq(FileTaskEntity::getModuleType, MODULE_TYPE)
|
||||
.eq(FileTaskEntity::getUserId, userId)
|
||||
.eq(FileTaskEntity::getStatus, status));
|
||||
return count == null ? 0L : count;
|
||||
}
|
||||
|
||||
private int countChunks(Long taskId, String scopeHash) {
|
||||
Long count = taskChunkMapper.selectCount(new LambdaQueryWrapper<TaskChunkEntity>()
|
||||
.eq(TaskChunkEntity::getTaskId, taskId)
|
||||
@@ -2273,40 +2091,6 @@ public class AppearancePatentTaskService extends AbstractFileTaskPipeline<Appear
|
||||
return count == null ? 0 : count.intValue();
|
||||
}
|
||||
|
||||
private AppearancePatentTaskItemVo toTaskItem(FileTaskEntity task) {
|
||||
AppearancePatentTaskItemVo vo = new AppearancePatentTaskItemVo();
|
||||
vo.setId(task.getId());
|
||||
vo.setTaskNo(task.getTaskNo());
|
||||
vo.setStatus(task.getStatus());
|
||||
vo.setErrorMessage(task.getErrorMessage());
|
||||
vo.setCreatedAt(fmt(task.getCreatedAt()));
|
||||
vo.setUpdatedAt(fmt(task.getUpdatedAt()));
|
||||
vo.setFinishedAt(fmt(task.getFinishedAt()));
|
||||
return vo;
|
||||
}
|
||||
|
||||
private AppearancePatentHistoryAssembler historyAssembler() {
|
||||
return new AppearancePatentHistoryAssembler(
|
||||
taskProgressSnapshotService,
|
||||
this::calculateDisplayProgressPercent,
|
||||
this::extractSnapshotDisplayPercent);
|
||||
}
|
||||
|
||||
private AppearancePatentHistoryItemVo toHistoryItem(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
|
||||
return historyAssembler().toHistoryItem(row, task, job);
|
||||
}
|
||||
|
||||
private int historyPriority(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
|
||||
return historyAssembler().historyPriority(row, task, job);
|
||||
}
|
||||
|
||||
private LocalDateTime historyActivityTime(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
|
||||
return historyAssembler().historyActivityTime(row, task, job);
|
||||
}
|
||||
|
||||
private String fmt(LocalDateTime t) {
|
||||
return t == null ? null : t.toString();
|
||||
}
|
||||
|
||||
private String firstNonBlank(String preferred, String fallback) {
|
||||
return preferred == null || preferred.isBlank() ? fallback : preferred.trim();
|
||||
|
||||
+321
@@ -0,0 +1,321 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service.support;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.task.TaskStatuses;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentDashboardVo;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryItemVo;
|
||||
import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentHistoryVo;
|
||||
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.task.mapper.FileResultMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
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.model.vo.TaskProgressLightBatchVo;
|
||||
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 外观专利的进度与视图组装(G 聚类),从 {@code AppearancePatentTaskService} <b>原样搬移</b>
|
||||
* (3A 拆薄第八笔):看板计数、历史列表、批量进度、文件构建进度百分比与快照节流落库,
|
||||
* 连同 500ms 节流豁免、终态带真实状态、显示百分比只增不减这些判据一个字没改。
|
||||
*
|
||||
* <p>无状态、按调用现取(与 {@code historyAssembler()} 同一先例),不新增 Spring bean。
|
||||
*/
|
||||
public class AppearancePatentProgressSupport {
|
||||
|
||||
private static final String STATUS_RUNNING = TaskStatuses.FileTask.RUNNING;
|
||||
private static final String STATUS_SUCCESS = TaskStatuses.FileTask.SUCCESS;
|
||||
private static final String STATUS_FAILED = TaskStatuses.FileTask.FAILED;
|
||||
|
||||
private final String moduleType;
|
||||
private final TaskProgressLightAssembler taskProgressLightAssembler;
|
||||
private final FileTaskMapper fileTaskMapper;
|
||||
private final FileResultMapper fileResultMapper;
|
||||
private final TaskFileJobService taskFileJobService;
|
||||
private final TaskProgressSnapshotService taskProgressSnapshotService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public AppearancePatentProgressSupport(String moduleType,
|
||||
TaskProgressLightAssembler taskProgressLightAssembler,
|
||||
FileTaskMapper fileTaskMapper,
|
||||
FileResultMapper fileResultMapper,
|
||||
TaskFileJobService taskFileJobService,
|
||||
TaskProgressSnapshotService taskProgressSnapshotService,
|
||||
ObjectMapper objectMapper) {
|
||||
this.moduleType = moduleType;
|
||||
this.taskProgressLightAssembler = taskProgressLightAssembler;
|
||||
this.fileTaskMapper = fileTaskMapper;
|
||||
this.fileResultMapper = fileResultMapper;
|
||||
this.taskFileJobService = taskFileJobService;
|
||||
this.taskProgressSnapshotService = taskProgressSnapshotService;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public TaskProgressLightBatchVo progressLight(List<Long> taskIds, Long userId) {
|
||||
return taskProgressLightAssembler.assemble(moduleType, userId, taskIds);
|
||||
}
|
||||
|
||||
public AppearancePatentDashboardVo dashboard(Long userId) {
|
||||
AppearancePatentDashboardVo vo = new AppearancePatentDashboardVo();
|
||||
vo.setPendingTaskCount(countTask(userId, STATUS_RUNNING));
|
||||
vo.setSuccessTaskCount(countTask(userId, STATUS_SUCCESS));
|
||||
vo.setFailedTaskCount(countTask(userId, STATUS_FAILED));
|
||||
vo.setProcessedTaskCount(vo.getSuccessTaskCount() + vo.getFailedTaskCount());
|
||||
return vo;
|
||||
}
|
||||
|
||||
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, moduleType)
|
||||
.eq(FileResultEntity::getUserId, userId)
|
||||
.orderByDesc(FileResultEntity::getCreatedAt)
|
||||
.last("limit " + safeLimit));
|
||||
Map<Long, FileTaskEntity> taskMap = new LinkedHashMap<>();
|
||||
List<Long> taskIds = rows.stream().map(FileResultEntity::getTaskId).filter(Objects::nonNull).distinct().toList();
|
||||
if (!taskIds.isEmpty()) {
|
||||
for (FileTaskEntity task : fileTaskMapper.selectList(new LambdaQueryWrapper<FileTaskEntity>()
|
||||
.select(FileTaskEntity::getId,
|
||||
FileTaskEntity::getStatus,
|
||||
FileTaskEntity::getCreatedAt,
|
||||
FileTaskEntity::getUpdatedAt,
|
||||
FileTaskEntity::getFinishedAt)
|
||||
.in(FileTaskEntity::getId, taskIds))) {
|
||||
taskMap.put(task.getId(), task);
|
||||
}
|
||||
}
|
||||
Map<Long, TaskFileJobEntity> jobMap = taskFileJobService.findAssembleJobsByResultIds(moduleType, rows.stream()
|
||||
.map(FileResultEntity::getId)
|
||||
.filter(Objects::nonNull)
|
||||
.toList());
|
||||
List<FileResultEntity> sortedRows = new ArrayList<>(rows);
|
||||
sortedRows.sort(Comparator
|
||||
.comparingInt((FileResultEntity row) -> historyPriority(row, taskMap.get(row.getTaskId()), jobMap.get(row.getId())))
|
||||
.thenComparing((FileResultEntity row) -> historyActivityTime(row, taskMap.get(row.getTaskId()), jobMap.get(row.getId())),
|
||||
Comparator.nullsLast(Comparator.reverseOrder()))
|
||||
.thenComparing(FileResultEntity::getCreatedAt, Comparator.nullsLast(Comparator.reverseOrder()))
|
||||
.thenComparing(FileResultEntity::getId, Comparator.nullsLast(Comparator.reverseOrder())));
|
||||
for (FileResultEntity row : sortedRows) {
|
||||
FileTaskEntity task = taskMap.get(row.getTaskId());
|
||||
vo.getItems().add(toHistoryItem(row, task, jobMap.get(row.getId())));
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
public AppearancePatentTaskBatchVo progressBatch(List<Long> taskIds) {
|
||||
AppearancePatentTaskBatchVo vo = new AppearancePatentTaskBatchVo();
|
||||
List<Long> normalizedIds = taskIds == null ? List.of() : taskIds.stream()
|
||||
.filter(id -> id != null && id > 0)
|
||||
.distinct()
|
||||
.toList();
|
||||
if (normalizedIds.isEmpty()) {
|
||||
return vo;
|
||||
}
|
||||
Map<Long, FileTaskEntity> taskMap = new LinkedHashMap<>();
|
||||
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 && moduleType.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, moduleType)
|
||||
.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(moduleType, resultRows.stream()
|
||||
.map(FileResultEntity::getId)
|
||||
.filter(Objects::nonNull)
|
||||
.toList());
|
||||
for (Long taskId : normalizedIds) {
|
||||
FileTaskEntity task = taskMap.get(taskId);
|
||||
if (task == null) {
|
||||
vo.getMissingTaskIds().add(taskId);
|
||||
continue;
|
||||
}
|
||||
AppearancePatentTaskDetailVo detail = new AppearancePatentTaskDetailVo();
|
||||
detail.setTask(toTaskItem(task));
|
||||
FileResultEntity resultRow = resultByTaskId.get(taskId);
|
||||
if (resultRow != null) {
|
||||
detail.getItems().add(toHistoryItem(resultRow, task, jobMap.get(resultRow.getId())));
|
||||
}
|
||||
vo.getItems().add(detail);
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
public void saveFileBuildProgress(FileTaskEntity task,
|
||||
TaskFileJobEntity job,
|
||||
int total,
|
||||
int completed,
|
||||
String message) {
|
||||
if (task == null || task.getId() == null || job == null || job.getId() == null) {
|
||||
return;
|
||||
}
|
||||
int safeTotal = Math.max(1, total);
|
||||
int safeCompleted = Math.max(0, Math.min(completed, safeTotal));
|
||||
TaskProgressSnapshotEntity existing = taskProgressSnapshotService.find(task.getId(), moduleType);
|
||||
int displayPercent = calculateDisplayProgressPercent(safeCompleted, safeTotal, job, LocalDateTime.now());
|
||||
int previousDisplayPercent = calculateSnapshotDisplayPercent(existing, job);
|
||||
if (previousDisplayPercent > displayPercent && safeCompleted < safeTotal) {
|
||||
displayPercent = previousDisplayPercent;
|
||||
}
|
||||
// 终态必须带任务真实状态落库:save() 只对终态豁免 500ms 节流,收尾三连写(组装/上传/完成)
|
||||
// 都在同一次调用里,若全按 RUNNING 写,只有第一条落地,满格「结果文件已生成」必被吞——
|
||||
// 看板就会永远停在 0/N 或 N-2/N(2026-10-04 生产实测)。对齐货源查询 isTerminalFileBuildProgress。
|
||||
boolean terminalProgress = safeCompleted >= safeTotal
|
||||
&& (STATUS_SUCCESS.equals(task.getStatus()) || STATUS_FAILED.equals(task.getStatus()));
|
||||
if (terminalProgress) {
|
||||
displayPercent = 100;
|
||||
}
|
||||
taskProgressSnapshotService.save(
|
||||
task.getId(),
|
||||
moduleType,
|
||||
terminalProgress ? task.getStatus() : STATUS_RUNNING,
|
||||
safeTotal,
|
||||
safeCompleted,
|
||||
0,
|
||||
job.getScopeKey(),
|
||||
message,
|
||||
Map.of("phase", "RESULT_FILE", "jobId", job.getId(), "displayPercent", displayPercent)
|
||||
);
|
||||
}
|
||||
|
||||
private int calculateDisplayProgressPercent(int current,
|
||||
int total,
|
||||
TaskFileJobEntity job,
|
||||
LocalDateTime baseTime) {
|
||||
if (total <= 0) {
|
||||
return 0;
|
||||
}
|
||||
current = Math.max(0, Math.min(current, total));
|
||||
int percent = Math.max(1, Math.min(99, (int) Math.floor(current * 100.0 / total)));
|
||||
if (job != null && STATUS_RUNNING.equals(job.getStatus())) {
|
||||
long elapsedSeconds = baseTime == null ? 0 : Math.max(0, Duration.between(baseTime, LocalDateTime.now()).getSeconds());
|
||||
if (current <= 0) {
|
||||
int firstRealProgressPercent = Math.max(1, Math.min(99, (int) Math.floor(100.0 / total)));
|
||||
int waitingCap = Math.max(8, Math.min(35, firstRealProgressPercent - 1));
|
||||
percent = Math.max(percent, Math.min(waitingCap, 8 + (int) (elapsedSeconds / 6)));
|
||||
} else if (current < total) {
|
||||
percent = Math.max(percent, Math.min(92, percent + (int) (elapsedSeconds / 10)));
|
||||
}
|
||||
}
|
||||
return percent;
|
||||
}
|
||||
|
||||
private int calculateSnapshotDisplayPercent(TaskProgressSnapshotEntity snapshot, TaskFileJobEntity job) {
|
||||
if (snapshot == null) {
|
||||
return 0;
|
||||
}
|
||||
int total = snapshot.getTotalCount() == null ? 0 : snapshot.getTotalCount();
|
||||
int current = snapshot.getSuccessCount() == null ? 0 : snapshot.getSuccessCount();
|
||||
int percent = total <= 0 ? 0 : calculateDisplayProgressPercent(current, total, job, snapshot.getUpdatedAt());
|
||||
return Math.max(percent, extractSnapshotDisplayPercent(snapshot));
|
||||
}
|
||||
|
||||
private int extractSnapshotDisplayPercent(TaskProgressSnapshotEntity snapshot) {
|
||||
if (snapshot == null || snapshot.getSnapshotJson() == null || snapshot.getSnapshotJson().isBlank()) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
JsonNode displayPercent = objectMapper.readTree(snapshot.getSnapshotJson()).path("displayPercent");
|
||||
if (!displayPercent.isNumber()) {
|
||||
return 0;
|
||||
}
|
||||
return Math.max(0, Math.min(100, displayPercent.asInt()));
|
||||
} catch (Exception ignored) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private long countTask(Long userId, String status) {
|
||||
Long count = fileTaskMapper.selectCount(new LambdaQueryWrapper<FileTaskEntity>()
|
||||
.eq(FileTaskEntity::getModuleType, moduleType)
|
||||
.eq(FileTaskEntity::getUserId, userId)
|
||||
.eq(FileTaskEntity::getStatus, status));
|
||||
return count == null ? 0L : count;
|
||||
}
|
||||
|
||||
private AppearancePatentTaskItemVo toTaskItem(FileTaskEntity task) {
|
||||
AppearancePatentTaskItemVo vo = new AppearancePatentTaskItemVo();
|
||||
vo.setId(task.getId());
|
||||
vo.setTaskNo(task.getTaskNo());
|
||||
vo.setStatus(task.getStatus());
|
||||
vo.setErrorMessage(task.getErrorMessage());
|
||||
vo.setCreatedAt(fmt(task.getCreatedAt()));
|
||||
vo.setUpdatedAt(fmt(task.getUpdatedAt()));
|
||||
vo.setFinishedAt(fmt(task.getFinishedAt()));
|
||||
return vo;
|
||||
}
|
||||
|
||||
private AppearancePatentHistoryAssembler historyAssembler() {
|
||||
return new AppearancePatentHistoryAssembler(
|
||||
taskProgressSnapshotService,
|
||||
this::calculateDisplayProgressPercent,
|
||||
this::extractSnapshotDisplayPercent);
|
||||
}
|
||||
|
||||
private AppearancePatentHistoryItemVo toHistoryItem(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
|
||||
return historyAssembler().toHistoryItem(row, task, job);
|
||||
}
|
||||
|
||||
private int historyPriority(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
|
||||
return historyAssembler().historyPriority(row, task, job);
|
||||
}
|
||||
|
||||
private LocalDateTime historyActivityTime(FileResultEntity row, FileTaskEntity task, TaskFileJobEntity job) {
|
||||
return historyAssembler().historyActivityTime(row, task, job);
|
||||
}
|
||||
|
||||
private String fmt(LocalDateTime t) {
|
||||
return t == null ? null : t.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user