refactor: 第二批收敛——公共 Excel 写出器 + 任务收尾零件下沉 + 前端上传区/任务面板抽取
This commit is contained in:
+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) {
|
||||
|
||||
Reference in New Issue
Block a user