yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productsales/MesWmProductSalesLineServiceImpl.java
@@ -7,16 +7,19 @@
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.line.MesWmProductSalesLinePageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.wm.productsales.vo.line.MesWmProductSalesLineSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesLineDO;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.productsales.MesWmProductSalesLineMapper;
import cn.iocoder.yudao.module.mes.enums.qc.MesQcCheckResultEnum;
import cn.iocoder.yudao.module.mes.enums.wm.MesWmQualityStatusEnum;
import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService;
import cn.iocoder.yudao.module.mes.service.mdm.MesMdmItemService;
import cn.iocoder.yudao.module.mes.service.wm.batch.MesWmBatchService;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.batch.MesWmBatchDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.wm.salesnotice.MesWmSalesNoticeLineDO;
import cn.iocoder.yudao.module.mes.service.wm.salesnotice.MesWmSalesNoticeLineService;
import cn.iocoder.yudao.module.mes.service.qc.oqc.MesQcOqcService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
@@ -24,6 +27,8 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;
@@ -34,7 +39,7 @@
/**
 * MES 销售出库单行 Service 实现类
 *
 * @author 芋道源码
 * @author 超级管理员
 */
@Service
@Validated
@@ -49,6 +54,8 @@
    @Resource
    private MesMdmItemService mdmItemService;
    @Resource
    private MesMdItemService mdItemService;
    @Resource
    private MesWmBatchService batchService;
    @Resource
    @Lazy
@@ -56,6 +63,9 @@
    @Resource
    @Lazy
    private MesWmSalesNoticeLineService salesNoticeLineService;
    @Resource
    @Lazy
    private MesQcOqcService oqcService;
    @Override
    public Long createProductSalesLine(MesWmProductSalesLineSaveReqVO createReqVO) {
@@ -64,6 +74,7 @@
        // 新增
        MesWmProductSalesLineDO line = BeanUtils.toBean(createReqVO, MesWmProductSalesLineDO.class);
        line.setBoxCount(calculateBoxCount(line.getItemId(), line.getQuantity()));
        productSalesLineMapper.insert(line);
        return line.getId();
    }
@@ -78,7 +89,36 @@
        // 更新
        MesWmProductSalesLineDO updateObj = BeanUtils.toBean(updateReqVO, MesWmProductSalesLineDO.class);
        updateObj.setBoxCount(calculateBoxCount(updateObj.getItemId(), updateObj.getQuantity()));
        productSalesLineMapper.updateById(updateObj);
        // 检查是否需要创建 OQC 检验单:从不需要检验变为需要检验
        if (Boolean.TRUE.equals(updateReqVO.getOqcCheckFlag())
                && !Boolean.TRUE.equals(oldLine.getOqcCheckFlag())) {
            // 获取出库单信息
            MesWmProductSalesDO sales = productSalesService.getProductSales(updateReqVO.getSalesId());
            if (sales == null) {
                log.warn("[updateProductSalesLine] 出库单不存在,salesId={}", updateReqVO.getSalesId());
                return;
            }
            // 构建出库明细 DO
            MesWmProductSalesLineDO line = new MesWmProductSalesLineDO();
            line.setId(updateReqVO.getId());
            line.setItemId(updateReqVO.getItemId());
            line.setBatchCode(updateReqVO.getBatchCode());
            line.setQuantity(updateReqVO.getQuantity());
            // 创建 OQC 检验单,让异常抛出
            Long oqcId = oqcService.createOqcFromProductSales(
                    sales.getId(), line, sales.getCode(), sales.getClientId());
            // 更新出库明细的 oqcId 和质量状态
            productSalesLineMapper.updateById(new MesWmProductSalesLineDO()
                    .setId(updateReqVO.getId())
                    .setOqcId(oqcId)
                    .setQualityStatus(MesWmQualityStatusEnum.PENDING.getStatus()));
        }
    }
    @Override
@@ -135,6 +175,21 @@
    }
    /**
     * 按 数量 / 物料每箱数量 向上取整计算箱数;未维护每箱数量时返回 0
     */
    private int calculateBoxCount(Long itemId, BigDecimal quantity) {
        if (itemId == null || quantity == null) {
            return 0;
        }
        MesMdItemDO item = mdItemService.getItem(itemId);
        if (item == null || item.getBoxQuantity() == null
                || item.getBoxQuantity().compareTo(BigDecimal.ZERO) <= 0) {
            return 0;
        }
        return quantity.divide(item.getBoxQuantity(), 0, RoundingMode.CEILING).intValue();
    }
    /**
     * 根据 batchCode 解析 batchId
     */
    private void fillBatchId(MesWmProductSalesLineSaveReqVO reqVO) {
@@ -175,7 +230,8 @@
            }
            MesWmSalesNoticeLineDO noticeLine = salesNoticeLineService.validateSalesNoticeLineExists(
                    noticeLineId, sales.getNoticeId());
            // 校验关键字段一致性:物料、数量、批次号、OQC 检验标识
            // 校验关键字段一致性:物料、数量、批次号
            // 注:OQC 检验标识允许在出库时独立设置,不强制与通知单一致
            if (ObjUtil.notEqual(reqVO.getItemId(), noticeLine.getItemId())) {
                throw exception(WM_PRODUCT_SALES_LINE_NOTICE_LINE_ITEM_MISMATCH);
            }
@@ -186,10 +242,6 @@
            if (StrUtil.isNotBlank(noticeLine.getBatchCode())
                    && ObjUtil.notEqual(reqVO.getBatchCode(), noticeLine.getBatchCode())) {
                throw exception(WM_PRODUCT_SALES_LINE_NOTICE_LINE_BATCH_MISMATCH);
            }
            if (noticeLine.getOqcCheckFlag() != null
                    && ObjUtil.notEqual(reqVO.getOqcCheckFlag(), noticeLine.getOqcCheckFlag())) {
                throw exception(WM_PRODUCT_SALES_LINE_NOTICE_LINE_OQC_MISMATCH);
            }
            return;
        }