对第二步修改重复文件名报错的尝试

This commit is contained in:
CXT-maker 2025-10-29 16:01:20 +08:00
parent 0f54983b30
commit 67a35cc661
3 changed files with 69 additions and 20 deletions

View File

@ -8,7 +8,10 @@ import top.r3944realms.docchecktoolrefactored.util.LoggerMarker;
import java.io.File;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Slf4j
public class LogicalAddressFileGenerator implements AddressFileGenerator {
private ProgressCallback callback;
@ -61,36 +64,66 @@ public class LogicalAddressFileGenerator implements AddressFileGenerator {
}
safeOnPhaseProgress(Phase.GENERATE_LOGICAL, 3, 3); // 3/3
safeOnPhaseCompleted(Phase.GENERATE_LOGICAL);
safeOnPhaseCompleted(Phase.GENERATE_LOGICAL);
} catch (RuntimeException e) {
// 直接重新抛出自定义的重复档号异常不让它被日志吞掉
if (e.getMessage() != null && e.getMessage().startsWith("存在重复档号:")) {
throw e;
}
log.info(LoggerMarker.RELEASE_MARKER, "生成逻辑地址文件时出错: {}", e.getMessage());
throw e;
} catch (Exception e) {
log.info(LoggerMarker.RELEASE_MARKER, "生成逻辑地址文件时出错: {}", e.getMessage());
throw new RuntimeException(e);
}
}
/**
* 生成页面级逻辑地址文件
*/
private void generatePageLevelFile(PrintWriter writer, List<Record> records, ProgressCallback callback) {
// 写入CSV头部
writer.println("逻辑文件名,逻辑地址");
int totalRecords = records.stream().mapToInt(r -> r.page).sum();
int current = 0;
// 处理每条记录
Set<String> seenCodes = new HashSet<>();
Set<String> duplicateCodes = new HashSet<>();
// 收集所有重复的档号
for (Record record : records) {
String archiveCode = record.archiveCode;
int page = record.page;
if (!seenCodes.add(archiveCode)) {
duplicateCodes.add(archiveCode);
}
}
// 为每页生成一行数据
for (int i = 1; i <= page; i++) {
// 生成逻辑文件名
String logicalFileName = String.format("%s-%04d", archiveCode, i);
// 如果存在重复档号记录日志并抛出异常
if (!duplicateCodes.isEmpty()) {
StringBuilder errorMsg = new StringBuilder("存在重复档号:\n");
for (String code : duplicateCodes) {
errorMsg.append(code).append("\n");
}
log.info(LoggerMarker.RELEASE_MARKER, "{}", errorMsg.toString());
throw new RuntimeException(errorMsg.toString());
}else {
// 写入CSV头部
writer.println("逻辑文件名,逻辑地址");
int totalRecords = records.stream().mapToInt(r -> r.page).sum();
int current = 0;
// 生成逻辑地址
String logicalAddress = generatePageLevelLogicalAddress(archiveCode, i);
// 处理每条记录
for (Record record : records) {
String archiveCode = record.archiveCode;
// 写入CSV行
writer.printf("%s,%s%n", logicalFileName, logicalAddress);
current++;
safeOnPhaseProgress(Phase.GENERATE_LOGICAL, current, totalRecords);
int page = record.page;
// 为每页生成一行数据
for (int i = 1; i <= page; i++) {
// 生成逻辑文件名
String logicalFileName = String.format("%s-%04d", archiveCode, i);
// 生成逻辑地址
String logicalAddress = generatePageLevelLogicalAddress(archiveCode, i);
// 写入CSV行
writer.printf("%s,%s%n", logicalFileName, logicalAddress);
current++;
safeOnPhaseProgress(Phase.GENERATE_LOGICAL, current, totalRecords);
}
}
}
}

View File

@ -189,9 +189,18 @@ public class PathCheckPaneController implements Initializable {
task.setOnFailed(e -> {
progressBar.closeProgress();
Throwable exception = task.getException();
currentTask.progressProperty().removeListener(progressChangeListener);
currentTask.messageProperty().removeListener(messageChangeListener);
result2TA.setText("检测过程中发生错误: " + exception.getMessage());
// 移除监听器时使用 task 而不是 currentTask
task.progressProperty().removeListener(progressChangeListener);
task.messageProperty().removeListener(messageChangeListener);
// 检查是否是重复档号的特殊信息
if (exception.getMessage() != null && exception.getMessage().startsWith("存在重复档号:")) {
result2TA.setText(exception.getMessage());
} else {
result2TA.setText("检测过程中发生错误: " + exception.getMessage());
}
addResultErrorStyle();
DialogUtil.showDetailedErrorDialog("错误", "检测过程中发生错误: ", exception.getMessage());
generateLogicalAddress2B.setDisable(false);
@ -219,6 +228,7 @@ public class PathCheckPaneController implements Initializable {
* @param actionEvent the action event
*/
@FXML void onGeneratePA(ActionEvent actionEvent) {
generatePhysicalAddress2B.setDisable(true);
removeResultStyle();
String folderPath = loadJPGFolder2TF.getText();

View File

@ -71,6 +71,12 @@ public class AddressFileGenerationTask extends Task<String> {
try {
generator.generateAddressFile(sourcePath, outputFile, folderType);
} catch (Exception e) {
// 不包装已经格式化好的重复档号异常
if (e instanceof RuntimeException &&
e.getMessage() != null &&
e.getMessage().startsWith("存在重复档号:")) {
throw e; // 直接抛出保留原始消息
}
throw new RuntimeException("地址文件生成失败", e);
}
});