云
2026-11-03 fd1efdba56dbf07943b82c971694579af034fc5a
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesLineServiceImpl.java
@@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.mes.service.wm.productsales;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.erp.api.sale.ErpSaleOrderApi;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ObjUtil;
@@ -35,7 +36,7 @@
/**
 * MES 销售出库单行 Service 实现类
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -60,14 +61,19 @@
    @Resource
    @Lazy
    private MesQcOqcService oqcService;
    @Resource
    private ErpSaleOrderApi saleOrderApi;
    @Override
    public Long createProductSalesLine(MesWmProductSalesLineSaveReqVO createReqVO) {
        // 校验数据
        validateLineSaveData(createReqVO);
        MesWmSalesNoticeLineDO noticeLine = validateLineSaveData(createReqVO);
        // 新增
        MesWmProductSalesLineDO line = BeanUtils.toBean(createReqVO, MesWmProductSalesLineDO.class);
        if (noticeLine != null) {
            line.setSaleOrderItemId(noticeLine.getSaleOrderItemId());
        }
        productSalesLineMapper.insert(line);
        return line.getId();
    }
@@ -78,10 +84,13 @@
        MesWmProductSalesLineDO oldLine = validateProductSalesLineExists(updateReqVO.getId());
        // 校验数据
        updateReqVO.setSalesId(oldLine.getSalesId());
        validateLineSaveData(updateReqVO);
        MesWmSalesNoticeLineDO noticeLine = validateLineSaveData(updateReqVO);
        // 更新
        MesWmProductSalesLineDO updateObj = BeanUtils.toBean(updateReqVO, MesWmProductSalesLineDO.class);
        if (noticeLine != null) {
            updateObj.setSaleOrderItemId(noticeLine.getSaleOrderItemId());
        }
        productSalesLineMapper.updateById(updateObj);
        // 检查是否需要创建 OQC 检验单:从不需要检验变为需要检验
@@ -98,6 +107,8 @@
            MesWmProductSalesLineDO line = new MesWmProductSalesLineDO();
            line.setId(updateReqVO.getId());
            line.setItemId(updateReqVO.getItemId());
            line.setSaleOrderItemId(updateObj.getSaleOrderItemId() != null
                    ? updateObj.getSaleOrderItemId() : oldLine.getSaleOrderItemId());
            line.setBatchCode(updateReqVO.getBatchCode());
            line.setQuantity(updateReqVO.getQuantity());
@@ -181,15 +192,26 @@
    /**
     * 校验保存时的关联数据
     */
    private void validateLineSaveData(MesWmProductSalesLineSaveReqVO reqVO) {
    private MesWmSalesNoticeLineDO validateLineSaveData(MesWmProductSalesLineSaveReqVO reqVO) {
        // 校验主单存在且为草稿状态
        MesWmProductSalesDO sales = productSalesService.validateProductSalesExistsAndDraft(reqVO.getSalesId());
        // 校验物料存在
        mdmItemService.validateItemExistsAndEnable(reqVO.getItemId());
        // 校验关联发货通知单行(存在性 + 归属 + 字段一致性)
        validateSalesNoticeLine(sales, reqVO);
        MesWmSalesNoticeLineDO noticeLine = validateSalesNoticeLine(sales, reqVO);
        if (noticeLine == null && sales.getSaleOrderId() != null) {
            if (reqVO.getSaleOrderItemId() == null) {
                throw exception(WM_SALES_NOTICE_SALE_ORDER_ITEM_EMPTY);
            }
            var item = saleOrderApi.getSaleOrderItem(reqVO.getSaleOrderItemId()).getCheckedData();
            if (item == null || !Objects.equals(item.getOrderId(), sales.getSaleOrderId())
                    || !Objects.equals(item.getProductId(), reqVO.getItemId())) {
                throw exception(WM_SALES_NOTICE_LINE_SALE_ITEM_MISMATCH);
            }
        }
        // 根据 batchCode 解析 batchId
        fillBatchId(reqVO);
        return noticeLine;
    }
    /**
@@ -198,7 +220,7 @@
     * @param sales 出库单
     * @param reqVO 出库行请求
     */
    private void validateSalesNoticeLine(MesWmProductSalesDO sales, MesWmProductSalesLineSaveReqVO reqVO) {
    private MesWmSalesNoticeLineDO validateSalesNoticeLine(MesWmProductSalesDO sales, MesWmProductSalesLineSaveReqVO reqVO) {
        Long noticeLineId = reqVO.getNoticeLineId();
        // 情况一:如果出库单关联了发货通知单,则必须关联发货通知单行
        if (sales.getNoticeId() != null) {
@@ -220,13 +242,14 @@
                    && ObjUtil.notEqual(reqVO.getBatchCode(), noticeLine.getBatchCode())) {
                throw exception(WM_PRODUCT_SALES_LINE_NOTICE_LINE_BATCH_MISMATCH);
            }
            return;
            return noticeLine;
        }
        // 情况二:如果出库单没有关联发货通知单,则不允许关联发货通知单行
        if (noticeLineId != null) {
            throw exception(WM_PRODUCT_SALES_LINE_SALES_NOTICE_LINE_NOT_ALLOWED);
        }
        return null;
    }
    @Override