2 天以前 3be684af887afe4c0e45f281cc53adfef6f12b78
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package cn.iocoder.yudao.module.mes.service.md.item;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mdm.api.category.MdmItemCategoryApi;
import cn.iocoder.yudao.module.mdm.api.category.dto.MdmItemCategoryRespDTO;
import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeListReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.md.item.vo.type.MesMdItemTypeSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemTypeDO;
import cn.iocoder.yudao.module.mes.dal.mysql.md.item.MesMdItemTypeMapper;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.*;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 物料产品分类 Service 实现类
 *
 * 兼容层实现:数据存储在 mes_md_item_type 表,但优先从 MDM 分类 API 获取
 * 实际分类数据已迁移到 mdm_item_category 表
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MesMdItemTypeServiceImpl implements MesMdItemTypeService {
 
    @Resource
    private MdmItemCategoryApi mdmItemCategoryApi;
 
    @Resource
    private MesMdItemTypeMapper itemTypeMapper;
 
    @Resource
    @Lazy
    private MesMdItemService itemService;
 
    @Override
    public Long createItemType(MesMdItemTypeSaveReqVO createReqVO) {
        // 校验数据
        validateItemTypeSaveData(createReqVO);
 
        // 插入到本地表(保持兼容)
        MesMdItemTypeDO itemType = BeanUtils.toBean(createReqVO, MesMdItemTypeDO.class);
        itemTypeMapper.insert(itemType);
        return itemType.getId();
    }
 
    @Override
    public void updateItemType(MesMdItemTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateItemTypeExists(updateReqVO.getId());
        // 校验数据
        validateItemTypeSaveData(updateReqVO);
 
        // 更新本地表
        MesMdItemTypeDO updateObj = BeanUtils.toBean(updateReqVO, MesMdItemTypeDO.class);
        itemTypeMapper.updateById(updateObj);
    }
 
    private void validateItemTypeSaveData(MesMdItemTypeSaveReqVO reqVO) {
        // 校验父分类编号的有效性
        validateParentItemType(reqVO.getId(), reqVO.getParentId());
        // 校验分类名称的唯一性
        validateItemTypeNameUnique(reqVO.getId(), reqVO.getParentId(), reqVO.getName());
        // 校验分类编码的唯一性
        validateItemTypeCodeUnique(reqVO.getId(), reqVO.getParentId(), reqVO.getCode());
    }
 
    @Override
    public void deleteItemType(Long id) {
        // 1.1 校验存在
        validateItemTypeExists(id);
        // 1.2 校验是否有子分类
        if (itemTypeMapper.selectCountByParentId(id) > 0) {
            throw exception(MD_ITEM_TYPE_EXITS_CHILDREN);
        }
        // 1.3 校验是否有物料
        if (itemService.getItemCountByItemTypeId(id) > 0) {
            throw exception(MD_ITEM_TYPE_EXITS_ITEM);
        }
 
        // 2. 删除
        itemTypeMapper.deleteById(id);
    }
 
    private void validateItemTypeExists(Long id) {
        // 优先从 MDM 获取
        CommonResult<MdmItemCategoryRespDTO> result = mdmItemCategoryApi.getItemCategory(id);
        if (result.isSuccess() && result.getData() != null) {
            return;
        }
        // 回退到本地表
        if (itemTypeMapper.selectById(id) == null) {
            throw exception(MD_ITEM_TYPE_NOT_EXISTS);
        }
    }
 
    private void validateParentItemType(Long id, Long parentId) {
        if (parentId == null || MesMdItemTypeDO.PARENT_ID_ROOT.equals(parentId)) {
            return;
        }
        // 1. 不能设置自己为父分类
        if (Objects.equals(id, parentId)) {
            throw exception(MD_ITEM_TYPE_PARENT_ERROR);
        }
        // 2. 父分类不存在 - 优先从 MDM 获取
        CommonResult<MdmItemCategoryRespDTO> result = mdmItemCategoryApi.getItemCategory(parentId);
        MesMdItemTypeDO parentItemType = null;
        if (result.isSuccess() && result.getData() != null) {
            parentItemType = convertToMesDO(result.getData());
        } else {
            parentItemType = itemTypeMapper.selectById(parentId);
        }
        if (parentItemType == null) {
            throw exception(MD_ITEM_TYPE_PARENT_NOT_EXITS);
        }
        // 3. 递归校验父分类,如果父分类是自己的子分类,则报错,避免形成环路
        if (id == null) {
            return;
        }
        for (int i = 0; i < Short.MAX_VALUE; i++) {
            parentId = parentItemType.getParentId();
            if (Objects.equals(id, parentId)) {
                throw exception(MD_ITEM_TYPE_PARENT_IS_CHILD);
            }
            if (parentId == null || MesMdItemTypeDO.PARENT_ID_ROOT.equals(parentId)) {
                break;
            }
            // 继续查找父级
            CommonResult<MdmItemCategoryRespDTO> parentResult = mdmItemCategoryApi.getItemCategory(parentId);
            if (parentResult.isSuccess() && parentResult.getData() != null) {
                parentItemType = convertToMesDO(parentResult.getData());
            } else {
                parentItemType = itemTypeMapper.selectById(parentId);
            }
            if (parentItemType == null) {
                break;
            }
        }
    }
 
    private void validateItemTypeNameUnique(Long id, Long parentId, String name) {
        MesMdItemTypeDO itemType = itemTypeMapper.selectByParentIdAndName(parentId, name);
        if (itemType == null) {
            return;
        }
        if (ObjUtil.notEqual(itemType.getId(), id)) {
            throw exception(MD_ITEM_TYPE_NAME_DUPLICATE);
        }
    }
 
    private void validateItemTypeCodeUnique(Long id, Long parentId, String code) {
        MesMdItemTypeDO itemType = itemTypeMapper.selectByParentIdAndCode(parentId, code);
        if (itemType == null) {
            return;
        }
        if (ObjUtil.notEqual(itemType.getId(), id)) {
            throw exception(MD_ITEM_TYPE_CODE_DUPLICATE);
        }
    }
 
    @Override
    public MesMdItemTypeDO getItemType(Long id) {
        // 优先从 MDM 获取
        CommonResult<MdmItemCategoryRespDTO> result = mdmItemCategoryApi.getItemCategory(id);
        if (result.isSuccess() && result.getData() != null) {
            return convertToMesDO(result.getData());
        }
        // 回退到本地表
        return itemTypeMapper.selectById(id);
    }
 
    @Override
    public MesMdItemTypeDO getItemTypeByCode(String code) {
        // 从本地表查
        return itemTypeMapper.selectByCode(code);
    }
 
    @Override
    public List<MesMdItemTypeDO> getItemTypeList(MesMdItemTypeListReqVO listReqVO) {
        // 从本地表查
        return itemTypeMapper.selectList(listReqVO);
    }
 
    @Override
    public List<MesMdItemTypeDO> getItemTypeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return itemTypeMapper.selectByIds(ids);
    }
 
    @Override
    public List<MesMdItemTypeDO> getItemTypeChildrenList(Long parentId) {
        List<MesMdItemTypeDO> allList = itemTypeMapper.selectList();
        List<MesMdItemTypeDO> result = new ArrayList<>();
        collectItemTypeChildren(result, parentId, allList);
        return result;
    }
 
    private void collectItemTypeChildren(List<MesMdItemTypeDO> result, Long parentId, List<MesMdItemTypeDO> allList) {
        for (MesMdItemTypeDO itemType : allList) {
            if (Objects.equals(itemType.getParentId(), parentId)) {
                result.add(itemType);
                collectItemTypeChildren(result, itemType.getId(), allList);
            }
        }
    }
 
    /**
     * 转换 MDM 分类 DTO 为 MES DO
     */
    private MesMdItemTypeDO convertToMesDO(MdmItemCategoryRespDTO dto) {
        MesMdItemTypeDO mesDO = new MesMdItemTypeDO();
        mesDO.setId(dto.getId());
        mesDO.setCode(dto.getCode());
        mesDO.setName(dto.getName());
        mesDO.setParentId(dto.getParentId());
        mesDO.setItemOrProduct(dto.getItemOrProduct());
        mesDO.setSort(dto.getSort());
        mesDO.setStatus(dto.getStatus());
        return mesDO;
    }
 
}