From ece0de15ad912ff6ae40d73b013d357f8fc57324 Mon Sep 17 00:00:00 2001 From: "han\\hanst" Date: Wed, 8 Apr 2026 11:19:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95Sheet=20=E5=A4=B1=E8=B4=A5=E5=9B=9E?= =?UTF-8?q?=E6=BB=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/CoDelExcelTXServiceImpl.java | 61 ++++++++++++------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/xujie/sys/modules/ecss/service/impl/CoDelExcelTXServiceImpl.java b/src/main/java/com/xujie/sys/modules/ecss/service/impl/CoDelExcelTXServiceImpl.java index f2dbfc41..b9e14a73 100644 --- a/src/main/java/com/xujie/sys/modules/ecss/service/impl/CoDelExcelTXServiceImpl.java +++ b/src/main/java/com/xujie/sys/modules/ecss/service/impl/CoDelExcelTXServiceImpl.java @@ -29,7 +29,9 @@ import org.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.support.TransactionTemplate; import org.springframework.web.multipart.MultipartFile; import jakarta.servlet.http.HttpServletRequest; @@ -49,6 +51,8 @@ public class CoDelExcelTXServiceImpl implements CoDelExcelTXService { private CoDelMapper coDelMapper; @Autowired SqlSession sqlSession; + @Autowired + private PlatformTransactionManager transactionManager; @Override public Map previewExcelTX(MultipartFile file, EcssCoDelNotifyHeaderData inData) { @@ -184,7 +188,6 @@ public class CoDelExcelTXServiceImpl implements CoDelExcelTXService { } @Override - @Transactional(rollbackFor = Exception.class) public Map> saveEcssCoDelNotifyByExcelTX(MultipartFile file, EcssCoDelNotifyHeaderData data, String deletedInvoices, HttpServletRequest request) throws Exception { List deletedInvoiceList = new ArrayList<>(); if (deletedInvoices != null && !deletedInvoices.trim().isEmpty()) { @@ -199,32 +202,48 @@ public class CoDelExcelTXServiceImpl implements CoDelExcelTXService { List successList = new ArrayList<>(); List failList = new ArrayList<>(); + TransactionTemplate sheetTxTemplate = new TransactionTemplate(transactionManager); + sheetTxTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); - Workbook workbook = WorkbookFactory.create(file.getInputStream()); - for (int i = 0; i < workbook.getNumberOfSheets(); i++) { - Sheet sheet = workbook.getSheetAt(i); - if (sheet.getPhysicalNumberOfRows() < 2) { - continue; - } - try { + try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) { + for (int i = 0; i < workbook.getNumberOfSheets(); i++) { + Sheet sheet = workbook.getSheetAt(i); + if (sheet.getPhysicalNumberOfRows() < 2) { + continue; + } String cmcInvoice = getInvoiceFromSheet(sheet); - if (cmcInvoice != null) { - if (deletedInvoiceList.contains(cmcInvoice)) { - continue; + try { + if (cmcInvoice != null) { + if (deletedInvoiceList.contains(cmcInvoice)) { + continue; + } + // 检查发票是否已存在,如果已存在则直接过滤掉(不加入成功也不加入失败列表) + List existingHeaders = coDelMapper.checkIfHasHeader(cmcInvoice); + if (existingHeaders != null && !existingHeaders.isEmpty()) { + continue; + } } - // 检查发票是否已存在,如果已存在则直接过滤掉(不加入成功也不加入失败列表) - List existingHeaders = coDelMapper.checkIfHasHeader(cmcInvoice); - if (existingHeaders != null && !existingHeaders.isEmpty()) { - continue; + // 每个Sheet独立事务,失败只回滚当前Sheet + sheetTxTemplate.executeWithoutResult(status -> { + try { + processSheet(sheet, data, request); + } catch (Exception e) { + status.setRollbackOnly(); + throw new RuntimeException(e); + } + }); + successList.add(cmcInvoice); + } catch (Exception e) { + Throwable cause = e; + while (cause.getCause() != null) { + cause = cause.getCause(); } + String errMsg = cause.getMessage() != null ? cause.getMessage() : e.getMessage(); + failList.add("Sheet [" + sheet.getSheetName() + "] 错误:" + errMsg); + log.error("Sheet导入失败已回滚: sheetName={}, cmcInvoice={}, err={}", sheet.getSheetName(), cmcInvoice, errMsg, e); } - processSheet(sheet, data, request); - successList.add(cmcInvoice); - } catch (Exception e) { - failList.add("Sheet [" + sheet.getSheetName() + "] 错误:" + e.getMessage()); } } - workbook.close(); Map> result = new HashMap<>(); result.put("success", successList);