liyong
5 天以前 9b386ddaa6c8892d346d1e09b8bea20cb9000f58
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
package cn.iocoder.yudao.module.mes.service.qc.iqc;
 
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.iqc.vo.line.MesQcIqcLinePageReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.defectrecord.MesQcDefectRecordDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.iqc.MesQcIqcLineDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.indicator.MesQcIndicatorDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.template.MesQcTemplateIndicatorDO;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.iqc.MesQcIqcLineMapper;
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 MesQcIqcLineServiceImpl implements MesQcIqcLineService {
 
    @Resource
    private MesQcIqcLineMapper iqcLineMapper;
 
    @Resource
    private MesQcIndicatorService indicatorService;
    @Resource
    private MesQcTemplateIndicatorService templateIndicatorService;
 
    @Override
    public MesQcIqcLineDO validateIqcLineExists(Long id) {
        MesQcIqcLineDO line = iqcLineMapper.selectById(id);
        if (line == null) {
            throw exception(QC_IQC_LINE_NOT_EXISTS);
        }
        return line;
    }
 
    @Override
    public MesQcIqcLineDO getIqcLine(Long id) {
        return iqcLineMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesQcIqcLineDO> getIqcLinePage(MesQcIqcLinePageReqVO pageReqVO) {
        return iqcLineMapper.selectPage(pageReqVO);
    }
 
    @Override
    public void createLinesFromTemplate(Long iqcId, Long templateId) {
        List<MesQcTemplateIndicatorDO> templateIndicators = templateIndicatorService.getTemplateIndicatorListByTemplateId(templateId);
        if (CollUtil.isEmpty(templateIndicators)) {
            return;
        }
        Map<Long, MesQcIndicatorDO> indicatorMap = indicatorService.getIndicatorMap(
                convertSet(templateIndicators, MesQcTemplateIndicatorDO::getIndicatorId));
        List<MesQcIqcLineDO> lines = convertList(templateIndicators, templateIndicator -> {
            MesQcIndicatorDO indicator = indicatorMap.get(templateIndicator.getIndicatorId());
            return new MesQcIqcLineDO()
                    .setIqcId(iqcId).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);
        });
        iqcLineMapper.insertBatch(lines);
    }
 
    @Override
    public void recalculateLineDefectStats(Long iqcId, List<MesQcDefectRecordDO> records) {
        List<MesQcIqcLineDO> lines = iqcLineMapper.selectListByIqcId(iqcId);
        if (CollUtil.isEmpty(lines)) {
            return;
        }
        List<MesQcIqcLineDO> updateLines = new ArrayList<>(lines.size());
        for (MesQcIqcLineDO line : lines) {
            int critical = 0;
            int major = 0;
            int 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 MesQcIqcLineDO().setId(line.getId())
                    .setCriticalQuantity(critical).setMajorQuantity(major).setMinorQuantity(minor));
        }
        for (MesQcIqcLineDO updateLine : updateLines) {
            iqcLineMapper.updateById(updateLine);
        }
    }
 
    @Override
    public List<MesQcIqcLineDO> getIqcLineListByIqcId(Long iqcId) {
        return iqcLineMapper.selectListByIqcId(iqcId);
    }
 
    @Override
    public void deleteListByIqcId(Long iqcId) {
        iqcLineMapper.deleteByIqcId(iqcId);
    }
 
    @Override
    public Long getIqcLineCountByUnitMeasureId(Long unitMeasureId) {
        return iqcLineMapper.selectCountByUnitMeasureId(unitMeasureId);
    }
 
}