|
|
@ -29,7 +29,9 @@ import org.apache.shiro.SecurityUtils; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
import org.springframework.core.io.ClassPathResource; |
|
|
import org.springframework.stereotype.Service; |
|
|
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 org.springframework.web.multipart.MultipartFile; |
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
|
|
|
|
|
|
@ -49,6 +51,8 @@ public class CoDelExcelTXServiceImpl implements CoDelExcelTXService { |
|
|
private CoDelMapper coDelMapper; |
|
|
private CoDelMapper coDelMapper; |
|
|
@Autowired |
|
|
@Autowired |
|
|
SqlSession sqlSession; |
|
|
SqlSession sqlSession; |
|
|
|
|
|
@Autowired |
|
|
|
|
|
private PlatformTransactionManager transactionManager; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public Map<String, Object> previewExcelTX(MultipartFile file, EcssCoDelNotifyHeaderData inData) { |
|
|
public Map<String, Object> previewExcelTX(MultipartFile file, EcssCoDelNotifyHeaderData inData) { |
|
|
@ -184,7 +188,6 @@ public class CoDelExcelTXServiceImpl implements CoDelExcelTXService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
|
public Map<String, List<String>> saveEcssCoDelNotifyByExcelTX(MultipartFile file, EcssCoDelNotifyHeaderData data, String deletedInvoices, HttpServletRequest request) throws Exception { |
|
|
public Map<String, List<String>> saveEcssCoDelNotifyByExcelTX(MultipartFile file, EcssCoDelNotifyHeaderData data, String deletedInvoices, HttpServletRequest request) throws Exception { |
|
|
List<String> deletedInvoiceList = new ArrayList<>(); |
|
|
List<String> deletedInvoiceList = new ArrayList<>(); |
|
|
if (deletedInvoices != null && !deletedInvoices.trim().isEmpty()) { |
|
|
if (deletedInvoices != null && !deletedInvoices.trim().isEmpty()) { |
|
|
@ -199,32 +202,48 @@ public class CoDelExcelTXServiceImpl implements CoDelExcelTXService { |
|
|
|
|
|
|
|
|
List<String> successList = new ArrayList<>(); |
|
|
List<String> successList = new ArrayList<>(); |
|
|
List<String> failList = new ArrayList<>(); |
|
|
List<String> 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); |
|
|
String cmcInvoice = getInvoiceFromSheet(sheet); |
|
|
if (cmcInvoice != null) { |
|
|
|
|
|
if (deletedInvoiceList.contains(cmcInvoice)) { |
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
if (cmcInvoice != null) { |
|
|
|
|
|
if (deletedInvoiceList.contains(cmcInvoice)) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
// 检查发票是否已存在,如果已存在则直接过滤掉(不加入成功也不加入失败列表) |
|
|
|
|
|
List<EcssCoDelNotifyHeaderData> existingHeaders = coDelMapper.checkIfHasHeader(cmcInvoice); |
|
|
|
|
|
if (existingHeaders != null && !existingHeaders.isEmpty()) { |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
// 检查发票是否已存在,如果已存在则直接过滤掉(不加入成功也不加入失败列表) |
|
|
|
|
|
List<EcssCoDelNotifyHeaderData> 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<String, List<String>> result = new HashMap<>(); |
|
|
Map<String, List<String>> result = new HashMap<>(); |
|
|
result.put("success", successList); |
|
|
result.put("success", successList); |
|
|
|