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
package cn.iocoder.yudao.module.mes.service.qc.template;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
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.template.vo.item.MesQcTemplateItemPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.qc.template.vo.item.MesQcTemplateItemSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.template.MesQcTemplateDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.template.MesQcTemplateItemDO;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.template.MesQcTemplateItemMapper;
import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService;
import cn.iocoder.yudao.module.mes.service.pro.task.MesProTaskService;
import cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.findFirst;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 质检方案-产品关联 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MesQcTemplateItemServiceImpl implements MesQcTemplateItemService {
 
    @Resource
    private MesQcTemplateItemMapper templateItemMapper;
 
    @Resource
    @Lazy
    private MesQcTemplateService templateService;
    @Resource
    private MesMdItemService mesMdItemService;
    @Resource
    @Lazy
    private MesProTaskService proTaskService;
    @Resource
    @Lazy
    private MesProWorkOrderService proWorkOrderService;
 
    @Override
    public Long createTemplateItem(MesQcTemplateItemSaveReqVO createReqVO) {
        // 校验保存参数
        validateTemplateItemSaveData(createReqVO);
 
        // 插入
        MesQcTemplateItemDO item = BeanUtils.toBean(createReqVO, MesQcTemplateItemDO.class);
        templateItemMapper.insert(item);
        return item.getId();
    }
 
    @Override
    public void updateTemplateItem(MesQcTemplateItemSaveReqVO updateReqVO) {
        // 校验保存参数
        validateTemplateItemSaveData(updateReqVO);
 
        // 更新
        MesQcTemplateItemDO updateObj = BeanUtils.toBean(updateReqVO, MesQcTemplateItemDO.class);
        templateItemMapper.updateById(updateObj);
    }
 
    private void validateTemplateItemSaveData(MesQcTemplateItemSaveReqVO saveReqVO) {
        if (saveReqVO.getId() != null) {
            // 校验存在
            validateTemplateItemExists(saveReqVO.getId());
        }
        // 校验方案存在
        templateService.validateTemplateExists(saveReqVO.getTemplateId());
        // 校验产品在此方案中唯一
        validateTemplateItemNotDuplicate(saveReqVO.getId(), saveReqVO.getTemplateId(), saveReqVO.getItemId());
    }
 
    @Override
    public void deleteTemplateItem(Long id) {
        // 校验存在
        validateTemplateItemExists(id);
        // 删除
        templateItemMapper.deleteById(id);
    }
 
    private void validateTemplateItemExists(Long id) {
        if (templateItemMapper.selectById(id) == null) {
            throw exception(QC_TEMPLATE_ITEM_NOT_EXISTS);
        }
    }
 
    private void validateTemplateItemNotDuplicate(Long id, Long templateId, Long itemId) {
        MesQcTemplateItemDO existing = templateItemMapper.selectByTemplateIdAndItemId(templateId, itemId);
        if (existing == null) {
            return;
        }
        if (ObjUtil.notEqual(existing.getId(), id)) {
            throw exception(QC_TEMPLATE_ITEM_DUPLICATE);
        }
    }
 
    @Override
    public MesQcTemplateItemDO getTemplateItem(Long id) {
        return templateItemMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesQcTemplateItemDO> getTemplateItemPage(MesQcTemplateItemPageReqVO pageReqVO) {
        return templateItemMapper.selectPage(pageReqVO);
    }
 
    @Override
    public void deleteTemplateItemByTemplateId(Long templateId) {
        templateItemMapper.deleteByTemplateId(templateId);
    }
 
    @Override
    public MesQcTemplateItemDO getRequiredTemplateByItemIdAndType(Long itemId, Integer qcType) {
        List<MesQcTemplateItemDO> templateItems = getTemplateItemList(itemId, qcType, null, null);
        if (CollUtil.isEmpty(templateItems)) {
            throw exception(QC_NO_TEMPLATE);
        }
        return templateItems.get(0);
    }
 
    @Override
    public List<MesQcTemplateItemDO> getTemplateItemList(Long itemId, Integer qcType, Long workOrderId, Long taskId) {
        // 1. 解析真实产品物料 ID:任务优先,其次工单,最后直接用入参
        Long resolvedItemId = itemId;
        if (taskId != null) {
            resolvedItemId = proTaskService.validateTaskExists(taskId).getItemId();
        } else if (workOrderId != null) {
            resolvedItemId = proWorkOrderService.validateWorkOrderExists(workOrderId).getProductId();
        }
        if (resolvedItemId == null) {
            return Collections.emptyList();
        }
        // 2. mes_md_item.id 转 mdm_item.id(产品关联表存的是 mdm 物料)
        MesMdItemDO item = mesMdItemService.getItem(resolvedItemId);
        if (item == null || item.getMdmItemId() == null) {
            return Collections.emptyList();
        }
        // 3. 查出该物料关联的所有产品关联
        List<MesQcTemplateItemDO> templateItems = templateItemMapper.selectListByItemId(item.getMdmItemId());
        if (CollUtil.isEmpty(templateItems)) {
            return Collections.emptyList();
        }
        // 4. 过滤:方案启用且检测种类包含 qcType,返回全部匹配项
        List<MesQcTemplateDO> templates = templateService.getTemplateList(
                convertSet(templateItems, MesQcTemplateItemDO::getTemplateId));
        Set<Long> matchedTemplateIds = templates.stream()
                .filter(t -> CommonStatusEnum.isEnable(t.getStatus())
                        && CollUtil.contains(t.getTypes(), qcType))
                .map(MesQcTemplateDO::getId)
                .collect(Collectors.toSet());
        return templateItems.stream()
                .filter(t -> matchedTemplateIds.contains(t.getTemplateId()))
                .collect(Collectors.toList());
    }
 
}