10 天以前 d5cd948848595b0cffe098ba542a9c879b0f9165
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
package cn.iocoder.yudao.module.mes.service.qc.oqc;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.mes.controller.admin.qc.oqc.vo.line.MesQcOqcLinePageReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.defectrecord.MesQcDefectRecordDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.indicator.MesQcIndicatorDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.oqc.MesQcOqcLineDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.template.MesQcTemplateIndicatorDO;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.oqc.MesQcOqcLineMapper;
import cn.iocoder.yudao.module.mes.enums.qc.MesQcDefectLevelEnum;
import cn.iocoder.yudao.module.mes.service.qc.indicator.MesQcIndicatorService;
import cn.iocoder.yudao.module.mes.service.qc.template.MesQcTemplateIndicatorService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 出货检验单行 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MesQcOqcLineServiceImpl implements MesQcOqcLineService {
 
    @Resource
    private MesQcOqcLineMapper oqcLineMapper;
 
    @Resource
    private MesQcIndicatorService indicatorService;
    @Resource
    private MesQcTemplateIndicatorService templateIndicatorService;
 
    @Override
    public MesQcOqcLineDO validateOqcLineExists(Long id) {
        MesQcOqcLineDO line = oqcLineMapper.selectById(id);
        if (line == null) {
            throw exception(QC_OQC_LINE_NOT_EXISTS);
        }
        return line;
    }
 
    @Override
    public MesQcOqcLineDO getOqcLine(Long id) {
        return oqcLineMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesQcOqcLineDO> getOqcLinePage(MesQcOqcLinePageReqVO pageReqVO) {
        return oqcLineMapper.selectPage(pageReqVO);
    }
 
    @Override
    public void createLinesFromTemplate(Long oqcId, Long templateId) {
        List<MesQcTemplateIndicatorDO> templateIndicators = templateIndicatorService.getTemplateIndicatorListByTemplateId(templateId);
        if (CollUtil.isEmpty(templateIndicators)) {
            return;
        }
        Map<Long, MesQcIndicatorDO> indicatorMap = indicatorService.getIndicatorMap(
                convertSet(templateIndicators, MesQcTemplateIndicatorDO::getIndicatorId));
        List<MesQcOqcLineDO> lines = convertList(templateIndicators, templateIndicator -> {
            MesQcIndicatorDO indicator = indicatorMap.get(templateIndicator.getIndicatorId());
            return new MesQcOqcLineDO()
                    .setOqcId(oqcId).setIndicatorId(templateIndicator.getIndicatorId())
                    .setTool(indicator != null ? indicator.getTool() : null)
                    .setCheckMethod(templateIndicator.getCheckMethod())
                    .setStandardValue(templateIndicator.getStandardValue()).setUnitMeasureId(templateIndicator.getUnitMeasureId())
                    .setMaxThreshold(templateIndicator.getThresholdMax()).setMinThreshold(templateIndicator.getThresholdMin())
                    .setCriticalQuantity(0).setMajorQuantity(0).setMinorQuantity(0);
        });
        oqcLineMapper.insertBatch(lines);
    }
 
    @Override
    public void recalculateLineDefectStats(Long oqcId, List<MesQcDefectRecordDO> records) {
        List<MesQcOqcLineDO> lines = oqcLineMapper.selectListByOqcId(oqcId);
        if (CollUtil.isEmpty(lines)) {
            return;
        }
        List<MesQcOqcLineDO> updateLines = new ArrayList<>(lines.size());
        for (MesQcOqcLineDO line : lines) {
            int critical = 0, major = 0, minor = 0;
            for (MesQcDefectRecordDO record : records) {
                if (ObjUtil.notEqual(record.getLineId(), line.getId())) {
                    continue;
                }
                int quantity = ObjUtil.defaultIfNull(record.getQuantity(), 1);
                if (Objects.equals(record.getLevel(), MesQcDefectLevelEnum.CRITICAL.getType())) {
                    critical += quantity;
                } else if (Objects.equals(record.getLevel(), MesQcDefectLevelEnum.MAJOR.getType())) {
                    major += quantity;
                } else if (Objects.equals(record.getLevel(), MesQcDefectLevelEnum.MINOR.getType())) {
                    minor += quantity;
                } else {
                    throw exception(QC_DEFECT_RECORD_LEVEL_UNKNOWN);
                }
            }
            updateLines.add(new MesQcOqcLineDO().setId(line.getId())
                    .setCriticalQuantity(critical).setMajorQuantity(major).setMinorQuantity(minor));
        }
        for (MesQcOqcLineDO updateLine : updateLines) {
            oqcLineMapper.updateById(updateLine);
        }
    }
 
    @Override
    public List<MesQcOqcLineDO> getOqcLineListByOqcId(Long oqcId) {
        return oqcLineMapper.selectListByOqcId(oqcId);
    }
 
    @Override
    public void deleteByOqcId(Long oqcId) {
        oqcLineMapper.deleteByOqcId(oqcId);
    }
 
    @Override
    public Long getOqcLineCountByUnitMeasureId(Long unitMeasureId) {
        return oqcLineMapper.selectCountByUnitMeasureId(unitMeasureId);
    }
 
}