| | |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.PlatformTransactionManager; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.transaction.support.TransactionTemplate; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | |
| | | |
| | | @Resource |
| | | private MesWmItemReceiptLineService itemReceiptLineService; |
| | | @Resource |
| | | private PlatformTransactionManager transactionManager; |
| | | @Resource |
| | | private MesWmItemReceiptDetailService itemReceiptDetailService; |
| | | @Resource |
| | |
| | | // ==================== 批量导入 ==================== |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesWmDocImportRespVO importItemReceiptList(List<MesWmItemReceiptImportExcelVO> importList) { |
| | | MesWmDocImportRespVO respVO = MesWmDocImportRespVO.empty(); |
| | | if (CollUtil.isEmpty(importList)) { |
| | |
| | | saveReqVO.setRemark(StrUtil.trim(head.getRemark())); |
| | | Long receiptId = createItemReceipt(saveReqVO); |
| | | // 3.4 逐行创建明细 |
| | | int createdLines = 0; |
| | | for (MesWmItemReceiptImportExcelVO row : rows) { |
| | | createItemReceiptLineByImport(receiptId, row, respVO, code); |
| | | if (createItemReceiptLineByImport(receiptId, row, respVO, code)) { |
| | | createdLines++; |
| | | } |
| | | } |
| | | respVO.getCreateCodes().add(code); |
| | | if (createdLines == 0) { |
| | | // 没有任何有效明细时不保留空单,也不报告导入成功。 |
| | | itemReceiptMapper.deleteById(receiptId); |
| | | } else { |
| | | respVO.getCreateCodes().add(code); |
| | | } |
| | | } catch (Exception e) { |
| | | respVO.getFailureCodes().put(code, e.getMessage() == null ? "导入失败" : e.getMessage()); |
| | | } |
| | |
| | | /** |
| | | * 导入时创建单行明细;失败只记录不抛出,避免影响整张单据的其它行 |
| | | */ |
| | | private void createItemReceiptLineByImport(Long receiptId, MesWmItemReceiptImportExcelVO row, |
| | | private boolean createItemReceiptLineByImport(Long receiptId, MesWmItemReceiptImportExcelVO row, |
| | | MesWmDocImportRespVO respVO, String receiptCode) { |
| | | String itemCode = StrUtil.trim(row.getItemCode()); |
| | | MesMdItemDO item = itemCode == null ? null : mdItemService.getItemByCode(itemCode); |
| | | if (item == null) { |
| | | respVO.getFailureCodes().put(receiptCode, "物料编码[" + itemCode + "]不存在,该行已跳过"); |
| | | return; |
| | | return false; |
| | | } |
| | | if (row.getReceivedQuantity() == null || row.getReceivedQuantity().signum() <= 0) { |
| | | respVO.getFailureCodes().put(receiptCode, "物料[" + itemCode + "]入库数量必须大于 0,该行已跳过"); |
| | | return; |
| | | return false; |
| | | } |
| | | try { |
| | | MesWmItemReceiptLineSaveReqVO lineReqVO = new MesWmItemReceiptLineSaveReqVO(); |
| | |
| | | lineReqVO.setExpireDate(parseImportDateTime(row.getExpireDate())); |
| | | lineReqVO.setLotNumber(StrUtil.trim(row.getLotNumber())); |
| | | lineReqVO.setRemark(StrUtil.trim(row.getRemark())); |
| | | itemReceiptLineService.createItemReceiptLine(lineReqVO); |
| | | // 每行独立事务:批次校验失败时只回滚该行,避免污染整次导入事务。 |
| | | new TransactionTemplate(transactionManager).executeWithoutResult( |
| | | status -> itemReceiptLineService.createItemReceiptLine(lineReqVO)); |
| | | return true; |
| | | } catch (Exception e) { |
| | | respVO.getFailureCodes().put(receiptCode, "物料[" + itemCode + "]明细创建失败:" + e.getMessage()); |
| | | return false; |
| | | } |
| | | } |
| | | |