houzhongjian
2024-10-16 7da8f196dee8e3c526c009a4bc7f5983ece6bb97
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.service.dept;
H 2
3 import cn.hutool.core.collection.CollUtil;
4 import com.iailab.framework.common.enums.CommonStatusEnum;
5 import com.iailab.framework.common.pojo.PageResult;
6 import com.iailab.framework.common.util.object.BeanUtils;
7 import com.iailab.module.system.controller.admin.dept.vo.post.PostPageReqVO;
8 import com.iailab.module.system.controller.admin.dept.vo.post.PostSaveReqVO;
9 import com.iailab.module.system.dal.dataobject.dept.PostDO;
10 import com.iailab.module.system.dal.mysql.dept.PostMapper;
11 import org.springframework.stereotype.Service;
12 import org.springframework.validation.annotation.Validated;
13
14 import javax.annotation.Resource;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19
20 import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
21 import static com.iailab.framework.common.util.collection.CollectionUtils.convertMap;
22 import static com.iailab.module.system.enums.ErrorCodeConstants.*;
23
24 /**
25  * 岗位 Service 实现类
26  *
27  * @author iailab
28  */
29 @Service
30 @Validated
31 public class PostServiceImpl implements PostService {
32
33     @Resource
34     private PostMapper postMapper;
35
36     @Override
37     public Long createPost(PostSaveReqVO createReqVO) {
38         // 校验正确性
39         validatePostForCreateOrUpdate(null, createReqVO.getName(), createReqVO.getCode());
40
41         // 插入岗位
42         PostDO post = BeanUtils.toBean(createReqVO, PostDO.class);
43         postMapper.insert(post);
44         return post.getId();
45     }
46
47     @Override
48     public void updatePost(PostSaveReqVO updateReqVO) {
49         // 校验正确性
50         validatePostForCreateOrUpdate(updateReqVO.getId(), updateReqVO.getName(), updateReqVO.getCode());
51
52         // 更新岗位
53         PostDO updateObj = BeanUtils.toBean(updateReqVO, PostDO.class);
54         postMapper.updateById(updateObj);
55     }
56
57     @Override
58     public void deletePost(Long id) {
59         // 校验是否存在
60         validatePostExists(id);
61         // 删除部门
62         postMapper.deleteById(id);
63     }
64
65     private void validatePostForCreateOrUpdate(Long id, String name, String code) {
66         // 校验自己存在
67         validatePostExists(id);
68         // 校验岗位名的唯一性
69         validatePostNameUnique(id, name);
70         // 校验岗位编码的唯一性
71         validatePostCodeUnique(id, code);
72     }
73
74     private void validatePostNameUnique(Long id, String name) {
75         PostDO post = postMapper.selectByName(name);
76         if (post == null) {
77             return;
78         }
79         // 如果 id 为空,说明不用比较是否为相同 id 的岗位
80         if (id == null) {
81             throw exception(POST_NAME_DUPLICATE);
82         }
83         if (!post.getId().equals(id)) {
84             throw exception(POST_NAME_DUPLICATE);
85         }
86     }
87
88     private void validatePostCodeUnique(Long id, String code) {
89         PostDO post = postMapper.selectByCode(code);
90         if (post == null) {
91             return;
92         }
93         // 如果 id 为空,说明不用比较是否为相同 id 的岗位
94         if (id == null) {
95             throw exception(POST_CODE_DUPLICATE);
96         }
97         if (!post.getId().equals(id)) {
98             throw exception(POST_CODE_DUPLICATE);
99         }
100     }
101
102     private void validatePostExists(Long id) {
103         if (id == null) {
104             return;
105         }
106         if (postMapper.selectById(id) == null) {
107             throw exception(POST_NOT_FOUND);
108         }
109     }
110
111     @Override
112     public List<PostDO> getPostList(Collection<Long> ids) {
113         if (CollUtil.isEmpty(ids)) {
114             return Collections.emptyList();
115         }
116         return postMapper.selectBatchIds(ids);
117     }
118
119     @Override
120     public List<PostDO> getPostList(Collection<Long> ids, Collection<Integer> statuses) {
121         return postMapper.selectList(ids, statuses);
122     }
123
124     @Override
125     public PageResult<PostDO> getPostPage(PostPageReqVO reqVO) {
126         return postMapper.selectPage(reqVO);
127     }
128
129     @Override
130     public PostDO getPost(Long id) {
131         return postMapper.selectById(id);
132     }
133
134     @Override
135     public void validatePostList(Collection<Long> ids) {
136         if (CollUtil.isEmpty(ids)) {
137             return;
138         }
139         // 获得岗位信息
140         List<PostDO> posts = postMapper.selectBatchIds(ids);
141         Map<Long, PostDO> postMap = convertMap(posts, PostDO::getId);
142         // 校验
143         ids.forEach(id -> {
144             PostDO post = postMap.get(id);
145             if (post == null) {
146                 throw exception(POST_NOT_FOUND);
147             }
148             if (!CommonStatusEnum.ENABLE.getStatus().equals(post.getStatus())) {
149                 throw exception(POST_NOT_ENABLE, post.getName());
150             }
151         });
152     }
153 }