refactor: 第二批收敛——公共 Excel 写出器 + 任务收尾零件下沉 + 前端上传区/任务面板抽取

This commit is contained in:
2026-10-01 15:00:52 +08:00
parent 4b29a150f3
commit fa4bd57016
32 changed files with 2467 additions and 2826 deletions
@@ -0,0 +1,135 @@
package com.nanri.aiimage.common.util;
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.xssf.streaming.SXSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 公共 SXSSF 流式写出器:统一 {@code new SXSSFWorkbook(200) + setCompressTempFiles(true)}
* + write + try/finally {@code close()+dispose()} 骨架,并提供空值安全的单元格写入与列宽换算。
*
* <p>此前 8 个 {@code *ExcelAssemblyService} 与独立写出实现各自复制这段骨架和
* {@code value == null ? "" : value}、{@code n * 256} 样板;本类把骨架收敛到一处,
* 表头定义与行映射仍保留在各装配类中(调用方持有 Sheet/Row,本类只提供工作簿生命周期
* 与零件方法),因此日期/数字/特殊样式等模块语义不会被偷偷改掉。
*
* <p>用法:
* <pre>{@code
* try (SxssfWorkbookWriter writer = SxssfWorkbookWriter.open()) {
* Sheet sheet = writer.createSheet("结果", HEADER, new int[]{20, 18, 18});
* for (RowDto item : rows) {
* Row row = sheet.createRow(rowIndex++);
* SxssfWorkbookWriter.setText(row, 0, item.getAsin());
* }
* writer.write(outputXlsx);
* }
* }</pre>
*/
public final class SxssfWorkbookWriter implements AutoCloseable {
/** 各模块历史实现统一的滚动窗口大小(ShopDataCrawl 流式路径的自定义窗口除外)。 */
public static final int DEFAULT_ROW_ACCESS_WINDOW = 200;
private final SXSSFWorkbook workbook;
private CellStyle headerStyle;
private SxssfWorkbookWriter(int rowAccessWindow) {
this.workbook = new SXSSFWorkbook(rowAccessWindow);
this.workbook.setCompressTempFiles(true);
}
public static SxssfWorkbookWriter open() {
return new SxssfWorkbookWriter(DEFAULT_ROW_ACCESS_WINDOW);
}
public static SxssfWorkbookWriter open(int rowAccessWindow) {
return new SxssfWorkbookWriter(rowAccessWindow);
}
public SXSSFWorkbook workbook() {
return workbook;
}
/** 建表 + 第 0 行表头 + 列宽(单位:字符,内部换算 n*256)。columnWidthChars 为 null 时不设列宽。 */
public Sheet createSheet(String sheetName, String[] headers, int[] columnWidthChars) {
Sheet sheet = workbook.createSheet(sheetName);
writeHeaderRow(sheet, headers);
if (columnWidthChars != null) {
applyColumnWidths(sheet, columnWidthChars);
}
return sheet;
}
/** 在 sheet 第 0 行写入表头文字(不做样式;需要表头样式的调用方再逐格 setCellStyle)。 */
public void writeHeaderRow(Sheet sheet, String[] headers) {
Row headerRow = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
headerRow.createCell(i).setCellValue(headers[i]);
}
}
/** 加粗表头样式(与 Publish/SimilarAsin 历史实现一致:bold Font + 空 CellStyle),惰性创建并复用。 */
public CellStyle headerStyle() {
if (headerStyle == null) {
Font font = workbook.createFont();
font.setBold(true);
CellStyle style = workbook.createCellStyle();
style.setFont(font);
headerStyle = style;
}
return headerStyle;
}
/** 写出文件(内部开 FileOutputStream)。失败时抛 IOException,工作簿由 close() 兜底释放。 */
public void write(File output) throws IOException {
try (FileOutputStream fos = new FileOutputStream(output)) {
workbook.write(fos);
}
}
/** 历史骨架的固定收尾:close() 吞异常 + dispose() 释放 SXSSF 临时文件。 */
@Override
public void close() {
try {
workbook.close();
} catch (Exception ignored) {
}
workbook.dispose();
}
// ------------------------------------------------------------------
// 静态零件:空值安全写入与列宽
// ------------------------------------------------------------------
/** null 写成空字符串,其余原样写入(与各模块 safe()/valueOf() 语义一致)。 */
public static void setText(Row row, int column, String value) {
row.createCell(column).setCellValue(value == null ? "" : value);
}
/** 数值单元格:null 写成空字符串,否则写 doubleValue(与 Withdraw writeMoney/CollectData intOrZero 语义一致)。 */
public static void setNumber(Row row, int column, Number value) {
if (value == null) {
row.createCell(column).setCellValue("");
} else {
row.createCell(column).setCellValue(value.doubleValue());
}
}
/** 列宽换算:字符数 * 256。 */
public static int width(int charCount) {
return charCount * 256;
}
/** 按字符数依次设置第 0..n-1 列宽。 */
public static void applyColumnWidths(Sheet sheet, int[] columnWidthChars) {
for (int i = 0; i < columnWidthChars.length; i++) {
sheet.setColumnWidth(i, columnWidthChars[i] * 256);
}
}
}
@@ -1,16 +1,15 @@
package com.nanri.aiimage.modules.collectdata.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.util.SxssfWorkbookWriter;
import com.nanri.aiimage.modules.collectdata.model.dto.CollectDataSummaryRowDto;
import com.nanri.aiimage.modules.collectdata.model.vo.CollectDataResultRowVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
@@ -85,17 +84,14 @@ public class CollectDataExcelAssemblyService {
if (outputXlsx == null) {
throw new BusinessException("生成采集数据 Excel 失败: 输出文件路径为空");
}
SXSSFWorkbook workbook = new SXSSFWorkbook(200);
workbook.setCompressTempFiles(true);
SxssfWorkbookWriter writer = SxssfWorkbookWriter.open();
// 先写目标同目录的临时文件,成功后原子落位:写入或落位失败时目标路径
// 不残留半成品,临时文件由 catch/finally 清理(SXSSF 滚动窗口文件由 dispose 释放)。
File tmpFile = new File(outputXlsx.getAbsolutePath() + ".tmp");
try {
long detailRowCount = writeDetailSheet(workbook, itemsSource);
writeSummarySheet(workbook, summaries, rawItemsSource);
try (FileOutputStream outputStream = new FileOutputStream(tmpFile)) {
workbook.write(outputStream);
}
long detailRowCount = writeDetailSheet(writer, itemsSource);
writeSummarySheet(writer, summaries, rawItemsSource);
writer.write(tmpFile);
Files.move(tmpFile.toPath(), outputXlsx.toPath(),
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
return detailRowCount;
@@ -104,11 +100,7 @@ public class CollectDataExcelAssemblyService {
deleteQuietly(tmpFile);
throw new BusinessException("生成采集数据 Excel 失败: " + ex.getMessage());
} finally {
try {
workbook.close();
} catch (Exception ignored) {
}
workbook.dispose();
writer.close();
deleteQuietly(tmpFile);
}
}
@@ -130,12 +122,10 @@ public class CollectDataExcelAssemblyService {
}
}
private long writeDetailSheet(SXSSFWorkbook workbook, RowBatchSource itemsSource) {
Sheet sheet = workbook.createSheet(SHEET_DETAIL_NAME);
Row headerRow = sheet.createRow(0);
private long writeDetailSheet(SxssfWorkbookWriter writer, RowBatchSource itemsSource) {
Sheet sheet = writer.createSheet(SHEET_DETAIL_NAME, SHEET_DETAIL_HEADER, null);
for (int i = 0; i < SHEET_DETAIL_HEADER.length; i++) {
headerRow.createCell(i).setCellValue(SHEET_DETAIL_HEADER[i]);
sheet.setColumnWidth(i, (i == 3 ? 24 : 18) * 256);
sheet.setColumnWidth(i, SxssfWorkbookWriter.width(i == 3 ? 24 : 18));
}
// state[0]=下一个写入行号,state[1]=已写明细行数(与原 List 路径 rows.size() 对齐)
long[] state = new long[]{1L, 0L};
@@ -149,12 +139,12 @@ public class CollectDataExcelAssemblyService {
continue;
}
Row row = sheet.createRow((int) state[0]++);
row.createCell(0).setCellValue(safe(item.getBrand()));
row.createCell(1).setCellValue(safe(item.getAsin()));
row.createCell(2).setCellValue(safe(item.getPrice()));
row.createCell(3).setCellValue(safe(item.getSellerName()));
row.createCell(4).setCellValue(safe(item.getKeyword()));
row.createCell(5).setCellValue(safe(item.getDeliveryMethod()));
SxssfWorkbookWriter.setText(row, 0, item.getBrand());
SxssfWorkbookWriter.setText(row, 1, item.getAsin());
SxssfWorkbookWriter.setText(row, 2, item.getPrice());
SxssfWorkbookWriter.setText(row, 3, item.getSellerName());
SxssfWorkbookWriter.setText(row, 4, item.getKeyword());
SxssfWorkbookWriter.setText(row, 5, item.getDeliveryMethod());
state[1]++;
}
});
@@ -168,14 +158,12 @@ public class CollectDataExcelAssemblyService {
* - 当 summaries 为空(Python 端尚未接入或解析失败)时,回退到基于 rawItems 自聚合,
* 保证 Excel 不会因迁移过渡期而出现空表。等 Python 端全部接入并稳定后,可移除 fallback 分支。
*/
private void writeSummarySheet(SXSSFWorkbook workbook,
private void writeSummarySheet(SxssfWorkbookWriter writer,
List<CollectDataSummaryRowDto> summaries,
RowBatchSource rawItemsSource) {
Sheet sheet = workbook.createSheet(SHEET_SUMMARY_NAME);
Row headerRow = sheet.createRow(0);
Sheet sheet = writer.createSheet(SHEET_SUMMARY_NAME, SHEET_SUMMARY_HEADER, null);
for (int i = 0; i < SHEET_SUMMARY_HEADER.length; i++) {
headerRow.createCell(i).setCellValue(SHEET_SUMMARY_HEADER[i]);
sheet.setColumnWidth(i, (i == 0 ? 28 : 12) * 256);
sheet.setColumnWidth(i, SxssfWorkbookWriter.width(i == 0 ? 28 : 12));
}
if (summaries != null && !summaries.isEmpty()) {
@@ -186,18 +174,18 @@ public class CollectDataExcelAssemblyService {
continue;
}
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(safe(item.getKeyword()));
row.createCell(1).setCellValue(intOrZero(item.getFba()));
row.createCell(2).setCellValue(intOrZero(item.getFbm()));
row.createCell(3).setCellValue(intOrZero(item.getAmz()));
row.createCell(4).setCellValue(intOrZero(item.getNoneCount()));
SxssfWorkbookWriter.setText(row, 0, item.getKeyword());
SxssfWorkbookWriter.setNumber(row, 1, intOrZero(item.getFba()));
SxssfWorkbookWriter.setNumber(row, 2, intOrZero(item.getFbm()));
SxssfWorkbookWriter.setNumber(row, 3, intOrZero(item.getAmz()));
SxssfWorkbookWriter.setNumber(row, 4, intOrZero(item.getNoneCount()));
Integer totalPage = item.getTotalPage();
if (totalPage != null && totalPage > 0) {
row.createCell(5).setCellValue(totalPage);
SxssfWorkbookWriter.setNumber(row, 5, totalPage);
} else {
row.createCell(5).setCellValue("");
SxssfWorkbookWriter.setText(row, 5, "");
}
row.createCell(6).setCellValue(intOrZero(item.getAsinFilter()));
SxssfWorkbookWriter.setNumber(row, 6, intOrZero(item.getAsinFilter()));
}
return;
}
@@ -40,6 +40,7 @@ import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import com.nanri.aiimage.modules.task.service.TaskFinalizeSupport;
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopMatchResultVo;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopIndexService;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService;
@@ -1260,6 +1261,7 @@ public class DeleteBrandRunService {
}
private FileTaskEntity loadTaskForExecution(Long taskId) {
// DeleteBrandTaskCacheService 未继承 AbstractModuleTaskCacheService,保持本地实现
Map<Long, FileTaskEntity> cachedTasks = deleteBrandTaskCacheService.getTaskCacheBatch(List.of(taskId));
FileTaskEntity cached = cachedTasks.get(taskId);
if (cached != null) {
@@ -1273,16 +1275,7 @@ public class DeleteBrandRunService {
}
private List<FileTaskEntity> selectTasksByIdsInBatches(List<Long> taskIds) {
List<FileTaskEntity> tasks = new ArrayList<>();
if (taskIds == null || taskIds.isEmpty()) {
return tasks;
}
int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize());
for (int start = 0; start < taskIds.size(); start += batchSize) {
int end = Math.min(start + batchSize, taskIds.size());
tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end)));
}
return tasks;
return TaskFinalizeSupport.selectTasksByIdsInBatches(fileTaskMapper, taskPressureProperties, taskIds);
}
private List<FileTaskEntity> selectTaskHistoryFieldsByIdsInBatches(List<Long> taskIds) {
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.patroldelete.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.util.SxssfWorkbookWriter;
import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCartRatioDto;
import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCountryMetricRowDto;
import com.nanri.aiimage.modules.patroldelete.model.dto.PatrolDeleteCountrySectionDto;
@@ -8,11 +9,9 @@ import com.nanri.aiimage.modules.patroldelete.model.vo.PatrolDeleteResultItemVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
@Service
@@ -38,16 +37,23 @@ public class PatrolDeleteExcelAssemblyService {
"国家", "购物车比例",
"国家", "购物车比例"
};
private static final int[] COLUMN_WIDTHS = {
20,
14, 10, 12, 16,
14, 10, 12, 16,
14, 10, 12, 16,
14, 10, 12, 16,
14, 10, 12, 16,
10, 16,
10, 16,
10, 16,
10, 16,
10, 16
};
public void writeWorkbook(File outputXlsx, List<PatrolDeleteResultItemVo> items) {
SXSSFWorkbook workbook = new SXSSFWorkbook(200);
workbook.setCompressTempFiles(true);
try (FileOutputStream outputStream = new FileOutputStream(outputXlsx)) {
Sheet sheet = workbook.createSheet("巡店删除结果");
Row headerRow = sheet.createRow(0);
for (int columnIndex = 0; columnIndex < HEADER.length; columnIndex++) {
headerRow.createCell(columnIndex).setCellValue(HEADER[columnIndex]);
}
try (SxssfWorkbookWriter writer = SxssfWorkbookWriter.open()) {
Sheet sheet = writer.createSheet("巡店删除结果", HEADER, COLUMN_WIDTHS);
int rowIndex = 1;
for (PatrolDeleteResultItemVo item : items) {
@@ -57,7 +63,7 @@ public class PatrolDeleteExcelAssemblyService {
int shopRowCount = maxCountryRowCount(item.getCountrySections());
for (int shopRowIndex = 0; shopRowIndex < shopRowCount; shopRowIndex++) {
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(shopRowIndex == 0 ? safe(item.getShopName()) : "");
SxssfWorkbookWriter.setText(row, 0, shopRowIndex == 0 ? safe(item.getShopName()) : "");
writeCountryColumns(row, 1, findSection(item.getCountrySections(), COUNTRY_DE), shopRowIndex);
writeCountryColumns(row, 5, findSection(item.getCountrySections(), COUNTRY_UK), shopRowIndex);
writeCountryColumns(row, 9, findSection(item.getCountrySections(), COUNTRY_FR), shopRowIndex);
@@ -74,17 +80,10 @@ public class PatrolDeleteExcelAssemblyService {
rowIndex++;
}
applyDefaultColumnWidths(sheet);
workbook.write(outputStream);
writer.write(outputXlsx);
} catch (Exception ex) {
log.warn("[patrol-delete] write workbook failed: {}", ex.getMessage());
throw new BusinessException("generate patrol-delete excel failed: " + ex.getMessage());
} finally {
try {
workbook.close();
} catch (Exception ignored) {
}
workbook.dispose();
}
}
@@ -131,38 +130,19 @@ public class PatrolDeleteExcelAssemblyService {
if (section != null && section.getRows() != null && rowIndex < section.getRows().size()) {
metric = section.getRows().get(rowIndex);
}
row.createCell(startColumn).setCellValue(metric == null ? "" : safe(metric.getStatus()));
row.createCell(startColumn + 1).setCellValue(metric == null ? "" : safe(metric.getQuantity()));
row.createCell(startColumn + 2).setCellValue(metric == null ? "" : safe(metric.getDeleteQuantity()));
row.createCell(startColumn + 3).setCellValue(metric == null ? "" : safe(metric.getProcessStatus()));
SxssfWorkbookWriter.setText(row, startColumn, metric == null ? "" : safe(metric.getStatus()));
SxssfWorkbookWriter.setText(row, startColumn + 1, metric == null ? "" : safe(metric.getQuantity()));
SxssfWorkbookWriter.setText(row, startColumn + 2, metric == null ? "" : safe(metric.getDeleteQuantity()));
SxssfWorkbookWriter.setText(row, startColumn + 3, metric == null ? "" : safe(metric.getProcessStatus()));
}
private void writeCartRatioColumns(Row row, int startColumn, List<PatrolDeleteCartRatioDto> ratios, int ratioIndex) {
PatrolDeleteCartRatioDto ratio = ratios != null && ratioIndex < ratios.size() ? ratios.get(ratioIndex) : null;
row.createCell(startColumn).setCellValue(ratio == null ? "" : safe(ratio.getCountry()));
row.createCell(startColumn + 1).setCellValue(ratio == null ? "" : safe(ratio.getRatio()));
SxssfWorkbookWriter.setText(row, startColumn, ratio == null ? "" : safe(ratio.getCountry()));
SxssfWorkbookWriter.setText(row, startColumn + 1, ratio == null ? "" : safe(ratio.getRatio()));
}
private String safe(String value) {
return value == null ? "" : value;
}
private void applyDefaultColumnWidths(Sheet sheet) {
int[] widths = {
20,
14, 10, 12, 16,
14, 10, 12, 16,
14, 10, 12, 16,
14, 10, 12, 16,
14, 10, 12, 16,
10, 16,
10, 16,
10, 16,
10, 16,
10, 16
};
for (int i = 0; i < widths.length; i++) {
sheet.setColumnWidth(i, widths[i] * 256);
}
}
}
@@ -6,7 +6,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessCodes;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.config.TaskPressureProperties;
@@ -30,6 +29,7 @@ import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import com.nanri.aiimage.modules.task.service.TaskFinalizeSupport;
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
import com.nanri.aiimage.modules.task.service.TaskResultItemService;
import com.nanri.aiimage.modules.task.service.TaskScopePayloadStorageService;
@@ -90,60 +90,15 @@ public class PatrolDeleteTaskService {
private final org.springframework.transaction.PlatformTransactionManager transactionManager;
private FileTaskEntity loadTaskForExecution(Long taskId) {
Map<Long, FileTaskEntity> cachedTasks = taskCacheService.getTaskCacheBatch(List.of(taskId));
FileTaskEntity cached = cachedTasks.get(taskId);
if (cached != null) {
return cached;
}
FileTaskEntity dbTask = fileTaskMapper.selectById(taskId);
if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) {
taskCacheService.saveTaskCache(dbTask);
}
return dbTask;
return TaskFinalizeSupport.loadTaskForExecution(fileTaskMapper, taskCacheService, MODULE_TYPE, taskId);
}
private Map<Long, FileTaskEntity> loadTaskMapByIds(List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalizedTaskIds = taskIds.stream()
.filter(taskId -> taskId != null && taskId > 0)
.distinct()
.toList();
if (normalizedTaskIds.isEmpty()) {
return result;
}
Map<Long, FileTaskEntity> cachedTasks = taskCacheService.getTaskCacheBatch(normalizedTaskIds);
result.putAll(cachedTasks);
List<Long> missingTaskIds = normalizedTaskIds.stream()
.filter(taskId -> !cachedTasks.containsKey(taskId))
.toList();
if (!missingTaskIds.isEmpty()) {
for (FileTaskEntity dbTask : selectTasksByIdsInBatches(missingTaskIds)) {
if (dbTask == null || !MODULE_TYPE.equals(dbTask.getModuleType())) {
continue;
}
result.put(dbTask.getId(), dbTask);
if ("RUNNING".equals(dbTask.getStatus())) {
taskCacheService.saveTaskCache(dbTask);
}
}
}
return result;
return TaskFinalizeSupport.loadTaskMapByIds(fileTaskMapper, taskPressureProperties, taskCacheService, MODULE_TYPE, taskIds);
}
private List<FileTaskEntity> selectTasksByIdsInBatches(List<Long> taskIds) {
List<FileTaskEntity> tasks = new ArrayList<>();
if (taskIds == null || taskIds.isEmpty()) {
return tasks;
}
int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize());
for (int start = 0; start < taskIds.size(); start += batchSize) {
int end = Math.min(start + batchSize, taskIds.size());
tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end)));
}
return tasks;
return TaskFinalizeSupport.selectTasksByIdsInBatches(fileTaskMapper, taskPressureProperties, taskIds);
}
public ProductRiskDashboardVo dashboard(Long userId) {
@@ -1099,19 +1054,11 @@ public class PatrolDeleteTaskService {
}
private TaskDistributedLockService.LockHandle acquireTaskLockOrThrow(Long taskId) {
TaskDistributedLockService.LockHandle lockHandle = acquireTaskLock(taskId);
if (lockHandle == null) {
log.warn("[patrol-delete] 任务锁竞争,拒绝本次提交 taskId={}", taskId);
throw new BusinessException(BusinessCodes.TASK_BUSY, "任务正在处理中,请稍后再试");
}
return lockHandle;
return TaskFinalizeSupport.acquireTaskLockOrThrow(taskDistributedLockService, MODULE_TYPE, taskId, "patrol-delete");
}
private TaskDistributedLockService.LockHandle acquireTaskLock(Long taskId) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(MODULE_TYPE, taskId);
return TaskFinalizeSupport.acquireTaskLock(taskDistributedLockService, MODULE_TYPE, taskId);
}
/**
@@ -1134,14 +1081,7 @@ public class PatrolDeleteTaskService {
}
private void markResultFailed(FileResultEntity row, String message) {
row.setSuccess(RESULT_FAILED);
row.setErrorMessage(blankToNull(message));
row.setResultFilename(null);
row.setResultFileUrl(null);
row.setResultFileSize(0L);
row.setResultContentType(null);
row.setRowCount(0);
fileResultMapper.updateById(row);
TaskFinalizeSupport.markResultFailed(fileResultMapper, row, blankToNull(message));
}
private boolean isResultFinished(FileResultEntity row) {
@@ -1,16 +1,15 @@
package com.nanri.aiimage.modules.pricetrack.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.util.SxssfWorkbookWriter;
import com.nanri.aiimage.modules.pricetrack.model.dto.PriceTrackSubmitResultRequest;
import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -36,20 +35,15 @@ public class PriceTrackExcelAssemblyService {
"修改次数",
"状态"
};
private static final int[] RESULT_COLUMN_WIDTHS = {22, 18, 12, 12, 12, 12, 20, 22, 20, 22, 22, 14, 18, 12, 12};
public void writeWorkbook(File outputXlsx, Map<String, List<PriceTrackSubmitResultRequest.AsinResult>> countries) {
Map<String, List<PriceTrackSubmitResultRequest.AsinResult>> safe = countries == null ? Map.of() : countries;
SXSSFWorkbook workbook = new SXSSFWorkbook(200);
workbook.setCompressTempFiles(true);
try (FileOutputStream fos = new FileOutputStream(outputXlsx)) {
try (SxssfWorkbookWriter writer = SxssfWorkbookWriter.open()) {
for (ProductRiskCountryCode code : ProductRiskCountryCode.values()) {
String countryCode = code.name();
String sheetName = sheetNameOf(code);
Sheet sheet = workbook.createSheet(sheetName);
Row headerRow = sheet.createRow(0);
for (int i = 0; i < RESULT_HEADER.length; i++) {
headerRow.createCell(i).setCellValue(RESULT_HEADER[i]);
}
Sheet sheet = writer.createSheet(sheetName, RESULT_HEADER, RESULT_COLUMN_WIDTHS);
List<PriceTrackSubmitResultRequest.AsinResult> rows = safe.getOrDefault(countryCode, List.of());
int rowIdx = 1;
for (PriceTrackSubmitResultRequest.AsinResult item : rows) {
@@ -57,34 +51,27 @@ public class PriceTrackExcelAssemblyService {
continue;
}
Row row = sheet.createRow(rowIdx++);
row.createCell(0).setCellValue(valueOf(item.getShopMallName()));
row.createCell(1).setCellValue(valueOf(item.getAsin()));
row.createCell(2).setCellValue(valueOf(item.getPrice()));
row.createCell(3).setCellValue(valueOf(item.getRecommendedPrice()));
row.createCell(4).setCellValue(valueOf(item.getShippingFee()));
row.createCell(5).setCellValue(valueOf(item.getMinimumPrice()));
row.createCell(6).setCellValue(valueOf(item.getFirstPlace()));
row.createCell(7).setCellValue(valueOf(item.getFirstShop()));
row.createCell(8).setCellValue(valueOf(item.getSecondPlace()));
row.createCell(9).setCellValue(valueOf(item.getSecondShop()));
row.createCell(10).setCellValue(valueOf(item.getCartShopName()));
row.createCell(11).setCellValue(valueOf(item.getPriceChangePrice()));
row.createCell(12).setCellValue(valueOf(item.getPriceChangeStatus()));
row.createCell(13).setCellValue(valueOf(item.getModifyCount()));
row.createCell(14).setCellValue(valueOf(item.getStatus()));
SxssfWorkbookWriter.setText(row, 0, item.getShopMallName());
SxssfWorkbookWriter.setText(row, 1, item.getAsin());
SxssfWorkbookWriter.setText(row, 2, item.getPrice());
SxssfWorkbookWriter.setText(row, 3, item.getRecommendedPrice());
SxssfWorkbookWriter.setText(row, 4, item.getShippingFee());
SxssfWorkbookWriter.setText(row, 5, item.getMinimumPrice());
SxssfWorkbookWriter.setText(row, 6, item.getFirstPlace());
SxssfWorkbookWriter.setText(row, 7, item.getFirstShop());
SxssfWorkbookWriter.setText(row, 8, item.getSecondPlace());
SxssfWorkbookWriter.setText(row, 9, item.getSecondShop());
SxssfWorkbookWriter.setText(row, 10, item.getCartShopName());
SxssfWorkbookWriter.setText(row, 11, item.getPriceChangePrice());
SxssfWorkbookWriter.setText(row, 12, item.getPriceChangeStatus());
SxssfWorkbookWriter.setText(row, 13, item.getModifyCount());
SxssfWorkbookWriter.setText(row, 14, item.getStatus());
}
applyDefaultColumnWidths(sheet);
}
workbook.write(fos);
writer.write(outputXlsx);
} catch (Exception ex) {
log.warn("[price-track] write workbook failed: {}", ex.getMessage());
throw new BusinessException("generate price-track excel failed: " + ex.getMessage());
} finally {
try {
workbook.close();
} catch (Exception ignored) {
}
workbook.dispose();
}
}
@@ -116,10 +103,6 @@ public class PriceTrackExcelAssemblyService {
return out;
}
private static String valueOf(String value) {
return value == null ? "" : value;
}
private static String sheetNameOf(ProductRiskCountryCode code) {
return switch (code) {
case DE -> "德国";
@@ -129,11 +112,4 @@ public class PriceTrackExcelAssemblyService {
case UK -> "英国";
};
}
private void applyDefaultColumnWidths(Sheet sheet) {
int[] widths = {22, 18, 12, 12, 12, 12, 20, 22, 20, 22, 22, 14, 18, 12, 12};
for (int i = 0; i < widths.length; i++) {
sheet.setColumnWidth(i, widths[i] * 256);
}
}
}
@@ -3,7 +3,6 @@ package com.nanri.aiimage.modules.pricetrack.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessCodes;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.util.ExcelStreamReader;
@@ -33,6 +32,7 @@ import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import com.nanri.aiimage.modules.task.service.TaskFinalizeSupport;
import com.nanri.aiimage.modules.task.service.TaskResultPayloadService;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService;
import cn.hutool.core.util.IdUtil;
@@ -103,58 +103,17 @@ public class PriceTrackTaskService {
private final org.springframework.transaction.PlatformTransactionManager transactionManager;
private FileTaskEntity loadTaskForExecution(Long taskId) {
Map<Long, FileTaskEntity> cachedTasks = priceTrackTaskCacheService.getTaskCacheBatch(List.of(taskId));
FileTaskEntity cached = cachedTasks.get(taskId);
if (cached != null && hasText(cached.getRequestJson())) {
return cached;
}
FileTaskEntity dbTask = fileTaskMapper.selectById(taskId);
if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) {
priceTrackTaskCacheService.saveTaskCache(dbTask);
}
return dbTask;
// 缓存行仅当 requestJson 非空时视为可用(历史语义),否则回落 DB
return TaskFinalizeSupport.loadTaskForExecution(fileTaskMapper, priceTrackTaskCacheService, MODULE_TYPE, taskId,
cached -> cached != null && hasText(cached.getRequestJson()));
}
private Map<Long, FileTaskEntity> loadTaskMapByIds(List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalizedTaskIds = taskIds.stream()
.filter(taskId -> taskId != null && taskId > 0)
.distinct()
.toList();
if (normalizedTaskIds.isEmpty()) {
return result;
}
Map<Long, FileTaskEntity> cachedTasks = priceTrackTaskCacheService.getTaskCacheBatch(normalizedTaskIds);
result.putAll(cachedTasks);
List<Long> missingTaskIds = normalizedTaskIds.stream()
.filter(taskId -> !cachedTasks.containsKey(taskId))
.toList();
for (FileTaskEntity dbTask : selectTasksByIdsInBatches(missingTaskIds)) {
if (dbTask == null || !MODULE_TYPE.equals(dbTask.getModuleType())) {
continue;
}
result.put(dbTask.getId(), dbTask);
if ("RUNNING".equals(dbTask.getStatus())) {
priceTrackTaskCacheService.saveTaskCache(dbTask);
}
}
return result;
return TaskFinalizeSupport.loadTaskMapByIds(fileTaskMapper, taskPressureProperties, priceTrackTaskCacheService, MODULE_TYPE, taskIds);
}
private List<FileTaskEntity> selectTasksByIdsInBatches(List<Long> taskIds) {
List<FileTaskEntity> tasks = new ArrayList<>();
if (taskIds == null || taskIds.isEmpty()) {
return tasks;
}
int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize());
for (int start = 0; start < taskIds.size(); start += batchSize) {
int end = Math.min(start + batchSize, taskIds.size());
tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end)));
}
return tasks;
return TaskFinalizeSupport.selectTasksByIdsInBatches(fileTaskMapper, taskPressureProperties, taskIds);
}
private List<FileTaskEntity> selectTaskHistoryFieldsByIdsInBatches(List<Long> taskIds) {
@@ -849,14 +808,7 @@ public class PriceTrackTaskService {
}
private void markResultFailed(FileResultEntity fr, String message) {
fr.setSuccess(0);
fr.setErrorMessage(message);
fr.setResultFilename(null);
fr.setResultFileUrl(null);
fr.setResultFileSize(0L);
fr.setRowCount(0);
fr.setResultContentType(null);
fileResultMapper.updateById(fr);
TaskFinalizeSupport.markResultFailed(fileResultMapper, fr, message);
}
private void assembleShopResult(FileResultEntity result,
@@ -2294,12 +2246,7 @@ public class PriceTrackTaskService {
}
private TaskDistributedLockService.LockHandle acquireTaskLockOrThrow(Long taskId) {
TaskDistributedLockService.LockHandle lockHandle = acquireTaskLock(taskId);
if (lockHandle == null) {
log.warn("[price-track] 任务锁竞争,拒绝本次提交 taskId={}", taskId);
throw new BusinessException(BusinessCodes.TASK_BUSY, "任务正在处理中,请稍后再试");
}
return lockHandle;
return TaskFinalizeSupport.acquireTaskLockOrThrow(taskDistributedLockService, MODULE_TYPE, taskId, "price-track");
}
/**
@@ -2317,10 +2264,7 @@ public class PriceTrackTaskService {
}
private TaskDistributedLockService.LockHandle acquireTaskLock(Long taskId) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(MODULE_TYPE, taskId);
return TaskFinalizeSupport.acquireTaskLock(taskDistributedLockService, MODULE_TYPE, taskId);
}
private static boolean hasText(String value) {
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.productrisk.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.util.SxssfWorkbookWriter;
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskRowDto;
import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode;
import lombok.extern.slf4j.Slf4j;
@@ -16,7 +17,6 @@ import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -44,15 +44,10 @@ public class ProductRiskExcelAssemblyService {
public void writeWorkbook(File outputXlsx, String shopDisplayName, Map<String, List<ProductRiskRowDto>> countries) {
Map<String, List<ProductRiskRowDto>> safe = countries == null ? Map.of() : countries;
SXSSFWorkbook workbook = new SXSSFWorkbook(200);
workbook.setCompressTempFiles(true);
try (FileOutputStream fos = new FileOutputStream(outputXlsx)) {
try (SxssfWorkbookWriter writer = SxssfWorkbookWriter.open()) {
SXSSFWorkbook workbook = writer.workbook();
for (String sheetName : SHEET_ORDER) {
Sheet sheet = workbook.createSheet(sheetName);
Row headerRow = sheet.createRow(0);
for (int c = 0; c < HEADER.length; c++) {
headerRow.createCell(c).setCellValue(HEADER[c]);
}
Sheet sheet = writer.createSheet(sheetName, HEADER, null);
List<ProductRiskRowDto> rows = safe.getOrDefault(sheetName, List.of());
CellStyle detailStyle = createDetailStyle(workbook);
CellStyle detailAsinStyle = createDetailAsinStyle(workbook);
@@ -66,12 +61,12 @@ public class ProductRiskExcelAssemblyService {
for (int offset = 0; offset < groupSize; offset++) {
Row row = sheet.createRow(rowIdx + offset);
if (offset == 0) {
createTextCell(row, 0, firstNonBlank(dto == null ? null : dto.getShopName(), shopDisplayName));
createTextCell(row, 1, dto == null ? null : dto.getProductAsinSku());
createTextCell(row, 2, dto == null ? null : dto.getStatus());
createTextCell(row, 3, completionDisplayValue(dto));
createTextCell(row, 4, dto == null ? null : dto.getRemoveAsin());
createTextCell(row, 5, dto == null ? null : dto.getRemoveStatus());
SxssfWorkbookWriter.setText(row, 0, firstNonBlank(dto == null ? null : dto.getShopName(), shopDisplayName));
SxssfWorkbookWriter.setText(row, 1, dto == null ? null : dto.getProductAsinSku());
SxssfWorkbookWriter.setText(row, 2, dto == null ? null : dto.getStatus());
SxssfWorkbookWriter.setText(row, 3, completionDisplayValue(dto));
SxssfWorkbookWriter.setText(row, 4, dto == null ? null : dto.getRemoveAsin());
SxssfWorkbookWriter.setText(row, 5, dto == null ? null : dto.getRemoveStatus());
}
if (offset < details.size()) {
ProductRiskRowDto.ViolationDetail detail = details.get(offset);
@@ -85,21 +80,15 @@ public class ProductRiskExcelAssemblyService {
}
rowIdx += groupSize;
}
applyDetailHeaderStyle(headerRow, detailStyle);
applyDetailHeaderStyle(sheet.getRow(0), detailStyle);
applyDefaultColumnWidths(sheet);
}
workbook.write(fos);
writer.write(outputXlsx);
} catch (BusinessException ex) {
throw ex;
} catch (Exception ex) {
log.warn("[product-risk] write workbook failed: {}", ex.getMessage());
throw new BusinessException("generate product-risk excel failed: " + ex.getMessage());
} finally {
try {
workbook.close();
} catch (Exception ignored) {
}
workbook.dispose();
}
}
@@ -130,11 +119,6 @@ public class ProductRiskExcelAssemblyService {
return out;
}
private static void createTextCell(Row row, int index, String value) {
Cell cell = row.createCell(index);
cell.setCellValue(value == null ? "" : value);
}
private static void createStyledTextCell(Row row, int index, String value, CellStyle style) {
Cell cell = row.createCell(index);
cell.setCellValue(value == null ? "" : value);
@@ -2,7 +2,6 @@ package com.nanri.aiimage.modules.productrisk.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessCodes;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.config.TaskPressureProperties;
@@ -29,6 +28,7 @@ import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import com.nanri.aiimage.modules.task.service.TaskFinalizeSupport;
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
import com.nanri.aiimage.modules.task.service.TaskResultItemService;
import com.nanri.aiimage.modules.task.service.TaskResultPayloadService;
@@ -91,58 +91,15 @@ public class ProductRiskTaskService {
private final org.springframework.transaction.PlatformTransactionManager transactionManager;
private FileTaskEntity loadTaskForExecution(Long taskId) {
Map<Long, FileTaskEntity> cachedTasks = productRiskTaskCacheService.getTaskCacheBatch(List.of(taskId));
FileTaskEntity cached = cachedTasks.get(taskId);
if (cached != null) {
return cached;
}
FileTaskEntity dbTask = fileTaskMapper.selectById(taskId);
if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) {
productRiskTaskCacheService.saveTaskCache(dbTask);
}
return dbTask;
return TaskFinalizeSupport.loadTaskForExecution(fileTaskMapper, productRiskTaskCacheService, MODULE_TYPE, taskId);
}
private Map<Long, FileTaskEntity> loadTaskMapByIds(List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalizedTaskIds = taskIds.stream()
.filter(taskId -> taskId != null && taskId > 0)
.distinct()
.toList();
if (normalizedTaskIds.isEmpty()) {
return result;
}
Map<Long, FileTaskEntity> cachedTasks = productRiskTaskCacheService.getTaskCacheBatch(normalizedTaskIds);
result.putAll(cachedTasks);
List<Long> missingTaskIds = normalizedTaskIds.stream()
.filter(taskId -> !cachedTasks.containsKey(taskId))
.toList();
for (FileTaskEntity dbTask : selectTasksByIdsInBatches(missingTaskIds)) {
if (dbTask == null || !MODULE_TYPE.equals(dbTask.getModuleType())) {
continue;
}
result.put(dbTask.getId(), dbTask);
if ("RUNNING".equals(dbTask.getStatus())) {
productRiskTaskCacheService.saveTaskCache(dbTask);
}
}
return result;
return TaskFinalizeSupport.loadTaskMapByIds(fileTaskMapper, taskPressureProperties, productRiskTaskCacheService, MODULE_TYPE, taskIds);
}
private List<FileTaskEntity> selectTasksByIdsInBatches(List<Long> taskIds) {
List<FileTaskEntity> tasks = new ArrayList<>();
if (taskIds == null || taskIds.isEmpty()) {
return tasks;
}
int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize());
for (int start = 0; start < taskIds.size(); start += batchSize) {
int end = Math.min(start + batchSize, taskIds.size());
tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end)));
}
return tasks;
return TaskFinalizeSupport.selectTasksByIdsInBatches(fileTaskMapper, taskPressureProperties, taskIds);
}
private List<FileTaskEntity> selectTaskHistoryFieldsByIdsInBatches(List<Long> taskIds) {
@@ -839,14 +796,7 @@ public class ProductRiskTaskService {
}
private void markResultFailed(FileResultEntity fr, String message) {
fr.setSuccess(0);
fr.setErrorMessage(message);
fr.setResultFilename(null);
fr.setResultFileUrl(null);
fr.setResultFileSize(0L);
fr.setRowCount(0);
fr.setResultContentType(null);
fileResultMapper.updateById(fr);
TaskFinalizeSupport.markResultFailed(fileResultMapper, fr, message);
}
private void markResultNoData(FileResultEntity fr, String message) {
@@ -1100,19 +1050,11 @@ public class ProductRiskTaskService {
}
private TaskDistributedLockService.LockHandle acquireTaskLockOrThrow(Long taskId) {
TaskDistributedLockService.LockHandle lockHandle = acquireTaskLock(taskId);
if (lockHandle == null) {
log.warn("[product-risk] 任务锁竞争,拒绝本次提交 taskId={}", taskId);
throw new BusinessException(BusinessCodes.TASK_BUSY, "任务正在处理中,请稍后再试");
}
return lockHandle;
return TaskFinalizeSupport.acquireTaskLockOrThrow(taskDistributedLockService, MODULE_TYPE, taskId, "product-risk");
}
private TaskDistributedLockService.LockHandle acquireTaskLock(Long taskId) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(MODULE_TYPE, taskId);
return TaskFinalizeSupport.acquireTaskLock(taskDistributedLockService, MODULE_TYPE, taskId);
}
/**
@@ -2,15 +2,13 @@ package com.nanri.aiimage.modules.publish.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.util.ExcelStreamReader;
import com.nanri.aiimage.common.util.SxssfWorkbookWriter;
import com.nanri.aiimage.modules.publish.model.dto.PublishRowDto;
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 org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import java.io.File;
@@ -96,27 +94,19 @@ public class PublishWorkbookService {
if (parent != null) {
parent.mkdirs();
}
SXSSFWorkbook workbook = new SXSSFWorkbook(200);
workbook.setCompressTempFiles(true);
try (FileOutputStream output = new FileOutputStream(outputFile)) {
CellStyle headerStyle = createHeaderStyle(workbook);
try (SxssfWorkbookWriter writer = SxssfWorkbookWriter.open()) {
CellStyle headerStyle = writer.headerStyle();
String country = countrySheetName(publishCountry);
// 工作表名必须是 Excel 合法名(≤31 字符、不含 []:*?/\ ),而 country 来自接口参数,
// 用户填了长文本/特殊字符时直接 createSheet 会抛异常导致"生成上架结果 Excel 失败"。
// uniqueSheetName 是项目内已有的净化样板(dedupe 同款 WorkbookUtil.createSafeSheetName),
// 此处只净化工作表名;单元格里的国家值仍按原口径写入。
Sheet sheet = workbook.createSheet(uniqueSheetName(country, new LinkedHashSet<>()));
Sheet sheet = writer.workbook().createSheet(uniqueSheetName(country, new LinkedHashSet<>()));
writeSheet(sheet, rows, headerStyle, country);
workbook.write(output);
writer.write(outputFile);
return outputFile;
} catch (Exception ex) {
throw new BusinessException("生成上架结果 Excel 失败: " + safeMessage(ex));
} finally {
workbook.dispose();
try {
workbook.close();
} catch (Exception ignored) {
}
}
}
@@ -228,20 +218,10 @@ public class PublishWorkbookService {
setText(row, 7, value.getSyncCountries());
}
int[] widths = {14, 18, 12, 24, 12, 14, 14, 28};
for (int index = 0; index < widths.length; index++) {
sheet.setColumnWidth(index, widths[index] * 256);
}
SxssfWorkbookWriter.applyColumnWidths(sheet, widths);
sheet.createFreezePane(0, 1);
}
private CellStyle createHeaderStyle(Workbook workbook) {
Font font = workbook.createFont();
font.setBold(true);
CellStyle style = workbook.createCellStyle();
style.setFont(font);
return style;
}
private void setText(Row row, int columnIndex, String value) {
row.createCell(columnIndex).setCellValue(value == null ? "" : value);
}
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.queryasin.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.util.SxssfWorkbookWriter;
import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinAsinStatusDto;
import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinCountryAsinsDto;
import com.nanri.aiimage.modules.queryasin.model.dto.QueryAsinCountryResultDto;
@@ -8,11 +9,9 @@ import com.nanri.aiimage.modules.queryasin.model.vo.QueryAsinResultItemVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@@ -40,14 +39,10 @@ public class QueryAsinExcelAssemblyService {
};
public void writeWorkbook(File outputXlsx, List<QueryAsinResultItemVo> items) {
SXSSFWorkbook workbook = new SXSSFWorkbook(200);
workbook.setCompressTempFiles(true);
try (FileOutputStream outputStream = new FileOutputStream(outputXlsx)) {
Sheet sheet = workbook.createSheet("查询ASIN结果");
Row headerRow = sheet.createRow(0);
try (SxssfWorkbookWriter writer = SxssfWorkbookWriter.open()) {
Sheet sheet = writer.createSheet("查询ASIN结果", HEADER, null);
for (int columnIndex = 0; columnIndex < HEADER.length; columnIndex++) {
headerRow.createCell(columnIndex).setCellValue(HEADER[columnIndex]);
sheet.setColumnWidth(columnIndex, (columnIndex == 0 ? 22 : 18) * 256);
sheet.setColumnWidth(columnIndex, SxssfWorkbookWriter.width(columnIndex == 0 ? 22 : 18));
}
int rowIndex = 1;
@@ -59,27 +54,21 @@ public class QueryAsinExcelAssemblyService {
int maxRows = maxRows(resultsByCountry);
for (int i = 0; i < maxRows; i++) {
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(i == 0 ? safe(item.getShopName()) : "");
SxssfWorkbookWriter.setText(row, 0, i == 0 ? safe(item.getShopName()) : "");
for (int c = 0; c < COUNTRIES.size(); c++) {
List<QueryAsinAsinStatusDto> rows = resultsByCountry.get(COUNTRIES.get(c));
QueryAsinAsinStatusDto result = rows != null && i < rows.size() ? rows.get(i) : null;
int asinColumn = 1 + c * 2;
row.createCell(asinColumn).setCellValue(result == null ? "" : safe(result.getAsin()));
row.createCell(asinColumn + 1).setCellValue(result == null ? "" : safe(result.getStatus()));
SxssfWorkbookWriter.setText(row, asinColumn, result == null ? "" : safe(result.getAsin()));
SxssfWorkbookWriter.setText(row, asinColumn + 1, result == null ? "" : safe(result.getStatus()));
}
}
rowIndex++;
}
workbook.write(outputStream);
writer.write(outputXlsx);
} catch (Exception ex) {
log.warn("[query-asin] write workbook failed: {}", ex.getMessage());
throw new BusinessException("生成查询 ASIN Excel 失败: " + ex.getMessage());
} finally {
try {
workbook.close();
} catch (Exception ignored) {
}
workbook.dispose();
}
}
@@ -6,7 +6,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessCodes;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.config.TaskPressureProperties;
@@ -30,6 +29,7 @@ import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import com.nanri.aiimage.modules.task.service.TaskFinalizeSupport;
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
import com.nanri.aiimage.modules.task.service.TaskResultItemService;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService;
@@ -101,60 +101,15 @@ public class QueryAsinTaskService {
private final org.springframework.transaction.PlatformTransactionManager transactionManager;
private FileTaskEntity loadTaskForExecution(Long taskId) {
Map<Long, FileTaskEntity> cachedTasks = taskCacheService.getTaskCacheBatch(List.of(taskId));
FileTaskEntity cached = cachedTasks.get(taskId);
if (cached != null) {
return cached;
}
FileTaskEntity dbTask = fileTaskMapper.selectById(taskId);
if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) {
taskCacheService.saveTaskCache(dbTask);
}
return dbTask;
return TaskFinalizeSupport.loadTaskForExecution(fileTaskMapper, taskCacheService, MODULE_TYPE, taskId);
}
private Map<Long, FileTaskEntity> loadTaskMapByIds(List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalizedTaskIds = taskIds.stream()
.filter(taskId -> taskId != null && taskId > 0)
.distinct()
.toList();
if (normalizedTaskIds.isEmpty()) {
return result;
}
Map<Long, FileTaskEntity> cachedTasks = taskCacheService.getTaskCacheBatch(normalizedTaskIds);
result.putAll(cachedTasks);
List<Long> missingTaskIds = normalizedTaskIds.stream()
.filter(taskId -> !cachedTasks.containsKey(taskId))
.toList();
if (!missingTaskIds.isEmpty()) {
for (FileTaskEntity dbTask : selectTasksByIdsInBatches(missingTaskIds)) {
if (dbTask == null || !MODULE_TYPE.equals(dbTask.getModuleType())) {
continue;
}
result.put(dbTask.getId(), dbTask);
if ("RUNNING".equals(dbTask.getStatus())) {
taskCacheService.saveTaskCache(dbTask);
}
}
}
return result;
return TaskFinalizeSupport.loadTaskMapByIds(fileTaskMapper, taskPressureProperties, taskCacheService, MODULE_TYPE, taskIds);
}
private List<FileTaskEntity> selectTasksByIdsInBatches(List<Long> taskIds) {
List<FileTaskEntity> tasks = new ArrayList<>();
if (taskIds == null || taskIds.isEmpty()) {
return tasks;
}
int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize());
for (int start = 0; start < taskIds.size(); start += batchSize) {
int end = Math.min(start + batchSize, taskIds.size());
tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end)));
}
return tasks;
return TaskFinalizeSupport.selectTasksByIdsInBatches(fileTaskMapper, taskPressureProperties, taskIds);
}
public ProductRiskDashboardVo dashboard(Long userId) {
@@ -1075,19 +1030,11 @@ public class QueryAsinTaskService {
}
private TaskDistributedLockService.LockHandle acquireTaskLockOrThrow(Long taskId) {
TaskDistributedLockService.LockHandle lockHandle = acquireTaskLock(taskId);
if (lockHandle == null) {
log.warn("[query-asin] 任务锁竞争,拒绝本次提交 taskId={}", taskId);
throw new BusinessException(BusinessCodes.TASK_BUSY, "任务正在处理中,请稍后再试");
}
return lockHandle;
return TaskFinalizeSupport.acquireTaskLockOrThrow(taskDistributedLockService, MODULE_TYPE, taskId, "query-asin");
}
private TaskDistributedLockService.LockHandle acquireTaskLock(Long taskId) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(MODULE_TYPE, taskId);
return TaskFinalizeSupport.acquireTaskLock(taskDistributedLockService, MODULE_TYPE, taskId);
}
/**
@@ -1110,14 +1057,7 @@ public class QueryAsinTaskService {
}
private void markResultFailed(FileResultEntity row, String message) {
row.setSuccess(RESULT_FAILED);
row.setErrorMessage(blankToNull(message));
row.setResultFilename(null);
row.setResultFileUrl(null);
row.setResultFileSize(0L);
row.setResultContentType(null);
row.setRowCount(0);
fileResultMapper.updateById(row);
TaskFinalizeSupport.markResultFailed(fileResultMapper, row, blankToNull(message));
}
private boolean isResultFinished(FileResultEntity row) {
@@ -42,6 +42,7 @@ import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity;
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import com.nanri.aiimage.modules.task.service.TaskFinalizeSupport;
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
import com.nanri.aiimage.modules.task.service.TaskResultItemService;
import com.nanri.aiimage.modules.task.service.TransientPayloadStorageService;
@@ -288,60 +289,15 @@ public class ShopDataCrawlTaskService {
}
private FileTaskEntity loadTaskForExecution(Long taskId) {
Map<Long, FileTaskEntity> cachedTasks = taskCacheService.getTaskCacheBatch(List.of(taskId));
FileTaskEntity cached = cachedTasks.get(taskId);
if (cached != null) {
return cached;
}
FileTaskEntity dbTask = fileTaskMapper.selectById(taskId);
if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) {
taskCacheService.saveTaskCache(dbTask);
}
return dbTask;
return TaskFinalizeSupport.loadTaskForExecution(fileTaskMapper, taskCacheService, MODULE_TYPE, taskId);
}
private Map<Long, FileTaskEntity> loadTaskMapByIds(List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
if (taskIds == null || taskIds.isEmpty()) {
return result;
}
List<Long> normalizedTaskIds = taskIds.stream()
.filter(taskId -> taskId != null && taskId > 0)
.distinct()
.toList();
if (normalizedTaskIds.isEmpty()) {
return result;
}
Map<Long, FileTaskEntity> cachedTasks = taskCacheService.getTaskCacheBatch(normalizedTaskIds);
result.putAll(cachedTasks);
List<Long> missingTaskIds = normalizedTaskIds.stream()
.filter(taskId -> !cachedTasks.containsKey(taskId))
.toList();
if (!missingTaskIds.isEmpty()) {
for (FileTaskEntity dbTask : selectTasksByIdsInBatches(missingTaskIds)) {
if (dbTask == null || !MODULE_TYPE.equals(dbTask.getModuleType())) {
continue;
}
result.put(dbTask.getId(), dbTask);
if ("RUNNING".equals(dbTask.getStatus())) {
taskCacheService.saveTaskCache(dbTask);
}
}
}
return result;
return TaskFinalizeSupport.loadTaskMapByIds(fileTaskMapper, taskPressureProperties, taskCacheService, MODULE_TYPE, taskIds);
}
private List<FileTaskEntity> selectTasksByIdsInBatches(List<Long> taskIds) {
List<FileTaskEntity> tasks = new ArrayList<>();
if (taskIds == null || taskIds.isEmpty()) {
return tasks;
}
int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize());
for (int start = 0; start < taskIds.size(); start += batchSize) {
int end = Math.min(start + batchSize, taskIds.size());
tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end)));
}
return tasks;
return TaskFinalizeSupport.selectTasksByIdsInBatches(fileTaskMapper, taskPressureProperties, taskIds);
}
public ProductRiskDashboardVo dashboard(Long userId) {
@@ -2866,26 +2822,15 @@ public class ShopDataCrawlTaskService {
}
private TaskDistributedLockService.LockHandle acquireTaskLockOrThrow(Long taskId) {
TaskDistributedLockService.LockHandle lockHandle = acquireTaskLock(taskId);
if (lockHandle == null) {
log.warn("[shop-data-crawl] 任务锁竞争,拒绝本次提交 taskId={}", taskId);
throw new BusinessException(BusinessCodes.TASK_BUSY, "任务正在处理中,请稍后再试");
}
return lockHandle;
return TaskFinalizeSupport.acquireTaskLockOrThrow(taskDistributedLockService, MODULE_TYPE, taskId, "shop-data-crawl");
}
private TaskDistributedLockService.LockHandle acquireTaskLock(Long taskId) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(MODULE_TYPE, taskId);
return TaskFinalizeSupport.acquireTaskLock(taskDistributedLockService, MODULE_TYPE, taskId);
}
private TaskDistributedLockService.LockHandle acquireTaskLock(Long taskId, long waitMillis) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(MODULE_TYPE, taskId, waitMillis);
return TaskFinalizeSupport.acquireTaskLock(taskDistributedLockService, MODULE_TYPE, taskId, waitMillis);
}
private void markResultSuccess(FileResultEntity row) {
@@ -2901,14 +2846,7 @@ public class ShopDataCrawlTaskService {
}
private void markResultFailed(FileResultEntity row, String message) {
row.setSuccess(RESULT_FAILED);
row.setErrorMessage(blankToNull(message));
row.setResultFilename(null);
row.setResultFileUrl(null);
row.setResultFileSize(0L);
row.setResultContentType(null);
row.setRowCount(0);
fileResultMapper.updateById(row);
TaskFinalizeSupport.markResultFailed(fileResultMapper, row, blankToNull(message));
}
private boolean isResultFinished(FileResultEntity row) {
@@ -1,16 +1,15 @@
package com.nanri.aiimage.modules.shopmatch.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.util.SxssfWorkbookWriter;
import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode;
import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchRowDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -19,20 +18,12 @@ import java.util.Map;
@Slf4j
public class ShopMatchExcelAssemblyService {
private static final String[] HEADER = {"ASIN", "状态"};
public void writeWorkbook(File outputXlsx, Map<String, List<ShopMatchRowDto>> countries) {
Map<String, List<ShopMatchRowDto>> safe = countries == null ? Map.of() : countries;
SXSSFWorkbook workbook = new SXSSFWorkbook(200);
workbook.setCompressTempFiles(true);
try (FileOutputStream fos = new FileOutputStream(outputXlsx)) {
try (SxssfWorkbookWriter writer = SxssfWorkbookWriter.open()) {
for (ProductRiskCountryCode code : ProductRiskCountryCode.values()) {
String sheetName = code.getSheetName();
Sheet sheet = workbook.createSheet(sheetName);
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("ASIN");
header.createCell(1).setCellValue("最低价");
header.createCell(2).setCellValue("状态");
Sheet sheet = writer.createSheet(sheetName, new String[]{"ASIN", "最低价", "状态"}, new int[]{20, 18, 18});
List<ShopMatchRowDto> rows = safe.getOrDefault(sheetName, List.of());
int rowIndex = 1;
for (ShopMatchRowDto item : rows) {
@@ -40,24 +31,15 @@ public class ShopMatchExcelAssemblyService {
continue;
}
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(item.getAsin() == null ? "" : item.getAsin());
row.createCell(1).setCellValue(item.getMinimumPrice() == null ? "" : item.getMinimumPrice());
row.createCell(2).setCellValue(item.getStatus() == null ? "" : item.getStatus());
SxssfWorkbookWriter.setText(row, 0, item.getAsin());
SxssfWorkbookWriter.setText(row, 1, item.getMinimumPrice());
SxssfWorkbookWriter.setText(row, 2, item.getStatus());
}
sheet.setColumnWidth(0, 20 * 256);
sheet.setColumnWidth(1, 18 * 256);
sheet.setColumnWidth(2, 18 * 256);
}
workbook.write(fos);
writer.write(outputXlsx);
} catch (Exception ex) {
log.warn("[shop-match] write workbook failed: {}", ex.getMessage());
throw new BusinessException("generate shop-match excel failed: " + ex.getMessage());
} finally {
try {
workbook.close();
} catch (Exception ignored) {
}
workbook.dispose();
}
}
@@ -4,7 +4,6 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessCodes;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.config.TaskPressureProperties;
@@ -41,6 +40,7 @@ import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import com.nanri.aiimage.modules.task.service.TaskFinalizeSupport;
import com.nanri.aiimage.modules.task.service.TaskResultPayloadService;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService;
import lombok.RequiredArgsConstructor;
@@ -939,14 +939,7 @@ public class ShopMatchTaskService {
}
private void markResultFailed(FileResultEntity result, String message) {
result.setSuccess(0);
result.setErrorMessage(message);
result.setResultFilename(null);
result.setResultFileUrl(null);
result.setResultFileSize(0L);
result.setRowCount(0);
result.setResultContentType(null);
fileResultMapper.updateById(result);
TaskFinalizeSupport.markResultFailed(fileResultMapper, result, message);
}
private void updateTaskStatusFromLatestRows(FileTaskEntity task, List<FileResultEntity> latest) {
@@ -1040,19 +1033,11 @@ public class ShopMatchTaskService {
}
private TaskDistributedLockService.LockHandle acquireTaskLockOrThrow(Long taskId) {
TaskDistributedLockService.LockHandle lockHandle = acquireTaskLock(taskId);
if (lockHandle == null) {
log.warn("[shop-match] 任务锁竞争,拒绝本次提交 taskId={}", taskId);
throw new BusinessException(BusinessCodes.TASK_BUSY, "任务正在处理中,请稍后再试");
}
return lockHandle;
return TaskFinalizeSupport.acquireTaskLockOrThrow(taskDistributedLockService, MODULE_TYPE, taskId, "shop-match");
}
private TaskDistributedLockService.LockHandle acquireTaskLock(Long taskId) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(MODULE_TYPE, taskId);
return TaskFinalizeSupport.acquireTaskLock(taskDistributedLockService, MODULE_TYPE, taskId);
}
/**
@@ -0,0 +1,190 @@
package com.nanri.aiimage.modules.task.service;
import com.nanri.aiimage.common.exception.BusinessCodes;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.config.TaskPressureProperties;
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 lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
/**
* 业务模块 *TaskService 收尾/执行骨架的公共零件(无状态静态工具):
* 任务实体加载(模块缓存优先、DB 兜底)、分批任务加载、结果行失败落库、任务分布式锁获取。
*
* <p>此前这些零件在 8 个业务模块的 *TaskService 里逐行同构复制(loadTaskForExecution /
* loadTaskMapByIds / markResultFailed / acquireTaskLock / acquireTaskLockOrThrow /
* selectTasksByIdsInBatches),差异点只有三类,均已参数化:
* <ul>
* <li>模块标识 {@code moduleType}(数据隔离与锁键);</li>
* <li>模块任务缓存服务 {@link AbstractModuleTaskCacheService};</li>
* <li>锁竞争日志标签 {@code logTag}(如 {@code [price-track]})。</li>
* </ul>
*
* <p>已知的非同构变体不强制收敛,调用方保留本地实现:
* PriceTrack 的 loadTaskForExecution 要求缓存行 requestJson 非空(用带 Predicate 的重载);
* ShopMatch 的 loadTaskMapByIds 带缓存/DB 新旧复活防护且缓存 RUNNING+SCHEDULED;
* Withdraw 的 loadTaskMapByIds 先按 {@code limit(50)} 归一化任务 id(本地先归一化再传入即可);
* Brand/AppearancePatent/CollectData/DeleteBrand 等的锁方法带自定义 TTL、等待或日志语义。
*
* <p>依赖仅限 task 模块内部(mapper/entity/锁服务)与 config 包(TaskPressureProperties),
* 不触碰任何业务模块,符合 ArchUnit 边界(task→业务 依赖保持为 0)。
*/
@Slf4j
public final class TaskFinalizeSupport {
private TaskFinalizeSupport() {
}
// ------------------------------------------------------------------
// 任务实体加载(执行前)
// ------------------------------------------------------------------
/** 标准变体:模块缓存命中即直接返回,否则读 DB 并按 moduleType 匹配回填缓存。 */
public static FileTaskEntity loadTaskForExecution(FileTaskMapper fileTaskMapper,
AbstractModuleTaskCacheService<?> cacheService,
String moduleType,
Long taskId) {
return loadTaskForExecution(fileTaskMapper, cacheService, moduleType, taskId, cached -> cached != null);
}
/**
* 带「缓存行可用性」判定的变体:缓存命中但 cachedUsable 判定不可用(如 requestJson 为空)
* 时继续走 DB。cachedUsable 收到的入参可能为 null。
*/
public static FileTaskEntity loadTaskForExecution(FileTaskMapper fileTaskMapper,
AbstractModuleTaskCacheService<?> cacheService,
String moduleType,
Long taskId,
Predicate<FileTaskEntity> cachedUsable) {
Map<Long, FileTaskEntity> cachedTasks = cacheService.getTaskCacheBatch(List.of(taskId));
FileTaskEntity cached = cachedTasks.get(taskId);
if (cachedUsable.test(cached)) {
return cached;
}
FileTaskEntity dbTask = fileTaskMapper.selectById(taskId);
if (dbTask != null && moduleType.equals(dbTask.getModuleType())) {
cacheService.saveTaskCache(dbTask);
}
return dbTask;
}
// ------------------------------------------------------------------
// 批量任务加载
// ------------------------------------------------------------------
/** 标准变体:归一化任务 id → 缓存批量查 → 缺失的按批读 DB(moduleType 匹配),RUNNING 行回填缓存。 */
public static Map<Long, FileTaskEntity> loadTaskMapByIds(FileTaskMapper fileTaskMapper,
TaskPressureProperties taskPressureProperties,
AbstractModuleTaskCacheService<?> cacheService,
String moduleType,
List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
List<Long> normalizedTaskIds = taskIds == null ? List.of() : taskIds.stream()
.filter(taskId -> taskId != null && taskId > 0)
.distinct()
.toList();
if (normalizedTaskIds.isEmpty()) {
return result;
}
Map<Long, FileTaskEntity> cachedTasks = cacheService.getTaskCacheBatch(normalizedTaskIds);
result.putAll(cachedTasks);
List<Long> missingTaskIds = normalizedTaskIds.stream()
.filter(taskId -> !cachedTasks.containsKey(taskId))
.toList();
for (FileTaskEntity dbTask : selectTasksByIdsInBatches(fileTaskMapper, taskPressureProperties, missingTaskIds)) {
if (dbTask == null || !moduleType.equals(dbTask.getModuleType())) {
continue;
}
result.put(dbTask.getId(), dbTask);
if ("RUNNING".equals(dbTask.getStatus())) {
cacheService.saveTaskCache(dbTask);
}
}
return result;
}
/** 按任务压力配置的批量大小分批 selectBatchIds(各模块历史实现逐行一致)。 */
public static List<FileTaskEntity> selectTasksByIdsInBatches(FileTaskMapper fileTaskMapper,
TaskPressureProperties taskPressureProperties,
List<Long> taskIds) {
List<FileTaskEntity> tasks = new ArrayList<>();
if (taskIds == null || taskIds.isEmpty()) {
return tasks;
}
int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize());
for (int start = 0; start < taskIds.size(); start += batchSize) {
int end = Math.min(start + batchSize, taskIds.size());
tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end)));
}
return tasks;
}
// ------------------------------------------------------------------
// 结果行失败落库
// ------------------------------------------------------------------
/**
* 结果行置为失败并清空文件字段后落库(success=0、rowSize=0、文件名/URL/ContentType 置空)。
* errorMessage 原样写入;历史上先把空串归一化为 null 的模块(PatrolDelete/ShopDataCrawl/
* QueryAsin/Withdraw)在调用前自行 {@code blankToNull(message)},保持原行为。
*/
public static void markResultFailed(FileResultMapper fileResultMapper, FileResultEntity row, String message) {
row.setSuccess(0);
row.setErrorMessage(message);
row.setResultFilename(null);
row.setResultFileUrl(null);
row.setResultFileSize(0L);
row.setRowCount(0);
row.setResultContentType(null);
fileResultMapper.updateById(row);
}
// ------------------------------------------------------------------
// 任务分布式锁
// ------------------------------------------------------------------
/** 标准变体:taskId 非法返回 null;否则按模块标识获取锁(默认等待时长)。 */
public static TaskDistributedLockService.LockHandle acquireTaskLock(TaskDistributedLockService taskDistributedLockService,
String moduleType,
Long taskId) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(moduleType, taskId);
}
/** 带 waitMillis 的变体(ShopDataCrawl 等使用)。 */
public static TaskDistributedLockService.LockHandle acquireTaskLock(TaskDistributedLockService taskDistributedLockService,
String moduleType,
Long taskId,
long waitMillis) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(moduleType, taskId, waitMillis);
}
/**
* 锁竞争即拒绝提交并抛 TASK_BUSY(提交路径用)。logTag 传模块标签(如 {@code price-track}),
* 仅用于 warn 日志文案,不影响行为。
*/
public static TaskDistributedLockService.LockHandle acquireTaskLockOrThrow(TaskDistributedLockService taskDistributedLockService,
String moduleType,
Long taskId,
String logTag) {
TaskDistributedLockService.LockHandle lockHandle = acquireTaskLock(taskDistributedLockService, moduleType, taskId);
if (lockHandle == null) {
log.warn("[{}] 任务锁竞争,拒绝本次提交 taskId={}", logTag, taskId);
throw new BusinessException(BusinessCodes.TASK_BUSY, "任务正在处理中,请稍后再试");
}
return lockHandle;
}
}
@@ -1,18 +1,16 @@
package com.nanri.aiimage.modules.withdraw.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.util.SxssfWorkbookWriter;
import com.nanri.aiimage.modules.withdraw.model.dto.WithdrawRowDto;
import com.nanri.aiimage.modules.withdraw.model.enums.WithdrawStatus;
import com.nanri.aiimage.modules.withdraw.model.vo.WithdrawResultItemVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
@@ -35,11 +33,11 @@ public class WithdrawExcelAssemblyService {
);
public void writeWorkbook(File outputXlsx, List<WithdrawResultItemVo> items) {
SXSSFWorkbook workbook = new SXSSFWorkbook(200);
workbook.setCompressTempFiles(true);
try (FileOutputStream outputStream = new FileOutputStream(outputXlsx)) {
Sheet sheet = workbook.createSheet("Sheet1");
writeHeader(sheet);
try (SxssfWorkbookWriter writer = SxssfWorkbookWriter.open()) {
Sheet sheet = writer.createSheet("Sheet1", HEADER, null);
for (int columnIndex = 0; columnIndex < HEADER.length; columnIndex++) {
sheet.setColumnWidth(columnIndex, SxssfWorkbookWriter.width(columnIndex == 0 ? 24 : 18));
}
BigDecimal totalShopAmount = totalShopAmount(items);
BigDecimal totalWithdrawAmount = totalWithdrawAmount(items);
@@ -52,7 +50,7 @@ public class WithdrawExcelAssemblyService {
List<WithdrawRowDto> rows = item.getRows() == null ? List.of() : item.getRows();
if (rows.isEmpty()) {
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(safe(item.getShopName()));
SxssfWorkbookWriter.setText(row, 0, safe(item.getShopName()));
if (!totalsWritten) {
writeTotals(row, totalShopAmount, totalWithdrawAmount);
totalsWritten = true;
@@ -61,15 +59,15 @@ public class WithdrawExcelAssemblyService {
for (int i = 0; i < rows.size(); i++) {
WithdrawRowDto data = rows.get(i);
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(i == 0 ? safe(item.getShopName()) : "");
row.createCell(1).setCellValue(formatCountry(data == null ? null : data.getCountry()));
writeMoney(row.createCell(2), data == null ? null : data.getShopAmount());
SxssfWorkbookWriter.setText(row, 0, i == 0 ? safe(item.getShopName()) : "");
SxssfWorkbookWriter.setText(row, 1, formatCountry(data == null ? null : data.getCountry()));
SxssfWorkbookWriter.setNumber(row, 2, data == null ? null : data.getShopAmount());
if (!isNegativeWithdraw(data)) {
writeMoney(row.createCell(3), data == null ? null : data.getWithdrawAmount());
SxssfWorkbookWriter.setNumber(row, 3, data == null ? null : data.getWithdrawAmount());
} else {
row.createCell(3).setCellValue("");
SxssfWorkbookWriter.setText(row, 3, "");
}
row.createCell(4).setCellValue(formatStatus(data));
SxssfWorkbookWriter.setText(row, 4, formatStatus(data));
if (!totalsWritten) {
writeTotals(row, totalShopAmount, totalWithdrawAmount);
totalsWritten = true;
@@ -78,20 +76,14 @@ public class WithdrawExcelAssemblyService {
}
Row subtotal = sheet.createRow(rowIndex++);
writeMoney(subtotal.createCell(2), shopSubtotal(rows));
writeMoney(subtotal.createCell(3), withdrawSubtotal(rows));
SxssfWorkbookWriter.setNumber(subtotal, 2, shopSubtotal(rows));
SxssfWorkbookWriter.setNumber(subtotal, 3, withdrawSubtotal(rows));
rowIndex++;
}
workbook.write(outputStream);
writer.write(outputXlsx);
} catch (Exception ex) {
log.warn("[withdraw] write workbook failed: {}", ex.getMessage());
throw new BusinessException("生成取款 Excel 失败: " + ex.getMessage());
} finally {
try {
workbook.close();
} catch (Exception ignored) {
}
workbook.dispose();
}
}
@@ -107,17 +99,9 @@ public class WithdrawExcelAssemblyService {
return count;
}
private void writeHeader(Sheet sheet) {
Row headerRow = sheet.createRow(0);
for (int columnIndex = 0; columnIndex < HEADER.length; columnIndex++) {
headerRow.createCell(columnIndex).setCellValue(HEADER[columnIndex]);
sheet.setColumnWidth(columnIndex, (columnIndex == 0 ? 24 : 18) * 256);
}
}
private void writeTotals(Row row, BigDecimal totalShopAmount, BigDecimal totalWithdrawAmount) {
writeMoney(row.createCell(6), totalShopAmount);
writeMoney(row.createCell(7), totalWithdrawAmount);
SxssfWorkbookWriter.setNumber(row, 6, totalShopAmount);
SxssfWorkbookWriter.setNumber(row, 7, totalWithdrawAmount);
}
private BigDecimal totalShopAmount(List<WithdrawResultItemVo> items) {
@@ -186,14 +170,6 @@ public class WithdrawExcelAssemblyService {
return COUNTRY_NAMES.getOrDefault(value.toUpperCase(), value);
}
private void writeMoney(Cell cell, BigDecimal value) {
if (value == null) {
cell.setCellValue("");
return;
}
cell.setCellValue(value.doubleValue());
}
private String safe(String value) {
return value == null ? "" : value;
}
@@ -6,7 +6,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessCodes;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.config.TaskPressureProperties;
@@ -19,6 +18,7 @@ import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
import com.nanri.aiimage.modules.task.service.TaskDistributedLockService;
import com.nanri.aiimage.modules.task.service.TaskFileJobService;
import com.nanri.aiimage.modules.task.service.TaskFinalizeSupport;
import com.nanri.aiimage.modules.task.service.TaskProgressSnapshotService;
import com.nanri.aiimage.modules.task.service.TaskResultItemService;
import com.nanri.aiimage.modules.withdraw.model.dto.WithdrawCreateTaskRequest;
@@ -529,49 +529,16 @@ public class WithdrawTaskService {
}
private FileTaskEntity loadTaskForExecution(Long taskId) {
Map<Long, FileTaskEntity> cachedTasks = taskCacheService.getTaskCacheBatch(List.of(taskId));
FileTaskEntity cached = cachedTasks.get(taskId);
if (cached != null) {
return cached;
}
FileTaskEntity dbTask = fileTaskMapper.selectById(taskId);
if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) {
taskCacheService.saveTaskCache(dbTask);
}
return dbTask;
return TaskFinalizeSupport.loadTaskForExecution(fileTaskMapper, taskCacheService, MODULE_TYPE, taskId);
}
private Map<Long, FileTaskEntity> loadTaskMapByIds(List<Long> taskIds) {
Map<Long, FileTaskEntity> result = new LinkedHashMap<>();
List<Long> normalizedTaskIds = normalizeTaskIds(taskIds);
if (normalizedTaskIds.isEmpty()) {
return result;
}
Map<Long, FileTaskEntity> cachedTasks = taskCacheService.getTaskCacheBatch(normalizedTaskIds);
result.putAll(cachedTasks);
List<Long> missingTaskIds = normalizedTaskIds.stream().filter(taskId -> !cachedTasks.containsKey(taskId)).toList();
for (FileTaskEntity dbTask : selectTasksByIdsInBatches(missingTaskIds)) {
if (dbTask != null && MODULE_TYPE.equals(dbTask.getModuleType())) {
result.put(dbTask.getId(), dbTask);
if ("RUNNING".equals(dbTask.getStatus())) {
taskCacheService.saveTaskCache(dbTask);
}
}
}
return result;
// Withdraw 特有:任务 id 归一化带 limit(50) 上限,保留在本地后传入公共加载
return TaskFinalizeSupport.loadTaskMapByIds(fileTaskMapper, taskPressureProperties, taskCacheService, MODULE_TYPE, normalizeTaskIds(taskIds));
}
private List<FileTaskEntity> selectTasksByIdsInBatches(List<Long> taskIds) {
List<FileTaskEntity> tasks = new ArrayList<>();
if (taskIds == null || taskIds.isEmpty()) {
return tasks;
}
int batchSize = Math.max(1, taskPressureProperties.getDbSelectBatchSize());
for (int start = 0; start < taskIds.size(); start += batchSize) {
int end = Math.min(start + batchSize, taskIds.size());
tasks.addAll(fileTaskMapper.selectBatchIds(taskIds.subList(start, end)));
}
return tasks;
return TaskFinalizeSupport.selectTasksByIdsInBatches(fileTaskMapper, taskPressureProperties, taskIds);
}
private Map<Long, FileTaskEntity> loadHistoryTaskMap(List<FileResultEntity> entities) {
@@ -1013,14 +980,7 @@ public class WithdrawTaskService {
}
private void markResultFailed(FileResultEntity row, String message) {
row.setSuccess(RESULT_FAILED);
row.setErrorMessage(blankToNull(message));
row.setResultFilename(null);
row.setResultFileUrl(null);
row.setResultFileSize(0L);
row.setResultContentType(null);
row.setRowCount(0);
fileResultMapper.updateById(row);
TaskFinalizeSupport.markResultFailed(fileResultMapper, row, blankToNull(message));
}
private boolean isResultFinished(FileResultEntity row) {
@@ -1055,19 +1015,11 @@ public class WithdrawTaskService {
}
private TaskDistributedLockService.LockHandle acquireTaskLockOrThrow(Long taskId) {
TaskDistributedLockService.LockHandle lockHandle = acquireTaskLock(taskId);
if (lockHandle == null) {
log.warn("[withdraw] 任务锁竞争,拒绝本次提交 taskId={}", taskId);
throw new BusinessException(BusinessCodes.TASK_BUSY, "任务正在处理中,请稍后再试");
}
return lockHandle;
return TaskFinalizeSupport.acquireTaskLockOrThrow(taskDistributedLockService, MODULE_TYPE, taskId, "withdraw");
}
private TaskDistributedLockService.LockHandle acquireTaskLock(Long taskId) {
if (taskId == null || taskId <= 0) {
return null;
}
return taskDistributedLockService.acquire(MODULE_TYPE, taskId);
return TaskFinalizeSupport.acquireTaskLock(taskDistributedLockService, MODULE_TYPE, taskId);
}
/**
@@ -5,17 +5,12 @@
<div class="main-content">
<aside class="left-panel left-panel--with-template">
<div class="section-title">上传文件</div>
<div class="upload-zone">
<div class="hint">选择 Excel 后点击解析,系统会识别表格里的 ID、ASIN、国家信息;同一 ID 有多行时只保留第一行。</div>
<div class="btns">
<button type="button" class="opt-btn" @click="selectFiles">选择 Excel</button>
<button type="button" class="opt-btn" @click="selectFolder">选择文件夹</button>
</div>
<div class="selected-files">
<span v-if="!selectedFileNames.length">暂未选择文件</span>
<span v-for="name in selectedFileNames" v-else :key="name">{{ name }}</span>
</div>
</div>
<FileUploadZone
hint="选择 Excel 后点击解析,系统会识别表格里的 ID、ASIN、国家信息;同一 ID 有多行时只保留第一行。"
:names="selectedFileNames"
@select-files="selectFiles"
@select-folder="selectFolder"
/>
<div class="prompt-card">
<div class="section-title">新增条件要求</div>
@@ -76,6 +71,7 @@
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
import ModuleTemplateDownload from '@/shared/components/ModuleTemplateDownload.vue'
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView, TaskStatCard } from '@/shared/components/tasks/types'
@@ -87,34 +83,47 @@ import {
getAppearancePatentResultDownloadUrl,
getAppearancePatentTaskProgressBatch,
parseAppearancePatent,
type AppearancePatentDashboardVo,
type AppearancePatentHistoryItem,
type AppearancePatentParseVo,
type UploadedFileRef,
type UploadFileVo,
} from '@/shared/api/java-modules'
import { expandBrandFolderRecursive } from '@/shared/api/brand'
import { getPywebviewApi } from '@/shared/bridges/pywebview'
import { getTaskPollIntervalMs } from '@/shared/task-progress-config'
import { getStoredApiSecret } from '@/shared/utils/api-secret-store'
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import { useExcelFileUpload } from '@/shared/composables/useExcelFileUpload'
import {
EXCEL_EXTENSIONS,
checkParseResult,
checkSelectedFiles,
} from '@/shared/dispatch-guard'
useLiveTaskBoard,
activeFileJobStatus,
hasActiveFileJob,
shouldShowFallbackJobProgress,
} from '@/shared/composables/useLiveTaskBoard'
import { getModuleProgressLight } from '@/shared/api/progress-light.ts'
import { createRowsVersionTracker } from '@/shared/api/task-progress-polling.ts'
import { checkParseResult } from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui'
import { formatDateTime } from '@/shared/utils/datetime'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete'
import { getModuleProgressLight } from '@/shared/api/progress-light.ts'
import { createRowsVersionTracker } from '@/shared/api/task-progress-polling.ts'
import { confirmTaskDeletion } from '@/shared/task-delete-confirm'
import { getStoredApiSecret } from '@/shared/utils/api-secret-store'
// ---- 文件选择 / 上传(公共组合式函数)----
const {
files: uploadedFiles,
selectedPaths: selectedFileNames,
selectFiles,
selectFolder,
} = useExcelFileUpload({
requireUploadCapability: true,
pickFilesUnavailableWarning: '当前环境不支持文件选择或上传',
folderUnavailableWarning: '当前环境不支持文件夹选择,请在本地客户端中打开',
errorFallbackMessage: (action) => (action === 'files' ? '文件上传失败' : '选择失败'),
onUploaded: () => {
parseResult.value = null
queuedTaskSummary.value = null
},
onFailed: () => {
parseResult.value = null
queuedTaskSummary.value = null
},
})
const selectedFileNames = ref<string[]>([])
const uploadedFiles = ref<UploadFileVo[]>([])
const parseResult = ref<AppearancePatentParseVo | null>(null)
type TaskSummary = Pick<AppearancePatentParseVo, 'taskId' | 'totalRows' | 'acceptedRows' | 'groupCount' | 'droppedRows'>
const queuedTaskSummary = ref<TaskSummary | null>(null)
@@ -122,30 +131,66 @@ const queuedTaskSummary = ref<TaskSummary | null>(null)
const aiPrompt = ref('')
const parsing = ref(false)
const pushing = ref(false)
const pollingTaskIds = ref<number[]>([])
const pendingFileTaskIds = ref<number[]>([])
const pollTimer = ref<number | null>(null)
const pollingInFlight = ref(false)
const HISTORY_CACHE_TTL_MS = 3000
let historyInFlight: Promise<void> | null = null
let lastHistoryLoadedAt = 0
let disposed = false
const timers = createCategorizedTimers('appearance-patent')
const dashboard = ref<AppearancePatentDashboardVo>({
pendingTaskCount: 0,
processedTaskCount: 0,
successTaskCount: 0,
failedTaskCount: 0,
/** 结果行版本跟踪(审查 F5 后续):版本未变的任务跳过行明细拉取 */
const rowsVersionTracker = createRowsVersionTracker()
function uidForStorage() {
return typeof window !== 'undefined' ? window.localStorage.getItem('uid') || '0' : '0'
}
// ---- 实时任务面板(与货源查询 Tab 的公共骨架抽到 useLiveTaskBoard)----
const board = useLiveTaskBoard<AppearancePatentHistoryItem>({
scope: 'appearance-patent',
pollingStorageKey: () => `appearance-patent:tasks:${uidForStorage()}`,
fetchHistory: getAppearancePatentHistory,
fetchProgressBatch: getAppearancePatentTaskProgressBatch,
fetchDashboard: getAppearancePatentDashboard,
normalizeTaskStatus,
isResultPreparing,
addPendingAfterFinalStatus: (_task, item) => Boolean(item && isResultPreparing(item)),
keepPendingFileTask: (_item, taskStatus) => taskStatus === 'SUCCESS' || taskStatus === 'FAILED',
settleOnTaskStatuses: ['SUCCESS', 'FAILED'],
beforeBatch: async (taskIds, pendingIds) => {
// 行数据按需拉取(审查 F5 后续):等结果文件(pending)时必须强刷;其余情况版本未变的任务跳过
const mustForce = pendingIds.length > 0
if (!mustForce) {
try {
const light = await getModuleProgressLight('appearancePatent', taskIds)
const heavyIds = rowsVersionTracker.selectTasksNeedingRows(light.items || [], taskIds)
if (!heavyIds.length && !(light.missingTaskIds || []).length) {
// 行数据与状态都没变:本轮不拉行明细,直接结束
return null
}
return { taskIds: heavyIds, force: false }
} catch {
// 轻量端点不可用(老后端/网络)→ 照旧全量拉取,行为与原来一致
return { taskIds, force: false }
}
}
return { taskIds, force: true }
},
// 轮询回调抛错时上抛、轮询停止(与原实现一致)
pollErrorMode: 'propagate',
getPendingParseItem: () => pendingParseItem.value,
download: {
buildUrl: (resultId) => getAppearancePatentResultDownloadUrl(resultId),
fallbackFilename: 'appearance-patent',
progressKeyPrefix: 'appearance-patent',
},
deleteTask: {
shouldDeleteTask: (item) => item.taskStatus === 'RUNNING' && item.taskId != null,
deleteTask: (taskId) => deleteAppearancePatentTask(taskId),
deleteHistory: (resultId) => deleteAppearancePatentHistory(resultId),
onTaskDeleted: (item) => board.removePollingTask(item.taskId as number),
},
})
const historyItems = ref<AppearancePatentHistoryItem[]>([])
const liveProgressItems = ref<Record<number, AppearancePatentHistoryItem>>({})
const visibleTaskSummary = computed(() => parseResult.value || queuedTaskSummary.value)
const pendingParseItem = computed<AppearancePatentHistoryItem | null>(() => {
const result = parseResult.value
if (!result?.taskId) return null
const exists = historyItems.value.some((item) => item.taskId === result.taskId)
const exists = board.historyItems.value.some((item) => item.taskId === result.taskId)
if (exists) return null
return {
taskId: result.taskId,
@@ -155,47 +200,20 @@ const pendingParseItem = computed<AppearancePatentHistoryItem | null>(() => {
success: false,
}
})
const currentItems = computed(() => {
const activeIds = new Set([...pollingTaskIds.value, ...pendingFileTaskIds.value])
const activeItems = historyItems.value
.filter((item) => {
const status = normalizeTaskStatus(item)
return item.taskId != null && (activeIds.has(item.taskId) || status === 'RUNNING' || status === 'PENDING')
})
.map((item) => mergeCurrentTaskItem(item))
for (const [taskIdText, liveItem] of Object.entries(liveProgressItems.value)) {
const taskId = Number(taskIdText)
if (!Number.isFinite(taskId) || taskId <= 0) continue
if (!activeIds.has(taskId) && normalizeTaskStatus(liveItem) !== 'RUNNING' && normalizeTaskStatus(liveItem) !== 'PENDING') {
continue
}
if (activeItems.some((item) => item.taskId === taskId)) continue
activeItems.push(mergeCurrentTaskItem(liveItem))
}
if (!pendingParseItem.value) return activeItems
const exists = activeItems.some((item) => item.taskId === pendingParseItem.value?.taskId)
return exists ? activeItems : [pendingParseItem.value, ...activeItems]
})
const historyOnlyItems = computed(() =>
historyItems.value.filter((i) => {
const status = normalizeTaskStatus(i)
return status !== 'RUNNING' && status !== 'PENDING'
}),
)
const patentCards = computed<TaskStatCard[]>(() => [
{ label: '运行中任务', value: dashboard.value.pendingTaskCount },
{ label: '已结束任务', value: dashboard.value.processedTaskCount },
{ label: '成功任务', value: dashboard.value.successTaskCount },
{ label: '失败任务', value: dashboard.value.failedTaskCount },
{ label: '运行中任务', value: board.dashboard.value.pendingTaskCount },
{ label: '已结束任务', value: board.dashboard.value.processedTaskCount },
{ label: '成功任务', value: board.dashboard.value.successTaskCount },
{ label: '失败任务', value: board.dashboard.value.failedTaskCount },
])
const currentTaskViews = computed<TaskItemView[]>(() =>
currentItems.value.map(toPatentTaskView),
board.currentItems.value.map(toPatentTaskView),
)
const historyTaskViews = computed<TaskItemView[]>(() =>
historyOnlyItems.value.map(toPatentTaskView),
board.historyOnlyItems.value.map(toPatentTaskView),
)
function itemSource(item: TaskItemView) {
@@ -203,7 +221,7 @@ function itemSource(item: TaskItemView) {
}
function toPatentTaskView(item: AppearancePatentHistoryItem): TaskItemView {
const showProgress = showFileProgress(item)
const showProgress = board.showFileProgress(item)
const progressPercent = fileProgressPercent(item)
return {
key: `patent-${item.taskId ?? item.resultId ?? item.sourceFilename}`,
@@ -228,20 +246,113 @@ function toPatentTaskView(item: AppearancePatentHistoryItem): TaskItemView {
}
}
function mergeCurrentTaskItem(item: AppearancePatentHistoryItem) {
if (item.taskId == null) return item
const liveItem = liveProgressItems.value[item.taskId]
if (!liveItem) return item
return {
...item,
...liveItem,
sourceFilename: liveItem.sourceFilename || item.sourceFilename,
resultFilename: liveItem.resultFilename || item.resultFilename,
rowCount: liveItem.rowCount ?? item.rowCount,
createdAt: liveItem.createdAt || item.createdAt,
}
// ---- 本模块的状态语义(与货源查询 Tab 不同,保留在 Tab 内)----
function statusText(item: AppearancePatentHistoryItem) {
const status = normalizeTaskStatus(item)
if (status === 'RUNNING') return taskStatusText(status)
if (status === 'PENDING') return taskStatusText(status, { text: { PENDING: '已解析,等待后台执行' } })
// 已判死(含心跳超时)的任务始终显示失败:后台检测可能仍在跑、文件生成后下载按钮会自动出现,
// 但状态文案不能是"结果生成中",否则用户会以为任务还在执行(2026-09-28 29473/29479)
if (status === 'FAILED') return isResultBuildFailed(item) ? '结果生成失败' : taskStatusText('FAILED')
if (isResultPreparing(item)) return '结果生成中'
if (isResultBuildFailed(item)) return '结果生成失败'
if (hasDownloadableResult(item)) return taskStatusText('SUCCESS')
if (status === 'SUCCESS' || item.success) return taskStatusText('SUCCESS')
return '等待中'
}
function statusClass(item: AppearancePatentHistoryItem) {
const status = normalizeTaskStatus(item)
if (status === 'FAILED' || isResultBuildFailed(item)) return taskStatusClass('FAILED')
if (status === 'RUNNING' || isResultPreparing(item)) return taskStatusClass('RUNNING')
if (hasDownloadableResult(item)) return taskStatusClass('SUCCESS')
if (status === 'SUCCESS' || item.success) return taskStatusClass('SUCCESS')
return 'pending'
}
function isResultPreparing(item: AppearancePatentHistoryItem) {
const status = normalizeTaskStatus(item)
const fileStatus = (item.fileStatus || '').toUpperCase()
if (status !== 'SUCCESS' && status !== 'FAILED') return false
if (canDownload(item)) return false
if (fileStatus === 'FAILED' || fileStatus === 'SUCCESS') return false
return status === 'SUCCESS' || hasActiveFileJob(item)
}
function normalizeTaskStatus(item: AppearancePatentHistoryItem) {
return (item.taskStatus || '').toUpperCase()
}
function isResultBuildFailed(item: AppearancePatentHistoryItem) {
return (item.fileStatus || '').toUpperCase() === 'FAILED'
}
function pendingResultHint(item: AppearancePatentHistoryItem) {
if (isResultBuildFailed(item)) {
return item.fileError || '结果文件生成失败,请稍后重试或检查后端日志'
}
if (board.showFileProgress(item)) {
return ''
}
if (!isResultPreparing(item)) return ''
const doneLabel = normalizeTaskStatus(item) === 'FAILED' ? '任务已失败' : '任务已完成'
const fileStatus = (item.fileStatus || '').toUpperCase()
if (fileStatus === 'PENDING') {
return `${doneLabel},结果文件排队中,请稍候`
}
return `${doneLabel},正在生成结果文件,下载按钮稍后出现`
}
function canDownload(item: AppearancePatentHistoryItem) {
return Boolean(item.resultId && (item.fileReady || item.downloadUrl))
}
function hasDownloadableResult(item: AppearancePatentHistoryItem) {
return canDownload(item)
}
function fileProgressPercent(item: AppearancePatentHistoryItem) {
const percent = Number(item.fileProgressPercent || 0)
if (Number.isFinite(percent) && percent > 0) {
return Math.max(0, Math.min(100, Math.round(percent)))
}
if (shouldShowFallbackJobProgress(item)) {
return activeFileJobStatus(item) === 'PENDING' ? 6 : 10
}
return 0
}
function displayFileProgressMessage(item: AppearancePatentHistoryItem, fallback: string) {
const message = (item.fileProgressMessage || '').trim()
if (!message) return fallback
return translateFileProgressMessage(message)
}
function translateFileProgressMessage(message: string) {
const submittedMatch = message.match(/^LLM submitted (\d+) batches, completed (\d+), waiting for results$/)
if (submittedMatch) {
return `已提交 ${submittedMatch[1]} 批,已完成 ${submittedMatch[2]} 批,等待结果`
}
const completedMatch = message.match(/^LLM completed (\d+) batches, waiting for remaining Python data$/)
if (completedMatch) {
return `已完成 ${completedMatch[1]} 批,等待后续数据`
}
const translations: Record<string, string> = {
'Receiving Python data, waiting for 50 rows before LLM': '正在接收检测数据,累计 50 条后提交检测',
'Submitting LLM': '正在提交检测',
'LLM submitted, waiting for result': '已提交检测,等待结果',
'Waiting for Python upload': '等待后续数据',
'LLM results ready, assembling xlsx': '检测结果已返回,正在生成结果文件',
'Assembling xlsx': '正在生成结果文件',
'Uploading result file': '正在上传结果文件',
'Result file generated': '结果文件已生成',
}
return translations[message] || message
}
// ---- 解析与提交(本模块特有)----
function effectiveAiPrompt() {
// 用户没在文本框填东西就送空字符串,后端会跳过 prompt 字段。
return aiPrompt.value.trim()
@@ -251,15 +362,6 @@ function effectiveLlmApiKey() {
return getStoredApiSecret('appearance-patent').trim()
}
function uidForStorage() {
return typeof window !== 'undefined' ? window.localStorage.getItem('uid') || '0' : '0'
}
function pollingKey() {
return `appearance-patent:tasks:${uidForStorage()}`
}
function toTaskSummary(result: AppearancePatentParseVo): TaskSummary {
return {
taskId: result.taskId,
@@ -270,80 +372,6 @@ function toTaskSummary(result: AppearancePatentParseVo): TaskSummary {
}
}
function savePollingIds() {
if (typeof window === 'undefined') return
const ids = Array.from(new Set([...pollingTaskIds.value, ...pendingFileTaskIds.value]))
if (!ids.length) window.localStorage.removeItem(pollingKey())
else window.localStorage.setItem(pollingKey(), JSON.stringify(ids))
}
function loadPollingIds() {
if (typeof window === 'undefined') return
try {
const raw = window.localStorage.getItem(pollingKey())
const ids = raw ? JSON.parse(raw) : []
pollingTaskIds.value = Array.isArray(ids)
? ids.filter((id): id is number => typeof id === 'number' && id > 0)
: []
} catch {
pollingTaskIds.value = []
}
}
async function selectFiles() {
const api = getPywebviewApi()
if (!api?.select_brand_xlsx_files || !api.upload_file_to_java) {
ElMessage.warning('当前环境不支持文件选择或上传')
return
}
const paths = await api.select_brand_xlsx_files()
if (!paths?.length) return
if (!(await passGuard(checkSelectedFiles(paths, { allowedExtensions: EXCEL_EXTENSIONS })))) return
try {
const files = await uploadPathsToJava(getPywebviewApi(), paths)
uploadedFiles.value = files
selectedFileNames.value = files.map((f) => f.relativePath || f.originalFilename || f.fileKey)
parseResult.value = null
queuedTaskSummary.value = null
} catch (error) {
// 上传失败时清空选择,避免残留上一批文件让用户误以为新文件已就绪
uploadedFiles.value = []
selectedFileNames.value = []
parseResult.value = null
queuedTaskSummary.value = null
ElMessage.error(error instanceof Error ? error.message : '文件上传失败')
}
}
async function selectFolder() {
const api = getPywebviewApi()
if (!api?.select_brand_folder) {
ElMessage.warning('当前环境不支持文件夹选择,请在本地客户端中打开')
return
}
try {
const folder = await api.select_brand_folder()
if (!folder) return
const result = await expandBrandFolderRecursive(folder)
if (!result.success || !result.items?.length) {
ElMessage.warning(result.error || '该文件夹下没有可用的 xlsx 文件')
return
}
if (!(await passGuard(checkSelectedFiles(result.items, { allowedExtensions: EXCEL_EXTENSIONS })))) return
const files = await uploadPathsToJava(getPywebviewApi(), result.items)
uploadedFiles.value = files
selectedFileNames.value = result.items.map((item) => item.relativePath || item.absolutePath)
parseResult.value = null
queuedTaskSummary.value = null
ElMessage.success(`已选择文件夹内 ${result.items.length} 个 Excel 文件`)
} catch (error) {
uploadedFiles.value = []
selectedFileNames.value = []
parseResult.value = null
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
/**
* 本模块按 id 列分组,同组只检测 1 次并把结论复制给组内其它行。
* 生产事故(任务 28805):源表 id 列 904 行全填 "1",整表塌成 1 组,105 秒就"完成"了。
@@ -424,10 +452,10 @@ async function pushToPythonQueue() {
pushing.value = true
try {
queuedTaskSummary.value = toTaskSummary(currentParseResult)
addPollingTask(taskId)
board.addPollingTask(taskId)
clearParsedTask()
await loadDashboard()
await loadHistory({ force: true })
await board.loadDashboard()
await board.loadHistory({ force: true })
ElMessage.success('已提交,后台自动执行中(无需打开浏览器)')
} finally {
pushing.value = false
@@ -438,476 +466,22 @@ function clearParsedTask() {
parseResult.value = null
}
function addPollingTask(taskId: number) {
if (!pollingTaskIds.value.includes(taskId)) {
pollingTaskIds.value = [...pollingTaskIds.value, taskId]
savePollingIds()
}
ensurePolling()
}
function removePollingTask(taskId: number) {
pollingTaskIds.value = pollingTaskIds.value.filter((id) => id !== taskId)
if (!pendingFileTaskIds.value.includes(taskId)) {
const next = { ...liveProgressItems.value }
delete next[taskId]
liveProgressItems.value = next
}
savePollingIds()
}
function addPendingFileTask(taskId: number) {
if (!pendingFileTaskIds.value.includes(taskId)) {
pendingFileTaskIds.value = [...pendingFileTaskIds.value, taskId]
savePollingIds()
}
ensurePolling()
}
function removePendingFileTask(taskId: number) {
pendingFileTaskIds.value = pendingFileTaskIds.value.filter((id) => id !== taskId)
if (!pollingTaskIds.value.includes(taskId)) {
const next = { ...liveProgressItems.value }
delete next[taskId]
liveProgressItems.value = next
}
savePollingIds()
}
function ensurePolling() {
if (disposed) return
if (pollTimer.value != null) return
scheduleNextPoll(true)
}
function stopPolling() {
if (pollTimer.value != null) {
timers.clearTimer('task-poll', pollTimer.value)
pollTimer.value = null
}
}
function scheduleNextPoll(immediate = false) {
if (disposed) return
if (pollTimer.value != null) {
if (!immediate) return
timers.clearTimer('task-poll', pollTimer.value)
pollTimer.value = null
}
const run = async () => {
pollTimer.value = null
if (disposed) return
if (!pollingTaskIds.value.length && !pendingFileTaskIds.value.length) return
await refreshTaskProgress()
if (!disposed && pollTimer.value == null && (pollingTaskIds.value.length || pendingFileTaskIds.value.length)) {
pollTimer.value = timers.setTimeout('task-poll', run, getTaskPollIntervalMs())
}
}
if (immediate) void run()
else pollTimer.value = timers.setTimeout('task-poll', run, getTaskPollIntervalMs())
}
/** 结果行版本跟踪(审查 F5 后续):版本未变的任务跳过行明细拉取 */
const rowsVersionTracker = createRowsVersionTracker()
async function refreshTaskProgress() {
if (pollingInFlight.value || (!pollingTaskIds.value.length && !pendingFileTaskIds.value.length)) {
if (!pollingTaskIds.value.length && !pendingFileTaskIds.value.length) stopPolling()
return
}
pollingInFlight.value = true
try {
let shouldRefreshDashboard = false
let shouldRefreshHistory = false
const taskIds = Array.from(new Set([...pollingTaskIds.value, ...pendingFileTaskIds.value]))
if (taskIds.length) {
// 行数据按需拉取(审查 F5 后续):等结果文件(pending)时必须强刷;其余情况版本未变的任务跳过
let heavyIds = taskIds
const mustForce = pendingFileTaskIds.value.length > 0
if (!mustForce) {
try {
const light = await getModuleProgressLight('appearancePatent', taskIds)
heavyIds = rowsVersionTracker.selectTasksNeedingRows(light.items || [], taskIds)
if (!heavyIds.length && !(light.missingTaskIds || []).length) {
// 行数据与状态都没变:本轮不拉行明细,直接结束
return
}
} catch {
// 轻量端点不可用(老后端/网络)→ 照旧全量拉取,行为与原来一致
heavyIds = taskIds
}
}
const batch = await getAppearancePatentTaskProgressBatch(heavyIds, { force: mustForce })
for (const detail of batch.items || []) {
const task = detail.task
if (!task?.id) continue
const item = detail.items?.[0]
if (item) {
liveProgressItems.value = {
...liveProgressItems.value,
[task.id]: item,
}
mergeHistoryItem(item)
}
if (task.status === 'SUCCESS' || task.status === 'FAILED') {
removePollingTask(task.id)
shouldRefreshDashboard = true
if (item && isResultPreparing(item)) addPendingFileTask(task.id)
else removePendingFileTask(task.id)
}
if (item && (task.status === 'SUCCESS' || task.status === 'FAILED') && pendingFileTaskIds.value.includes(task.id)) {
const fileStatus = (item.fileStatus || '').toUpperCase()
if (item.fileReady || fileStatus === 'SUCCESS' || fileStatus === 'FAILED') {
removePendingFileTask(task.id)
shouldRefreshDashboard = true
shouldRefreshHistory = true
}
}
}
if (batch.missingTaskIds?.length) {
batch.missingTaskIds.forEach((taskId) => {
removePollingTask(taskId)
removePendingFileTask(taskId)
})
shouldRefreshHistory = true
shouldRefreshDashboard = true
}
}
if (shouldRefreshDashboard) {
await loadDashboard()
}
if (shouldRefreshHistory) {
await loadHistory()
}
settlePendingFileTasks()
} finally {
pollingInFlight.value = false
}
}
function mergeHistoryItem(item: AppearancePatentHistoryItem) {
if (!item.taskId && !item.resultId) return
const index = historyItems.value.findIndex((row) =>
(item.resultId != null && row.resultId === item.resultId)
|| (item.taskId != null && row.taskId === item.taskId),
)
if (index >= 0) {
historyItems.value[index] = { ...historyItems.value[index], ...item }
} else {
historyItems.value = [item, ...historyItems.value]
}
}
function settlePendingFileTasks() {
if (!pendingFileTaskIds.value.length) return
const remaining: number[] = []
for (const taskId of pendingFileTaskIds.value) {
const item = historyItems.value.find((row) => row.taskId === taskId)
if (!item) {
remaining.push(taskId)
continue
}
const taskStatus = (item.taskStatus || '').toUpperCase()
const fileStatus = (item.fileStatus || '').toUpperCase()
if (item.fileReady || fileStatus === 'SUCCESS' || fileStatus === 'FAILED') {
continue
}
if (taskStatus === 'SUCCESS' || taskStatus === 'FAILED') {
remaining.push(taskId)
}
}
pendingFileTaskIds.value = remaining
savePollingIds()
if (pendingFileTaskIds.value.length) ensurePolling()
}
function seedPendingFileTasksFromHistory() {
const ids = historyItems.value
.filter((item) => item.taskId != null && isResultPreparing(item))
.map((item) => item.taskId as number)
pendingFileTaskIds.value = Array.from(new Set(ids))
savePollingIds()
if (pendingFileTaskIds.value.length) ensurePolling()
}
function seedRunningTasksFromHistory() {
const ids = historyItems.value
.filter((item) => item.taskId != null && normalizeTaskStatus(item) === 'RUNNING')
.map((item) => item.taskId as number)
if (!ids.length) return
pollingTaskIds.value = Array.from(new Set([...pollingTaskIds.value, ...ids]))
savePollingIds()
ensurePolling()
}
async function loadDashboard() {
dashboard.value = await getAppearancePatentDashboard()
}
async function loadHistory(options: { force?: boolean } = {}) {
const now = Date.now()
if (!options.force && historyItems.value.length && now - lastHistoryLoadedAt < HISTORY_CACHE_TTL_MS) return
if (historyInFlight) return historyInFlight
historyInFlight = getAppearancePatentHistory()
.then((res) => {
historyItems.value = mergeHistoryItemsPreservingLiveProgress(historyItems.value, res.items || [])
lastHistoryLoadedAt = Date.now()
})
.finally(() => {
historyInFlight = null
})
return historyInFlight
}
function mergeHistoryItemsPreservingLiveProgress(
previousItems: AppearancePatentHistoryItem[],
incomingItems: AppearancePatentHistoryItem[],
) {
const previousByTaskId = new Map<number, AppearancePatentHistoryItem>()
const previousByResultId = new Map<number, AppearancePatentHistoryItem>()
for (const item of previousItems || []) {
if (item.taskId != null) previousByTaskId.set(item.taskId, item)
if (item.resultId != null) previousByResultId.set(item.resultId, item)
}
return (incomingItems || []).map((incoming) => {
const previous = incoming.resultId != null
? previousByResultId.get(incoming.resultId)
: incoming.taskId != null
? previousByTaskId.get(incoming.taskId)
: undefined
return mergeHistoryItemPreservingLiveProgress(previous, incoming)
})
}
function mergeHistoryItemPreservingLiveProgress(
previous: AppearancePatentHistoryItem | undefined,
incoming: AppearancePatentHistoryItem,
) {
if (!previous) return incoming
const shouldPreserveLiveProgress =
(normalizeTaskStatus(previous) === 'RUNNING' || isResultPreparing(previous) || showFileProgress(previous))
&& !showFileProgress(incoming)
&& !canDownload(incoming)
if (!shouldPreserveLiveProgress) {
return incoming
}
return {
...incoming,
fileJobId: incoming.fileJobId ?? previous.fileJobId,
fileStatus: incoming.fileStatus || previous.fileStatus,
fileError: incoming.fileError || previous.fileError,
fileReady: incoming.fileReady || previous.fileReady,
fileProgressPercent: hasMeaningfulProgressValue(incoming.fileProgressPercent)
? incoming.fileProgressPercent
: previous.fileProgressPercent,
fileProgressCurrent: hasMeaningfulProgressValue(incoming.fileProgressCurrent)
? incoming.fileProgressCurrent
: previous.fileProgressCurrent,
fileProgressTotal: hasMeaningfulProgressValue(incoming.fileProgressTotal)
? incoming.fileProgressTotal
: previous.fileProgressTotal,
fileProgressMessage: (incoming.fileProgressMessage || '').trim()
? incoming.fileProgressMessage
: previous.fileProgressMessage,
}
}
function hasMeaningfulProgressValue(value: unknown) {
const num = Number(value)
return Number.isFinite(num) && num > 0
}
function statusText(item: AppearancePatentHistoryItem) {
const status = normalizeTaskStatus(item)
if (status === 'RUNNING') return taskStatusText(status)
if (status === 'PENDING') return taskStatusText(status, { text: { PENDING: '已解析,等待后台执行' } })
// 已判死(含心跳超时)的任务始终显示失败:后台检测可能仍在跑、文件生成后下载按钮会自动出现,
// 但状态文案不能是"结果生成中",否则用户会以为任务还在执行(2026-09-28 29473/29479)
if (status === 'FAILED') return isResultBuildFailed(item) ? '结果生成失败' : taskStatusText('FAILED')
if (isResultPreparing(item)) return '结果生成中'
if (isResultBuildFailed(item)) return '结果生成失败'
if (hasDownloadableResult(item)) return taskStatusText('SUCCESS')
if (status === 'SUCCESS' || item.success) return taskStatusText('SUCCESS')
return '等待中'
}
function statusClass(item: AppearancePatentHistoryItem) {
const status = normalizeTaskStatus(item)
if (status === 'FAILED' || isResultBuildFailed(item)) return taskStatusClass('FAILED')
if (status === 'RUNNING' || isResultPreparing(item)) return taskStatusClass('RUNNING')
if (hasDownloadableResult(item)) return taskStatusClass('SUCCESS')
if (status === 'SUCCESS' || item.success) return taskStatusClass('SUCCESS')
return 'pending'
}
function isResultPreparing(item: AppearancePatentHistoryItem) {
const status = normalizeTaskStatus(item)
const fileStatus = (item.fileStatus || '').toUpperCase()
if (status !== 'SUCCESS' && status !== 'FAILED') return false
if (canDownload(item)) return false
if (fileStatus === 'FAILED' || fileStatus === 'SUCCESS') return false
return status === 'SUCCESS' || hasActiveFileJob(item)
}
function normalizeTaskStatus(item: AppearancePatentHistoryItem) {
return (item.taskStatus || '').toUpperCase()
}
function isResultBuildFailed(item: AppearancePatentHistoryItem) {
return (item.fileStatus || '').toUpperCase() === 'FAILED'
}
function pendingResultHint(item: AppearancePatentHistoryItem) {
if (isResultBuildFailed(item)) {
return item.fileError || '结果文件生成失败,请稍后重试或检查后端日志'
}
if (showFileProgress(item)) {
return ''
}
if (!isResultPreparing(item)) return ''
const doneLabel = normalizeTaskStatus(item) === 'FAILED' ? '任务已失败' : '任务已完成'
const fileStatus = (item.fileStatus || '').toUpperCase()
if (fileStatus === 'PENDING') {
return `${doneLabel},结果文件排队中,请稍候`
}
return `${doneLabel},正在生成结果文件,下载按钮稍后出现`
}
function canDownload(item: AppearancePatentHistoryItem) {
return Boolean(item.resultId && (item.fileReady || item.downloadUrl))
}
function hasDownloadableResult(item: AppearancePatentHistoryItem) {
return canDownload(item)
}
function fileProgressPercent(item: AppearancePatentHistoryItem) {
const percent = Number(item.fileProgressPercent || 0)
if (Number.isFinite(percent) && percent > 0) {
return Math.max(0, Math.min(100, Math.round(percent)))
}
if (shouldShowFallbackJobProgress(item)) {
return activeFileJobStatus(item) === 'PENDING' ? 6 : 10
}
return 0
}
function displayFileProgressMessage(item: AppearancePatentHistoryItem, fallback: string) {
const message = (item.fileProgressMessage || '').trim()
if (!message) return fallback
return translateFileProgressMessage(message)
}
function translateFileProgressMessage(message: string) {
const submittedMatch = message.match(/^LLM submitted (\d+) batches, completed (\d+), waiting for results$/)
if (submittedMatch) {
return `已提交 ${submittedMatch[1]} 批,已完成 ${submittedMatch[2]} 批,等待结果`
}
const completedMatch = message.match(/^LLM completed (\d+) batches, waiting for remaining Python data$/)
if (completedMatch) {
return `已完成 ${completedMatch[1]} 批,等待后续数据`
}
const translations: Record<string, string> = {
'Receiving Python data, waiting for 50 rows before LLM': '正在接收检测数据,累计 50 条后提交检测',
'Submitting LLM': '正在提交检测',
'LLM submitted, waiting for result': '已提交检测,等待结果',
'Waiting for Python upload': '等待后续数据',
'LLM results ready, assembling xlsx': '检测结果已返回,正在生成结果文件',
'Assembling xlsx': '正在生成结果文件',
'Uploading result file': '正在上传结果文件',
'Result file generated': '结果文件已生成',
}
return translations[message] || message
}
function showFileProgress(item: AppearancePatentHistoryItem) {
const status = normalizeTaskStatus(item)
return (status === 'RUNNING' || isResultPreparing(item)) && (hasExplicitFileProgress(item) || shouldShowFallbackJobProgress(item))
}
function hasExplicitFileProgress(item: AppearancePatentHistoryItem) {
return hasFileProgressCount(item)
|| Number(item.fileProgressPercent || 0) > 0
|| Boolean((item.fileProgressMessage || '').trim())
}
function hasFileProgressCount(item: AppearancePatentHistoryItem) {
return (item.fileProgressTotal || 0) > 0
}
function activeFileJobStatus(item: AppearancePatentHistoryItem) {
return (item.fileStatus || '').toUpperCase()
}
function hasActiveFileJob(item: AppearancePatentHistoryItem) {
const fileStatus = activeFileJobStatus(item)
return item.fileJobId != null || fileStatus === 'PENDING' || fileStatus === 'RUNNING'
}
function shouldShowFallbackJobProgress(item: AppearancePatentHistoryItem) {
return hasActiveFileJob(item) && !hasExplicitFileProgress(item)
}
async function downloadResult(item: AppearancePatentHistoryItem) {
if (!item.resultId) return
const url = item.downloadUrl || getAppearancePatentResultDownloadUrl(item.resultId)
const filename = item.resultFilename || `${item.sourceFilename || 'appearance-patent'}.xlsx`
const result = await saveUrlWithProgress(url, filename, `appearance-patent:${item.resultId}`)
if (result.success) {
ElMessage.success(`已保存: ${result.path || filename}`)
} else if (result.error && result.error !== '用户取消') {
ElMessage.error(result.error)
}
}
async function deleteTaskRecord(item: AppearancePatentHistoryItem) {
if (!(await confirmTaskDeletion(item))) return
try {
if (item.taskStatus === 'RUNNING' && item.taskId) {
await deleteAppearancePatentTask(item.taskId)
removePollingTask(item.taskId)
} else if (item.resultId) {
await deleteAppearancePatentHistory(item.resultId)
}
await loadDashboard()
await loadHistory({ force: true })
ElMessage.success('已删除')
} catch (e) {
ElMessage.error(e instanceof Error ? e.message : '删除失败')
}
}
const { batchDeleteHistory, deleteTaskRecord, downloadResult } = board
onMounted(async () => {
loadPollingIds()
board.loadPollingIds()
await Promise.all([
loadDashboard().catch(() => undefined),
loadHistory().catch(() => undefined),
board.loadDashboard().catch(() => undefined),
board.loadHistory().catch(() => undefined),
])
seedRunningTasksFromHistory()
seedPendingFileTasksFromHistory()
if (pollingTaskIds.value.length || pendingFileTaskIds.value.length) ensurePolling()
board.seedRunningTasksFromHistory()
board.seedPendingFileTasksFromHistory()
board.ensurePollingIfAny()
})
onUnmounted(() => {
disposed = true
stopPolling()
timers.clearScope()
board.dispose()
})
/**
* 历史记录批量删除:复用单条删除逻辑(删除后原函数各自刷新列表)
*/
async function batchDeleteHistory(views: TaskItemView[]) {
const { total, failed } = await runBatchDelete(views, (view) => deleteTaskRecord(itemSource(view)))
if (!total) return
if (failed > 0) {
ElMessage.warning(`删除完成,${failed} 条失败`)
} else {
ElMessage.success(`已删除 ${total} 条历史记录`)
}
}
</script>
<style scoped>
@@ -916,18 +490,14 @@ async function batchDeleteHistory(views: TaskItemView[]) {
.left-panel { width: 400px; background: #242424; padding: 20px; overflow-y: auto; border-right: 1px solid #2e3a52; }
.right-panel { flex: 1; min-width: 0; background: #242424; display: flex; flex-direction: column; }
.section-title, .subsection-title { font-size: 13px; color: #a0acbe; margin-bottom: 10px; }
.upload-zone { border: 1px dashed #3e4a62; border-radius: 10px; padding: 18px; background: #242424; margin-bottom: 18px; }
.hint, .loading-msg, .files, .muted { color: #5e6878; font-size: 12px; line-height: 1.5; }
.link { color: #6ea8fe; text-decoration: none; }
.link:hover { color: #9fc5ff; }
.btns, .run-row { display: flex; gap: 10px; flex-wrap: wrap; }
.opt-btn, .btn-run, .btn-delete, .download { border: none; cursor: pointer; border-radius: 7px; }
.opt-btn { padding: 8px 14px; color: #c8d2e2; background: #242424; border: 1px solid #3e4a62; }
.run-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn-run, .btn-delete, .download { border: none; cursor: pointer; border-radius: 7px; }
.btn-run { padding: 10px 18px; color: #f5f8fc; background: #3498db; font-weight: 600; }
.btn-queue { background: #27ae60; }
.btn-run:disabled { opacity: .55; cursor: not-allowed; }
.selected-files { margin-top: 14px; color: #a0acbe; font-size: 12px; word-break: break-all; }
.selected-files span { display: block; margin: 4px 0; }
.prompt-card, .secret-card { margin-bottom: 18px; }
.secret-card { display: none; }
.prompt-input { width: 100%; box-sizing: border-box; resize: vertical; min-height: 180px; padding: 10px 12px; border: 1px solid #333; border-radius: 8px; background: #222b3d; color: #c8d2e2; font-size: 12px; line-height: 1.6; outline: none; }
@@ -5,25 +5,16 @@
<div class="main-content">
<aside class="left-panel">
<div class="section-title">待检品牌文件</div>
<div class="upload-zone">
<div class="hint">
Excel 每行含「品牌」列即会加入检测。可多选文件或选择整个文件夹(.xlsx)。检测由本机客户端执行,请在桌面客户端中打开本页面。
</div>
<div class="btns">
<button type="button" class="opt-btn" :disabled="!hasBridge" @click="selectFiles">选择 Excel</button>
<button type="button" class="opt-btn" :disabled="!hasBridge" @click="selectFolder">选择文件夹</button>
</div>
<div class="selected-files">
<template v-if="selectedPaths.length">
<span v-for="name in displayPaths" :key="name" :title="name">{{ baseName(name) }}</span>
<span v-if="selectedPaths.length > displayPaths.length" class="more-line">
还有 {{ selectedPaths.length - displayPaths.length }} 个文件未展开显示
</span>
</template>
<span v-else>暂未选择文件</span>
</div>
<div v-if="!hasBridge" class="bridge-tip">当前浏览器环境无桌面客户端桥接,仅可预览页面;请用本机客户端打开。</div>
</div>
<FileUploadZone
hint="Excel 每行含「品牌」列即会加入检测。可多选文件或选择整个文件夹(.xlsx)。检测由本机客户端执行,请在桌面客户端中打开本页面。"
:names="selectedPaths"
:max-display="8"
:disabled="!hasBridge"
name-title
:footer-tip="hasBridge ? '' : '当前浏览器环境无桌面客户端桥接,仅可预览页面;请用本机客户端打开。'"
@select-files="selectFiles"
@select-folder="selectFolder"
/>
<div class="section-title">匹配策略</div>
<div class="radio-cards">
@@ -107,6 +98,7 @@
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue'
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView, TaskStatCard } from '@/shared/components/tasks/types'
import { getPywebviewApi } from '@/shared/bridges/pywebview'
@@ -119,18 +111,42 @@ import {
runBrandNow,
type BrandTaskItem,
} from '@/shared/api/brand'
import { checkSelectedFiles, EXCEL_EXTENSIONS } from '@/shared/dispatch-guard.ts'
import { passGuard } from '@/shared/dispatch-guard-ui'
import { useExcelFileUpload } from '@/shared/composables/useExcelFileUpload'
import { getTaskPollIntervalMs } from '@/shared/task-progress-config'
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
import type { UploadFileVo } from '@/shared/api/upload.ts'
import type { UploadedFileRef } from '@/shared/api/types/upload.ts'
import { formatDateTime } from '@/shared/utils/datetime'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete'
const uploadedFiles = ref<UploadFileVo[]>([])
const {
files: uploadedFiles,
selectedPaths,
selectFiles,
selectFolder,
} = useExcelFileUpload({
pickFilesUnavailableWarning: '当前环境不支持文件选择,请在本机客户端中打开',
folderUnavailableWarning: '当前环境不支持文件夹选择,请在本机客户端中打开',
folderEmptyWarning: (error) => error || '该文件夹下没有 xlsx 文件',
filesSuccessMessage: (files) => `已选择并上传 ${files.length} 个 Excel 文件`,
folderSuccessMessage: (files) => `已选择并上传文件夹内 ${files.length} 个 Excel 文件`,
errorFallbackMessage: (action) => (action === 'files' ? '文件上传失败' : '文件夹选择失败'),
uploadOptions: { uploadOss: true },
// 展示名与原实现一致:始终取上传结果的相对路径/文件名(文件、文件夹两个动作都一样)
filePathsMode: 'uploaded',
folderPathsMode: 'uploaded',
// 原实现上传失败时保留上一批已上传文件,仅提示错误
clearOnError: false,
// 新版桌面客户端桥在本机展开文件夹(服务器无法访问用户本机目录)
expandFolder: async (folder) => {
const bridge = getPywebviewApi()
if (!bridge?.expand_brand_folder) {
ElMessage.warning('当前桌面客户端版本不支持文件夹展开,请使用"选择文件"(可多选)')
return { success: false }
}
return await bridge.expand_brand_folder(folder)
},
})
const strategy = ref<'Terms' | 'Simple'>('Simple')
const submitting = ref(false)
const tasks = ref<BrandTaskItem[]>([])
@@ -143,15 +159,6 @@ const hasUid = computed(() => {
const numeric = Number(raw.trim())
return Number.isFinite(numeric) && numeric > 0
})
// 展示用:已上传文件列表的展示名(优先相对路径,其次原文件名)
const selectedPaths = computed(() =>
uploadedFiles.value.map((f) => f.relativePath || f.originalFilename || f.fileKey),
)
const displayPaths = computed(() => selectedPaths.value.slice(0, 8))
function baseName(path: string) {
return path.split(/[\\/]/).pop() || path
}
const timers = createCategorizedTimers('brand-brand-task')
let pollTimer: number | null = null
@@ -361,53 +368,6 @@ function toBrandTaskView(item: BrandTaskItem): TaskItemView {
}
}
/** 上传本地 xlsx 到 Java 临时目录,成功后回填 fileUrl(服务器本地路径,Java 直接读取) */
async function selectFiles() {
const bridge = getPywebviewApi()
if (!bridge?.select_brand_xlsx_files) {
ElMessage.warning('当前环境不支持文件选择,请在本机客户端中打开')
return
}
const paths = await bridge.select_brand_xlsx_files()
if (!paths?.length) return
if (!(await passGuard(checkSelectedFiles(paths, { allowedExtensions: EXCEL_EXTENSIONS })))) return
try {
const files = await uploadPathsToJava(getPywebviewApi(), paths, { uploadOss: true })
uploadedFiles.value = files
ElMessage.success(`已选择并上传 ${files.length} 个 Excel 文件`)
} catch (error) {
ElMessage.error(error instanceof Error ? error.message : '文件上传失败')
}
}
async function selectFolder() {
const bridge = getPywebviewApi()
if (!bridge?.select_brand_folder) {
ElMessage.warning('当前环境不支持文件夹选择,请在本机客户端中打开')
return
}
const folder = await bridge.select_brand_folder()
if (!folder) return
// 新版桌面客户端桥在本机展开文件夹(服务器无法访问用户本机目录)
if (!bridge.expand_brand_folder) {
ElMessage.warning('当前桌面客户端版本不支持文件夹展开,请使用"选择文件"(可多选)')
return
}
try {
const res = await bridge.expand_brand_folder(folder)
if (!res.success || !res.items?.length) {
ElMessage.warning(res.error || '该文件夹下没有 xlsx 文件')
return
}
if (!(await passGuard(checkSelectedFiles(res.items.map((i) => i.absolutePath), { allowedExtensions: EXCEL_EXTENSIONS })))) return
const files = await uploadPathsToJava(getPywebviewApi(), res.items, { uploadOss: true })
uploadedFiles.value = files
ElMessage.success(`已选择并上传文件夹内 ${files.length} 个 Excel 文件`)
} catch (error) {
ElMessage.error(error instanceof Error ? error.message : '文件夹选择失败')
}
}
async function submitRun() {
if (!hasBridge.value) {
ElMessage.warning('请在桌面客户端中打开本页面执行品牌检测')
@@ -568,16 +528,20 @@ async function batchDeleteHistory(views: TaskItemView[]) {
.section-title { font-size: 13px; color: #a0acbe; margin: 12px 0 10px; }
.hint, .loading-msg, .files, .muted { color: #5e6878; font-size: 12px; line-height: 1.5; }
.hint { margin-bottom: 12px; }
.upload-zone { border: 1px dashed #3e4a62; border-radius: 10px; padding: 16px; background: #242424; margin-bottom: 6px; }
/* upload-zone 已抽到 FileUploadZone 组件;这里只保留本 Tab 的差异样式 */
:deep(.upload-zone) { padding: 16px; margin-bottom: 6px; }
:deep(.upload-zone .hint) { margin-bottom: 12px; }
:deep(.upload-zone .opt-btn) { font-size: 12px; }
:deep(.upload-zone .opt-btn:hover:not(:disabled)) { color: #3498db; border-color: #3498db; }
:deep(.upload-zone .opt-btn:disabled) { opacity: 0.5; cursor: not-allowed; }
:deep(.selected-files) { margin-top: 12px; }
:deep(.selected-files span) { margin: 3px 0; }
:deep(.bridge-tip) { margin-top: 10px; color: #e6a23c; font-size: 11px; }
.btns, .run-row { display: flex; gap: 10px; flex-wrap: wrap; }
.run-row { margin-top: 14px; }
.opt-btn { padding: 8px 14px; color: #c8d2e2; background: #242424; border: 1px solid #3e4a62; border-radius: 7px; cursor: pointer; font-size: 12px; }
.opt-btn:hover:not(:disabled) { color: #3498db; border-color: #3498db; }
.opt-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.selected-files { margin-top: 12px; color: #a0acbe; font-size: 12px; word-break: break-all; }
.selected-files span { display: block; margin: 3px 0; }
.selected-files .more-line { color: #5e6878; }
.bridge-tip { margin-top: 10px; color: #e6a23c; font-size: 11px; }
.env-tip { margin-top: 10px; padding: 8px 10px; border-radius: 8px; background: rgba(230, 162, 60, 0.1); color: #e6a23c; font-size: 12px; line-height: 1.6; }
.radio-cards { display: flex; flex-direction: column; gap: 8px; margin-bottom: 6px; }
.radio-card { display: flex; flex-direction: column; gap: 3px; padding: 10px 12px; border: 1px solid #333; border-radius: 8px; background: #242424; cursor: pointer; }
@@ -5,22 +5,13 @@
<div class="main-content">
<aside class="left-panel left-panel--with-template">
<div class="section-title">上传文件</div>
<div class="upload-zone">
<div class="hint">选择需要采集的 Excel 文件或文件夹(默认根目录:采集前数据.xlsx),系统将按下方筛选条件自动采集数据。</div>
<div class="btns">
<button type="button" class="opt-btn" @click="selectFiles">选择 Excel</button>
<button type="button" class="opt-btn" @click="selectFolder">选择文件夹</button>
</div>
<div class="selected-files">
<template v-if="selectedFileNames.length">
<span v-for="name in displayFileNames" :key="name">{{ name }}</span>
<span v-if="selectedFileNames.length > displayFileNames.length" class="more-line">
还有 {{ selectedFileNames.length - displayFileNames.length }} 个文件未展开显示
</span>
</template>
<span v-else>暂未选择文件</span>
</div>
</div>
<FileUploadZone
hint="选择需要采集的 Excel 文件或文件夹(默认根目录:采集前数据.xlsx),系统将按下方筛选条件自动采集数据。"
:names="selectedFileNames"
:max-display="8"
@select-files="selectFiles"
@select-folder="selectFolder"
/>
<div class="section-title">筛选条件</div>
<div class="filter-zone">
@@ -147,19 +138,18 @@
import { computed, onBeforeUnmount, onMounted, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
import ModuleTemplateDownload from '@/shared/components/ModuleTemplateDownload.vue'
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView, TaskStatCard } from '@/shared/components/tasks/types'
import { expandBrandFolderRecursive } from '@/shared/api/brand'
import { getPywebviewApi } from '@/shared/bridges/pywebview'
import { useExcelFileUpload } from '@/shared/composables/useExcelFileUpload'
import { useTaskProgressLoop } from '@/shared/composables/useTaskProgressLoop'
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import {
EXCEL_EXTENSIONS,
checkParseResult,
checkQueuePayload,
checkSelectedFiles,
} from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui'
import {
@@ -177,12 +167,10 @@ import {
type CollectDataHistoryItem,
type CollectDataParseVo,
type CollectDataTaskDetailVo,
type UploadFileVo,
} from '@/shared/api/java-modules'
import { getPollingProgressBatch } from '@/shared/api/task-progress-polling.ts'
import { formatDateTime } from '@/shared/utils/datetime'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete'
import { confirmTaskDeletion } from '@/shared/task-delete-confirm'
@@ -194,10 +182,27 @@ const COUNTRY_OPTIONS = [
{ code: 'ES', label: '西班牙' },
] as const
const selectedFileNames = ref<string[]>([])
const uploadedFiles = ref<UploadFileVo[]>([])
const submitting = ref(false)
const pushing = ref(false)
const {
files: uploadedFiles,
selectedPaths: selectedFileNames,
selectFiles,
selectFolder,
} = useExcelFileUpload({
requireUploadCapability: true,
pickFilesUnavailableWarning: '当前环境不支持文件选择或上传',
folderUnavailableWarning: '当前环境不支持文件夹选择,请在本地客户端中打开',
errorFallbackMessage: (action) => (action === 'files' ? '文件选择失败' : '选择失败'),
onUploaded: () => {
lastTaskId.value = null
lastParseVo.value = null
},
onFailed: () => {
lastTaskId.value = null
lastParseVo.value = null
},
})
const filters = reactive<{
minAmount: number | null
@@ -314,8 +319,6 @@ const progressLoop = useTaskProgressLoop<CollectDataTaskDetailVo>({
},
})
const displayFileNames = computed(() => selectedFileNames.value.slice(0, 8))
function selectCountry(code: string) {
if (!COUNTRY_OPTIONS.some((row) => row.code === code)) return
if (selectedCountryCode.value === code) return
@@ -359,61 +362,6 @@ async function loadCountryPreference() {
}
}
async function selectFiles() {
const api = getPywebviewApi()
if (!api?.select_brand_xlsx_files || !api.upload_file_to_java) {
ElMessage.warning('当前环境不支持文件选择或上传')
return
}
try {
const paths = await api.select_brand_xlsx_files()
if (!paths?.length) return
if (!(await passGuard(checkSelectedFiles(paths, { allowedExtensions: EXCEL_EXTENSIONS })))) return
const files = await uploadPathsToJava(getPywebviewApi(), paths)
uploadedFiles.value = files
selectedFileNames.value = files.map((f) => f.relativePath || f.originalFilename || f.fileKey)
lastTaskId.value = null
lastParseVo.value = null
} catch (error) {
// 上传失败时清空选择,避免残留上一批文件让用户误以为新文件已就绪
uploadedFiles.value = []
selectedFileNames.value = []
lastTaskId.value = null
lastParseVo.value = null
ElMessage.error(error instanceof Error ? error.message : '文件选择失败')
}
}
async function selectFolder() {
const api = getPywebviewApi()
if (!api?.select_brand_folder) {
ElMessage.warning('当前环境不支持文件夹选择,请在本地客户端中打开')
return
}
try {
const folder = await api.select_brand_folder()
if (!folder) return
const result = await expandBrandFolderRecursive(folder)
if (!result.success || !result.items?.length) {
ElMessage.warning(result.error || '该文件夹下没有可用的 xlsx 文件')
return
}
if (!(await passGuard(checkSelectedFiles(result.items, { allowedExtensions: EXCEL_EXTENSIONS })))) return
const files = await uploadPathsToJava(getPywebviewApi(), result.items)
uploadedFiles.value = files
selectedFileNames.value = result.items.map((item) => item.relativePath || item.absolutePath)
lastTaskId.value = null
lastParseVo.value = null
ElMessage.success(`已选择文件夹内 ${result.items.length} 个 Excel 文件`)
} catch (error) {
uploadedFiles.value = []
selectedFileNames.value = []
lastTaskId.value = null
lastParseVo.value = null
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
function buildFiltersPayload() {
const minAmount = filters.minAmount === null || filters.minAmount === undefined || Number.isNaN(filters.minAmount)
? null
@@ -791,7 +739,6 @@ async function batchDeleteHistory(views: TaskItemView[]) {
.left-panel { width: 400px; background: #242424; padding: 20px; overflow-y: auto; border-right: 1px solid #2e3a52; }
.right-panel { flex: 1; min-width: 0; background: #242424; display: flex; flex-direction: column; }
.section-title { font-size: 13px; color: #a0acbe; margin: 12px 0 10px; }
.upload-zone { border: 1px dashed #3e4a62; border-radius: 10px; padding: 18px; background: #242424; margin-bottom: 18px; }
.hint, .loading-msg, .files, .muted { color: #5e6878; font-size: 12px; line-height: 1.5; }
.btns, .run-row { display: flex; gap: 10px; flex-wrap: wrap; }
.opt-btn, .btn-run, .btn-delete, .download { border: none; cursor: pointer; border-radius: 7px; }
@@ -799,9 +746,6 @@ async function batchDeleteHistory(views: TaskItemView[]) {
.btn-run { padding: 10px 18px; color: #f5f8fc; background: #3498db; font-weight: 600; }
.btn-run.btn-queue { background: #27ae60; }
.btn-run:disabled { opacity: .55; cursor: not-allowed; }
.selected-files { margin-top: 14px; color: #a0acbe; font-size: 12px; word-break: break-all; }
.selected-files span { display: block; margin: 4px 0; }
.selected-files .more-line { color: #5e6878; }
.filter-zone {
border: 1px solid #2e3a52;
@@ -5,23 +5,16 @@
<div class="main-content">
<aside class="left-panel">
<div class="section-title">选择文件</div>
<div class="upload-zone">
<div class="hint">选择需要转换的 Excel 文件或文件夹,将按所选模板生成 txt 文件。</div>
<div class="btns">
<button type="button" class="opt-btn" @click="selectConvertFiles">选择 Excel 文件</button>
<button type="button" class="opt-btn" @click="selectConvertFolder">选择文件夹</button>
</div>
<div class="selected-files clean-placeholder">
<template v-if="convertSelectedPaths.length">
<span v-for="path in convertDisplayPaths" :key="path">{{ path }}</span>
<span v-if="convertSelectedPaths.length > convertDisplayPaths.length" class="more-line">
还有 {{ convertSelectedPaths.length - convertDisplayPaths.length }} 个文件未展开显示
</span>
</template>
<span v-else>暂未选择待转换文件</span>
</div>
</div>
<FileUploadZone
hint="选择需要转换的 Excel 文件或文件夹,将按所选模板生成 txt 文件。"
:names="convertSelectedPaths"
:max-display="8"
files-label="选择 Excel 文件"
empty-text="暂未选择待转换文件"
files-class="clean-placeholder"
@select-files="selectConvertFiles"
@select-folder="selectConvertFolder"
/>
<div class="section-title">模板选择</div>
<div class="option-group">
@@ -152,9 +145,9 @@
import { computed, onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView, TaskStatCard } from '@/shared/components/tasks/types'
import { expandBrandFolderRecursive } from '@/shared/api/brand'
import {
deleteConvertHistory,
getConvertHistory,
@@ -164,23 +157,38 @@ import {
type ConvertResultItem,
type ConvertTemplateVo,
} from '@/shared/api/java-modules'
import { getPywebviewApi, type UploadedJavaFile } from '@/shared/bridges/pywebview'
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import { EXCEL_EXTENSIONS, checkSelectedFiles, collectDistinctErrors, guardBlocked } from '@/shared/dispatch-guard'
import { collectDistinctErrors, guardBlocked } from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui'
import { useExcelFileUpload } from '@/shared/composables/useExcelFileUpload'
import { formatDateTime } from '@/shared/utils/datetime'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete'
const convertSelectedPaths = ref<string[]>([])
const {
files: convertUploadedFiles,
selectedPaths: convertSelectedPaths,
selectFiles: selectConvertFiles,
selectFolder: selectConvertFolder,
} = useExcelFileUpload({
// 展示原始本机路径;文件夹展开后只展示相对路径(与原实现一致)
filePathsMode: 'raw',
folderPathsMode: 'relative',
filesSuccessMessage: (_files, paths) => `已选择 ${paths.length} 个待转换文件`,
folderSuccessMessage: (_files, items) => `已选择文件夹内 ${items.length} 个 xlsx 文件`,
folderEmptyWarning: (error) => error || '该文件夹下没有 xlsx 文件',
onFolderPicked: (folder) => {
convertArchiveName.value = folder.split(/[/\\]/).filter(Boolean).pop() || ''
},
onUploaded: (info) => {
if (info.action === 'files') convertArchiveName.value = ''
},
})
const convertArchiveName = ref('')
const convertUploadedFiles = ref<UploadedJavaFile[]>([])
const convertRunning = ref(false)
const convertResultItems = ref<ConvertResultItem[]>([])
const convertTemplates = ref<ConvertTemplateVo[]>([])
const convertTemplateId = ref('')
const convertDisplayPaths = computed(() => convertSelectedPaths.value.slice(0, 8))
const currentConvertTemplate = computed(
() => convertTemplates.value.find((item) => item.id === convertTemplateId.value) || null,
)
@@ -267,61 +275,6 @@ async function loadConvertTemplates(preferredTemplateId?: string) {
convertTemplateId.value = convertTemplates.value[0]?.id || ''
}
async function handleSelectedPaths(paths: string[], successMessage: string) {
convertSelectedPaths.value = paths
convertArchiveName.value = ''
convertUploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), paths)
ElMessage.success(successMessage)
}
async function selectConvertFiles() {
const api = getPywebviewApi()
if (!api?.select_brand_xlsx_files) {
ElMessage.warning('当前环境不支持文件选择,请在本机客户端中打开')
return
}
try {
const paths = await api.select_brand_xlsx_files()
if (!paths?.length) return
if (!(await passGuard(checkSelectedFiles(paths, { allowedExtensions: EXCEL_EXTENSIONS })))) return
await handleSelectedPaths(paths, `已选择 ${paths.length} 个待转换文件`)
} catch (error) {
convertSelectedPaths.value = []
convertUploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
async function selectConvertFolder() {
const api = getPywebviewApi()
if (!api?.select_brand_folder) {
ElMessage.warning('当前环境不支持文件夹选择,请在本机客户端中打开')
return
}
try {
const folder = await api.select_brand_folder()
if (!folder) return
convertArchiveName.value = folder.split(/[/\\]/).filter(Boolean).pop() || ''
const result = await expandBrandFolderRecursive(folder)
if (result.success && result.items?.length) {
if (!(await passGuard(checkSelectedFiles(result.items, { allowedExtensions: EXCEL_EXTENSIONS })))) return
convertSelectedPaths.value = result.items.map((item) => item.relativePath)
convertUploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), result.items)
ElMessage.success(`已选择文件夹内 ${result.items.length} 个 xlsx 文件`)
return
}
ElMessage.warning(result.error || '该文件夹下没有 xlsx 文件')
} catch (error) {
convertSelectedPaths.value = []
convertUploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
async function submitConvertRun() {
if (!convertUploadedFiles.value.length) {
ElMessage.warning('请先选择待转换 Excel 文件或文件夹')
@@ -460,17 +413,15 @@ async function batchDeleteHistory(views: TaskItemView[]) {
margin-bottom: 10px;
}
.upload-zone {
border: 1px dashed #3e4a62;
border-radius: 10px;
/* upload-zone 已抽到 FileUploadZone 组件;这里保留本 Tab 的差异样式 */
:deep(.upload-zone) {
padding: 24px;
text-align: center;
background: #242424;
margin-bottom: 20px;
transition: all 0.2s ease;
}
.upload-zone:hover {
:deep(.upload-zone:hover) {
border-color: #3498db;
background: #242424;
}
@@ -482,11 +433,42 @@ async function batchDeleteHistory(views: TaskItemView[]) {
line-height: 1.5;
}
.btns {
display: flex;
gap: 10px;
:deep(.upload-zone .hint) {
font-size: 13px;
margin-bottom: 12px;
}
:deep(.upload-zone .btns) {
justify-content: center;
flex-wrap: wrap;
}
:deep(.upload-zone .opt-btn) {
padding: 8px 16px;
font-size: 13px;
border-radius: 6px;
}
:deep(.upload-zone .opt-btn:hover) {
color: #3498db;
background: #333;
border-color: #3498db;
}
:deep(.upload-zone .opt-btn:disabled) {
cursor: not-allowed;
opacity: 0.55;
}
:deep(.selected-files) {
font-size: 12px;
color: #5e6878;
max-height: 112px;
overflow-y: auto;
text-align: left;
}
:deep(.more-line) {
color: #a0acbe;
}
.opt-btn,
@@ -529,25 +511,6 @@ async function batchDeleteHistory(views: TaskItemView[]) {
opacity: 0.55;
}
.selected-files {
margin-top: 14px;
font-size: 12px;
color: #5e6878;
max-height: 112px;
overflow-y: auto;
text-align: left;
}
.selected-files span {
display: block;
margin: 4px 0;
word-break: break-all;
}
.more-line {
color: #a0acbe;
}
.option-group {
margin-bottom: 20px;
}
@@ -5,23 +5,16 @@
<div class="main-content">
<aside class="left-panel">
<div class="section-title">选择文件</div>
<div class="upload-zone">
<div class="hint">选择需要清洗的 Excel 文件或文件夹,后续将按所选规则输出处理结果。</div>
<div class="btns">
<button type="button" class="opt-btn" @click="selectCleanFiles">选择 Excel 文件</button>
<button type="button" class="opt-btn" @click="selectCleanFolder">选择文件夹</button>
</div>
<div class="selected-files clean-placeholder">
<template v-if="cleanSelectedPaths.length">
<span v-for="path in cleanDisplayPaths" :key="path">{{ path }}</span>
<span v-if="cleanSelectedPaths.length > cleanDisplayPaths.length" class="more-line">
还有 {{ cleanSelectedPaths.length - cleanDisplayPaths.length }} 个文件未展开显示
</span>
</template>
<span v-else>暂未选择待清洗文件</span>
</div>
</div>
<FileUploadZone
hint="选择需要清洗的 Excel 文件或文件夹,后续将按所选规则输出处理结果。"
:names="cleanSelectedPaths"
:max-display="8"
files-label="选择 Excel 文件"
empty-text="暂未选择待清洗文件"
files-class="clean-placeholder"
@select-files="selectCleanFiles"
@select-folder="selectCleanFolder"
/>
<div class="section-title">保留列设置</div>
<div class="option-group column-option-group">
@@ -131,23 +124,42 @@
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView, TaskStatCard } from '@/shared/components/tasks/types'
import { expandBrandFolderRecursive } from '@/shared/api/brand'
import { deleteDedupeHistory, getDedupeHistory, getDedupeResultDownloadUrl, getDedupeRunProgress, getExcelInfo, runDedupe, type DedupeResultItem, type DedupeRunProgressVo, type DedupeRunVo } from '@/shared/api/java-modules'
import { getPywebviewApi, type UploadedJavaFile } from '@/shared/bridges/pywebview'
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import { EXCEL_EXTENSIONS, checkSelectedFiles, collectDistinctErrors, guardBlocked } from '@/shared/dispatch-guard'
import { collectDistinctErrors, guardBlocked } from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui'
import { useExcelFileUpload } from '@/shared/composables/useExcelFileUpload'
import { formatDateTime } from '@/shared/utils/datetime'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete'
const cleanAvailableColumns = ref<string[]>([])
const cleanSelectedColumns = ref<string[]>([])
const cleanSelectedPaths = ref<string[]>([])
const {
files: cleanUploadedFiles,
selectedPaths: cleanSelectedPaths,
selectFiles: selectCleanFiles,
selectFolder: selectCleanFolder,
} = useExcelFileUpload({
// 展示原始本机路径;文件夹展开后只展示相对路径(与原实现一致)
filePathsMode: 'raw',
folderPathsMode: 'relative',
filesSuccessMessage: (_files, paths) => `已选择 ${paths.length} 个待清洗文件`,
folderSuccessMessage: (_files, items) => `已选择文件夹内 ${items.length} 个 xlsx 文件`,
folderEmptyWarning: (error) => error || '该文件夹下没有 xlsx 文件',
onFolderPicked: (folder) => {
cleanArchiveName.value = folder.split(/[/\\]/).filter(Boolean).pop() || ''
},
onUploaded: async (info) => {
if (info.action === 'files') cleanArchiveName.value = ''
if (info.files.length > 0) {
await loadCleanHeaders(info.files[0].fileKey)
}
},
})
const cleanArchiveName = ref('')
const cleanUploadedFiles = ref<UploadedJavaFile[]>([])
const cleanKeepIntegerIds = ref(false)
const cleanKeepUnderscoreIds = ref(true)
const cleanKeepIntegerMainIdsWhenNoSubIds = ref(true)
@@ -155,7 +167,6 @@ const cleanRunning = ref(false)
const cleanProgressProcessed = ref(0)
const cleanResultItems = ref<DedupeResultItem[]>([])
const cleanSummary = ref<DedupeRunVo>({ total: 0, successCount: 0, failedCount: 0, items: [] })
const cleanDisplayPaths = computed(() => cleanSelectedPaths.value.slice(0, 8))
// 最近一次去重运行信息:用于右侧"当前任务"卡的占位展示(开始时间/运行ID/进度)
const latestRunId = ref('')
const cleanRunStartedAt = ref('')
@@ -255,67 +266,6 @@ async function loadCleanHeaders(fileKey: string) {
cleanSelectedColumns.value = defaultSelectedHeaders.filter((header) => orderedHeaders.includes(header))
}
async function handleSelectedPaths(paths: string[], successMessage: string) {
cleanSelectedPaths.value = paths
cleanArchiveName.value = ''
cleanUploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), paths)
if (cleanUploadedFiles.value.length > 0) {
await loadCleanHeaders(cleanUploadedFiles.value[0].fileKey)
}
ElMessage.success(successMessage)
}
async function selectCleanFiles() {
const api = getPywebviewApi()
if (!api?.select_brand_xlsx_files) {
ElMessage.warning('当前环境不支持文件选择,请在本机客户端中打开')
return
}
try {
const paths = await api.select_brand_xlsx_files()
if (!paths?.length) return
if (!(await passGuard(checkSelectedFiles(paths, { allowedExtensions: EXCEL_EXTENSIONS })))) return
await handleSelectedPaths(paths, `已选择 ${paths.length} 个待清洗文件`)
} catch (error) {
cleanSelectedPaths.value = []
cleanUploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
async function selectCleanFolder() {
const api = getPywebviewApi()
if (!api?.select_brand_folder) {
ElMessage.warning('当前环境不支持文件夹选择,请在本机客户端中打开')
return
}
try {
const folder = await api.select_brand_folder()
if (!folder) return
cleanArchiveName.value = folder.split(/[/\\]/).filter(Boolean).pop() || ''
const result = await expandBrandFolderRecursive(folder)
if (result.success && result.items?.length) {
if (!(await passGuard(checkSelectedFiles(result.items, { allowedExtensions: EXCEL_EXTENSIONS })))) return
cleanSelectedPaths.value = result.items.map((item) => item.relativePath)
cleanUploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), result.items)
if (cleanUploadedFiles.value.length > 0) {
await loadCleanHeaders(cleanUploadedFiles.value[0].fileKey)
}
ElMessage.success(`已选择文件夹内 ${result.items.length} 个 xlsx 文件`)
return
}
ElMessage.warning(result.error || '该文件夹下没有 xlsx 文件')
} catch (error) {
cleanSelectedPaths.value = []
cleanUploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
async function submitCleanRun() {
if (!cleanUploadedFiles.value.length) {
ElMessage.warning('请先选择待清洗 Excel 文件或文件夹')
@@ -519,33 +469,55 @@ async function batchDeleteHistory(views: TaskItemView[]) {
margin-bottom: 10px;
}
.upload-zone {
border: 1px dashed #3e4a62;
border-radius: 10px;
/* upload-zone 已抽到 FileUploadZone 组件;这里保留本 Tab 的差异样式 */
:deep(.upload-zone) {
padding: 24px;
text-align: center;
background: #242424;
margin-bottom: 20px;
transition: all 0.2s ease;
}
.upload-zone:hover {
:deep(.upload-zone:hover) {
border-color: #3498db;
background: #242424;
}
.hint {
color: #5e6878;
:deep(.upload-zone .hint) {
font-size: 13px;
margin-bottom: 12px;
line-height: 1.5;
}
.btns {
display: flex;
gap: 10px;
:deep(.upload-zone .btns) {
justify-content: center;
flex-wrap: wrap;
}
:deep(.upload-zone .opt-btn) {
padding: 8px 16px;
font-size: 13px;
border-radius: 6px;
}
:deep(.upload-zone .opt-btn:hover) {
color: #3498db;
background: #333;
border-color: #3498db;
}
:deep(.upload-zone .opt-btn:disabled) {
cursor: not-allowed;
opacity: 0.55;
}
:deep(.selected-files) {
font-size: 12px;
color: #5e6878;
max-height: 112px;
overflow-y: auto;
text-align: left;
}
:deep(.more-line) {
color: #a0acbe;
}
.opt-btn,
@@ -577,25 +549,6 @@ async function batchDeleteHistory(views: TaskItemView[]) {
border-color: #3498db;
}
.selected-files {
margin-top: 14px;
font-size: 12px;
color: #5e6878;
max-height: 112px;
overflow-y: auto;
text-align: left;
}
.selected-files span {
display: block;
margin: 4px 0;
word-break: break-all;
}
.more-line {
color: #a0acbe;
}
.option-group {
margin-bottom: 20px;
}
@@ -5,23 +5,16 @@
<div class="main-content">
<aside class="left-panel left-panel--with-template">
<div class="section-title">选择文件</div>
<div class="upload-zone">
<div class="hint">上传删除品牌 Excel;直接上传文件或上传文件夹批量导入都可以,每个 Excel 文件名会作为店铺名。</div>
<div class="btns">
<button type="button" class="opt-btn" @click="selectFiles">选择 Excel 文件</button>
<button type="button" class="opt-btn" @click="selectFolder">选择文件夹</button>
</div>
<div class="selected-files clean-placeholder split-selected-files">
<template v-if="selectedPaths.length">
<span v-for="path in displayPaths" :key="path">{{ path }}</span>
<span v-if="selectedPaths.length > displayPaths.length" class="more-line">
还有 {{ selectedPaths.length - displayPaths.length }} 个文件未展开显示
</span>
</template>
<span v-else>暂未选择删除品牌文件</span>
</div>
</div>
<FileUploadZone
hint="上传删除品牌 Excel;直接上传文件或上传文件夹批量导入都可以,每个 Excel 文件名会作为店铺名。"
:names="selectedPaths"
:max-display="10"
files-label="选择 Excel 文件"
empty-text="暂未选择删除品牌文件"
files-class="clean-placeholder split-selected-files"
@select-files="selectFiles"
@select-folder="selectFolder"
/>
<div class="section-title">执行说明</div>
<div class="option-group">
@@ -110,11 +103,11 @@
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
import ModuleTemplateDownload from '@/shared/components/ModuleTemplateDownload.vue'
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView, TaskStatCard } from '@/shared/components/tasks/types'
import ZiniaoVersionSetting from '@/shared/components/ZiniaoVersionSetting.vue'
import { expandBrandFolderRecursive } from '@/shared/api/brand'
import {
deleteDeleteBrandHistory,
getDeleteBrandHistory,
@@ -127,20 +120,18 @@ import {
type DeleteBrandRunVo,
type DeleteBrandTaskDetailVo,
} from '@/shared/api/java-modules'
import { getPywebviewApi, type UploadedJavaFile } from '@/shared/bridges/pywebview'
import { getPywebviewApi } from '@/shared/bridges/pywebview'
import { getTaskPollIntervalMs } from '@/shared/task-progress-config'
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import {
EXCEL_EXTENSIONS,
checkQueuePayload,
checkSelectedFiles,
guardBlocked,
} from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui'
import { useExcelFileUpload } from '@/shared/composables/useExcelFileUpload'
import { useZiniaoVersion } from '@/shared/utils/ziniao-version'
import { formatDateTime } from '@/shared/utils/datetime'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete'
interface SessionDeleteBrandItem extends DeleteBrandResultItem {
@@ -163,8 +154,20 @@ function getStorageKey() {
return `delete-brand:current-tasks:${uid}`
}
const selectedPaths = ref<string[]>([])
const uploadedFiles = ref<UploadedJavaFile[]>([])
const {
files: uploadedFiles,
selectedPaths,
selectFiles,
selectFolder,
} = useExcelFileUpload({
pickFilesUnavailableWarning: '当前环境不支持文件选择,请在本机客户端中打开',
folderUnavailableWarning: '当前环境不支持文件夹选择,请在本机客户端中打开',
folderEmptyWarning: (error) => error || '该文件夹下没有 xlsx 文件',
// 展示原始本机路径;文件夹展开后展示 relativePath || absolutePath(与原实现一致)
filePathsMode: 'raw',
filesSuccessMessage: (_files, paths) => `已选择 ${paths.length} 个删除品牌文件`,
folderSuccessMessage: (_files, items) => `已选择文件夹内 ${items.length} 个 xlsx 文件`,
})
const running = ref(false)
const resultItems = ref<DeleteBrandResultItem[]>([])
const sessionTasks = ref<StoredCurrentTask[]>([])
@@ -183,7 +186,6 @@ const autoRetryTimer = ref<number | null>(null)
const waitingForBackendRecovery = ref(false)
let disposed = false
const timers = createCategorizedTimers('delete-brand')
const displayPaths = computed(() => selectedPaths.value.slice(0, 10))
const currentSectionItems = computed(() => {
const sortedTasks = [...sessionTasks.value].sort((left, right) => (right.createdAt || 0) - (left.createdAt || 0))
@@ -1013,53 +1015,6 @@ function stopPolling() {
clearAutoRetryTimer()
}
async function selectFiles() {
const api = getPywebviewApi()
if (!api?.select_brand_xlsx_files) {
ElMessage.warning('当前环境不支持文件选择,请在本机客户端中打开')
return
}
try {
const paths = await api.select_brand_xlsx_files()
if (!paths?.length) return
if (!(await passGuard(checkSelectedFiles(paths, { allowedExtensions: EXCEL_EXTENSIONS })))) return
selectedPaths.value = paths
uploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), paths)
ElMessage.success(`已选择 ${paths.length} 个删除品牌文件`)
} catch (error) {
// 上传失败时清空选择,避免残留上一批文件让用户误以为新文件已就绪
selectedPaths.value = []
uploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
async function selectFolder() {
const api = getPywebviewApi()
if (!api?.select_brand_folder) {
ElMessage.warning('当前环境不支持文件夹选择,请在本机客户端中打开')
return
}
try {
const folder = await api.select_brand_folder()
if (!folder) return
const result = await expandBrandFolderRecursive(folder)
if (!result.success || !result.items?.length) {
ElMessage.warning(result.error || '该文件夹下没有 xlsx 文件')
return
}
if (!(await passGuard(checkSelectedFiles(result.items, { allowedExtensions: EXCEL_EXTENSIONS })))) return
selectedPaths.value = result.items.map((item) => item.relativePath || item.absolutePath)
uploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), result.items)
ElMessage.success(`已选择文件夹内 ${result.items.length} 个 xlsx 文件`)
} catch (error) {
selectedPaths.value = []
uploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
async function submitRun() {
if (!uploadedFiles.value.length) {
ElMessage.warning('请先选择待处理文件')
@@ -1583,26 +1538,56 @@ async function batchDeleteHistory(views: TaskItemView[]) {
margin-bottom: 10px;
}
.upload-zone {
border: 1px dashed #3e4a62;
border-radius: 10px;
/* upload-zone 已抽到 FileUploadZone 组件;这里保留本 Tab 的差异样式 */
:deep(.upload-zone) {
padding: 24px;
text-align: center;
background: #242424;
margin-bottom: 20px;
transition: all 0.2s ease;
}
.upload-zone:hover {
:deep(.upload-zone:hover) {
border-color: #3498db;
background: #242424;
}
.hint {
color: #5e6878;
:deep(.upload-zone .hint) {
font-size: 13px;
margin-bottom: 12px;
line-height: 1.5;
}
:deep(.upload-zone .btns) {
justify-content: center;
}
:deep(.upload-zone .opt-btn) {
padding: 8px 16px;
font-size: 13px;
border-radius: 6px;
}
:deep(.upload-zone .opt-btn:hover) {
color: #3498db;
background: #333;
border-color: #3498db;
}
:deep(.selected-files) {
font-size: 12px;
color: #5e6878;
max-height: 112px;
overflow-y: auto;
text-align: left;
}
:deep(.more-line) {
color: #a0acbe;
}
:deep(.split-selected-files) {
max-height: 120px;
min-height: 72px;
padding-right: 4px;
}
.btns {
@@ -1636,31 +1621,6 @@ async function batchDeleteHistory(views: TaskItemView[]) {
border-color: #3498db;
}
.selected-files {
margin-top: 14px;
font-size: 12px;
color: #5e6878;
max-height: 112px;
overflow-y: auto;
text-align: left;
}
.selected-files span {
display: block;
margin: 4px 0;
word-break: break-all;
}
.more-line {
color: #a0acbe;
}
.split-selected-files {
max-height: 120px;
min-height: 72px;
padding-right: 4px;
}
.option-group {
margin-bottom: 20px;
}
@@ -40,19 +40,16 @@
<template v-if="asinModeEnabled">
<div class="section-title">ASIN文档 <span class="tag-badge">自定义模式</span></div>
<div class="upload-zone">
<div class="hint">上传包含ASIN的表格文件,按文档内的ASIN进行跟价。</div>
<div class="btns">
<button type="button" class="opt-btn" @click="selectAsinFile">选择ASIN文件</button>
<button type="button" class="opt-btn" @click="selectAsinFolder">选择文件夹</button>
</div>
<div class="selected-files clean-placeholder">
<template v-if="asinFiles.length">
<span v-for="(f, i) in asinFiles" :key="i">{{ f }}</span>
</template>
<span v-else class="text-muted">暂未选择</span>
</div>
</div>
<FileUploadZone
hint="上传包含ASIN的表格文件,按文档内的ASIN进行跟价。"
:names="asinFiles"
files-label="选择ASIN文件"
empty-text="暂未选择"
muted-empty
files-class="clean-placeholder"
@select-files="selectAsinFile"
@select-folder="selectAsinFolder"
/>
</template>
<div class="price-track-shop-candidates">
@@ -219,17 +216,17 @@ import { ElMessage } from 'element-plus'
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
import ModuleTemplateDownload from '@/shared/components/ModuleTemplateDownload.vue'
import ZiniaoVersionSetting from '@/shared/components/ZiniaoVersionSetting.vue'
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView, TaskStatCard } from '@/shared/components/tasks/types'
import { expandBrandFolderRecursive } from '@/shared/api/brand'
import { getPywebviewApi, type PywebviewApi, type UploadedJavaFile } from '@/shared/bridges/pywebview'
import { getPywebviewApi, type PywebviewApi } from '@/shared/bridges/pywebview'
import { useExcelFileUpload } from '@/shared/composables/useExcelFileUpload'
import { getTaskPollIntervalMs } from '@/shared/task-progress-config'
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import {
EXCEL_CSV_EXTENSIONS,
checkQueuePayload,
checkSelectedFiles,
} from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui'
import { useZiniaoVersion } from '@/shared/utils/ziniao-version'
@@ -268,7 +265,6 @@ import {
import { getPollingProgressBatch } from '@/shared/api/task-progress-polling.ts'
import { formatDateTime } from '@/shared/utils/datetime'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete'
import { isRecordMissingError } from '@/shared/utils/task-queue-state.ts'
import { useTablePaging } from '@/shared/composables/useTablePaging.ts'
@@ -318,8 +314,35 @@ const statusModeEnabled = ref(true) // 按商品状态(全量)
const asinModeEnabled = ref(false) // 按指定ASIN文档(自定义)
// 上传文件
const asinFiles = ref<string[]>([]) // ASIN文档
const asinUploadedFiles = ref<UploadedJavaFile[]>([])
const {
files: asinUploadedFiles,
selectedPaths: asinFiles,
selectFiles: selectAsinFile,
selectFolder: selectAsinFolder,
} = useExcelFileUpload({
allowedExtensions: EXCEL_CSV_EXTENSIONS,
// 上传失败时清空选择,避免本地路径残留被当作服务器路径提交给 Java
uploadOptions: { returnEmptyWhenUnavailable: true },
filePathsMode: 'raw',
filesSuccessMessage: (_files, paths) => `已选择 ${paths.length} 个ASIN文件`,
folderSuccessMessage: (_files, items) => `已选择文件夹内 ${items.length} 个ASIN文件`,
folderUnavailableWarning: '当前环境不支持文件夹选择,请在本地客户端中打开',
pickFiles: async () => {
const api = getPywebviewApi()
if (api?.select_files) {
const result = await api.select_files({
filters: [{ name: 'Excel/CSV', extensions: 'xlsx,xls,csv' }],
multiple: true,
})
return result?.paths || null
}
if (api?.select_brand_xlsx_files) {
return await api.select_brand_xlsx_files()
}
ElMessage.warning('当前环境不支持文件选择,请在本地客户端中打开')
return null
},
})
// 是否有有效模式
const hasValidMode = computed(() => statusModeEnabled.value || asinModeEnabled.value)
@@ -533,67 +556,6 @@ function saveActiveLoopRunId() {
setStorageJson(activeLoopStorageKey(), activeLoopRunId.value, !activeLoopRunId.value)
}
// ========== 文件上传 ==========
async function selectAsinFile() {
const api = getPywebviewApi()
let paths: string[] = []
if (api?.select_files) {
const result = await api.select_files({
filters: [{ name: 'Excel/CSV', extensions: 'xlsx,xls,csv' }],
multiple: true,
})
if (!result?.paths?.length) return
if (!(await passGuard(checkSelectedFiles(result.paths, { allowedExtensions: EXCEL_CSV_EXTENSIONS })))) return
paths = result.paths
} else if (api?.select_brand_xlsx_files) {
const selected = await api.select_brand_xlsx_files()
if (!selected?.length) return
if (!(await passGuard(checkSelectedFiles(selected, { allowedExtensions: EXCEL_CSV_EXTENSIONS })))) return
paths = selected
} else {
ElMessage.warning('当前环境不支持文件选择,请在本地客户端中打开')
return
}
try {
asinFiles.value = paths
asinUploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), paths, { returnEmptyWhenUnavailable: true })
ElMessage.success(`已选择 ${paths.length} 个ASIN文件`)
} catch (error) {
// 上传失败时清空选择,避免本地路径残留被当作服务器路径提交给 Java
asinFiles.value = []
asinUploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
async function selectAsinFolder() {
const api = getPywebviewApi()
if (!api?.select_brand_folder) {
ElMessage.warning('当前环境不支持文件夹选择,请在本地客户端中打开')
return
}
try {
const folder = await api.select_brand_folder()
if (!folder) return
const result = await expandBrandFolderRecursive(folder)
if (!result.success || !result.items?.length) {
ElMessage.warning(result.error || '该文件夹下没有可用的 xlsx 文件')
return
}
if (!(await passGuard(checkSelectedFiles(result.items, { allowedExtensions: EXCEL_CSV_EXTENSIONS })))) return
asinFiles.value = result.items.map((item) => item.relativePath || item.absolutePath)
asinUploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), result.items, { returnEmptyWhenUnavailable: true })
ElMessage.success(`已选择文件夹内 ${result.items.length} 个ASIN文件`)
} catch (error) {
// 上传失败时清空选择,避免残留上一批文件让用户误以为新文件已就绪
asinFiles.value = []
asinUploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
// ========== 国家顺序相关 ==========
function resolveAsinRequestPaths() {
// 提交上传返回的 fileKey(与去重/转换/采集等模块一致):服务端按 key 反查上传临时目录,
@@ -1798,29 +1760,23 @@ async function batchDeleteHistory(views: TaskItemView[]) {
margin-bottom: 10px;
}
.upload-zone {
border: 1px dashed #3e4a62;
border-radius: 10px;
/* upload-zone 已抽到 FileUploadZone 组件;这里保留本 Tab 的差异样式 */
:deep(.upload-zone) {
padding: 16px;
background: #242424;
margin-bottom: 20px;
}
.hint {
color: #5e6878;
:deep(.upload-zone .hint) {
font-size: 12px;
margin-bottom: 12px;
line-height: 1.5;
text-align: left;
}
.btns {
display: flex;
gap: 10px;
:deep(.upload-zone .btns) {
margin-bottom: 10px;
}
.selected-files {
:deep(.selected-files) {
margin-top: 8px;
font-size: 12px;
display: flex;
@@ -1836,11 +1792,11 @@ async function batchDeleteHistory(views: TaskItemView[]) {
gap: 4px;
}
.selected-files span:not(.more-line) {
:deep(.selected-files span) {
color: #a0acbe;
}
.more-line {
:deep(.more-line) {
color: #5e6878;
font-style: italic;
}
@@ -5,29 +5,19 @@
<div class="main-content">
<aside class="left-panel left-panel--with-template">
<div class="section-title">上传模块</div>
<div class="upload-zone">
<div class="hint">
选择需要上架的 Excel 文件或文件夹;每个 Excel 文件名会作为店铺名进行匹配。
</div>
<div class="btns">
<button type="button" class="opt-btn" :disabled="selectionDisabled" @click="selectFiles">
{{ uploading ? '上传中...' : '选择 Excel 文件' }}
</button>
<button type="button" class="opt-btn" :disabled="selectionDisabled" @click="selectFolder">
选择文件夹
</button>
</div>
<div class="selected-files clean-placeholder publish-selected-files">
<template v-if="selectedPaths.length">
<span v-for="path in displayPaths" :key="path">{{ path }}</span>
<span v-if="selectedPaths.length > displayPaths.length" class="more-line">
还有 {{ selectedPaths.length - displayPaths.length }} 个文件未展开显示
</span>
</template>
<span v-else>暂未选择上架文件</span>
</div>
</div>
<FileUploadZone
hint="选择需要上架的 Excel 文件或文件夹;每个 Excel 文件名会作为店铺名进行匹配。"
:names="selectedPaths"
:max-display="8"
files-label="选择 Excel 文件"
uploading-label="上传中..."
:uploading="uploading"
:disabled="selectionDisabled"
empty-text="暂未选择上架文件"
files-class="clean-placeholder publish-selected-files"
@select-files="selectFiles"
@select-folder="selectFolder"
/>
<div class="section-title">国家选择</div>
<div class="country-group">
@@ -187,11 +177,11 @@ import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
import ModuleTemplateDownload from '@/shared/components/ModuleTemplateDownload.vue'
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView, TaskStatCard } from '@/shared/components/tasks/types'
import ZiniaoVersionSetting from '@/shared/components/ZiniaoVersionSetting.vue'
import { expandBrandFolderRecursive } from '@/shared/api/brand'
import {
activatePublishFile,
activatePublishTask,
@@ -206,23 +196,21 @@ import {
type PublishFileItem,
type PublishTaskDetailVo,
} from '@/shared/api/java-modules'
import { getPywebviewApi, type UploadedJavaFile } from '@/shared/bridges/pywebview'
import { getPywebviewApi } from '@/shared/bridges/pywebview'
import { useExcelFileUpload } from '@/shared/composables/useExcelFileUpload'
import { useTaskProgressLoop } from '@/shared/composables/useTaskProgressLoop'
import { getTaskPollIntervalMs } from '@/shared/task-progress-config'
import { createCategorizedTimers } from '@/shared/utils/categorized-timers'
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
import {
EXCEL_EXTENSIONS,
checkParseResult,
checkQueuePayload,
checkSelectedFiles,
collectDistinctErrors,
guardBlocked,
} from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui'
import { useZiniaoVersion, type ZiniaoVersion } from '@/shared/utils/ziniao-version'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete'
const COUNTRY_OPTIONS = [
@@ -252,9 +240,23 @@ interface StoredPublishQueueState {
pending: PublishQueueBatch[]
}
const selectedPaths = ref<string[]>([])
const uploadedFiles = ref<UploadedJavaFile[]>([])
const uploading = ref(false)
const {
files: uploadedFiles,
selectedPaths,
uploading,
selectFiles,
selectFolder,
} = useExcelFileUpload({
requireUploadCapability: true,
folderRequireUploadCapability: true,
pickFilesUnavailableWarning: '当前环境不支持文件选择或上传,请在本机客户端中打开',
folderUnavailableWarning: '当前环境不支持文件夹选择或上传,请在本机客户端中打开',
folderEmptyWarning: (error) => error || '该文件夹下没有 xlsx 文件',
// 展示原始本机路径;文件夹展开后展示 relativePath || absolutePath(与原实现一致)
filePathsMode: 'raw',
filesSuccessMessage: (files) => `已上传 ${files.length} 个待上架文件`,
folderSuccessMessage: (files) => `已上传文件夹内 ${files.length} 个 xlsx 文件`,
})
const parsing = ref(false)
const queueWorkerRunning = ref(false)
const queueMessage = ref('')
@@ -370,7 +372,6 @@ const progressLoop = useTaskProgressLoop<PublishTaskDetailVo>({
},
})
const displayPaths = computed(() => selectedPaths.value.slice(0, 8))
const activeBatchHasWork = computed(() => pendingFileIds.value.length > 0 || activeFileId.value != null)
const hasQueueWork = computed(() => activeBatchHasWork.value || queuedBatches.value.length > 0)
const selectionDisabled = computed(() => uploading.value || parsing.value)
@@ -481,60 +482,6 @@ function onSyncCountryChange(code: string, event: Event) {
syncCountries.value = COUNTRY_OPTIONS.map((item) => item.code).filter((item) => selected.has(item))
}
async function selectFiles() {
const api = getPywebviewApi()
if (!api?.select_brand_xlsx_files || !api.upload_file_to_java) {
ElMessage.warning('当前环境不支持文件选择或上传,请在本机客户端中打开')
return
}
try {
const paths = await api.select_brand_xlsx_files()
if (!paths?.length) return
if (!(await passGuard(checkSelectedFiles(paths, { allowedExtensions: EXCEL_EXTENSIONS })))) return
uploading.value = true
const uploaded = await uploadPathsToJava(getPywebviewApi(), paths)
selectedPaths.value = paths
uploadedFiles.value = uploaded
ElMessage.success(`已上传 ${uploaded.length} 个待上架文件`)
} catch (error) {
// 上传失败时清空选择,避免残留上一批文件让用户误以为新文件已就绪
selectedPaths.value = []
uploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
} finally {
uploading.value = false
}
}
async function selectFolder() {
const api = getPywebviewApi()
if (!api?.select_brand_folder || !api.upload_file_to_java) {
ElMessage.warning('当前环境不支持文件夹选择或上传,请在本机客户端中打开')
return
}
try {
const folder = await api.select_brand_folder()
if (!folder) return
const result = await expandBrandFolderRecursive(folder)
if (!result.success || !result.items?.length) {
ElMessage.warning(result.error || '该文件夹下没有 xlsx 文件')
return
}
if (!(await passGuard(checkSelectedFiles(result.items, { allowedExtensions: EXCEL_EXTENSIONS })))) return
uploading.value = true
const uploaded = await uploadPathsToJava(getPywebviewApi(), result.items)
selectedPaths.value = result.items.map((item) => item.relativePath || item.absolutePath)
uploadedFiles.value = uploaded
ElMessage.success(`已上传文件夹内 ${uploaded.length} 个 xlsx 文件`)
} catch (error) {
selectedPaths.value = []
uploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
} finally {
uploading.value = false
}
}
function isMatchedFile(file: PublishFileItem) {
const matchStatus = normalizeStatus(file.matchStatus)
return Boolean(
@@ -1209,7 +1156,12 @@ async function batchDeleteHistory(views: TaskItemView[]) {
.left-panel { width: 400px; background: #242424; padding: 20px; overflow-y: auto; border-right: 1px solid #2e3a52; }
.right-panel { flex: 1; display: flex; flex-direction: column; min-width: 0; background: #242424; }
.section-title { margin-bottom: 10px; color: #a0acbe; font-size: 13px; }
.upload-zone { margin-bottom: 20px; padding: 20px; border: 1px dashed #3e4a62; border-radius: 8px; background: #242424; text-align: center; }
/* upload-zone 已抽到 FileUploadZone 组件;这里保留本 Tab 的差异样式 */
:deep(.upload-zone) { margin-bottom: 20px; padding: 20px; border-radius: 8px; text-align: center; }
:deep(.upload-zone .btns) { justify-content: center; }
:deep(.upload-zone .opt-btn) { padding: 8px 16px; font-size: 13px; border-radius: 6px; }
:deep(.upload-zone .opt-btn:hover:not(:disabled)) { border-color: #3498db; color: #67b7ef; }
:deep(.upload-zone .opt-btn:disabled) { opacity: .55; cursor: not-allowed; }
.hint, .loading-msg { color: #5e6878; font-size: 12px; line-height: 1.5; }
.btns, .run-row { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; }
.btns { justify-content: center; }
@@ -1217,9 +1169,8 @@ async function batchDeleteHistory(views: TaskItemView[]) {
.opt-btn { padding: 8px 16px; border: 1px solid #3e4a62; background: #242424; color: #c8d2e2; font-size: 13px; }
.opt-btn:hover:not(:disabled) { border-color: #3498db; color: #67b7ef; }
.opt-btn:disabled, .btn-run:disabled { opacity: .55; cursor: not-allowed; }
.selected-files { max-height: 120px; min-height: 72px; margin-top: 14px; overflow-y: auto; color: #5e6878; font-size: 12px; text-align: left; }
.selected-files span { display: block; margin: 4px 0; word-break: break-all; }
.more-line { color: #a0acbe; }
:deep(.publish-selected-files) { max-height: 120px; min-height: 72px; overflow-y: auto; color: #5e6878; }
:deep(.more-line) { color: #a0acbe; }
.country-group { margin-bottom: 12px; padding: 14px; border: 1px solid #323e58; border-radius: 8px; background: #242424; }
.country-group-title { margin-bottom: 10px; color: #a0acbe; font-size: 12px; }
.country-checks { display: flex; flex-wrap: wrap; gap: 10px 16px; }
File diff suppressed because it is too large Load Diff
@@ -5,23 +5,16 @@
<div class="main-content">
<aside class="left-panel">
<div class="section-title">选择文件</div>
<div class="upload-zone">
<div class="hint">选择需要拆分的 Excel 文件或文件夹,按规则拆成多个 Excel 文件。</div>
<div class="btns">
<button type="button" class="opt-btn" @click="selectSplitFiles">选择 Excel 文件</button>
<button type="button" class="opt-btn" @click="selectSplitFolder">选择文件夹</button>
</div>
<div class="selected-files clean-placeholder split-selected-files">
<template v-if="splitSelectedPaths.length">
<span v-for="path in splitDisplayPaths" :key="path">{{ path }}</span>
<span v-if="splitSelectedPaths.length > splitDisplayPaths.length" class="more-line">
还有 {{ splitSelectedPaths.length - splitDisplayPaths.length }} 个文件未展开显示
</span>
</template>
<span v-else>暂未选择待拆分文件</span>
</div>
</div>
<FileUploadZone
hint="选择需要拆分的 Excel 文件或文件夹,按规则拆成多个 Excel 文件。"
:names="splitSelectedPaths"
:max-display="8"
files-label="选择 Excel 文件"
empty-text="暂未选择待拆分文件"
files-class="clean-placeholder split-selected-files"
@select-files="selectSplitFiles"
@select-folder="selectSplitFolder"
/>
<div class="section-title">保留列设置</div>
<div class="option-group column-option-group">
@@ -139,21 +132,40 @@
import { computed, onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView, TaskStatCard } from '@/shared/components/tasks/types'
import { expandBrandFolderRecursive } from '@/shared/api/brand'
import { deleteSplitHistory, getExcelInfo, getSplitHistory, getSplitResultDownloadUrl, runSplit, type SplitResultItem } from '@/shared/api/java-modules'
import { getPywebviewApi, type UploadedJavaFile } from '@/shared/bridges/pywebview'
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import { EXCEL_EXTENSIONS, checkSelectedFiles, collectDistinctErrors, guardBlocked } from '@/shared/dispatch-guard'
import { collectDistinctErrors, guardBlocked } from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui'
import { useExcelFileUpload } from '@/shared/composables/useExcelFileUpload'
import { formatDateTime } from '@/shared/utils/datetime'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
import { runBatchDelete } from '@/shared/utils/batch-delete'
const splitSelectedPaths = ref<string[]>([])
const {
files: splitUploadedFiles,
selectedPaths: splitSelectedPaths,
selectFiles: selectSplitFiles,
selectFolder: selectSplitFolder,
} = useExcelFileUpload({
// 展示原始本机路径;文件夹展开后只展示相对路径(与原实现一致)
filePathsMode: 'raw',
folderPathsMode: 'relative',
filesSuccessMessage: (_files, paths) => `已选择 ${paths.length} 个待拆分文件`,
folderSuccessMessage: (_files, items) => `已选择文件夹内 ${items.length} 个 xlsx 文件`,
folderEmptyWarning: (error) => error || '该文件夹下没有 xlsx 文件',
onFolderPicked: (folder) => {
splitArchiveName.value = folder.split(/[/\\]/).filter(Boolean).pop() || ''
},
onUploaded: async (info) => {
if (info.action === 'files') splitArchiveName.value = ''
if (info.files.length > 0) {
await loadSplitInfo(info.files[0].fileKey)
}
},
})
const splitArchiveName = ref('')
const splitUploadedFiles = ref<UploadedJavaFile[]>([])
const splitAvailableColumns = ref<string[]>([])
const splitSelectedColumns = ref<string[]>([])
const splitTotalRows = ref(0)
@@ -162,7 +174,6 @@ const splitRowsPerFile = ref<number | null>(2000)
const splitParts = ref<number | null>(5)
const splitRunning = ref(false)
const splitResultItems = ref<SplitResultItem[]>([])
const splitDisplayPaths = computed(() => splitSelectedPaths.value.slice(0, 8))
// ---- 右侧任务面板统一视图(TaskCenterPanel)----
const splitCards = computed<TaskStatCard[]>(() => [
@@ -232,67 +243,6 @@ async function loadSplitInfo(fileKey: string) {
splitTotalRows.value = result.totalRows || 0
}
async function handleSelectedPaths(paths: string[], successMessage: string) {
splitSelectedPaths.value = paths
splitArchiveName.value = ''
splitUploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), paths)
if (splitUploadedFiles.value.length > 0) {
await loadSplitInfo(splitUploadedFiles.value[0].fileKey)
}
ElMessage.success(successMessage)
}
async function selectSplitFiles() {
const api = getPywebviewApi()
if (!api?.select_brand_xlsx_files) {
ElMessage.warning('当前环境不支持文件选择,请在本机客户端中打开')
return
}
try {
const paths = await api.select_brand_xlsx_files()
if (!paths?.length) return
if (!(await passGuard(checkSelectedFiles(paths, { allowedExtensions: EXCEL_EXTENSIONS })))) return
await handleSelectedPaths(paths, `已选择 ${paths.length} 个待拆分文件`)
} catch (error) {
splitSelectedPaths.value = []
splitUploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
async function selectSplitFolder() {
const api = getPywebviewApi()
if (!api?.select_brand_folder) {
ElMessage.warning('当前环境不支持文件夹选择,请在本机客户端中打开')
return
}
try {
const folder = await api.select_brand_folder()
if (!folder) return
splitArchiveName.value = folder.split(/[/\\]/).filter(Boolean).pop() || ''
const result = await expandBrandFolderRecursive(folder)
if (result.success && result.items?.length) {
if (!(await passGuard(checkSelectedFiles(result.items, { allowedExtensions: EXCEL_EXTENSIONS })))) return
splitSelectedPaths.value = result.items.map((item) => item.relativePath)
splitUploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), result.items)
if (splitUploadedFiles.value.length > 0) {
await loadSplitInfo(splitUploadedFiles.value[0].fileKey)
}
ElMessage.success(`已选择文件夹内 ${result.items.length} 个 xlsx 文件`)
return
}
ElMessage.warning(result.error || '该文件夹下没有 xlsx 文件')
} catch (error) {
splitSelectedPaths.value = []
splitUploadedFiles.value = []
ElMessage.error(error instanceof Error ? error.message : '选择失败')
}
}
async function submitSplitRun() {
if (!splitUploadedFiles.value.length) {
ElMessage.warning('请先选择待拆分 Excel 文件或文件夹')
@@ -448,33 +398,61 @@ async function batchDeleteHistory(views: TaskItemView[]) {
margin-bottom: 10px;
}
.upload-zone {
border: 1px dashed #3e4a62;
border-radius: 10px;
/* upload-zone 已抽到 FileUploadZone 组件;这里保留本 Tab 的差异样式 */
:deep(.upload-zone) {
padding: 24px;
text-align: center;
background: #242424;
margin-bottom: 20px;
transition: all 0.2s ease;
}
.upload-zone:hover {
:deep(.upload-zone:hover) {
border-color: #3498db;
background: #242424;
}
.hint {
color: #5e6878;
:deep(.upload-zone .hint) {
font-size: 13px;
margin-bottom: 12px;
line-height: 1.5;
}
.btns {
display: flex;
gap: 10px;
:deep(.upload-zone .btns) {
justify-content: center;
flex-wrap: wrap;
}
:deep(.upload-zone .opt-btn) {
padding: 8px 16px;
font-size: 13px;
border-radius: 6px;
}
:deep(.upload-zone .opt-btn:hover) {
color: #3498db;
background: #333;
border-color: #3498db;
}
:deep(.upload-zone .opt-btn:disabled) {
cursor: not-allowed;
opacity: 0.55;
}
:deep(.selected-files) {
font-size: 12px;
color: #5e6878;
max-height: 112px;
overflow-y: auto;
text-align: left;
}
:deep(.more-line) {
color: #a0acbe;
}
:deep(.split-selected-files) {
max-height: 120px;
min-height: 72px;
padding-right: 4px;
}
.opt-btn,
@@ -506,31 +484,6 @@ async function batchDeleteHistory(views: TaskItemView[]) {
border-color: #3498db;
}
.selected-files {
margin-top: 14px;
font-size: 12px;
color: #5e6878;
max-height: 112px;
overflow-y: auto;
text-align: left;
}
.selected-files span {
display: block;
margin: 4px 0;
word-break: break-all;
}
.more-line {
color: #a0acbe;
}
.split-selected-files {
max-height: 120px;
min-height: 72px;
padding-right: 4px;
}
.option-group {
margin-bottom: 20px;
}
@@ -7,6 +7,8 @@ export interface UploadedJavaFile {
localPath: string;
size: number;
relativePath?: string;
/** OSS 上传模式下返回的跨节点访问地址(uploadOss=true 时桌面桥可能附带) */
url?: string;
}
export type ProxyMode = 1 | 2;
@@ -0,0 +1,142 @@
<template>
<div class="upload-zone">
<div class="hint">{{ hint }}</div>
<div class="btns">
<button type="button" class="opt-btn" :disabled="disabled" @click="emit('select-files')">
{{ uploading && uploadingLabel ? uploadingLabel : filesLabel }}
</button>
<button v-if="showFolderButton" type="button" class="opt-btn" :disabled="disabled" @click="emit('select-folder')">
{{ folderLabel }}
</button>
</div>
<div class="selected-files" :class="filesClass">
<template v-if="names.length">
<span v-for="name in visibleNames" :key="name" :title="nameTitle ? name : undefined">{{ name }}</span>
<span v-if="names.length > visibleNames.length" class="more-line">
还有 {{ names.length - visibleNames.length }} 个文件未展开显示
</span>
</template>
<span v-else :class="{ 'text-muted': mutedEmpty }">{{ emptyText }}</span>
</div>
<div v-if="footerTip" class="bridge-tip">{{ footerTip }}</div>
</div>
</template>
<script setup lang="ts">
import { computed } from 'vue'
/**
* 「上传文件」区块:选择 Excel / 文件夹按钮 + 已选文件列表。
*
* 抽自各品牌 Tab 逐字重复的 upload-zone 模板与样式;差异点全部 props 化。
* Tab 保留自己的业务状态与 selectFiles / selectFolder 实现(通常来自
* useExcelFileUpload),组件只负责展示与点击事件。
*/
const props = withDefaults(
defineProps<{
/** 区块顶部提示文案 */
hint: string
/** 已选文件展示列表(完整列表,超出 maxDisplay 时折叠) */
names: string[]
/** 未选择时的占位文案 */
emptyText?: string
/** 「选择 Excel」按钮文案 */
filesLabel?: string
/** 上传中时「选择 Excel」按钮的文案(配合 uploading) */
uploadingLabel?: string
/** 「选择文件夹」按钮文案 */
folderLabel?: string
/** 是否显示「选择文件夹」按钮 */
showFolderButton?: boolean
/** 两个按钮是否禁用 */
disabled?: boolean
/** 上传中状态(配合 uploadingLabel) */
uploading?: boolean
/** 超过该数量折叠展示;不传则全量展示 */
maxDisplay?: number
/** selected-files 容器的附加 class(如 clean-placeholder) */
filesClass?: string
/** 空态文案是否加 text-muted */
mutedEmpty?: boolean
/** 文件名 span 是否带 title 提示完整路径 */
nameTitle?: boolean
/** 区块底部的附加提示(如浏览器环境的桥接提示),空串不显示 */
footerTip?: string
}>(),
{
emptyText: '暂未选择文件',
filesLabel: '选择 Excel',
folderLabel: '选择文件夹',
showFolderButton: true,
disabled: false,
uploading: false,
mutedEmpty: false,
nameTitle: false,
footerTip: '',
},
)
const emit = defineEmits<{
(event: 'select-files'): void
(event: 'select-folder'): void
}>()
const visibleNames = computed(() =>
props.maxDisplay && props.maxDisplay > 0 ? props.names.slice(0, props.maxDisplay) : props.names,
)
</script>
<style scoped>
/* 与多数品牌 Tab 原有的 upload-zone 写法保持一致;个别 Tab 的差异样式由
Tab 自身用 :deep(.upload-zone) 等覆写 */
.upload-zone {
border: 1px dashed #3e4a62;
border-radius: 10px;
padding: 18px;
background: #242424;
margin-bottom: 18px;
}
.hint {
color: #5e6878;
font-size: 12px;
line-height: 1.5;
}
.btns {
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.opt-btn {
border: none;
cursor: pointer;
border-radius: 7px;
padding: 8px 14px;
color: #c8d2e2;
background: #242424;
border: 1px solid #3e4a62;
}
.selected-files {
margin-top: 14px;
color: #a0acbe;
font-size: 12px;
word-break: break-all;
}
.selected-files span {
display: block;
margin: 4px 0;
}
.selected-files .more-line {
color: #5e6878;
}
.selected-files .text-muted {
color: #5e6878;
font-style: italic;
}
</style>
@@ -0,0 +1,210 @@
import { ref, type Ref } from 'vue'
import { ElMessage } from 'element-plus'
import { getPywebviewApi, type UploadedJavaFile } from '../bridges/pywebview.ts'
import { expandBrandFolderRecursive, type BrandExpandFolderItem } from '../api/types/modules/brand.ts'
import { EXCEL_EXTENSIONS, checkSelectedFiles } from '../dispatch-guard.ts'
import { passGuard } from '../dispatch-guard-ui.ts'
import { uploadPathsToJava, type UploadPathsToJavaOptions } from '../utils/upload-to-java.ts'
/**
* 「选文件 / 选文件夹 → 校验 → 上传 → 重置状态」组合式函数。
*
* 抽自各品牌 Tab 逐字重复的 selectFiles / selectFolder:能力检查 → 桥选路径 →
* passGuard(checkSelectedFiles(...)) → uploadPathsToJava(...) → 回填展示列表 →
* 成功 / 失败提示。各 Tab 的差异点(提示文案、展示路径来源、上传参数、成功后
* 的本地状态重置)全部参数化,保持每个 Tab 的既有行为与文案不变。
*
* 使用方式:
* const upload = useExcelFileUpload({
* onUploaded: () => { parseResult.value = null },
* })
* upload.selectFiles() / upload.selectFolder()
* upload.files / upload.selectedPaths / upload.uploading
*/
/** 选文件动作:成功回填的展示路径来源 */
export type ExcelFilePathsMode = 'uploaded' | 'raw'
/** 选文件夹动作:成功回填的展示路径来源 */
export type ExcelFolderPathsMode = 'items' | 'relative' | 'uploaded'
export interface ExcelFileUploadSuccessInfo {
action: 'files' | 'folder'
/** 上传结果列表 */
files: UploadedJavaFile[]
/** 展示用路径列表(与 selectedPaths ref 一致) */
displayPaths: string[]
/** 选文件动作的原始本机路径 */
paths?: string[]
/** 选文件夹动作的文件夹路径 */
folder?: string
/** 选文件夹动作展开出的条目 */
items?: BrandExpandFolderItem[]
}
export interface UseExcelFileUploadOptions {
/** 允许的扩展名;默认 EXCEL_EXTENSIONS */
allowedExtensions?: readonly string[]
/** 透传给 uploadPathsToJava 的参数(uploadOss / returnEmptyWhenUnavailable) */
uploadOptions?: UploadPathsToJavaOptions
/**
* 自定义「选择文件」实现(默认走桥的 select_brand_xlsx_files)。
* 返回 null / undefined / 空数组一律视为取消。
*/
pickFiles?: () => Promise<string[] | null | undefined>
/** 自定义「展开文件夹」实现(默认 expandBrandFolderRecursive) */
expandFolder?: (folder: string) => Promise<{ success: boolean; items?: BrandExpandFolderItem[]; error?: string }>
/** 选文件能力检查是否同时要求 upload_file_to_java;默认 false */
requireUploadCapability?: boolean
/** 选文件夹能力检查是否同时要求 upload_file_to_java;默认 false */
folderRequireUploadCapability?: boolean
/** 选文件能力缺失时的 warning 文案 */
pickFilesUnavailableWarning?: string
/** 选文件夹能力缺失时的 warning 文案 */
folderUnavailableWarning?: string
/** 文件夹下没有可用文件时的 warning 文案;默认 (error) => error || '该文件夹下没有可用的 xlsx 文件' */
folderEmptyWarning?: (error?: string) => string
/** 选文件成功提示;返回空串则不提示(默认不提示) */
filesSuccessMessage?: (files: UploadedJavaFile[], paths: string[]) => string
/** 选文件夹成功提示;返回空串则不提示 */
folderSuccessMessage?: (files: UploadedJavaFile[], items: BrandExpandFolderItem[]) => string
/** 失败时 ElMessage.error 的兜底文案;默认 '选择失败' */
errorFallbackMessage?: string | ((action: 'files' | 'folder') => string)
/** 选文件成功后回填展示列表的方式;默认 'uploaded'(取上传结果相对路径/文件名) */
filePathsMode?: ExcelFilePathsMode
/** 选文件夹成功后回填展示列表的方式;默认 'items'(relativePath || absolutePath) */
folderPathsMode?: ExcelFolderPathsMode
/** 上传成功回调(重置 Tab 本地解析状态等);可为 async,成功提示会等它完成 */
onUploaded?: (info: ExcelFileUploadSuccessInfo) => void | Promise<void>
/** 上传失败回调(清理 Tab 本地状态);失败时组件自身已清空 files/selectedPaths(除非 clearOnError=false) */
onFailed?: (action: 'files' | 'folder') => void
/** 选到文件夹后(展开前)回调;用于记录文件夹名等 Tab 本地状态 */
onFolderPicked?: (folder: string) => void
/** 上传失败时是否清空 files/selectedPaths;默认 true(品牌检测 Tab 上传失败保留旧文件) */
clearOnError?: boolean
}
export interface UseExcelFileUploadReturn {
/** 上传成功的结果列表 */
files: Ref<UploadedJavaFile[]>
/** 展示用路径列表(文件动作为原始路径或上传名,文件夹动作为展开条目路径) */
selectedPaths: Ref<string[]>
/** 上传进行中(仅覆盖 uploadPathsToJava 调用窗口) */
uploading: Ref<boolean>
selectFiles: () => Promise<void>
selectFolder: () => Promise<void>
/** 清空已选 / 已上传(不触发 onFailed) */
clear: () => void
}
function defaultErrorMessage(error: unknown, action: 'files' | 'folder', fallback: string | ((a: 'files' | 'folder') => string)) {
if (error instanceof Error && error.message) return error.message
return typeof fallback === 'function' ? fallback(action) : fallback
}
export function useExcelFileUpload(options: UseExcelFileUploadOptions = {}): UseExcelFileUploadReturn {
const allowedExtensions = options.allowedExtensions ?? EXCEL_EXTENSIONS
const requireUploadCapability = options.requireUploadCapability ?? false
const folderRequireUploadCapability = options.folderRequireUploadCapability ?? false
const pickWarning = options.pickFilesUnavailableWarning ?? '当前环境不支持文件选择,请在本机客户端中打开'
const folderWarning = options.folderUnavailableWarning ?? '当前环境不支持文件夹选择,请在本机客户端中打开'
const emptyWarning = options.folderEmptyWarning ?? ((error?: string) => error || '该文件夹下没有可用的 xlsx 文件')
const fallbackMessage = options.errorFallbackMessage ?? '选择失败'
const filePathsMode = options.filePathsMode ?? 'uploaded'
const folderPathsMode = options.folderPathsMode ?? 'items'
const clearOnError = options.clearOnError ?? true
const files = ref<UploadedJavaFile[]>([])
const selectedPaths = ref<string[]>([])
const uploading = ref(false)
function resetLocal() {
files.value = []
selectedPaths.value = []
}
function fail(action: 'files' | 'folder', error: unknown) {
if (clearOnError) resetLocal()
options.onFailed?.(action)
ElMessage.error(defaultErrorMessage(error, action, fallbackMessage))
}
async function selectFiles() {
try {
let paths: string[] | null | undefined
if (options.pickFiles) {
paths = await options.pickFiles()
} else {
const api = getPywebviewApi()
if (!api?.select_brand_xlsx_files || (requireUploadCapability && !api.upload_file_to_java)) {
ElMessage.warning(pickWarning)
return
}
paths = await api.select_brand_xlsx_files()
}
if (!paths?.length) return
if (!(await passGuard(checkSelectedFiles(paths, { allowedExtensions })))) return
uploading.value = true
const uploaded = await uploadPathsToJava(getPywebviewApi(), paths, options.uploadOptions)
files.value = uploaded
selectedPaths.value = filePathsMode === 'raw'
? [...paths]
: uploaded.map((f) => f.relativePath || f.originalFilename || f.fileKey)
await options.onUploaded?.({ action: 'files', files: uploaded, displayPaths: selectedPaths.value, paths: [...paths] })
const message = options.filesSuccessMessage?.(uploaded, paths)
if (message) ElMessage.success(message)
} catch (error) {
fail('files', error)
} finally {
uploading.value = false
}
}
async function selectFolder() {
try {
const api = getPywebviewApi()
if (!api?.select_brand_folder || (folderRequireUploadCapability && !api.upload_file_to_java)) {
ElMessage.warning(folderWarning)
return
}
const folder = await api.select_brand_folder()
if (!folder) return
options.onFolderPicked?.(folder)
const result = options.expandFolder
? await options.expandFolder(folder)
: await expandBrandFolderRecursive(folder)
if (!result.success || !result.items?.length) {
ElMessage.warning(emptyWarning(result.error))
return
}
if (!(await passGuard(checkSelectedFiles(result.items, { allowedExtensions })))) return
uploading.value = true
const uploaded = await uploadPathsToJava(getPywebviewApi(), result.items, options.uploadOptions)
files.value = uploaded
selectedPaths.value = resolveFolderPaths(folderPathsMode, uploaded, result.items)
await options.onUploaded?.({ action: 'folder', files: uploaded, displayPaths: selectedPaths.value, folder, items: result.items })
const message = options.folderSuccessMessage?.(uploaded, result.items)
if (message) ElMessage.success(message)
} catch (error) {
fail('folder', error)
} finally {
uploading.value = false
}
}
function clear() {
resetLocal()
}
return { files, selectedPaths, uploading, selectFiles, selectFolder, clear }
}
function resolveFolderPaths(
mode: ExcelFolderPathsMode,
uploaded: UploadedJavaFile[],
items: BrandExpandFolderItem[],
): string[] {
if (mode === 'relative') return items.map((item) => item.relativePath)
if (mode === 'uploaded') return uploaded.map((f) => f.relativePath || f.originalFilename || f.fileKey)
return items.map((item) => item.relativePath || item.absolutePath)
}
@@ -0,0 +1,713 @@
import { computed, ref, type ComputedRef, type Ref } from 'vue'
import { ElMessage } from 'element-plus'
import { getTaskPollIntervalMs } from '../task-progress-config.ts'
import { createCategorizedTimers } from '../utils/categorized-timers.ts'
import { saveUrlWithProgress } from '../utils/download-progress.ts'
import { runBatchDelete } from '../utils/batch-delete.ts'
import { confirmTaskDeletion } from '../task-delete-confirm.ts'
import type { TaskItemView } from '../components/tasks/types'
/**
* 「实时任务面板」组合式函数(BrandSimilarAsinTab / BrandAppearancePatentTab 的公共骨架)。
*
* 封装两个 Tab 逐字克隆的那部分:轮询 ID 持久化(localStorage,含 uid 后缀)、
* 双队列轮询(任务队列 + 结果文件生成队列)、live 进度合并兜底、history 去重合并、
* pending 文件任务结算、seed(从历史恢复)、下载 / 删除 / 批量删除胶水。
*
* 两 Tab 的真实差异全部参数化(见 UseLiveTaskBoardOptions):
* - 状态归一化 normalizeTaskStatus / isResultPreparing(两 Tab 语义不同)
* - 终态后是否登记 pending 文件任务、哪些任务状态参与文件结算
* - 进度批量请求的预/后处理(SimilarAsin 的 force 节流、Patent 的 light 行版本检查)
* - 轮询回调抛错时是否打断轮询(SimilarAsin 吞掉、Patent 上抛——保持原行为)
* - 下载 URL / 文件名兜底 / 进度 key 前缀、删除判定与本地清理
*
* 其余手写轮询的 Tab(DeleteBrand/PatrolDelete/QueryAsin/Withdraw 等)后续可渐进接入。
*/
/** 两个模块 history/progress item 的公共字段(SimilarAsinHistoryItem / AppearancePatentHistoryItem 结构一致) */
export interface LiveTaskHistoryItem {
resultId?: number
taskId?: number
sourceFilename?: string
resultFilename?: string
downloadUrl?: string
fileJobId?: number
fileStatus?: string
fileError?: string
fileReady?: boolean
fileProgressPercent?: number
fileProgressCurrent?: number
fileProgressTotal?: number
fileProgressMessage?: string
taskStatus?: string
success?: boolean
error?: string
rowCount?: number
createdAt?: string
startedAt?: string
finishedAt?: string
}
/** 批量进度响应的公共形状 */
export interface LiveTaskProgressDetail<TItem extends LiveTaskHistoryItem> {
task?: { id?: number; status?: string }
items?: TItem[]
}
export interface LiveTaskProgressBatch<TItem extends LiveTaskHistoryItem> {
items?: LiveTaskProgressDetail<TItem>[]
missingTaskIds?: number[]
}
export interface TaskBoardDashboardCounts {
pendingTaskCount: number
processedTaskCount: number
successTaskCount: number
failedTaskCount: number
}
export interface UseLiveTaskBoardOptions<T extends LiveTaskHistoryItem> {
/** categorized-timers 命名空间,例如 'similar-asin' */
scope: string
/** 轮询 ID 的 localStorage key(含 uid 后缀),如 () => `similar-asin:tasks:${uid}` */
pollingStorageKey: () => string
/** 历史记录接口 */
fetchHistory: () => Promise<{ items?: T[] }>
/** 批量进度接口 */
fetchProgressBatch: (taskIds: number[], options: { force: boolean }) => Promise<LiveTaskProgressBatch<T>>
/** 统计卡片接口 */
fetchDashboard: () => Promise<TaskBoardDashboardCounts>
/** 状态归一化(两 Tab 实现不同,必传) */
normalizeTaskStatus: (item: T) => string
/** 结果文件是否在生成中(两 Tab 实现不同,必传) */
isResultPreparing: (item: T) => boolean
/**
* 任务到达终态(SUCCESS/FAILED)后是否登记为「结果生成中」待跟踪任务。
* SimilarAsin: status === 'SUCCESS';Patent: item 存在且 isResultPreparing(item)。
*/
addPendingAfterFinalStatus: (task: { id?: number; status?: string }, item: T | undefined) => boolean
/**
* settlePendingFileTasks:对已找到 history 条目、文件未完成的任务,是否继续保留跟踪。
* SimilarAsin: taskStatus === 'SUCCESS';Patent: SUCCESS || FAILED。
*/
keepPendingFileTask: (item: T, taskStatus: string) => boolean
/** 终态后进行文件状态结算检查的任务状态集合。SimilarAsin: ['SUCCESS'];Patent: ['SUCCESS','FAILED'] */
settleOnTaskStatuses: string[]
/**
* 批量进度请求预处理:可裁剪 taskIds / 决定 force;返回 null 表示本轮直接跳过
* (Patent 的 light 行版本检查)。缺省为全量拉取、force=false。
*/
beforeBatch?: (
taskIds: number[],
pendingFileTaskIds: number[],
) => Promise<{ taskIds: number[]; force: boolean } | null> | { taskIds: number[]; force: boolean } | null
/** 批量进度请求后处理(SimilarAsin 的 force 节流登记)。 */
afterBatch?: (taskIds: number[], pendingFileTaskIds: number[], forced: boolean) => void
/**
* 轮询回调抛错时的处理:
* - 'swallow'(SimilarAsin):瞬时失败不打断轮询;
* - 'propagate'(Patent):异常上抛,轮询停止(与原实现一致)。
*/
pollErrorMode?: 'swallow' | 'propagate'
/** pending 文件任务被移除时的回调(SimilarAsin 的 forceThrottle.clear) */
onPendingFileTaskRemoved?: (taskId: number) => void
/** 当前任务区顶部的「解析待启动」占位条目;返回 null 表示没有 */
getPendingParseItem?: () => T | null
/** 下载胶水的模块差异 */
download?: {
/** resultId -> 下载地址(item.downloadUrl 优先) */
buildUrl: (resultId: number, item: T) => string
/** 文件名兜底前缀(item.sourceFilename 缺失时) */
fallbackFilename: string
/** 下载进度 key 前缀,如 'similar-asin' */
progressKeyPrefix: string
}
/** 删除胶水的模块差异 */
deleteTask?: {
/** 是否按「删除整个任务」处理(两 Tab 判定不同) */
shouldDeleteTask: (item: T, normalizedStatus: string) => boolean
deleteTask: (taskId: number) => Promise<unknown>
deleteHistory: (resultId: number) => Promise<unknown>
/** 任务删除成功后的本地清理 */
onTaskDeleted?: (item: T) => void
/** 结果记录删除成功后的本地清理 */
onHistoryDeleted?: (item: T) => void
}
}
export interface UseLiveTaskBoardReturn<T extends LiveTaskHistoryItem> {
/** 轮询中的任务队列(任务执行中) */
pollingTaskIds: Ref<number[]>
/** 结果文件生成中待跟踪队列(任务终态后文件仍未就绪) */
pendingFileTaskIds: Ref<number[]>
/** 最近一次 progress/batch 的实时进度缓存(history 兜底) */
liveProgressItems: Ref<Record<number, T>>
/** 历史记录(含进行中的;展示时经 historyOnlyItems 过滤) */
historyItems: Ref<T[]>
/** 统计卡片数据 */
dashboard: Ref<TaskBoardDashboardCounts>
/** 当前任务区条目(轮询/进行中 + live 兜底 + 解析占位) */
currentItems: ComputedRef<T[]>
/** 纯历史条目(排除 RUNNING/PENDING) */
historyOnlyItems: ComputedRef<T[]>
/** 从 localStorage 恢复轮询 ID */
loadPollingIds: () => void
/** 挂载后恢复种子:从历史回填 pending 文件任务与进行中任务 */
seedPendingFileTasksFromHistory: () => void
seedRunningTasksFromHistory: () => void
/** 有任何待轮询任务时启动轮询(与原 Tab 的 onMounted 守卫一致) */
ensurePollingIfAny: () => void
ensurePolling: () => void
stopPolling: () => void
/** 卸载清理(停止轮询 + 清空 timers) */
dispose: () => void
addPollingTask: (taskId: number) => void
removePollingTask: (taskId: number) => void
addPendingFileTask: (taskId: number) => void
removePendingFileTask: (taskId: number) => void
loadDashboard: () => Promise<void>
loadHistory: (options?: { force?: boolean }) => Promise<void>
refreshTaskProgress: () => Promise<void>
/** 状态判定(透出传入的实现与公共实现,供 Tab 的视图层函数复用) */
normalizeTaskStatus: (item: T) => string
isResultPreparing: (item: T) => boolean
canDownload: (item: T) => boolean
showFileProgress: (item: T) => boolean
downloadResult: (item: T) => Promise<void>
deleteTaskRecord: (item: T) => Promise<void>
/** 历史记录批量删除(复用单条删除逻辑) */
batchDeleteHistory: (views: TaskItemView[]) => Promise<void>
}
/** 模块无关的文件进度字段判定(两 Tab 逐字相同,抽为纯函数) */
export function hasMeaningfulProgressValue(value: unknown) {
const num = Number(value)
return Number.isFinite(num) && num > 0
}
export function activeFileJobStatus(item: LiveTaskHistoryItem) {
return (item.fileStatus || '').toUpperCase()
}
export function hasActiveFileJob(item: LiveTaskHistoryItem) {
const fileStatus = activeFileJobStatus(item)
return item.fileJobId != null || fileStatus === 'PENDING' || fileStatus === 'RUNNING'
}
export function hasFileProgressCount(item: LiveTaskHistoryItem) {
return (item.fileProgressTotal || 0) > 0
}
export function hasExplicitFileProgress(item: LiveTaskHistoryItem) {
return hasFileProgressCount(item)
|| Number(item.fileProgressPercent || 0) > 0
|| Boolean((item.fileProgressMessage || '').trim())
}
export function shouldShowFallbackJobProgress(item: LiveTaskHistoryItem) {
return hasActiveFileJob(item) && !hasExplicitFileProgress(item)
}
/** 两个 Tab 的 canDownload 实现逐字相同 */
export function defaultCanDownload(item: LiveTaskHistoryItem) {
return Boolean(item.resultId && (item.fileReady || item.downloadUrl))
}
const HISTORY_CACHE_TTL_MS = 3000
export function useLiveTaskBoard<T extends LiveTaskHistoryItem>(
options: UseLiveTaskBoardOptions<T>,
): UseLiveTaskBoardReturn<T> {
const timers = createCategorizedTimers(options.scope)
const pollErrorMode = options.pollErrorMode ?? 'swallow'
const pollingTaskIds = ref<number[]>([])
const pendingFileTaskIds = ref<number[]>([])
const liveProgressItems = ref({}) as Ref<Record<number, T>>
const historyItems = ref([]) as Ref<T[]>
const dashboard = ref<TaskBoardDashboardCounts>({
pendingTaskCount: 0,
processedTaskCount: 0,
successTaskCount: 0,
failedTaskCount: 0,
})
const pollTimer = ref<number | null>(null)
const pollingInFlight = ref(false)
let historyInFlight: Promise<void> | null = null
let lastHistoryLoadedAt = 0
let disposed = false
const { normalizeTaskStatus, isResultPreparing } = options
const canDownload = defaultCanDownload
function showFileProgress(item: T) {
const status = normalizeTaskStatus(item)
return (status === 'RUNNING' || isResultPreparing(item)) && (hasExplicitFileProgress(item) || shouldShowFallbackJobProgress(item))
}
// ---- 当前任务 / 纯历史(两 Tab 逐字相同)----
const currentItems = computed<T[]>(() => {
const activeIds = new Set([...pollingTaskIds.value, ...pendingFileTaskIds.value])
const activeItems = historyItems.value
.filter((item) => {
const status = normalizeTaskStatus(item)
return item.taskId != null && (activeIds.has(item.taskId) || status === 'RUNNING' || status === 'PENDING')
})
.map((item) => mergeCurrentTaskItem(item))
// 兜底:history 暂未返回该任务,但实时缓存里仍在跑,保留它在当前任务区
for (const [taskIdText, liveItem] of Object.entries(liveProgressItems.value)) {
const taskId = Number(taskIdText)
if (!Number.isFinite(taskId) || taskId <= 0) continue
if (!activeIds.has(taskId) && normalizeTaskStatus(liveItem) !== 'RUNNING' && normalizeTaskStatus(liveItem) !== 'PENDING') {
continue
}
if (activeItems.some((item) => item.taskId === taskId)) continue
activeItems.push(mergeCurrentTaskItem(liveItem))
}
const pendingParseItem = options.getPendingParseItem?.() ?? null
if (!pendingParseItem) return activeItems
const exists = activeItems.some((item) => item.taskId === pendingParseItem.taskId)
return exists ? activeItems : [pendingParseItem, ...activeItems]
})
const historyOnlyItems = computed<T[]>(() =>
historyItems.value.filter((i) => {
const status = normalizeTaskStatus(i)
return status !== 'RUNNING' && status !== 'PENDING'
}),
)
// ---- 轮询 ID 持久化(两 Tab 逐字相同,仅 key 不同)----
function pollingKey() {
return options.pollingStorageKey()
}
function savePollingIds() {
if (typeof window === 'undefined') return
const ids = Array.from(new Set([...pollingTaskIds.value, ...pendingFileTaskIds.value]))
if (!ids.length) window.localStorage.removeItem(pollingKey())
else window.localStorage.setItem(pollingKey(), JSON.stringify(ids))
}
function loadPollingIds() {
if (typeof window === 'undefined') return
try {
const raw = window.localStorage.getItem(pollingKey())
const ids = raw ? JSON.parse(raw) : []
pollingTaskIds.value = Array.isArray(ids)
? ids.filter((id): id is number => typeof id === 'number' && id > 0)
: []
} catch {
pollingTaskIds.value = []
}
}
// ---- 双队列维护(两 Tab 逐字相同;removePendingFileTask 多一个回调钩子)----
function addPollingTask(taskId: number) {
if (!pollingTaskIds.value.includes(taskId)) {
pollingTaskIds.value = [...pollingTaskIds.value, taskId]
savePollingIds()
}
ensurePolling()
}
function removePollingTask(taskId: number) {
pollingTaskIds.value = pollingTaskIds.value.filter((id) => id !== taskId)
if (!pendingFileTaskIds.value.includes(taskId)) {
const next = { ...liveProgressItems.value }
delete next[taskId]
liveProgressItems.value = next
}
savePollingIds()
}
function addPendingFileTask(taskId: number) {
if (!pendingFileTaskIds.value.includes(taskId)) {
pendingFileTaskIds.value = [...pendingFileTaskIds.value, taskId]
savePollingIds()
}
ensurePolling()
}
function removePendingFileTask(taskId: number) {
pendingFileTaskIds.value = pendingFileTaskIds.value.filter((id) => id !== taskId)
options.onPendingFileTaskRemoved?.(taskId)
if (!pollingTaskIds.value.includes(taskId)) {
const next = { ...liveProgressItems.value }
delete next[taskId]
liveProgressItems.value = next
}
savePollingIds()
}
// ---- 轮询循环(两 Tab 逐字相同;抛错处理按 pollErrorMode 参数化)----
function ensurePolling() {
if (disposed) return
if (pollTimer.value != null) return
scheduleNextPoll(true)
}
function ensurePollingIfAny() {
if (pollingTaskIds.value.length || pendingFileTaskIds.value.length) ensurePolling()
}
function stopPolling() {
if (pollTimer.value != null) {
timers.clearTimer('task-poll', pollTimer.value)
pollTimer.value = null
}
}
function scheduleNextPoll(immediate = false) {
if (disposed) return
if (pollTimer.value != null) {
if (!immediate) return
timers.clearTimer('task-poll', pollTimer.value)
pollTimer.value = null
}
if (immediate) void runPollCycle()
else pollTimer.value = timers.setTimeout('task-poll', runPollCycle, getTaskPollIntervalMs())
}
async function runPollCycle() {
pollTimer.value = null
if (disposed) return
if (!pollingTaskIds.value.length && !pendingFileTaskIds.value.length) return
if (pollErrorMode === 'propagate') {
await refreshTaskProgress()
scheduleFollowUp()
} else {
try {
await refreshTaskProgress()
} catch {
// A transient request failure must not stop progress polling permanently.
} finally {
scheduleFollowUp()
}
}
}
function scheduleFollowUp() {
if (!disposed && pollTimer.value == null && (pollingTaskIds.value.length || pendingFileTaskIds.value.length)) {
pollTimer.value = timers.setTimeout('task-poll', runPollCycle, getTaskPollIntervalMs())
}
}
// ---- 进度批量刷新(差异点经 beforeBatch / afterBatch / 钩子参数化)----
async function refreshTaskProgress() {
if (pollingInFlight.value || (!pollingTaskIds.value.length && !pendingFileTaskIds.value.length)) {
if (!pollingTaskIds.value.length && !pendingFileTaskIds.value.length) stopPolling()
return
}
pollingInFlight.value = true
try {
let shouldRefreshDashboard = false
let shouldRefreshHistory = false
const taskIds = Array.from(new Set([...pollingTaskIds.value, ...pendingFileTaskIds.value]))
if (taskIds.length) {
let batchIds = taskIds
let force = false
const prepared = options.beforeBatch
? await options.beforeBatch(taskIds, pendingFileTaskIds.value)
: { taskIds, force: false }
if (!prepared) {
// 预处理判断本轮无需拉取(Patent 的行版本未变路径):直接结束,不做结算
return
}
batchIds = prepared.taskIds
force = prepared.force
const batch = await options.fetchProgressBatch(batchIds, { force })
if (options.afterBatch) options.afterBatch(taskIds, pendingFileTaskIds.value, force)
for (const detail of batch.items || []) {
const task = detail.task
if (!task?.id) continue
const item = detail.items?.[0]
if (item) {
// 实时缓存:以最新一次 progress 为准,避免 history 接口字段缺失/延迟时丢失进度
liveProgressItems.value = {
...liveProgressItems.value,
[task.id]: item,
}
mergeHistoryItem(item)
}
if (task.status === 'SUCCESS' || task.status === 'FAILED') {
removePollingTask(task.id)
shouldRefreshDashboard = true
if (options.addPendingAfterFinalStatus(task, item)) addPendingFileTask(task.id)
else removePendingFileTask(task.id)
}
if (item && options.settleOnTaskStatuses.includes(task.status || '') && pendingFileTaskIds.value.includes(task.id)) {
const fileStatus = (item.fileStatus || '').toUpperCase()
if (item.fileReady || fileStatus === 'SUCCESS' || fileStatus === 'FAILED') {
removePendingFileTask(task.id)
shouldRefreshDashboard = true
shouldRefreshHistory = true
}
}
}
if (batch.missingTaskIds?.length) {
batch.missingTaskIds.forEach((taskId) => {
removePollingTask(taskId)
removePendingFileTask(taskId)
})
shouldRefreshHistory = true
shouldRefreshDashboard = true
}
}
if (shouldRefreshDashboard) {
await loadDashboard()
}
if (shouldRefreshHistory) {
await loadHistory()
}
settlePendingFileTasks()
} finally {
pollingInFlight.value = false
}
}
// ---- history 合并(两 Tab 逐字相同)----
function mergeHistoryItem(item: T) {
if (!item.taskId && !item.resultId) return
const index = historyItems.value.findIndex((row) =>
(item.resultId != null && row.resultId === item.resultId)
|| (item.taskId != null && row.taskId === item.taskId),
)
if (index >= 0) {
historyItems.value[index] = { ...historyItems.value[index], ...item }
} else {
historyItems.value = [item, ...historyItems.value]
}
}
function mergeCurrentTaskItem(item: T) {
if (item.taskId == null) return item
const liveItem = liveProgressItems.value[item.taskId]
if (!liveItem) return item
return {
...item,
...liveItem,
sourceFilename: liveItem.sourceFilename || item.sourceFilename,
resultFilename: liveItem.resultFilename || item.resultFilename,
rowCount: liveItem.rowCount ?? item.rowCount,
createdAt: liveItem.createdAt || item.createdAt,
}
}
function mergeHistoryItemsPreservingLiveProgress(
previousItems: T[],
incomingItems: T[],
) {
const previousByTaskId = new Map<number, T>()
const previousByResultId = new Map<number, T>()
for (const item of previousItems || []) {
if (item.taskId != null) previousByTaskId.set(item.taskId, item)
if (item.resultId != null) previousByResultId.set(item.resultId, item)
}
return (incomingItems || []).map((incoming) => {
const previous = incoming.resultId != null
? previousByResultId.get(incoming.resultId)
: incoming.taskId != null
? previousByTaskId.get(incoming.taskId)
: undefined
return mergeHistoryItemPreservingLiveProgress(previous, incoming)
})
}
function mergeHistoryItemPreservingLiveProgress(
previous: T | undefined,
incoming: T,
) {
if (!previous) return incoming
const shouldPreserveLiveProgress =
(normalizeTaskStatus(previous) === 'RUNNING' || isResultPreparing(previous) || showFileProgress(previous))
&& !showFileProgress(incoming)
&& !canDownload(incoming)
if (!shouldPreserveLiveProgress) {
return incoming
}
return {
...incoming,
fileJobId: incoming.fileJobId ?? previous.fileJobId,
fileStatus: incoming.fileStatus || previous.fileStatus,
fileError: incoming.fileError || previous.fileError,
fileReady: incoming.fileReady || previous.fileReady,
fileProgressPercent: hasMeaningfulProgressValue(incoming.fileProgressPercent)
? incoming.fileProgressPercent
: previous.fileProgressPercent,
fileProgressCurrent: hasMeaningfulProgressValue(incoming.fileProgressCurrent)
? incoming.fileProgressCurrent
: previous.fileProgressCurrent,
fileProgressTotal: hasMeaningfulProgressValue(incoming.fileProgressTotal)
? incoming.fileProgressTotal
: previous.fileProgressTotal,
fileProgressMessage: (incoming.fileProgressMessage || '').trim()
? incoming.fileProgressMessage
: previous.fileProgressMessage,
}
}
// ---- pending 文件任务结算与种子恢复(差异点经 keepPendingFileTask 参数化)----
function settlePendingFileTasks() {
if (!pendingFileTaskIds.value.length) return
const remaining: number[] = []
for (const taskId of pendingFileTaskIds.value) {
const item = historyItems.value.find((row) => row.taskId === taskId)
if (!item) {
remaining.push(taskId)
continue
}
const taskStatus = (item.taskStatus || '').toUpperCase()
const fileStatus = (item.fileStatus || '').toUpperCase()
if (item.fileReady || fileStatus === 'SUCCESS' || fileStatus === 'FAILED') {
continue
}
if (options.keepPendingFileTask(item, taskStatus)) {
remaining.push(taskId)
}
}
pendingFileTaskIds.value = remaining
savePollingIds()
if (pendingFileTaskIds.value.length) ensurePolling()
}
function seedPendingFileTasksFromHistory() {
const ids = historyItems.value
.filter((item) => item.taskId != null && isResultPreparing(item))
.map((item) => item.taskId as number)
pendingFileTaskIds.value = Array.from(new Set(ids))
savePollingIds()
if (pendingFileTaskIds.value.length) ensurePolling()
}
function seedRunningTasksFromHistory() {
const ids = historyItems.value
.filter((item) => item.taskId != null && normalizeTaskStatus(item) === 'RUNNING')
.map((item) => item.taskId as number)
if (!ids.length) return
pollingTaskIds.value = Array.from(new Set([...pollingTaskIds.value, ...ids]))
savePollingIds()
ensurePolling()
}
// ---- 数据加载(两 Tab 逐字相同,仅接口不同)----
async function loadDashboard() {
dashboard.value = await options.fetchDashboard()
}
async function loadHistory(opts: { force?: boolean } = {}) {
const now = Date.now()
if (!opts.force && historyItems.value.length && now - lastHistoryLoadedAt < HISTORY_CACHE_TTL_MS) return
if (historyInFlight) return historyInFlight
historyInFlight = options
.fetchHistory()
.then((res) => {
historyItems.value = mergeHistoryItemsPreservingLiveProgress(historyItems.value, res.items || [])
lastHistoryLoadedAt = Date.now()
})
.finally(() => {
historyInFlight = null
})
return historyInFlight
}
// ---- 下载 / 删除 / 批量删除胶水(差异点经 options 参数化)----
async function downloadResult(item: T) {
if (!item.resultId || !options.download) return
const url = item.downloadUrl || options.download.buildUrl(item.resultId, item)
const filename = item.resultFilename || `${item.sourceFilename || options.download.fallbackFilename}.xlsx`
const result = await saveUrlWithProgress(url, filename, `${options.download.progressKeyPrefix}:${item.resultId}`)
if (result.success) {
ElMessage.success(`已保存: ${result.path || filename}`)
} else if (result.error && result.error !== '用户取消') {
ElMessage.error(result.error)
}
}
async function deleteTaskRecord(item: T) {
if (!(await confirmTaskDeletion(item))) return
try {
const status = normalizeTaskStatus(item)
const deleteOptions = options.deleteTask
if (deleteOptions && deleteOptions.shouldDeleteTask(item, status)) {
await deleteOptions.deleteTask(item.taskId as number)
deleteOptions.onTaskDeleted?.(item)
} else if (item.resultId && deleteOptions) {
await deleteOptions.deleteHistory(item.resultId)
deleteOptions.onHistoryDeleted?.(item)
}
await loadDashboard()
await loadHistory({ force: true })
ElMessage.success('已删除')
} catch (e) {
ElMessage.error(e instanceof Error ? e.message : '删除失败')
}
}
/**
* 历史记录批量删除:复用单条删除逻辑(删除后原函数各自刷新列表)
*/
async function batchDeleteHistory(views: TaskItemView[]) {
const { total, failed } = await runBatchDelete(views, (view) => deleteTaskRecord(view.source as T))
if (!total) return
if (failed > 0) {
ElMessage.warning(`删除完成,${failed} 条失败`)
} else {
ElMessage.success(`已删除 ${total} 条历史记录`)
}
}
function dispose() {
disposed = true
stopPolling()
timers.clearScope()
}
return {
pollingTaskIds,
pendingFileTaskIds,
liveProgressItems,
historyItems,
dashboard,
currentItems,
historyOnlyItems,
loadPollingIds,
seedPendingFileTasksFromHistory,
seedRunningTasksFromHistory,
ensurePollingIfAny,
ensurePolling,
stopPolling,
dispose,
addPollingTask,
removePollingTask,
addPendingFileTask,
removePendingFileTask,
loadDashboard,
loadHistory,
refreshTaskProgress,
normalizeTaskStatus,
isResultPreparing,
canDownload,
showFileProgress,
downloadResult,
deleteTaskRecord,
batchDeleteHistory,
}
}