3 天以前 111270df037596a04df97f787ca8b9199dd99866
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package cn.iocoder.yudao.module.mes.service.qc.defectrecord;
 
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.qc.defectrecord.vo.MesQcDefectRecordPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.qc.defectrecord.vo.MesQcDefectRecordSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.defectrecord.MesQcDefectRecordDO;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.defectrecord.MesQcDefectRecordMapper;
import cn.iocoder.yudao.module.mes.enums.qc.MesQcTypeEnum;
import cn.iocoder.yudao.module.mes.service.qc.ipqc.MesQcIpqcLineService;
import cn.iocoder.yudao.module.mes.service.qc.ipqc.MesQcIpqcService;
import cn.iocoder.yudao.module.mes.service.qc.iqc.MesQcIqcLineService;
import cn.iocoder.yudao.module.mes.service.qc.iqc.MesQcIqcService;
import cn.iocoder.yudao.module.mes.service.qc.oqc.MesQcOqcLineService;
import cn.iocoder.yudao.module.mes.service.qc.oqc.MesQcOqcService;
import cn.iocoder.yudao.module.mes.service.qc.rqc.MesQcRqcLineService;
import cn.iocoder.yudao.module.mes.service.qc.rqc.MesQcRqcService;
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;
 
import java.util.List;
import java.util.Objects;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 质检缺陷记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MesQcDefectRecordServiceImpl implements MesQcDefectRecordService {
 
    @Resource
    private MesQcDefectRecordMapper defectRecordMapper;
 
    @Resource
    @Lazy
    private MesQcIqcService iqcService;
    @Resource
    @Lazy
    private MesQcIqcLineService iqcLineService;
    @Resource
    @Lazy
    private MesQcIpqcService ipqcService;
    @Resource
    @Lazy
    private MesQcIpqcLineService ipqcLineService;
    @Resource
    @Lazy
    private MesQcOqcService oqcService;
    @Resource
    @Lazy
    private MesQcOqcLineService oqcLineService;
    @Resource
    @Lazy
    private MesQcRqcService rqcService;
    @Resource
    @Lazy
    private MesQcRqcLineService rqcLineService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createDefectRecord(MesQcDefectRecordSaveReqVO createReqVO) {
        // 1. 校验检验单和检验行存在
        validateQcAndLineExists(createReqVO.getQcType(), createReqVO.getQcId(), createReqVO.getLineId());
 
        // 2. 插入
        MesQcDefectRecordDO record = BeanUtils.toBean(createReqVO, MesQcDefectRecordDO.class);
        defectRecordMapper.insert(record);
 
        // 3. 重新计算缺陷统计
        recalculateDefectStats(createReqVO.getQcType(), createReqVO.getQcId());
        return record.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDefectRecord(MesQcDefectRecordSaveReqVO updateReqVO) {
        // 1. 校验存在
        validateDefectRecordExists(updateReqVO.getId());
 
        // 2. 更新
        MesQcDefectRecordDO updateObj = BeanUtils.toBean(updateReqVO, MesQcDefectRecordDO.class);
        defectRecordMapper.updateById(updateObj);
 
        // 3. 重新计算缺陷统计
        recalculateDefectStats(updateReqVO.getQcType(), updateReqVO.getQcId());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDefectRecord(Long id) {
        // 1. 校验存在
        MesQcDefectRecordDO record = validateDefectRecordExists(id);
 
        // 2. 删除
        defectRecordMapper.deleteById(id);
 
        // 3. 重新计算缺陷统计
        recalculateDefectStats(record.getQcType(), record.getQcId());
    }
 
    @Override
    public MesQcDefectRecordDO getDefectRecord(Long id) {
        return defectRecordMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesQcDefectRecordDO> getDefectRecordPage(MesQcDefectRecordPageReqVO pageReqVO) {
        return defectRecordMapper.selectPage(pageReqVO);
    }
 
    @Override
    public void deleteListByQcTypeAndQcId(Integer qcType, Long qcId) {
        defectRecordMapper.deleteByQcTypeAndQcId(qcType, qcId);
    }
 
    // ==================== 校验方法 ====================
 
    private void validateQcAndLineExists(Integer qcType, Long qcId, Long lineId) {
        if (Objects.equals(qcType, MesQcTypeEnum.IQC.getType())) {
            iqcService.validateIqcExists(qcId);
            iqcLineService.validateIqcLineExists(lineId);
        } else if (Objects.equals(qcType, MesQcTypeEnum.IPQC.getType())) {
            ipqcService.validateIpqcExists(qcId);
            ipqcLineService.validateIpqcLineExists(lineId);
        } else if (Objects.equals(qcType, MesQcTypeEnum.OQC.getType())) {
            oqcService.validateOqcExists(qcId);
            oqcLineService.validateOqcLineExists(lineId);
        } else if (Objects.equals(qcType, MesQcTypeEnum.RQC.getType())) {
            rqcService.validateRqcExists(qcId);
            rqcLineService.validateRqcLineExists(lineId);
        } else {
            throw exception(QC_DEFECT_RECORD_QC_TYPE_UNSUPPORTED);
        }
    }
 
    private MesQcDefectRecordDO validateDefectRecordExists(Long id) {
        MesQcDefectRecordDO record = defectRecordMapper.selectById(id);
        if (record == null) {
            throw exception(QC_DEFECT_RECORD_NOT_EXISTS);
        }
        return record;
    }
 
    // ==================== 缺陷统计 ====================
 
    /**
     * 重新计算缺陷统计(行级 + 主表级),委托给对应的检验 Service
     *
     * @param qcType 检验类型
     * @param qcId   检验单 ID
     */
    private void recalculateDefectStats(Integer qcType, Long qcId) {
        List<MesQcDefectRecordDO> records = defectRecordMapper.selectListByQcTypeAndQcId(qcType, qcId);
        if (Objects.equals(qcType, MesQcTypeEnum.IQC.getType())) {
            iqcService.recalculateDefectStats(qcId, records);
        } else if (Objects.equals(qcType, MesQcTypeEnum.IPQC.getType())) {
            ipqcService.recalculateDefectStats(qcId, records);
        } else if (Objects.equals(qcType, MesQcTypeEnum.OQC.getType())) {
            oqcService.recalculateDefectStats(qcId, records);
        } else if (Objects.equals(qcType, MesQcTypeEnum.RQC.getType())) {
            rqcService.recalculateDefectStats(qcId, records);
        } else {
            throw exception(QC_DEFECT_RECORD_QC_TYPE_UNSUPPORTED);
        }
    }
 
}