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