云
4 天以前 f02d0df2f1dfc7a8b775cf50d45375e57628a5ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cn.iocoder.yudao.module.mes.service.qc.ncr;
 
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.ncr.MesQcNcrDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.oqc.MesQcOqcDO;
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.qc.oqc.MesQcOqcMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.productsales.MesWmProductSalesLineMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.wm.productsales.MesWmProductSalesMapper;
import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants;
import cn.iocoder.yudao.module.mes.enums.wm.MesWmProductSalesStatusEnum;
import cn.iocoder.yudao.module.mes.enums.wm.MesWmQualityStatusEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import java.util.Objects;
 
/** 调用方须持有 NCR 行锁并在同一事务内完成后续处置。 */
@Component
public class MesQcNcrOqcSourceClaim {
    @Resource private MesQcOqcMapper oqcMapper;
    @Resource private MesWmProductSalesMapper salesMapper;
    @Resource private MesWmProductSalesLineMapper lineMapper;
 
    public OqcClaimedSource claim(MesQcNcrDO ncr) {
        var qc = oqcMapper.selectById(ncr.getQcId());
        if (qc == null || !Objects.equals(qc.getSourceDocType(), MesBizTypeConstants.WM_PRODUCT_SALES)
                || qc.getSourceDocId() == null || qc.getSourceLineId() == null
                || !Objects.equals(qc.getItemId(), ncr.getItemId()) || qc.getUnqualifiedQuantity() == null
                || ncr.getUnqualifiedQuantity() == null || qc.getUnqualifiedQuantity().compareTo(ncr.getUnqualifiedQuantity()) != 0) {
            throw new ServiceException(400, "出货不合格单来源或数量不一致");
        }
        var sales = salesMapper.selectOne(new LambdaQueryWrapperX<MesWmProductSalesDO>()
                .eq(MesWmProductSalesDO::getId, qc.getSourceDocId()).last("FOR UPDATE"));
        var line = lineMapper.selectOne(new LambdaQueryWrapperX<MesWmProductSalesLineDO>()
                .eq(MesWmProductSalesLineDO::getId, qc.getSourceLineId()).last("FOR UPDATE"));
        if (sales == null || line == null || !Objects.equals(sales.getStatus(), MesWmProductSalesStatusEnum.CONFIRMED.getStatus())
                || !Objects.equals(line.getSalesId(), sales.getId()) || !Objects.equals(line.getOqcId(), qc.getId())
                || !Objects.equals(line.getQualityStatus(), MesWmQualityStatusEnum.FAIL.getStatus())
                || line.getNcrDispositionId() != null) {
            throw new ServiceException(400, "原出货行已处置或不处于等待检验处置状态");
        }
        lineMapper.updateById(new MesWmProductSalesLineDO().setId(line.getId()).setNcrDispositionId(ncr.getId()));
        return new OqcClaimedSource(qc, sales, line);
    }
 
    /** 已锁定的出货不合格来源(检验单、销售出库单、销售出库行)。 */
    public record OqcClaimedSource(MesQcOqcDO qc, MesWmProductSalesDO sales, MesWmProductSalesLineDO line) {
    }
}