wangxx 2 hafta önce
ebeveyn
işleme
a100f4323d

+ 18 - 7
airport-ledger/src/main/java/com/sundot/airport/ledger/service/impl/LedgerCombinedImportServiceImpl.java

@@ -26,6 +26,7 @@ import com.sundot.airport.system.mapper.BasePositionMapper;
26
 import org.apache.poi.ss.usermodel.*;
26
 import org.apache.poi.ss.usermodel.*;
27
 import org.springframework.beans.factory.annotation.Autowired;
27
 import org.springframework.beans.factory.annotation.Autowired;
28
 import org.springframework.stereotype.Service;
28
 import org.springframework.stereotype.Service;
29
+import org.springframework.transaction.annotation.Propagation;
29
 import org.springframework.transaction.annotation.Transactional;
30
 import org.springframework.transaction.annotation.Transactional;
30
 import org.springframework.web.multipart.MultipartFile;
31
 import org.springframework.web.multipart.MultipartFile;
31
 
32
 
@@ -63,6 +64,7 @@ public class LedgerCombinedImportServiceImpl implements ILedgerCombinedImportSer
63
     @Autowired private ILedgerDormFireSafetyService dormFireSafetyService;
64
     @Autowired private ILedgerDormFireSafetyService dormFireSafetyService;
64
     @Autowired private ILedgerTrainingIssueService trainingIssueService;
65
     @Autowired private ILedgerTrainingIssueService trainingIssueService;
65
     @Autowired private IScoreEventService scoreEventService;
66
     @Autowired private IScoreEventService scoreEventService;
67
+    @Autowired private LedgerCombinedImportServiceImpl self; // 自注入,用于嵌套事务
66
     @Autowired private SysUserMapper sysUserMapper;
68
     @Autowired private SysUserMapper sysUserMapper;
67
     @Autowired private SysDeptMapper sysDeptMapper;
69
     @Autowired private SysDeptMapper sysDeptMapper;
68
     @Autowired private SysPostMapper sysPostMapper;
70
     @Autowired private SysPostMapper sysPostMapper;
@@ -101,14 +103,14 @@ public class LedgerCombinedImportServiceImpl implements ILedgerCombinedImportSer
101
     // ════════════════════════════════════════════════════════════════
103
     // ════════════════════════════════════════════════════════════════
102
     //  入口
104
     //  入口
103
     // ════════════════════════════════════════════════════════════════
105
     // ════════════════════════════════════════════════════════════════
104
-    @Transactional(rollbackFor = Exception.class)
105
     @Override
106
     @Override
107
+    @Transactional(rollbackFor = Exception.class)
106
     public Map<String, String> importAll(MultipartFile file, String batchNo, String username) throws Exception {
108
     public Map<String, String> importAll(MultipartFile file, String batchNo, String username) throws Exception {
107
         Map<String, String> result = new LinkedHashMap<>();
109
         Map<String, String> result = new LinkedHashMap<>();
110
+        boolean hasError = false;
108
         try (Workbook wb = WorkbookFactory.create(file.getInputStream())) {
111
         try (Workbook wb = WorkbookFactory.create(file.getInputStream())) {
109
-            // 构建导入缓存
110
             buildImportCache();
112
             buildImportCache();
111
-            
113
+
112
             for (Map.Entry<String, String> entry : SHEET_ROUTE.entrySet()) {
114
             for (Map.Entry<String, String> entry : SHEET_ROUTE.entrySet()) {
113
                 String sheetName = entry.getKey();
115
                 String sheetName = entry.getKey();
114
                 String handler   = entry.getValue();
116
                 String handler   = entry.getValue();
@@ -118,19 +120,28 @@ public class LedgerCombinedImportServiceImpl implements ILedgerCombinedImportSer
118
                     continue;
120
                     continue;
119
                 }
121
                 }
120
                 try {
122
                 try {
121
-                    int count = dispatch(sheet, handler, batchNo, username);
122
-                    result.put(sheetName, "导入 " + count + " 条");
123
+                    int count = self.dispatchInNestedTransaction(sheet, handler, batchNo, username);
124
+                    result.put(sheetName, "导入成功 " + count + " 条");
123
                 } catch (Exception e) {
125
                 } catch (Exception e) {
124
-                    result.put(sheetName, "错误: " + e.getMessage());
126
+                    result.put(sheetName, "导入失败: " + e.getMessage());
127
+                    hasError = true;
125
                 }
128
                 }
126
             }
129
             }
127
         } finally {
130
         } finally {
128
-            // 导入完成后释放缓存
129
             clearImportCache();
131
             clearImportCache();
130
         }
132
         }
133
+        if (hasError) {
134
+            throw new RuntimeException("导入失败,所有数据已回滚。详情: " + result);
135
+        }
131
         return result;
136
         return result;
132
     }
137
     }
133
 
138
 
139
+
140
+    @Transactional(propagation = Propagation.NESTED, rollbackFor = Exception.class)
141
+    public int dispatchInNestedTransaction(Sheet sheet, String handler, String batchNo, String username) {
142
+        return dispatch(sheet, handler, batchNo, username);
143
+    }
144
+
134
     private int dispatch(Sheet sheet, String handler, String batchNo, String username) {
145
     private int dispatch(Sheet sheet, String handler, String batchNo, String username) {
135
         switch (handler) {
146
         switch (handler) {
136
             case "supervisionProblem":    return doSupervisionProblem(sheet, batchNo, username);
147
             case "supervisionProblem":    return doSupervisionProblem(sheet, batchNo, username);