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