|
|
@@ -3,24 +3,16 @@ package com.dtm.storage.service;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import javax.annotation.PreDestroy;
|
|
|
import java.time.Instant;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.ConcurrentMap;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
-import java.util.concurrent.Executors;
|
|
|
|
|
|
@Service
|
|
|
public class InventoryUploadTaskService {
|
|
|
private final StorageUploadService uploadService;
|
|
|
- private final ExecutorService executor = Executors.newSingleThreadExecutor(r -> {
|
|
|
- Thread thread = new Thread(r, "inventory-upload-task");
|
|
|
- thread.setDaemon(true);
|
|
|
- return thread;
|
|
|
- });
|
|
|
private final ConcurrentMap<String, TaskRecord> tasks = new ConcurrentHashMap<>();
|
|
|
|
|
|
public InventoryUploadTaskService(StorageUploadService uploadService) {
|
|
|
@@ -32,19 +24,11 @@ public class InventoryUploadTaskService {
|
|
|
MultipartFile assemblyFile,
|
|
|
MultipartFile productFile,
|
|
|
MultipartFile semiMappingFile) {
|
|
|
- StorageUploadService.UploadBatch batch = uploadService.saveUploadFiles(
|
|
|
- purchaseFile,
|
|
|
- salesFile,
|
|
|
- assemblyFile,
|
|
|
- productFile,
|
|
|
- semiMappingFile
|
|
|
- );
|
|
|
-
|
|
|
String taskId = UUID.randomUUID().toString().replace("-", "");
|
|
|
- TaskRecord record = new TaskRecord(taskId, batch);
|
|
|
+ TaskRecord record = new TaskRecord(taskId);
|
|
|
tasks.put(taskId, record);
|
|
|
|
|
|
- executor.submit(() -> runTask(record));
|
|
|
+ runTask(record, purchaseFile, salesFile, assemblyFile, productFile, semiMappingFile);
|
|
|
return record.toMap();
|
|
|
}
|
|
|
|
|
|
@@ -53,42 +37,66 @@ public class InventoryUploadTaskService {
|
|
|
return record == null ? null : record.toMap();
|
|
|
}
|
|
|
|
|
|
- private void runTask(TaskRecord record) {
|
|
|
+ private void runTask(TaskRecord record,
|
|
|
+ MultipartFile purchaseFile,
|
|
|
+ MultipartFile salesFile,
|
|
|
+ MultipartFile assemblyFile,
|
|
|
+ MultipartFile productFile,
|
|
|
+ MultipartFile semiMappingFile) {
|
|
|
record.status = "running";
|
|
|
record.message = "文件已上传,正在处理库存数据";
|
|
|
record.startedAt = Instant.now().toString();
|
|
|
try {
|
|
|
- uploadService.activateUploadBatch(record.batch);
|
|
|
+ Map<String, Object> result = uploadService.uploadFiles(
|
|
|
+ purchaseFile,
|
|
|
+ salesFile,
|
|
|
+ assemblyFile,
|
|
|
+ productFile,
|
|
|
+ semiMappingFile
|
|
|
+ );
|
|
|
record.status = "success";
|
|
|
record.message = "库存数据处理完成";
|
|
|
- record.result = record.batch.toResult();
|
|
|
+ record.result = result;
|
|
|
+ record.count = parseCount(result);
|
|
|
+ record.files = result == null ? null : result.get("files");
|
|
|
} catch (Exception e) {
|
|
|
record.status = "failed";
|
|
|
record.message = e.getMessage() == null ? "库存数据处理失败" : e.getMessage();
|
|
|
- uploadService.discardUploadBatch(record.batch);
|
|
|
} finally {
|
|
|
record.finishedAt = Instant.now().toString();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @PreDestroy
|
|
|
- public void shutdown() {
|
|
|
- executor.shutdownNow();
|
|
|
+ private Integer parseCount(Map<String, Object> result) {
|
|
|
+ if (result == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ Object countObj = result.get("count");
|
|
|
+ if (countObj instanceof Number) {
|
|
|
+ return ((Number) countObj).intValue();
|
|
|
+ }
|
|
|
+ if (countObj instanceof String) {
|
|
|
+ try {
|
|
|
+ return Integer.parseInt(((String) countObj).trim());
|
|
|
+ } catch (NumberFormatException ignored) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
private static class TaskRecord {
|
|
|
private final String taskId;
|
|
|
- private final StorageUploadService.UploadBatch batch;
|
|
|
private final String createdAt;
|
|
|
private volatile String startedAt;
|
|
|
private volatile String finishedAt;
|
|
|
private volatile String status;
|
|
|
private volatile String message;
|
|
|
private volatile Map<String, Object> result;
|
|
|
+ private volatile Integer count;
|
|
|
+ private volatile Object files;
|
|
|
|
|
|
- private TaskRecord(String taskId, StorageUploadService.UploadBatch batch) {
|
|
|
+ private TaskRecord(String taskId) {
|
|
|
this.taskId = taskId;
|
|
|
- this.batch = batch;
|
|
|
this.createdAt = Instant.now().toString();
|
|
|
this.status = "pending";
|
|
|
this.message = "文件已上传,等待后台处理";
|
|
|
@@ -103,8 +111,8 @@ public class InventoryUploadTaskService {
|
|
|
data.put("startedAt", startedAt);
|
|
|
data.put("finishedAt", finishedAt);
|
|
|
data.put("result", result);
|
|
|
- data.put("count", batch == null ? 0 : batch.getCount());
|
|
|
- data.put("files", batch == null ? null : batch.getFiles());
|
|
|
+ data.put("count", count == null ? 0 : count);
|
|
|
+ data.put("files", files);
|
|
|
return data;
|
|
|
}
|
|
|
}
|