| | |
| | | import cn.iocoder.yudao.module.mes.service.pro.task.MesProTaskService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.barcode.MesWmBarcodeService; |
| | | import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; |
| | | import cn.iocoder.yudao.module.erp.api.sale.ErpSaleOrderApi; |
| | | import cn.iocoder.yudao.module.erp.api.sale.dto.ErpSaleOrderRespDTO; |
| | | 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; |
| | |
| | | @Resource |
| | | private CrmCustomerApi customerApi; |
| | | |
| | | @Resource |
| | | @Lazy // 避免 erpSaleOrderApiImpl -> ... -> mesProWorkOrderServiceImpl 的循环依赖 |
| | | private ErpSaleOrderApi saleOrderApi; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createWorkOrder(MesProWorkOrderSaveReqVO createReqVO) { |
| | | // 1. 校验数据 |
| | | validateWorkOrderSaveData(null, createReqVO); |
| | | // 1.1 关联营销订单信息回填 |
| | | fillSaleOrderInfo(createReqVO); |
| | | |
| | | // 2.1 设置默认值 |
| | | if (createReqVO.getParentId() == null) { |
| | |
| | | workOrder.setStatus(MesProWorkOrderStatusEnum.PREPARE.getStatus()); |
| | | workOrderMapper.insert(workOrder); |
| | | |
| | | // 3. 自动生成 BOM:根据产品 BOM 生成工单 BOM |
| | | workOrderBomService.generateWorkOrderBom(workOrder.getId(), createReqVO, false); |
| | | // 3. 自动生成 BOM:根据产品 BOM 生成工单 BOM(无产品的运维/发电/检修类工单跳过) |
| | | if (createReqVO.getProductId() != null) { |
| | | workOrderBomService.generateWorkOrderBom(workOrder.getId(), createReqVO, false); |
| | | } |
| | | |
| | | // 4. 工序由前端选择产品后带入,不再自动生成 |
| | | |
| | |
| | | } |
| | | // 1.2 校验数据 |
| | | validateWorkOrderSaveData(updateReqVO.getId(), updateReqVO); |
| | | // 1.3 关联营销订单信息回填 |
| | | fillSaleOrderInfo(updateReqVO); |
| | | |
| | | // 2. 判断产品或数量是否变更,如果变更则重新生成 BOM(updated=true 会先清理旧数据) |
| | | // 2. 判断产品或数量是否变更,如果变更则重新生成 BOM(updated=true 会先清理旧数据;无产品的运维类工单跳过) |
| | | boolean productChanged = ObjUtil.notEqual(oldWorkOrder.getProductId(), updateReqVO.getProductId()); |
| | | boolean quantityChanged = oldWorkOrder.getQuantity().compareTo(updateReqVO.getQuantity()) != 0; |
| | | if (productChanged || quantityChanged) { |
| | | if ((productChanged || quantityChanged) && updateReqVO.getProductId() != null) { |
| | | workOrderBomService.generateWorkOrderBom(updateReqVO.getId(), updateReqVO, true); |
| | | } |
| | | // 2.1 工序由前端管理,产品变更时前端会重新获取工艺路线工序 |
| | |
| | | private void validateWorkOrderSaveData(Long id, MesProWorkOrderSaveReqVO reqVO) { |
| | | // 1. 校验编码唯一 |
| | | validateWorkOrderCodeUnique(id, reqVO.getCode()); |
| | | // 2. 校验产品存在 |
| | | itemService.validateItemExists(reqVO.getProductId()); |
| | | // 3. 校验批次配置(如果产品有 clientFlag=true,则 clientId 必填) |
| | | MesMdItemBatchConfigDO batchConfig = itemBatchConfigService.getItemBatchConfigByItemId(reqVO.getProductId()); |
| | | if (batchConfig != null && Boolean.TRUE.equals(batchConfig.getClientFlag()) && reqVO.getClientId() == null) { |
| | | throw exception(MD_CLIENT_NOT_EXISTS); |
| | | // 2. 产品非空时校验产品存在(运维/发电/检修类工单可无产品) |
| | | if (reqVO.getProductId() != null) { |
| | | itemService.validateItemExists(reqVO.getProductId()); |
| | | // 3. 校验批次配置(如果产品有 clientFlag=true,则 clientId 必填) |
| | | MesMdItemBatchConfigDO batchConfig = itemBatchConfigService.getItemBatchConfigByItemId(reqVO.getProductId()); |
| | | if (batchConfig != null && Boolean.TRUE.equals(batchConfig.getClientFlag()) && reqVO.getClientId() == null) { |
| | | throw exception(MD_CLIENT_NOT_EXISTS); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 关联营销订单(ERP 销售订单):校验存在性并回填单号、客户 |
| | | */ |
| | | private void fillSaleOrderInfo(MesProWorkOrderSaveReqVO reqVO) { |
| | | if (reqVO.getSaleOrderId() == null) { |
| | | return; |
| | | } |
| | | ErpSaleOrderRespDTO saleOrder = saleOrderApi.getSaleOrder(reqVO.getSaleOrderId()).getCheckedData(); |
| | | if (saleOrder == null) { |
| | | throw exception(PRO_WORK_ORDER_SALE_ORDER_NOT_EXISTS); |
| | | } |
| | | reqVO.setSaleOrderNo(saleOrder.getNo()); |
| | | if (reqVO.getClientId() == null && saleOrder.getCustomerId() != null) { |
| | | reqVO.setClientId(saleOrder.getCustomerId()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void updateProducedQuantity(Long id, BigDecimal incrQuantityProduced) { |
| | | // 校验工单存在 |