|
|
@@ -0,0 +1,119 @@
|
|
|
+package com.dtm.storage.service;
|
|
|
+
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+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;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class InventoryUploadTaskService {
|
|
|
+ private final StorageUploadService uploadService;
|
|
|
+ private final ConcurrentMap<String, TaskRecord> tasks = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ public InventoryUploadTaskService(StorageUploadService uploadService) {
|
|
|
+ this.uploadService = uploadService;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, Object> submit(MultipartFile purchaseFile,
|
|
|
+ MultipartFile salesFile,
|
|
|
+ MultipartFile assemblyFile,
|
|
|
+ MultipartFile productFile,
|
|
|
+ MultipartFile semiMappingFile) {
|
|
|
+ String taskId = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ TaskRecord record = new TaskRecord(taskId);
|
|
|
+ tasks.put(taskId, record);
|
|
|
+
|
|
|
+ runTask(record, purchaseFile, salesFile, assemblyFile, productFile, semiMappingFile);
|
|
|
+ return record.toMap();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, Object> getTask(String taskId) {
|
|
|
+ TaskRecord record = tasks.get(taskId);
|
|
|
+ return record == null ? null : record.toMap();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 {
|
|
|
+ Map<String, Object> result = uploadService.uploadFiles(
|
|
|
+ purchaseFile,
|
|
|
+ salesFile,
|
|
|
+ assemblyFile,
|
|
|
+ productFile,
|
|
|
+ semiMappingFile
|
|
|
+ );
|
|
|
+ record.status = "success";
|
|
|
+ record.message = "库存数据处理完成";
|
|
|
+ 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();
|
|
|
+ } finally {
|
|
|
+ record.finishedAt = Instant.now().toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 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) {
|
|
|
+ this.taskId = taskId;
|
|
|
+ this.createdAt = Instant.now().toString();
|
|
|
+ this.status = "pending";
|
|
|
+ this.message = "文件已上传,等待后台处理";
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> toMap() {
|
|
|
+ Map<String, Object> data = new LinkedHashMap<>();
|
|
|
+ data.put("taskId", taskId);
|
|
|
+ data.put("status", status);
|
|
|
+ data.put("message", message);
|
|
|
+ data.put("createdAt", createdAt);
|
|
|
+ data.put("startedAt", startedAt);
|
|
|
+ data.put("finishedAt", finishedAt);
|
|
|
+ data.put("result", result);
|
|
|
+ data.put("count", count == null ? 0 : count);
|
|
|
+ data.put("files", files);
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|