refactor: 第二批收敛——公共 Excel 写出器 + 任务收尾零件下沉 + 前端上传区/任务面板抽取
This commit is contained in:
+26
-38
@@ -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;
|
||||
}
|
||||
|
||||
+3
-10
@@ -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) {
|
||||
|
||||
+24
-44
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-67
@@ -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) {
|
||||
|
||||
+20
-44
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+9
-65
@@ -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) {
|
||||
|
||||
+12
-28
@@ -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);
|
||||
|
||||
+7
-65
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+6
-26
@@ -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);
|
||||
}
|
||||
|
||||
+8
-19
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-67
@@ -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) {
|
||||
|
||||
+8
-70
@@ -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) {
|
||||
|
||||
+7
-25
@@ -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
-19
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+190
@@ -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;
|
||||
}
|
||||
}
|
||||
+18
-42
@@ -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;
|
||||
}
|
||||
|
||||
+8
-56
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user