云
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cn.iocoder.yudao.module.mes.service.qc.ncr;
 
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.ncr.MesQcNcrDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.ncr.MesQcPurchaseReturnDO;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.ncr.MesQcPurchaseReturnMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.ncr.MesQcPurchaseConcessionMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Objects;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
 
@Service
public class MesQcPurchaseReturnService {
    @Resource private MesQcNcrPurchaseSourceValidator sourceValidator;
    @Resource private MesQcPurchaseReturnMapper mapper;
    @Resource private MesQcPurchaseConcessionMapper concessionMapper;
    @Resource private MesQcArrivalDispositionService arrivalDispositionService;
 
    /** 必须由已锁定 NCR 的处置事务调用。 */
    @Transactional(rollbackFor = Exception.class)
    public MesQcPurchaseReturnDO createForNcr(MesQcNcrDO ncr) {
        if (mapper.selectByNcrId(ncr.getId()) != null)
            throw new ServiceException(400, "该不合格单已生成采购退货单");
        var source = sourceValidator.validate(ncr);
        if (concessionMapper.selectByIqcId(ncr.getQcId()) != null || mapper.selectByIqcId(ncr.getQcId()) != null)
            throw new ServiceException(400, "该来料检验已有采购处置单,不能重复处置");
        if (source.notice().getVendorId() == null || source.notice().getPurchaseOrderId() == null
                || source.line().getPurchaseOrderItemId() == null)
            throw new ServiceException(400, "到货单缺少供应商、采购订单或采购明细,无法生成可追溯退货单");
        MesQcPurchaseReturnDO doc = new MesQcPurchaseReturnDO();
        doc.setCode("NCR-RET-" + ncr.getId());
        doc.setNcrId(ncr.getId());
        doc.setIqcId(ncr.getQcId());
        doc.setNoticeId(source.notice().getId());
        doc.setNoticeLineId(source.line().getId());
        doc.setPurchaseOrderId(source.notice().getPurchaseOrderId());
        doc.setPurchaseOrderItemId(source.line().getPurchaseOrderItemId());
        doc.setVendorId(source.notice().getVendorId());
        doc.setItemId(ncr.getItemId());
        doc.setVendorBatch(source.iqc().getVendorBatch());
        doc.setQuantity(ncr.getUnqualifiedQuantity());
        doc.setStatus(0);
        mapper.insert(doc);
        return doc;
    }
 
    public MesQcPurchaseReturnDO getByNcrId(Long ncrId) { return mapper.selectByNcrId(ncrId); }
 
    public PageResult<MesQcPurchaseReturnDO> getPage(PageParam page, Integer status) {
        if (status != null && status != 0 && status != 1) throw new ServiceException(400, "采购退货状态无效");
        return mapper.selectPage(page, status);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void confirm(Long id, String remark) {
        MesQcPurchaseReturnDO doc = mapper.selectByIdForUpdate(id);
        if (doc == null) throw new ServiceException(400, "采购退货单不存在");
        if (!Objects.equals(doc.getStatus(), 0)) throw new ServiceException(400, "采购退货单已确认退货");
        mapper.updateById(new MesQcPurchaseReturnDO().setId(id).setStatus(1)
                .setConfirmedBy(getLoginUserId()).setConfirmedAt(LocalDateTime.now()).setConfirmationRemark(remark));
        arrivalDispositionService.refresh(doc.getNoticeId());
    }
 
    public void validateConfirmed(Long ncrId) {
        MesQcPurchaseReturnDO doc = mapper.selectByNcrId(ncrId);
        if (doc == null || !Objects.equals(doc.getStatus(), 1))
            throw new ServiceException(400, "请先由采购仓库确认采购退货完成,再关闭不合格单");
    }
}