org.openea.eap.module.system.service.dept.PostServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eap-module-system-biz Show documentation
Show all versions of eap-module-system-biz Show documentation
system 模块下,我们放通用业务,支撑上层的核心业务。
例如说:用户、部门、权限、数据字典等等
The newest version!
package org.openea.eap.module.system.service.dept;
import cn.hutool.core.collection.CollUtil;
import org.openea.eap.framework.common.enums.CommonStatusEnum;
import org.openea.eap.framework.common.pojo.PageResult;
import org.openea.eap.framework.common.util.object.BeanUtils;
import org.openea.eap.module.system.controller.admin.dept.vo.post.PostPageReqVO;
import org.openea.eap.module.system.controller.admin.dept.vo.post.PostSaveReqVO;
import org.openea.eap.module.system.dal.dataobject.dept.PostDO;
import org.openea.eap.module.system.dal.mysql.dept.PostMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.openea.eap.framework.common.exception.util.ServiceExceptionUtil.exception;
import static org.openea.eap.framework.common.util.collection.CollectionUtils.convertMap;
import static org.openea.eap.module.system.enums.ErrorCodeConstants.*;
/**
* 岗位 Service 实现类
*
*/
@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);
}
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 getPostList(Collection ids) {
if (CollUtil.isEmpty(ids)) {
return Collections.emptyList();
}
return postMapper.selectBatchIds(ids);
}
@Override
public List getPostList(Collection ids, Collection statuses) {
return postMapper.selectList(ids, statuses);
}
@Override
public PageResult getPostPage(PostPageReqVO reqVO) {
return postMapper.selectPage(reqVO);
}
@Override
public PostDO getPost(Long id) {
return postMapper.selectById(id);
}
@Override
public void validatePostList(Collection ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 获得岗位信息
List posts = postMapper.selectBatchIds(ids);
Map 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());
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy