liyong
6 天以前 536774eea8902efc088c5b904ff75bbe0af33418
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
package cn.iocoder.yudao.module.mdm.service.category;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mdm.controller.admin.category.vo.MdmItemCategoryPageReqVO;
import cn.iocoder.yudao.module.mdm.controller.admin.category.vo.MdmItemCategorySaveReqVO;
import cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO;
import cn.iocoder.yudao.module.mdm.dal.mysql.category.MdmItemCategoryMapper;
import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper;
import jakarta.annotation.Resource;
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.mdm.enums.ErrorCodeConstants.*;
 
/**
 * MDM 物料分类 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MdmItemCategoryServiceImpl implements MdmItemCategoryService {
 
    @Resource
    private MdmItemCategoryMapper itemCategoryMapper;
 
    @Resource
    private MdmItemMapper itemMapper;
 
    @Override
    public Long createItemCategory(MdmItemCategorySaveReqVO createReqVO) {
        // 校验数据
        validateItemCategorySaveData(createReqVO);
 
        // 插入
        MdmItemCategoryDO itemCategory = BeanUtils.toBean(createReqVO, MdmItemCategoryDO.class);
        itemCategoryMapper.insert(itemCategory);
        return itemCategory.getId();
    }
 
    @Override
    public void updateItemCategory(MdmItemCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateItemCategoryExists(updateReqVO.getId());
        // 校验数据
        validateItemCategorySaveData(updateReqVO);
 
        // 更新
        MdmItemCategoryDO updateObj = BeanUtils.toBean(updateReqVO, MdmItemCategoryDO.class);
        itemCategoryMapper.updateById(updateObj);
    }
 
    @Override
    public void updateItemCategoryStatus(Long id, Integer status) {
        // 校验存在
        validateItemCategoryExists(id);
        // 更新状态
        itemCategoryMapper.updateById(new MdmItemCategoryDO().setId(id).setStatus(status));
    }
 
    @Override
    public void deleteItemCategory(Long id) {
        // 校验存在
        validateItemCategoryExists(id);
        // 校验是否有子分类
        if (itemCategoryMapper.selectCountByParentId(id) > 0) {
            throw exception(MDM_ITEM_CATEGORY_EXISTS_CHILDREN);
        }
        // 校验分类下是否有物料(直接使用 Mapper 避免循环依赖)
        if (itemMapper.selectCountByCategoryId(id) > 0) {
            throw exception(MDM_ITEM_CATEGORY_EXISTS_ITEM);
        }
        // 删除
        itemCategoryMapper.deleteById(id);
    }
 
    @Override
    public MdmItemCategoryDO validateItemCategoryExists(Long id) {
        MdmItemCategoryDO itemCategory = itemCategoryMapper.selectById(id);
        if (itemCategory == null) {
            throw exception(MDM_ITEM_CATEGORY_NOT_EXISTS);
        }
        return itemCategory;
    }
 
    private void validateItemCategorySaveData(MdmItemCategorySaveReqVO reqVO) {
        // 校验编码唯一
        MdmItemCategoryDO category = itemCategoryMapper.selectByCode(reqVO.getCode());
        if (category != null && ObjUtil.notEqual(category.getId(), reqVO.getId())) {
            throw exception(MDM_ITEM_CATEGORY_CODE_DUPLICATE);
        }
        // 校验同一父级下名称唯一
        MdmItemCategoryDO sameName = itemCategoryMapper.selectByParentIdAndName(
                reqVO.getParentId() != null ? reqVO.getParentId() : 0L, reqVO.getName());
        if (sameName != null && ObjUtil.notEqual(sameName.getId(), reqVO.getId())) {
            throw exception(MDM_ITEM_CATEGORY_NAME_DUPLICATE);
        }
        // 校验父分类存在
        if (reqVO.getParentId() != null && reqVO.getParentId() > 0) {
            if (itemCategoryMapper.selectById(reqVO.getParentId()) == null) {
                throw exception(MDM_ITEM_CATEGORY_PARENT_NOT_EXISTS);
            }
        }
    }
 
    @Override
    public MdmItemCategoryDO getItemCategory(Long id) {
        return itemCategoryMapper.selectById(id);
    }
 
    @Override
    public PageResult<MdmItemCategoryDO> getItemCategoryPage(MdmItemCategoryPageReqVO pageReqVO) {
        return itemCategoryMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<MdmItemCategoryDO> getItemCategoryList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return itemCategoryMapper.selectBatchIds(ids);
    }
 
    @Override
    public List<MdmItemCategoryDO> getItemCategoryChildrenList(Long id) {
        List<MdmItemCategoryDO> result = new ArrayList<>();
        // 添加自己
        MdmItemCategoryDO self = itemCategoryMapper.selectById(id);
        if (self != null) {
            result.add(self);
        }
        // 递归获取所有子分类
        loadChildren(id, result);
        return result;
    }
 
    private void loadChildren(Long parentId, List<MdmItemCategoryDO> result) {
        List<MdmItemCategoryDO> children = itemCategoryMapper.selectListByParentId(parentId);
        if (CollUtil.isNotEmpty(children)) {
            result.addAll(children);
            for (MdmItemCategoryDO child : children) {
                loadChildren(child.getId(), result);
            }
        }
    }
 
    @Override
    public List<MdmItemCategoryDO> getSimpleItemList() {
        return itemCategoryMapper.selectList(new MdmItemCategoryDO().setStatus(0));
    }
 
    @Override
    public MdmItemCategoryDO getItemCategoryByItemOrProduct(String itemOrProduct) {
        return itemCategoryMapper.selectByItemOrProduct(itemOrProduct);
    }
 
}