2026-06-29 940c8481d2fdcf51341db4ccd6fd6fcc2d43debb
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
package cn.iocoder.yudao.module.wms.service.md.item;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.controller.admin.md.item.vo.category.WmsItemCategoryListReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.md.item.vo.category.WmsItemCategorySaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.md.item.WmsItemCategoryDO;
import cn.iocoder.yudao.module.wms.dal.mysql.md.item.WmsItemCategoryMapper;
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.HashSet;
import java.util.List;
import java.util.Set;
 
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.module.wms.enums.ErrorCodeConstants.*;
 
/**
 * WMS 商品分类 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WmsItemCategoryServiceImpl implements WmsItemCategoryService {
 
    @Resource
    private WmsItemCategoryMapper categoryMapper;
 
    @Resource
    private WmsItemService itemService;
 
    @Override
    public Long createItemCategory(WmsItemCategorySaveReqVO createReqVO) {
        // 校验数据
        validateCategorySaveData(createReqVO);
 
        // 插入
        WmsItemCategoryDO category = BeanUtils.toBean(createReqVO, WmsItemCategoryDO.class);
        categoryMapper.insert(category);
        return category.getId();
    }
 
    @Override
    public void updateItemCategory(WmsItemCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateItemCategoryExists(updateReqVO.getId());
        // 校验数据
        validateCategorySaveData(updateReqVO);
 
        // 更新
        WmsItemCategoryDO updateObj = BeanUtils.toBean(updateReqVO, WmsItemCategoryDO.class);
        categoryMapper.updateById(updateObj);
    }
 
    private void validateCategorySaveData(WmsItemCategorySaveReqVO reqVO) {
        validateCategoryCodeUnique(reqVO.getId(), reqVO.getCode());
        validateCategoryNameUnique(reqVO.getId(), reqVO.getParentId(), reqVO.getName());
        validateParentCategory(reqVO.getId(), reqVO.getParentId());
    }
 
    private void validateCategoryCodeUnique(Long id, String code) {
        WmsItemCategoryDO category = categoryMapper.selectByCode(code);
        if (category == null) {
            return;
        }
        // 如果 id 为空,说明新增;和数据库已有 code 重复
        if (id == null) {
            throw exception(ITEM_CATEGORY_CODE_DUPLICATE);
        }
        if (ObjectUtil.notEqual(category.getId(), id)) {
            throw exception(ITEM_CATEGORY_CODE_DUPLICATE);
        }
    }
 
    private void validateCategoryNameUnique(Long id, Long parentId, String name) {
        WmsItemCategoryDO category = categoryMapper.selectByParentIdAndName(parentId, name);
        if (category == null) {
            return;
        }
        // 如果 id 为空,说明不用比较是否为相同 id 的商品分类
        if (id == null) {
            throw exception(ITEM_CATEGORY_NAME_DUPLICATE);
        }
        if (ObjectUtil.notEqual(category.getId(), id)) {
            throw exception(ITEM_CATEGORY_NAME_DUPLICATE);
        }
    }
 
    private void validateParentCategory(Long id, Long parentId) {
        if (parentId == null || WmsItemCategoryDO.PARENT_ID_ROOT.equals(parentId)) {
            return;
        }
        // 1. 不能设置自己为父分类
        if (ObjectUtil.equal(parentId, id)) {
            throw exception(ITEM_CATEGORY_PARENT_ERROR);
        }
        // 2. 父分类不存在
        WmsItemCategoryDO parentCategory = categoryMapper.selectById(parentId);
        if (parentCategory == null) {
            throw exception(ITEM_CATEGORY_PARENT_NOT_EXISTS);
        }
        // 3. 递归校验父分类,如果父分类是自己的子分类,则报错,避免形成环路
        if (id == null) { // id 为空,说明新增,不需要考虑环路
            return;
        }
        for (int i = 0; i < Short.MAX_VALUE; i++) {
            // 3.1 校验环路
            parentId = parentCategory.getParentId();
            if (ObjectUtil.equal(id, parentId)) {
                throw exception(ITEM_CATEGORY_PARENT_IS_CHILD);
            }
            // 3.2 继续递归上级父分类
            if (parentId == null || WmsItemCategoryDO.PARENT_ID_ROOT.equals(parentId)) {
                break;
            }
            parentCategory = categoryMapper.selectById(parentId);
            if (parentCategory == null) {
                break;
            }
        }
    }
 
    @Override
    public void deleteItemCategory(Long id) {
        // 校验存在
        validateItemCategoryExists(id);
        // 有子分类不能删
        if (categoryMapper.selectCountByParentId(id) > 0) {
            throw exception(ITEM_CATEGORY_HAS_CHILDREN);
        }
        // 校验分类下不存在商品
        if (itemService.getItemCountByCategoryId(id) > 0) {
            throw exception(ITEM_CATEGORY_HAS_ITEM);
        }
 
        // 删除
        categoryMapper.deleteById(id);
    }
 
    @Override
    public WmsItemCategoryDO validateItemCategoryExists(Long id) {
        WmsItemCategoryDO category = categoryMapper.selectById(id);
        if (category == null) {
            throw exception(ITEM_CATEGORY_NOT_EXISTS);
        }
        return category;
    }
 
    @Override
    public WmsItemCategoryDO getItemCategory(Long id) {
        return categoryMapper.selectById(id);
    }
 
    @Override
    public List<WmsItemCategoryDO> getItemCategoryList(WmsItemCategoryListReqVO listReqVO) {
        return categoryMapper.selectList(listReqVO);
    }
 
    @Override
    public List<WmsItemCategoryDO> getItemCategoryList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return categoryMapper.selectByIds(ids);
    }
 
    @Override
    public Set<Long> getSelfAndChildItemCategoryIdList(Long id) {
        if (id == null) {
            return null;
        }
        Set<Long> ids = new HashSet<>();
        ids.add(id);
        Collection<Long> parentIds = Collections.singleton(id);
        for (int i = 0; i < Short.MAX_VALUE; i++) {
            List<WmsItemCategoryDO> children = categoryMapper.selectListByParentIds(parentIds);
            if (CollUtil.isEmpty(children)) {
                break;
            }
            ids.addAll(convertSet(children, WmsItemCategoryDO::getId));
            parentIds = convertSet(children, WmsItemCategoryDO::getId);
        }
        return ids;
    }
 
}