wangxx 2 週間 前
コミット
a100f4323d
共有1 個のファイルを変更した18 個の追加7 個の削除を含む
  1. 18 7
      airport-ledger/src/main/java/com/sundot/airport/ledger/service/impl/LedgerCombinedImportServiceImpl.java

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