22 小时以前 7a681887e5a8e1aab49acc886974d354c69a280e
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package cn.iocoder.yudao.module.mes.service.qc.indicator;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mes.controller.admin.qc.indicator.vo.MesQcIndicatorPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.qc.indicator.vo.MesQcIndicatorSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.indicator.MesQcIndicatorDO;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.indicator.MesQcIndicatorMapper;
import cn.iocoder.yudao.module.mes.enums.qc.MesQcIndicatorItemTypeEnum;
import cn.iocoder.yudao.module.mes.enums.qc.MesQcResultValueTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 质检指标 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MesQcIndicatorServiceImpl implements MesQcIndicatorService {
 
    @Resource
    private MesQcIndicatorMapper indicatorMapper;
 
    @Override
    public Long createIndicator(MesQcIndicatorSaveReqVO createReqVO) {
        // 规范化层级字段
        normalizeHierarchyFields(createReqVO);
        // 校验相关参数
        validateIndicatorSaveData(null, createReqVO);
 
        // 插入
        MesQcIndicatorDO indicator = BeanUtils.toBean(createReqVO, MesQcIndicatorDO.class);
        indicatorMapper.insert(indicator);
        return indicator.getId();
    }
 
    @Override
    public void updateIndicator(MesQcIndicatorSaveReqVO updateReqVO) {
        // 校验存在
        validateIndicatorExists(updateReqVO.getId());
        // 规范化层级字段
        normalizeHierarchyFields(updateReqVO);
        // 校验相关参数
        validateIndicatorSaveData(updateReqVO.getId(), updateReqVO);
 
        // 更新
        MesQcIndicatorDO updateObj = BeanUtils.toBean(updateReqVO, MesQcIndicatorDO.class);
        indicatorMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteIndicator(Long id) {
        // 校验存在
        MesQcIndicatorDO indicator = validateIndicatorExists(id);
        // 校验无子项
        Long childCount = indicatorMapper.selectCount(new LambdaQueryWrapperX<MesQcIndicatorDO>()
                .eq(MesQcIndicatorDO::getParentId, id));
        if (childCount != null && childCount > 0) {
            throw exception(QC_INDICATOR_EXISTS_CHILDREN, indicator.getName());
        }
        // 删除
        indicatorMapper.deleteById(id);
    }
 
    private void normalizeHierarchyFields(MesQcIndicatorSaveReqVO saveReqVO) {
        if (saveReqVO.getParentId() == null) {
            saveReqVO.setParentId(0L);
        }
        if (saveReqVO.getItemType() == null) {
            saveReqVO.setItemType(MesQcIndicatorItemTypeEnum.RECORD.getType());
        }
        if (saveReqVO.getSortOrder() == null) {
            saveReqVO.setSortOrder(0);
        }
    }
 
    private void validateIndicatorSaveData(Long id, MesQcIndicatorSaveReqVO saveReqVO) {
        // 校验编码唯一
        validateIndicatorCodeUnique(id, saveReqVO.getCode());
        // 校验同层级名称唯一(不同分组下允许同名参数行,如"试样体积V/mL")
        validateIndicatorNameUnique(id, saveReqVO.getName(), saveReqVO.getParentId());
        // 校验层级合法性(父项存在、父项为分组项、最多两级)
        validateIndicatorHierarchy(id, saveReqVO);
        // 录入项必须指定结果值类型
        if (ObjUtil.notEqual(MesQcIndicatorItemTypeEnum.GROUP.getType(), saveReqVO.getItemType())
                && saveReqVO.getResultType() == null) {
            throw exception(QC_INDICATOR_RESULT_TYPE_REQUIRED);
        }
        // 校验结果值属性
        validateResultSpecification(saveReqVO.getResultType(), saveReqVO.getResultSpecification());
    }
 
    private void validateIndicatorHierarchy(Long id, MesQcIndicatorSaveReqVO saveReqVO) {
        Long parentId = saveReqVO.getParentId();
        if (parentId == null || parentId == 0L) {
            return;
        }
        if (ObjUtil.equal(parentId, id)) {
            throw exception(QC_INDICATOR_PARENT_NOT_GROUP, saveReqVO.getName());
        }
        MesQcIndicatorDO parent = indicatorMapper.selectById(parentId);
        if (parent == null) {
            throw exception(QC_INDICATOR_NOT_EXISTS);
        }
        if (ObjUtil.notEqual(MesQcIndicatorItemTypeEnum.GROUP.getType(), parent.getItemType())) {
            throw exception(QC_INDICATOR_PARENT_NOT_GROUP, parent.getName());
        }
        // 仅允许两级:父分组本身必须是顶级
        if (ObjUtil.defaultIfNull(parent.getParentId(), 0L) != 0L) {
            throw exception(QC_INDICATOR_PARENT_LEVEL_EXCEEDED, parent.getName());
        }
    }
 
    private MesQcIndicatorDO validateIndicatorExists(Long id) {
        MesQcIndicatorDO indicator = indicatorMapper.selectById(id);
        if (indicator == null) {
            throw exception(QC_INDICATOR_NOT_EXISTS);
        }
        return indicator;
    }
 
    private void validateIndicatorCodeUnique(Long id, String code) {
        MesQcIndicatorDO indicator = indicatorMapper.selectByCode(code);
        if (indicator == null) {
            return;
        }
        if (ObjUtil.notEqual(indicator.getId(), id)) {
            throw exception(QC_INDICATOR_CODE_DUPLICATE);
        }
    }
 
    private void validateIndicatorNameUnique(Long id, String name, Long parentId) {
        Long count = indicatorMapper.selectCount(new LambdaQueryWrapperX<MesQcIndicatorDO>()
                .eq(MesQcIndicatorDO::getName, name)
                .eq(MesQcIndicatorDO::getParentId, ObjUtil.defaultIfNull(parentId, 0L))
                .ne(id != null, MesQcIndicatorDO::getId, id));
        if (count != null && count > 0) {
            throw exception(QC_INDICATOR_NAME_DUPLICATE);
        }
    }
 
    private void validateResultSpecification(Integer resultType, String resultSpecification) {
        if (ObjectUtils.equalsAny(resultType, MesQcResultValueTypeEnum.FILE.getType(),
                MesQcResultValueTypeEnum.DICT.getType())) {
            if (StrUtil.isBlank(resultSpecification)) {
                throw exception(QC_INDICATOR_RESULT_SPECIFICATION_REQUIRED);
            }
        }
    }
 
    @Override
    public MesQcIndicatorDO getIndicator(Long id) {
        return indicatorMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesQcIndicatorDO> getIndicatorPage(MesQcIndicatorPageReqVO pageReqVO) {
        return indicatorMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<MesQcIndicatorDO> getIndicatorList() {
        return indicatorMapper.selectList();
    }
 
    @Override
    public List<MesQcIndicatorDO> getIndicatorList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return indicatorMapper.selectByIds(ids);
    }
 
    @Override
    public Map<Long, MesQcIndicatorDO> validateIndicatorListExists(Collection<Long> ids) {
        List<MesQcIndicatorDO> indicators = getIndicatorList(ids);
        if (indicators.size() != ids.size()) {
            throw exception(QC_INDICATOR_NOT_EXISTS);
        }
        return convertMap(indicators, MesQcIndicatorDO::getId);
    }
 
}