潘志宝
2024-09-09 ed81b7371e376df35448b81531d30dd9024bd44a
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.service.demo;
H 2
3 import org.springframework.stereotype.Service;
4 import javax.annotation.Resource;
5 import org.springframework.validation.annotation.Validated;
6 import org.springframework.transaction.annotation.Transactional;
7
8 import java.util.*;
9 import com.iailab.module.infra.controller.admin.demo.vo.*;
10 import com.iailab.module.infra.dal.dataobject.demo.InfraCategoryDO;
11 import com.iailab.framework.common.pojo.PageResult;
12 import com.iailab.framework.common.pojo.PageParam;
13 import com.iailab.framework.common.util.object.BeanUtils;
14
15 import com.iailab.module.infra.dal.mysql.demo.InfraCategoryMapper;
16
17 import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
18 import static com.iailab.module.infra.enums.ErrorCodeConstants.*;
19
20 /**
21  * 分类 Service 实现类
22  *
23  * @author iailab
24  */
25 @Service
26 @Validated
27 public class InfraCategoryServiceImpl implements InfraCategoryService {
28
29     @Resource
30     private InfraCategoryMapper categoryMapper;
31
32     @Override
33     public Long createCategory(InfraCategorySaveReqVO createReqVO) {
34         // 校验父编号的有效性
35         validateParentCategory(null, createReqVO.getParentId());
36         // 校验名字的唯一性
37         validateCategoryNameUnique(null, createReqVO.getParentId(), createReqVO.getName());
38
39         // 插入
40         InfraCategoryDO category = BeanUtils.toBean(createReqVO, InfraCategoryDO.class);
41         categoryMapper.insert(category);
42         // 返回
43         return category.getId();
44     }
45
46     @Override
47     public void updateCategory(InfraCategorySaveReqVO updateReqVO) {
48         // 校验存在
49         validateCategoryExists(updateReqVO.getId());
50         // 校验父编号的有效性
51         validateParentCategory(updateReqVO.getId(), updateReqVO.getParentId());
52         // 校验名字的唯一性
53         validateCategoryNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName());
54
55         // 更新
56         InfraCategoryDO updateObj = BeanUtils.toBean(updateReqVO, InfraCategoryDO.class);
57         categoryMapper.updateById(updateObj);
58     }
59
60     @Override
61     public void deleteCategory(Long id) {
62         // 校验存在
63         validateCategoryExists(id);
64         // 校验是否有子分类
65         if (categoryMapper.selectCountByParentId(id) > 0) {
66             throw exception(CATEGORY_EXITS_CHILDREN);
67         }
68         // 删除
69         categoryMapper.deleteById(id);
70     }
71
72     private void validateCategoryExists(Long id) {
73         if (categoryMapper.selectById(id) == null) {
74             throw exception(CATEGORY_NOT_EXISTS);
75         }
76     }
77
78     private void validateParentCategory(Long id, Long parentId) {
79         if (parentId == null || CategoryDO.PARENT_ID_ROOT.equals(parentId)) {
80             return;
81         }
82         // 1. 不能设置自己为父分类
83         if (Objects.equals(id, parentId)) {
84             throw exception(CATEGORY_PARENT_ERROR);
85         }
86         // 2. 父分类不存在
87         CategoryDO parentCategory = categoryMapper.selectById(parentId);
88         if (parentCategory == null) {
89             throw exception(CATEGORY_PARENT_NOT_EXITS);
90         }
91         // 3. 递归校验父分类,如果父分类是自己的子分类,则报错,避免形成环路
92         if (id == null) { // id 为空,说明新增,不需要考虑环路
93             return;
94         }
95         for (int i = 0; i < Short.MAX_VALUE; i++) {
96             // 3.1 校验环路
97             parentId = parentCategory.getParentId();
98             if (Objects.equals(id, parentId)) {
99                 throw exception(CATEGORY_PARENT_IS_CHILD);
100             }
101             // 3.2 继续递归下一级父分类
102             if (parentId == null || CategoryDO.PARENT_ID_ROOT.equals(parentId)) {
103                 break;
104             }
105             parentCategory = categoryMapper.selectById(parentId);
106             if (parentCategory == null) {
107                 break;
108             }
109         }
110     }
111
112     private void validateCategoryNameUnique(Long id, Long parentId, String name) {
113         CategoryDO category = categoryMapper.selectByParentIdAndName(parentId, name);
114         if (category == null) {
115             return;
116         }
117         // 如果 id 为空,说明不用比较是否为相同 id 的分类
118         if (id == null) {
119             throw exception(CATEGORY_NAME_DUPLICATE);
120         }
121         if (!Objects.equals(category.getId(), id)) {
122             throw exception(CATEGORY_NAME_DUPLICATE);
123         }
124     }
125
126     @Override
127     public InfraCategoryDO getCategory(Long id) {
128         return categoryMapper.selectById(id);
129     }
130
131     @Override
132     public List<InfraCategoryDO> getCategoryList(InfraCategoryListReqVO listReqVO) {
133         return categoryMapper.selectList(listReqVO);
134     }
135
136 }