更新新增三个模块
This commit is contained in:
+2
-1
@@ -46,7 +46,8 @@ public class SplitRunController {
|
||||
- splitMode=rows_per_file 时按固定条数切分;
|
||||
- splitMode=parts 时按总条数尽量均分;
|
||||
- 输出文件命名遵循 原文件名_split_序号.xlsx;
|
||||
- 返回结果中 rowCount 表示该结果文件包含的数据行数。
|
||||
- 本次拆分任务产生的所有 xlsx 会统一打成一个 zip 上传;
|
||||
- 返回结果中 rowCount 表示该压缩包包含的数据总行数,entries 表示压缩包内文件摘要。
|
||||
""")
|
||||
@ApiResponses({
|
||||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "执行成功,返回拆分结果统计", content = @Content(schema = @Schema(implementation = SplitRunVo.class))),
|
||||
|
||||
+3
@@ -30,4 +30,7 @@ public class SplitRunRequest {
|
||||
|
||||
@Schema(description = "按份数拆分时的份数")
|
||||
private Integer parts;
|
||||
|
||||
@Schema(description = "文件夹上传场景下的总压缩包名称(不含扩展名)")
|
||||
private String archiveName;
|
||||
}
|
||||
|
||||
+3
@@ -14,4 +14,7 @@ public class SplitSourceFileDto {
|
||||
|
||||
@Schema(description = "原始文件名")
|
||||
private String originalFilename;
|
||||
|
||||
@Schema(description = "相对上传目录路径")
|
||||
private String relativePath;
|
||||
}
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.nanri.aiimage.modules.split.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Schema(description = "拆分压缩包内文件项")
|
||||
public class SplitArchiveEntryVo {
|
||||
|
||||
@Schema(description = "文件名")
|
||||
private String filename;
|
||||
|
||||
@Schema(description = "该文件包含的数据行数")
|
||||
private Integer rowCount;
|
||||
}
|
||||
+9
-1
@@ -3,6 +3,8 @@ package com.nanri.aiimage.modules.split.model.vo;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Schema(description = "数据拆分结果项")
|
||||
public class SplitResultItemVo {
|
||||
@@ -22,9 +24,15 @@ public class SplitResultItemVo {
|
||||
@Schema(description = "错误信息")
|
||||
private String error;
|
||||
|
||||
@Schema(description = "结果行数")
|
||||
@Schema(description = "结果总行数")
|
||||
private Integer rowCount;
|
||||
|
||||
@Schema(description = "下载地址")
|
||||
private String downloadUrl;
|
||||
|
||||
@Schema(description = "压缩包内文件数量")
|
||||
private Integer entryCount;
|
||||
|
||||
@Schema(description = "压缩包内文件列表")
|
||||
private List<SplitArchiveEntryVo> entries;
|
||||
}
|
||||
|
||||
+210
-97
@@ -3,13 +3,15 @@ package com.nanri.aiimage.modules.split.service;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.config.StorageProperties;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.split.model.dto.SplitRunRequest;
|
||||
import com.nanri.aiimage.modules.split.model.dto.SplitSourceFileDto;
|
||||
import com.nanri.aiimage.modules.split.model.vo.SplitArchiveEntryVo;
|
||||
import com.nanri.aiimage.modules.split.model.vo.SplitResultItemVo;
|
||||
import com.nanri.aiimage.modules.split.model.vo.SplitRunVo;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
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;
|
||||
@@ -20,6 +22,7 @@ import org.apache.poi.ss.usermodel.DataFormatter;
|
||||
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.usermodel.WorkbookFactory;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -28,18 +31,28 @@ import org.springframework.stereotype.Service;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SplitRunService {
|
||||
|
||||
private static final String MODULE_TYPE = "SPLIT";
|
||||
private static final String ZIP_CONTENT_TYPE = "application/zip";
|
||||
private static final String SPLIT_MODE_ROWS_PER_FILE = "rows_per_file";
|
||||
private static final String SPLIT_MODE_PARTS = "parts";
|
||||
private static final String HEADER_SUFFIX_MARKER = "idASIN\u56fd\u5bb6\u72b6\u6001\u4ef7\u683c\u53d8\u4f53\u6570\u91cf";
|
||||
private static final String HEADER_STOP_COLUMN = "\u7f29\u7565\u56fe\u5730\u57408";
|
||||
private static final DateTimeFormatter ZIP_NAME_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
|
||||
|
||||
private final FileTaskMapper fileTaskMapper;
|
||||
private final FileResultMapper fileResultMapper;
|
||||
private final StorageProperties storageProperties;
|
||||
@@ -49,8 +62,8 @@ public class SplitRunService {
|
||||
validateRequest(request);
|
||||
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setTaskNo("SPLIT-" + IdUtil.getSnowflakeNextIdStr());
|
||||
task.setModuleType("SPLIT");
|
||||
task.setTaskNo(MODULE_TYPE + "-" + IdUtil.getSnowflakeNextIdStr());
|
||||
task.setModuleType(MODULE_TYPE);
|
||||
task.setTaskMode("IMMEDIATE");
|
||||
task.setStatus("SUCCESS");
|
||||
task.setSourceFileCount(request.getFiles().size());
|
||||
@@ -61,56 +74,42 @@ public class SplitRunService {
|
||||
task.setFinishedAt(LocalDateTime.now());
|
||||
fileTaskMapper.insert(task);
|
||||
|
||||
boolean folderMode = request.getArchiveName() != null && !request.getArchiveName().isBlank();
|
||||
List<SplitResultItemVo> items = new ArrayList<>();
|
||||
int successCount = 0;
|
||||
int failedCount = 0;
|
||||
List<TaskSplitChunkResult> allChunkResults = new ArrayList<>();
|
||||
List<String> successSourceNames = new ArrayList<>();
|
||||
|
||||
for (SplitSourceFileDto sourceFile : request.getFiles()) {
|
||||
try {
|
||||
File inputFile = findLocalSourceFile(sourceFile.getFileKey());
|
||||
if (inputFile == null || !inputFile.exists()) {
|
||||
throw new BusinessException("上传文件不存在,请重新上传");
|
||||
throw new BusinessException("Uploaded source file does not exist, please upload again.");
|
||||
}
|
||||
String inputName = sourceFile.getOriginalFilename() == null ? inputFile.getName() : sourceFile.getOriginalFilename();
|
||||
|
||||
String inputName = sourceFile.getOriginalFilename() == null
|
||||
? inputFile.getName()
|
||||
: sourceFile.getOriginalFilename();
|
||||
List<SplitChunkResult> chunkResults = splitExcelByLegacyRules(inputFile, inputName, request);
|
||||
for (SplitChunkResult chunkResult : chunkResults) {
|
||||
SplitResultItemVo item = new SplitResultItemVo();
|
||||
item.setSourceFilename(inputName);
|
||||
item.setOutputFilename(chunkResult.outputFile().getName());
|
||||
item.setSuccess(true);
|
||||
item.setRowCount(chunkResult.rowCount());
|
||||
|
||||
String ossObjectKey = ossStorageService.uploadResultFile(chunkResult.outputFile(), "SPLIT");
|
||||
String downloadUrl = ossStorageService.generateDownloadUrl(ossObjectKey);
|
||||
|
||||
FileResultEntity resultEntity = new FileResultEntity();
|
||||
resultEntity.setTaskId(task.getId());
|
||||
resultEntity.setModuleType("SPLIT");
|
||||
resultEntity.setSourceFilename(inputName);
|
||||
resultEntity.setResultFilename(chunkResult.outputFile().getName());
|
||||
resultEntity.setResultFileUrl(downloadUrl);
|
||||
resultEntity.setResultFileSize(chunkResult.outputFile().length());
|
||||
resultEntity.setResultContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
resultEntity.setRowCount(chunkResult.rowCount());
|
||||
resultEntity.setSuccess(1);
|
||||
resultEntity.setCreatedAt(LocalDateTime.now());
|
||||
fileResultMapper.insert(resultEntity);
|
||||
item.setDownloadUrl(downloadUrl);
|
||||
item.setResultId(resultEntity.getId());
|
||||
items.add(item);
|
||||
allChunkResults.add(new TaskSplitChunkResult(sourceFile.getRelativePath(), inputName, chunkResult.outputFile(), chunkResult.rowCount()));
|
||||
}
|
||||
successSourceNames.add(inputName);
|
||||
successCount++;
|
||||
} catch (Exception ex) {
|
||||
SplitResultItemVo item = new SplitResultItemVo();
|
||||
item.setSourceFilename(sourceFile.getOriginalFilename());
|
||||
item.setSuccess(false);
|
||||
item.setError(ex.getMessage());
|
||||
item.setEntries(new ArrayList<>());
|
||||
item.setEntryCount(0);
|
||||
items.add(item);
|
||||
failedCount++;
|
||||
|
||||
FileResultEntity resultEntity = new FileResultEntity();
|
||||
resultEntity.setTaskId(task.getId());
|
||||
resultEntity.setModuleType("SPLIT");
|
||||
resultEntity.setModuleType(MODULE_TYPE);
|
||||
resultEntity.setSourceFilename(sourceFile.getOriginalFilename());
|
||||
resultEntity.setSuccess(0);
|
||||
resultEntity.setErrorMessage(ex.getMessage());
|
||||
@@ -119,6 +118,38 @@ public class SplitRunService {
|
||||
}
|
||||
}
|
||||
|
||||
if (!allChunkResults.isEmpty()) {
|
||||
File zipFile = packageTaskSplitResultsAsZip(request.getArchiveName(), successSourceNames, allChunkResults);
|
||||
String ossObjectKey = ossStorageService.uploadResultFile(zipFile, MODULE_TYPE);
|
||||
String downloadUrl = ossStorageService.generateDownloadUrl(ossObjectKey);
|
||||
|
||||
SplitResultItemVo item = new SplitResultItemVo();
|
||||
item.setSourceFilename(folderMode && request.getArchiveName() != null && !request.getArchiveName().isBlank()
|
||||
? request.getArchiveName()
|
||||
: successSourceNames.size() == 1 ? successSourceNames.get(0) : "Current split task");
|
||||
item.setOutputFilename(zipFile.getName());
|
||||
item.setSuccess(true);
|
||||
item.setRowCount(allChunkResults.stream().mapToInt(TaskSplitChunkResult::rowCount).sum());
|
||||
item.setDownloadUrl(downloadUrl);
|
||||
item.setEntryCount(allChunkResults.size());
|
||||
item.setEntries(allChunkResults.stream().map(this::toArchiveEntry).toList());
|
||||
|
||||
FileResultEntity resultEntity = new FileResultEntity();
|
||||
resultEntity.setTaskId(task.getId());
|
||||
resultEntity.setModuleType(MODULE_TYPE);
|
||||
resultEntity.setSourceFilename(item.getSourceFilename());
|
||||
resultEntity.setResultFilename(zipFile.getName());
|
||||
resultEntity.setResultFileUrl(downloadUrl);
|
||||
resultEntity.setResultFileSize(zipFile.length());
|
||||
resultEntity.setResultContentType(ZIP_CONTENT_TYPE);
|
||||
resultEntity.setRowCount(item.getRowCount());
|
||||
resultEntity.setSuccess(1);
|
||||
resultEntity.setCreatedAt(LocalDateTime.now());
|
||||
fileResultMapper.insert(resultEntity);
|
||||
item.setResultId(resultEntity.getId());
|
||||
items.add(0, item);
|
||||
}
|
||||
|
||||
task.setSuccessFileCount(successCount);
|
||||
task.setFailedFileCount(failedCount);
|
||||
task.setResultJson(JSONUtil.toJsonStr(items));
|
||||
@@ -135,19 +166,21 @@ public class SplitRunService {
|
||||
|
||||
public List<SplitResultItemVo> listHistory() {
|
||||
return fileResultMapper.selectList(new LambdaQueryWrapper<FileResultEntity>()
|
||||
.eq(FileResultEntity::getModuleType, "SPLIT")
|
||||
.eq(FileResultEntity::getSuccess, 1)
|
||||
.eq(FileResultEntity::getModuleType, MODULE_TYPE)
|
||||
.orderByDesc(FileResultEntity::getCreatedAt)
|
||||
.last("limit 100"))
|
||||
.stream()
|
||||
.map(entity -> {
|
||||
SplitResultItemVo vo = new SplitResultItemVo();
|
||||
vo.setResultId(entity.getId());
|
||||
vo.setSourceFilename(entity.getCreatedAt() == null ? "" : entity.getCreatedAt().toLocalDate().toString().replace("-", ""));
|
||||
vo.setSourceFilename(entity.getSourceFilename());
|
||||
vo.setOutputFilename(entity.getResultFilename());
|
||||
vo.setDownloadUrl(entity.getResultFileUrl());
|
||||
vo.setDownloadUrl(entity.getSuccess() != null && entity.getSuccess() == 1 ? entity.getResultFileUrl() : null);
|
||||
vo.setRowCount(entity.getRowCount());
|
||||
vo.setSuccess(true);
|
||||
vo.setSuccess(entity.getSuccess() != null && entity.getSuccess() == 1);
|
||||
vo.setError(entity.getErrorMessage());
|
||||
vo.setEntryCount(0);
|
||||
vo.setEntries(new ArrayList<>());
|
||||
return vo;
|
||||
})
|
||||
.toList();
|
||||
@@ -155,55 +188,45 @@ public class SplitRunService {
|
||||
|
||||
public void deleteHistory(Long resultId) {
|
||||
FileResultEntity entity = fileResultMapper.selectById(resultId);
|
||||
if (entity == null || !"SPLIT".equals(entity.getModuleType())) {
|
||||
throw new BusinessException("记录不存在");
|
||||
if (entity == null || !MODULE_TYPE.equals(entity.getModuleType())) {
|
||||
throw new BusinessException("Split result record does not exist.");
|
||||
}
|
||||
fileResultMapper.deleteById(resultId);
|
||||
}
|
||||
|
||||
public Resource getResultFile(Long resultId) {
|
||||
FileResultEntity entity = fileResultMapper.selectById(resultId);
|
||||
if (entity == null || entity.getResultFileUrl() == null || !"SPLIT".equals(entity.getModuleType())) {
|
||||
throw new BusinessException("结果文件不存在");
|
||||
if (entity == null || entity.getResultFileUrl() == null || !MODULE_TYPE.equals(entity.getModuleType())) {
|
||||
throw new BusinessException("Split result file does not exist.");
|
||||
}
|
||||
|
||||
File file = new File(entity.getResultFileUrl());
|
||||
if (!file.exists()) {
|
||||
throw new BusinessException("结果文件不存在");
|
||||
throw new BusinessException("Split result file does not exist.");
|
||||
}
|
||||
return new FileSystemResource(file);
|
||||
}
|
||||
|
||||
private List<SplitChunkResult> splitExcelByLegacyRules(File inputFile, String originalFilename, SplitRunRequest request) throws Exception {
|
||||
private List<SplitChunkResult> splitExcelByLegacyRules(
|
||||
File inputFile,
|
||||
String originalFilename,
|
||||
SplitRunRequest request
|
||||
) throws Exception {
|
||||
DataFormatter formatter = new DataFormatter();
|
||||
try (FileInputStream fis = new FileInputStream(inputFile); Workbook workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create(fis)) {
|
||||
try (FileInputStream fis = new FileInputStream(inputFile);
|
||||
Workbook workbook = WorkbookFactory.create(fis)) {
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
Row headerRow = sheet.getRow(0);
|
||||
if (headerRow == null) {
|
||||
throw new BusinessException("Excel 表头为空");
|
||||
throw new BusinessException("Excel header row is empty.");
|
||||
}
|
||||
|
||||
Map<String, Integer> headerMap = new HashMap<>();
|
||||
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
|
||||
Cell cell = headerRow.getCell(i);
|
||||
String value = normalizeCellText(cell == null ? null : formatter.formatCellValue(cell));
|
||||
if (value.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
value = value.split("idASIN国家状态价格变体数量", 2)[0].trim().isEmpty()
|
||||
? value
|
||||
: value.split("idASIN国家状态价格变体数量", 2)[0].trim();
|
||||
if (headerMap.containsKey(value)) {
|
||||
continue;
|
||||
}
|
||||
headerMap.put(value, i);
|
||||
if ("缩略图地址8".equals(value)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
List<String> missing = request.getSelectedColumns().stream().filter(column -> !headerMap.containsKey(column)).toList();
|
||||
Map<String, Integer> headerMap = buildHeaderMap(headerRow, formatter);
|
||||
List<String> missing = request.getSelectedColumns().stream()
|
||||
.filter(column -> !headerMap.containsKey(column))
|
||||
.toList();
|
||||
if (!missing.isEmpty()) {
|
||||
throw new BusinessException("缺少列:" + String.join("、", missing));
|
||||
throw new BusinessException("Missing required columns: " + String.join(", ", missing));
|
||||
}
|
||||
|
||||
List<List<String>> rowsData = new ArrayList<>();
|
||||
@@ -215,39 +238,23 @@ public class SplitRunService {
|
||||
List<String> projected = new ArrayList<>();
|
||||
for (String column : request.getSelectedColumns()) {
|
||||
Integer sourceIndex = headerMap.get(column);
|
||||
String value = sourceIndex == null ? "" : normalizeCellText(formatter.formatCellValue(row.getCell(sourceIndex)));
|
||||
String value = sourceIndex == null
|
||||
? ""
|
||||
: normalizeCellText(formatter.formatCellValue(row.getCell(sourceIndex)));
|
||||
projected.add(value);
|
||||
}
|
||||
rowsData.add(projected);
|
||||
}
|
||||
|
||||
int totalRows = rowsData.size();
|
||||
if (totalRows == 0) {
|
||||
throw new BusinessException("没有可拆分的数据行");
|
||||
}
|
||||
|
||||
List<List<List<String>>> chunks = new ArrayList<>();
|
||||
if ("rows_per_file".equals(request.getSplitMode())) {
|
||||
int chunkSize = request.getRowsPerFile();
|
||||
for (int i = 0; i < totalRows; i += chunkSize) {
|
||||
chunks.add(rowsData.subList(i, Math.min(i + chunkSize, totalRows)));
|
||||
}
|
||||
} else {
|
||||
int actualParts = Math.min(request.getParts(), totalRows);
|
||||
int base = totalRows / actualParts;
|
||||
int remainder = totalRows % actualParts;
|
||||
int start = 0;
|
||||
for (int i = 0; i < actualParts; i++) {
|
||||
int size = base + (i < remainder ? 1 : 0);
|
||||
int end = start + size;
|
||||
chunks.add(rowsData.subList(start, end));
|
||||
start = end;
|
||||
}
|
||||
if (rowsData.isEmpty()) {
|
||||
throw new BusinessException("No data rows available for splitting.");
|
||||
}
|
||||
|
||||
List<List<List<String>>> chunks = buildChunks(rowsData, request);
|
||||
File outputDir = FileUtil.mkdir(FileUtil.file(storageProperties.getLocalTempDir(), "split-result"));
|
||||
String sourceStem = FileUtil.mainName(originalFilename);
|
||||
List<SplitChunkResult> results = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < chunks.size(); i++) {
|
||||
File outputFile = buildNamedOutputFile(outputDir, sourceStem + "_split_" + (i + 1) + ".xlsx");
|
||||
try (XSSFWorkbook outputWorkbook = new XSSFWorkbook()) {
|
||||
@@ -269,19 +276,63 @@ public class SplitRunService {
|
||||
}
|
||||
results.add(new SplitChunkResult(outputFile, chunks.get(i).size()));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Integer> buildHeaderMap(Row headerRow, DataFormatter formatter) {
|
||||
Map<String, Integer> headerMap = new HashMap<>();
|
||||
for (int i = 0; i < headerRow.getLastCellNum(); i++) {
|
||||
Cell cell = headerRow.getCell(i);
|
||||
String value = normalizeHeaderName(cell == null ? null : formatter.formatCellValue(cell));
|
||||
if (value.isBlank() || headerMap.containsKey(value)) {
|
||||
continue;
|
||||
}
|
||||
headerMap.put(value, i);
|
||||
if (HEADER_STOP_COLUMN.equals(value)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return headerMap;
|
||||
}
|
||||
|
||||
private List<List<List<String>>> buildChunks(List<List<String>> rowsData, SplitRunRequest request) {
|
||||
int totalRows = rowsData.size();
|
||||
List<List<List<String>>> chunks = new ArrayList<>();
|
||||
|
||||
if (SPLIT_MODE_ROWS_PER_FILE.equals(request.getSplitMode())) {
|
||||
int chunkSize = request.getRowsPerFile();
|
||||
for (int i = 0; i < totalRows; i += chunkSize) {
|
||||
chunks.add(rowsData.subList(i, Math.min(i + chunkSize, totalRows)));
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
|
||||
int actualParts = Math.min(request.getParts(), totalRows);
|
||||
int base = totalRows / actualParts;
|
||||
int remainder = totalRows % actualParts;
|
||||
int start = 0;
|
||||
for (int i = 0; i < actualParts; i++) {
|
||||
int size = base + (i < remainder ? 1 : 0);
|
||||
int end = start + size;
|
||||
chunks.add(rowsData.subList(start, end));
|
||||
start = end;
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
|
||||
private void validateRequest(SplitRunRequest request) {
|
||||
if (!List.of("rows_per_file", "parts").contains(request.getSplitMode())) {
|
||||
throw new BusinessException("拆分模式不合法");
|
||||
if (!List.of(SPLIT_MODE_ROWS_PER_FILE, SPLIT_MODE_PARTS).contains(request.getSplitMode())) {
|
||||
throw new BusinessException("Invalid split mode.");
|
||||
}
|
||||
if ("rows_per_file".equals(request.getSplitMode()) && (request.getRowsPerFile() == null || request.getRowsPerFile() <= 0)) {
|
||||
throw new BusinessException("每份条数不合法");
|
||||
if (SPLIT_MODE_ROWS_PER_FILE.equals(request.getSplitMode())
|
||||
&& (request.getRowsPerFile() == null || request.getRowsPerFile() <= 0)) {
|
||||
throw new BusinessException("Invalid rowsPerFile value.");
|
||||
}
|
||||
if ("parts".equals(request.getSplitMode()) && (request.getParts() == null || request.getParts() <= 0)) {
|
||||
throw new BusinessException("拆分份数不合法");
|
||||
if (SPLIT_MODE_PARTS.equals(request.getSplitMode())
|
||||
&& (request.getParts() == null || request.getParts() <= 0)) {
|
||||
throw new BusinessException("Invalid parts value.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -290,8 +341,11 @@ public class SplitRunService {
|
||||
if (!baseDir.exists()) {
|
||||
return null;
|
||||
}
|
||||
List<File> matchedFiles = FileUtil.loopFiles(baseDir, pathname -> pathname.isFile() && pathname.getName().startsWith(fileKey));
|
||||
return matchedFiles.isEmpty() ? null : matchedFiles.getFirst();
|
||||
List<File> matchedFiles = FileUtil.loopFiles(
|
||||
baseDir,
|
||||
pathname -> pathname.isFile() && pathname.getName().startsWith(fileKey)
|
||||
);
|
||||
return matchedFiles.isEmpty() ? null : matchedFiles.get(0);
|
||||
}
|
||||
|
||||
private File buildNamedOutputFile(File outputDir, String filename) {
|
||||
@@ -299,6 +353,7 @@ public class SplitRunService {
|
||||
if (!candidate.exists()) {
|
||||
return candidate;
|
||||
}
|
||||
|
||||
String mainName = FileUtil.mainName(filename);
|
||||
String extName = FileUtil.extName(filename);
|
||||
int index = 2;
|
||||
@@ -314,6 +369,61 @@ public class SplitRunService {
|
||||
}
|
||||
}
|
||||
|
||||
private File packageTaskSplitResultsAsZip(String archiveName, List<String> successSourceNames, List<TaskSplitChunkResult> chunkResults) {
|
||||
File outputDir = FileUtil.mkdir(FileUtil.file(storageProperties.getLocalTempDir(), "split-result"));
|
||||
String zipFilename = archiveName != null && !archiveName.isBlank()
|
||||
? archiveName + ".zip"
|
||||
: successSourceNames.size() == 1
|
||||
? FileUtil.mainName(successSourceNames.getFirst()) + ".zip"
|
||||
: LocalDateTime.now().format(ZIP_NAME_DATE_FORMATTER) + ".zip";
|
||||
File zipFile = buildNamedOutputFile(outputDir, zipFilename);
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
|
||||
for (TaskSplitChunkResult chunkResult : chunkResults) {
|
||||
String zipEntryName = buildFolderZipEntry(chunkResult.relativePath(), chunkResult.sourceFilename(), chunkResult.outputFile().getName(), true);
|
||||
ZipEntry entry = new ZipEntry(zipEntryName);
|
||||
zos.putNextEntry(entry);
|
||||
Files.copy(chunkResult.outputFile().toPath(), zos);
|
||||
zos.closeEntry();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new BusinessException("拆分结果打包失败:" + ex.getMessage());
|
||||
}
|
||||
return zipFile;
|
||||
}
|
||||
|
||||
private String buildFolderZipEntry(String relativePath, String inputName, String outputFilename, boolean includeSourceStemFolder) {
|
||||
String normalizedRelativePath = relativePath == null ? "" : relativePath.replace('\\', '/');
|
||||
String relativeDir = normalizedRelativePath.contains("/")
|
||||
? normalizedRelativePath.substring(0, normalizedRelativePath.lastIndexOf('/'))
|
||||
: "";
|
||||
List<String> parts = new ArrayList<>();
|
||||
if (!relativeDir.isBlank()) {
|
||||
parts.add(relativeDir);
|
||||
}
|
||||
if (includeSourceStemFolder) {
|
||||
parts.add(FileUtil.mainName(inputName));
|
||||
}
|
||||
parts.add(outputFilename);
|
||||
return String.join("/", parts);
|
||||
}
|
||||
|
||||
private SplitArchiveEntryVo toArchiveEntry(TaskSplitChunkResult chunkResult) {
|
||||
SplitArchiveEntryVo vo = new SplitArchiveEntryVo();
|
||||
vo.setFilename(chunkResult.outputFile().getName());
|
||||
vo.setRowCount(chunkResult.rowCount());
|
||||
return vo;
|
||||
}
|
||||
|
||||
private String normalizeHeaderName(String value) {
|
||||
String normalized = normalizeCellText(value);
|
||||
if (normalized.isBlank()) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
String prefix = normalized.split(HEADER_SUFFIX_MARKER, 2)[0].trim();
|
||||
return prefix.isEmpty() ? normalized : prefix;
|
||||
}
|
||||
|
||||
private String normalizeCellText(String value) {
|
||||
if (value == null) {
|
||||
return "";
|
||||
@@ -330,4 +440,7 @@ public class SplitRunService {
|
||||
|
||||
private record SplitChunkResult(File outputFile, int rowCount) {
|
||||
}
|
||||
|
||||
private record TaskSplitChunkResult(String relativePath, String sourceFilename, File outputFile, int rowCount) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user