hsy
9 天以前 e11706ad56843610601c14aa15868c4c3b908548
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package cn.iocoder.yudao.module.system.service.dept;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO;
import cn.iocoder.yudao.module.system.dal.mysql.dept.PostMapper;
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.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.LinkedHashMap;
 
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostImportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.post.PostImportRespVO;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
 
/**
 * 岗位 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class PostServiceImpl implements PostService {
 
    @Resource
    private PostMapper postMapper;
 
    @Override
    public Long createPost(PostSaveReqVO createReqVO) {
        // 校验正确性
        validatePostForCreateOrUpdate(null, createReqVO.getName(), createReqVO.getCode());
 
        // 插入岗位
        PostDO post = BeanUtils.toBean(createReqVO, PostDO.class);
        postMapper.insert(post);
        return post.getId();
    }
 
    @Override
    public void updatePost(PostSaveReqVO updateReqVO) {
        // 校验正确性
        validatePostForCreateOrUpdate(updateReqVO.getId(), updateReqVO.getName(), updateReqVO.getCode());
 
        // 更新岗位
        PostDO updateObj = BeanUtils.toBean(updateReqVO, PostDO.class);
        postMapper.updateById(updateObj);
    }
 
    @Override
    public void deletePost(Long id) {
        // 校验是否存在
        validatePostExists(id);
        // 删除岗位
        postMapper.deleteById(id);
    }
 
    @Override
    public void deletePostList(List<Long> ids) {
        postMapper.deleteByIds(ids);
    }
 
    private void validatePostForCreateOrUpdate(Long id, String name, String code) {
        // 校验自己存在
        validatePostExists(id);
        // 校验岗位名的唯一性
        validatePostNameUnique(id, name);
        // 校验岗位编码的唯一性
        validatePostCodeUnique(id, code);
    }
 
    private void validatePostNameUnique(Long id, String name) {
        PostDO post = postMapper.selectByName(name);
        if (post == null) {
            return;
        }
        // 如果 id 为空,说明不用比较是否为相同 id 的岗位
        if (id == null) {
            throw exception(POST_NAME_DUPLICATE);
        }
        if (!post.getId().equals(id)) {
            throw exception(POST_NAME_DUPLICATE);
        }
    }
 
    private void validatePostCodeUnique(Long id, String code) {
        PostDO post = postMapper.selectByCode(code);
        if (post == null) {
            return;
        }
        // 如果 id 为空,说明不用比较是否为相同 id 的岗位
        if (id == null) {
            throw exception(POST_CODE_DUPLICATE);
        }
        if (!post.getId().equals(id)) {
            throw exception(POST_CODE_DUPLICATE);
        }
    }
 
    private void validatePostExists(Long id) {
        if (id == null) {
            return;
        }
        if (postMapper.selectById(id) == null) {
            throw exception(POST_NOT_FOUND);
        }
    }
 
    @Override
    public List<PostDO> getPostList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return postMapper.selectByIds(ids);
    }
 
    @Override
    public List<PostDO> getPostList(Collection<Long> ids, Collection<Integer> statuses) {
        return postMapper.selectList(ids, statuses);
    }
 
    @Override
    public PageResult<PostDO> getPostPage(PostPageReqVO reqVO) {
        return postMapper.selectPage(reqVO);
    }
 
    @Override
    public PostDO getPost(Long id) {
        return postMapper.selectById(id);
    }
 
    @Override
    public void validatePostList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        // 获得岗位信息
        List<PostDO> posts = postMapper.selectByIds(ids);
        Map<Long, PostDO> postMap = convertMap(posts, PostDO::getId);
        // 校验
        ids.forEach(id -> {
            PostDO post = postMap.get(id);
            if (post == null) {
                throw exception(POST_NOT_FOUND);
            }
            if (!CommonStatusEnum.ENABLE.getStatus().equals(post.getStatus())) {
                throw exception(POST_NOT_ENABLE, post.getName());
            }
        });
    }
 
    @Override
    public PostImportRespVO importPostList(List<PostImportExcelVO> importPosts, boolean isUpdateSupport) {
        if (CollUtil.isEmpty(importPosts)) {
            throw exception(POST_IMPORT_LIST_IS_EMPTY);
        }
        PostImportRespVO respVO = PostImportRespVO.builder().createPostNames(new ArrayList<>())
                .updatePostNames(new ArrayList<>()).failurePostNames(new LinkedHashMap<>()).build();
        importPosts.forEach(importPost -> {
            // 校验,判断是否有不符合的原因
            try {
                validatePostForCreateOrUpdate(null, importPost.getName(), importPost.getCode());
            } catch (ServiceException ex) {
                // 如果是重复异常,而且支持更新
                if (isUpdateSupport && (POST_NAME_DUPLICATE.getCode().equals(ex.getCode()) || POST_CODE_DUPLICATE.getCode().equals(ex.getCode()))) {
                    // ignore
                } else {
                    respVO.getFailurePostNames().put(importPost.getName(), ex.getMessage());
                    return;
                }
            }
 
            // 判断如果存在,在进行更新
            PostDO existPost = postMapper.selectByName(importPost.getName());
            if (existPost == null) {
                existPost = postMapper.selectByCode(importPost.getCode());
            }
            if (existPost == null) {
                PostDO post = BeanUtils.toBean(importPost, PostDO.class);
                postMapper.insert(post);
                respVO.getCreatePostNames().add(importPost.getName());
                return;
            }
 
            // 如果存在,判断是否允许更新
            if (!isUpdateSupport) {
                respVO.getFailurePostNames().put(importPost.getName(), "岗位名或编码已经存在");
                return;
            }
 
            // 更新岗位
            PostDO updatePost = BeanUtils.toBean(importPost, PostDO.class);
            updatePost.setId(existPost.getId());
            postMapper.updateById(updatePost);
            respVO.getUpdatePostNames().add(importPost.getName());
        });
        return respVO;
    }
}