| | |
| | | import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmItemReceiptStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmTransactionTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.service.md.vendor.MesMdVendorService; |
| | | import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.service.qc.iqc.MesQcIqcService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.arrivalnotice.MesWmArrivalNoticeService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.arrivalnotice.MesWmArrivalNoticeLineService; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.arrivalnotice.MesWmArrivalNoticeLineDO; |
| | | import cn.iocoder.yudao.module.mes.service.wm.transaction.MesWmTransactionService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.transaction.dto.MesWmTransactionSaveReqDTO; |
| | | import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi; |
| | | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; |
| | | import cn.iocoder.yudao.module.erp.api.purchase.ErpPurchaseOrderApi; |
| | | import cn.iocoder.yudao.module.erp.api.purchase.ErpSupplierApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| | |
| | | */ |
| | | @Service |
| | | @Validated |
| | | @Slf4j |
| | | public class MesWmItemReceiptServiceImpl implements MesWmItemReceiptService { |
| | | |
| | | @Resource |
| | |
| | | @Lazy |
| | | private MesWmArrivalNoticeService arrivalNoticeService; |
| | | @Resource |
| | | private MesMdVendorService vendorService; |
| | | @Lazy |
| | | private ErpSupplierApi supplierApi; |
| | | @Resource |
| | | @Lazy |
| | | private MesQcIqcService iqcService; |
| | | @Resource |
| | | private MesWmTransactionService wmTransactionService; |
| | | |
| | | @Resource |
| | | private MesWmArrivalNoticeLineService arrivalNoticeLineService; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | |
| | | @Resource |
| | | private ErpPurchaseOrderApi purchaseOrderApi; |
| | | |
| | | @Resource |
| | | private MdmItemApi mdmItemApi; |
| | | |
| | | @Override |
| | | public Long createItemReceipt(MesWmItemReceiptSaveReqVO createReqVO) { |
| | |
| | | private void validateItemReceiptSaveData(MesWmItemReceiptSaveReqVO reqVO) { |
| | | // 校验编码唯一 |
| | | validateCodeUnique(reqVO.getId(), reqVO.getCode()); |
| | | // 校验供应商存在 |
| | | vendorService.validateVendorExistsAndEnable(reqVO.getVendorId()); |
| | | // 校验供应商存在(采购入库使用 ERP 供应商) |
| | | supplierApi.validateSupplier(reqVO.getVendorId()); |
| | | // 校验到货通知单存在 |
| | | if (reqVO.getNoticeId() != null) { |
| | | MesWmArrivalNoticeDO notice = arrivalNoticeService.validateArrivalNoticeReadyForReceipt(reqVO.getNoticeId()); |
| | |
| | | throw exception(WM_ITEM_RECEIPT_NO_LINE); |
| | | } |
| | | |
| | | // 校验物料批次管理:如果物料开启批次管理,则明细的批次号不能为空 |
| | | validateBatchManagement(lines); |
| | | |
| | | // 提交(草稿 → 待上架) |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id).setStatus(MesWmItemReceiptStatusEnum.APPROVING.getStatus())); |
| | | } |
| | | |
| | | /** |
| | | * 校验物料批次管理:如果物料开启批次管理,则行的批次号不能为空 |
| | | */ |
| | | private void validateBatchManagement(List<MesWmItemReceiptLineDO> lines) { |
| | | // 获取所有物料ID |
| | | List<Long> itemIds = CollectionUtils.convertList(lines, MesWmItemReceiptLineDO::getItemId); |
| | | Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap(itemIds); |
| | | |
| | | // 遍历每一行,检查物料是否开启批次管理 |
| | | for (MesWmItemReceiptLineDO line : lines) { |
| | | MdmItemRespDTO item = itemMap.get(line.getItemId()); |
| | | if (item == null) { |
| | | continue; |
| | | } |
| | | // 如果物料开启批次管理,检查行上的批次号是否为空 |
| | | if (Boolean.TRUE.equals(item.getIsBatchManaged()) && line.getBatchId() == null) { |
| | | throw exception(WM_ITEM_RECEIPT_BATCH_REQUIRED, item.getCode(), item.getName()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (receipt.getNoticeId() != null) { |
| | | arrivalNoticeService.finishArrivalNotice(receipt.getNoticeId()); |
| | | } |
| | | |
| | | // 5. 回写 ERP 采购订单的入库数量 |
| | | updateErpPurchaseOrderInCount(receipt); |
| | | } |
| | | |
| | | /** |
| | | * 回写 ERP 采购订单的入库数量 |
| | | */ |
| | | private void updateErpPurchaseOrderInCount(MesWmItemReceiptDO receipt) { |
| | | // 获取入库单行 |
| | | List<MesWmItemReceiptLineDO> lines = itemReceiptLineService.getItemReceiptLineListByReceiptId(receipt.getId()); |
| | | if (CollUtil.isEmpty(lines)) { |
| | | return; |
| | | } |
| | | // 用于存储采购订单 ID,避免重复更新 |
| | | Long purchaseOrderId = null; |
| | | // 遍历行,按 purchaseOrderItemId 更新入库数量 |
| | | for (MesWmItemReceiptLineDO line : lines) { |
| | | if (line.getPurchaseOrderItemId() != null && line.getReceivedQuantity() != null) { |
| | | purchaseOrderApi.updateOrderItemInCount(line.getPurchaseOrderItemId(), line.getReceivedQuantity()); |
| | | // 获取采购订单 ID(从第一个有效的明细项获取) |
| | | if (purchaseOrderId == null) { |
| | | cn.iocoder.yudao.module.erp.api.purchase.dto.ErpPurchaseOrderItemRespDTO itemDTO = |
| | | purchaseOrderApi.getPurchaseOrderItem(line.getPurchaseOrderItemId()).getCheckedData(); |
| | | if (itemDTO != null && itemDTO.getOrderId() != null) { |
| | | purchaseOrderId = itemDTO.getOrderId(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // 更新采购订单的入库状态 |
| | | if (purchaseOrderId != null) { |
| | | purchaseOrderApi.updateOrderInStatus(purchaseOrderId); |
| | | } |
| | | } |
| | | |
| | | private void createTransactionList(MesWmItemReceiptDO receipt) { |
| | | List<MesWmItemReceiptDetailDO> details = itemReceiptDetailService.getItemReceiptDetailListByReceiptId(receipt.getId()); |
| | | wmTransactionService.createTransactionList(convertList(details, detail -> new MesWmTransactionSaveReqDTO() |
| | | .setType(MesWmTransactionTypeEnum.IN.getType()).setItemId(detail.getItemId()) |
| | | .setQuantity(detail.getQuantity()) // 入库数量为正数 |
| | | .setBatchId(detail.getBatchId()) |
| | | .setWarehouseId(detail.getWarehouseId()).setLocationId(detail.getLocationId()).setAreaId(detail.getAreaId()) |
| | | .setVendorId(receipt.getVendorId()).setReceiptTime(receipt.getReceiptDate()) |
| | | .setBizType(MesBizTypeConstants.WM_ITEM_RECEIPT_IN).setBizId(receipt.getId()) |
| | | .setBizCode(receipt.getCode()).setBizLineId(detail.getLineId()))); |
| | | // 获取所有行信息,用于获取批次号 |
| | | List<MesWmItemReceiptLineDO> lines = itemReceiptLineService.getItemReceiptLineListByReceiptId(receipt.getId()); |
| | | Map<Long, MesWmItemReceiptLineDO> lineMap = CollectionUtils.convertMap(lines, MesWmItemReceiptLineDO::getId); |
| | | |
| | | wmTransactionService.createTransactionList(convertList(details, detail -> { |
| | | // 从行上获取批次信息(如果明细没有批次号) |
| | | MesWmItemReceiptLineDO line = lineMap.get(detail.getLineId()); |
| | | Long batchId = detail.getBatchId() != null ? detail.getBatchId() : |
| | | (line != null ? line.getBatchId() : null); |
| | | String batchCode = line != null ? line.getBatchCode() : null; |
| | | |
| | | return new MesWmTransactionSaveReqDTO() |
| | | .setType(MesWmTransactionTypeEnum.IN.getType()).setItemId(detail.getItemId()) |
| | | .setQuantity(detail.getQuantity()) |
| | | .setBatchId(batchId).setBatchCode(batchCode) |
| | | .setWarehouseId(detail.getWarehouseId()).setLocationId(detail.getLocationId()).setAreaId(detail.getAreaId()) |
| | | .setVendorId(receipt.getVendorId()).setReceiptTime(receipt.getReceiptDate()) |
| | | .setBizType(MesBizTypeConstants.WM_ITEM_RECEIPT_IN).setBizId(receipt.getId()) |
| | | .setBizCode(receipt.getCode()).setBizLineId(detail.getLineId()); |
| | | })); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return itemReceiptMapper.selectByIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long generateItemReceiptFromArrivalNotice(Long arrivalNoticeId) { |
| | | // 1. 校验到货通知单存在且状态为待入库 |
| | | MesWmArrivalNoticeDO notice = arrivalNoticeService.validateArrivalNoticeReadyForReceipt(arrivalNoticeId); |
| | | |
| | | // 2. 校验是否已生成过入库单(防止重复生成) |
| | | MesWmItemReceiptDO existReceipt = itemReceiptMapper.selectByNoticeId(arrivalNoticeId); |
| | | if (existReceipt != null) { |
| | | throw exception(WM_ITEM_RECEIPT_ALREADY_EXISTS); |
| | | } |
| | | |
| | | // 3. 获取到货通知单行 |
| | | List<MesWmArrivalNoticeLineDO> lines = arrivalNoticeLineService.getArrivalNoticeLineListByNoticeId(arrivalNoticeId); |
| | | if (CollUtil.isEmpty(lines)) { |
| | | throw exception(WM_ARRIVAL_NOTICE_NO_LINE); |
| | | } |
| | | |
| | | // 4. 生成入库单编码 |
| | | String code = generateItemReceiptCode(); |
| | | |
| | | // 5. 创建入库单主表 |
| | | MesWmItemReceiptDO receipt = new MesWmItemReceiptDO(); |
| | | receipt.setCode(code); |
| | | receipt.setName(notice.getName() + "入库单"); |
| | | receipt.setNoticeId(arrivalNoticeId); |
| | | receipt.setPurchaseOrderCode(notice.getPurchaseOrderCode()); |
| | | receipt.setVendorId(notice.getVendorId()); |
| | | receipt.setReceiptDate(java.time.LocalDateTime.now()); // 入库时间为当前时间 |
| | | receipt.setStatus(MesWmItemReceiptStatusEnum.PREPARE.getStatus()); |
| | | itemReceiptMapper.insert(receipt); |
| | | |
| | | // 6. 遍历行创建入库单行 |
| | | for (MesWmArrivalNoticeLineDO noticeLine : lines) { |
| | | MesWmItemReceiptLineDO receiptLine = new MesWmItemReceiptLineDO(); |
| | | receiptLine.setReceiptId(receipt.getId()); |
| | | receiptLine.setArrivalNoticeLineId(noticeLine.getId()); |
| | | receiptLine.setPurchaseOrderItemId(noticeLine.getPurchaseOrderItemId()); |
| | | receiptLine.setItemId(noticeLine.getItemId()); |
| | | // 使用合格数量(如果有 IQC 则是合格数量,否则是到货数量) |
| | | receiptLine.setReceivedQuantity(noticeLine.getQualifiedQuantity() != null ? noticeLine.getQualifiedQuantity() : noticeLine.getArrivalQuantity()); |
| | | itemReceiptLineService.createItemReceiptLineFromArrivalNoticeLine(receiptLine); |
| | | } |
| | | |
| | | return receipt.getId(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitItemReceiptApproval(Long id, String processDefinitionKey) { |
| | | // 1. 校验存在 + 草稿状态 |
| | | validateItemReceiptExistsAndDraft(id); |
| | | |
| | | // 2. 创建 BPM 流程实例 |
| | | Long userId = getCurrentUserId(); |
| | | String processInstanceId = processInstanceApi.createProcessInstance(userId, |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 3. 更新入库单状态为审批中 |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id) |
| | | .setStatus(MesWmItemReceiptStatusEnum.APPROVING.getStatus()) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateItemReceiptAuditStatus(Long id, Integer bpmResult) { |
| | | // 1. 校验存在 |
| | | MesWmItemReceiptDO receipt = validateItemReceiptExists(id); |
| | | if (!MesWmItemReceiptStatusEnum.APPROVING.getStatus().equals(receipt.getStatus())) { |
| | | log.warn("[updateItemReceiptAuditStatus] 入库单({}) 不处于审批中状态", id); |
| | | return; |
| | | } |
| | | |
| | | // 2. 根据审批结果更新状态 |
| | | Integer newStatus = convertBpmResultToReceiptStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id).setStatus(newStatus)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换 BPM 审批结果为入库单状态 |
| | | */ |
| | | private Integer convertBpmResultToReceiptStatus(Integer bpmResult) { |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | return MesWmItemReceiptStatusEnum.APPROVED.getStatus(); |
| | | } else if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmResult)) { |
| | | return MesWmItemReceiptStatusEnum.PREPARE.getStatus(); |
| | | } else if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmResult)) { |
| | | return MesWmItemReceiptStatusEnum.CANCELED.getStatus(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前用户 ID |
| | | */ |
| | | private Long getCurrentUserId() { |
| | | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication != null && authentication.getPrincipal() instanceof Long) { |
| | | return (Long) authentication.getPrincipal(); |
| | | } |
| | | return 1L; // 默认用户 |
| | | } |
| | | |
| | | /** |
| | | * 生成入库单编码 |
| | | */ |
| | | private String generateItemReceiptCode() { |
| | | // TODO: 使用编码规则服务生成编码 |
| | | return "IR" + System.currentTimeMillis(); |
| | | } |
| | | |
| | | } |