| | |
| | | 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; |
| | |
| | | /** |
| | | * 岗位 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | @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; |
| | | } |
| | | } |