完善 DeptServiceImpl 单元测试

This commit is contained in:
YunaiV
2023-02-01 00:07:34 +08:00
parent ba78d9964a
commit 1011de3278
11 changed files with 186 additions and 126 deletions

View File

@@ -28,14 +28,14 @@ public class DeptApiImpl implements DeptApi {
}
@Override
public List<DeptRespDTO> getDepts(Collection<Long> ids) {
List<DeptDO> depts = deptService.getDepts(ids);
public List<DeptRespDTO> getDeptList(Collection<Long> ids) {
List<DeptDO> depts = deptService.getDeptList(ids);
return DeptConvert.INSTANCE.convertList03(depts);
}
@Override
public void validDepts(Collection<Long> ids) {
deptService.validDepts(ids);
public void validateDeptList(Collection<Long> ids) {
deptService.validateDeptList(ids);
}
}

View File

@@ -58,7 +58,7 @@ public class DeptController {
@ApiOperation("获取部门列表")
@PreAuthorize("@ss.hasPermission('system:dept:query')")
public CommonResult<List<DeptRespVO>> listDepts(DeptListReqVO reqVO) {
List<DeptDO> list = deptService.getSimpleDepts(reqVO);
List<DeptDO> list = deptService.getDeptList(reqVO);
list.sort(Comparator.comparing(DeptDO::getSort));
return success(DeptConvert.INSTANCE.convertList(list));
}
@@ -69,7 +69,7 @@ public class DeptController {
// 获得部门列表,只要开启状态的
DeptListReqVO reqVO = new DeptListReqVO();
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
List<DeptDO> list = deptService.getSimpleDepts(reqVO);
List<DeptDO> list = deptService.getDeptList(reqVO);
// 排序后,返回给前端
list.sort(Comparator.comparing(DeptDO::getSort));
return success(DeptConvert.INSTANCE.convertList02(list));

View File

@@ -52,7 +52,7 @@ public interface DeptService {
* @param reqVO 筛选条件请求 VO
* @return 部门列表
*/
List<DeptDO> getSimpleDepts(DeptListReqVO reqVO);
List<DeptDO> getDeptList(DeptListReqVO reqVO);
/**
* 获得所有子部门,从缓存中
@@ -61,7 +61,7 @@ public interface DeptService {
* @param recursive 是否递归获取所有
* @return 子部门列表
*/
List<DeptDO> getDeptsByParentIdFromCache(Long parentId, boolean recursive);
List<DeptDO> getDeptListByParentIdFromCache(Long parentId, boolean recursive);
/**
* 获得部门信息数组
@@ -69,7 +69,21 @@ public interface DeptService {
* @param ids 部门编号数组
* @return 部门信息数组
*/
List<DeptDO> getDepts(Collection<Long> ids);
List<DeptDO> getDeptList(Collection<Long> ids);
/**
* 获得指定编号的部门 Map
*
* @param ids 部门编号数组
* @return 部门 Map
*/
default Map<Long, DeptDO> getDeptMap(Collection<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return Collections.emptyMap();
}
List<DeptDO> list = getDeptList(ids);
return CollectionUtils.convertMap(list, DeptDO::getId);
}
/**
* 获得部门信息
@@ -86,27 +100,6 @@ public interface DeptService {
*
* @param ids 角色编号数组
*/
void validDepts(Collection<Long> ids);
void validateDeptList(Collection<Long> ids);
/**
* 获得指定编号的部门列表
*
* @param ids 部门编号数组
* @return 部门列表
*/
List<DeptDO> getSimpleDepts(Collection<Long> ids);
/**
* 获得指定编号的部门 Map
*
* @param ids 部门编号数组
* @return 部门 Map
*/
default Map<Long, DeptDO> getDeptMap(Collection<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return Collections.emptyMap();
}
List<DeptDO> list = getSimpleDepts(ids);
return CollectionUtils.convertMap(list, DeptDO::getId);
}
}

View File

@@ -2,8 +2,6 @@ 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.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptCreateReqVO;
@@ -19,13 +17,11 @@ import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -95,7 +91,7 @@ public class DeptServiceImpl implements DeptService {
if (reqVO.getParentId() == null) {
reqVO.setParentId(DeptIdEnum.ROOT.getId());
}
checkCreateOrUpdate(null, reqVO.getParentId(), reqVO.getName());
validateForCreateOrUpdate(null, reqVO.getParentId(), reqVO.getName());
// 插入部门
DeptDO dept = DeptConvert.INSTANCE.convert(reqVO);
deptMapper.insert(dept);
@@ -110,7 +106,7 @@ public class DeptServiceImpl implements DeptService {
if (reqVO.getParentId() == null) {
reqVO.setParentId(DeptIdEnum.ROOT.getId());
}
checkCreateOrUpdate(reqVO.getId(), reqVO.getParentId(), reqVO.getName());
validateForCreateOrUpdate(reqVO.getId(), reqVO.getParentId(), reqVO.getName());
// 更新部门
DeptDO updateObj = DeptConvert.INSTANCE.convert(reqVO);
deptMapper.updateById(updateObj);
@@ -121,10 +117,10 @@ public class DeptServiceImpl implements DeptService {
@Override
public void deleteDept(Long id) {
// 校验是否存在
checkDeptExists(id);
validateDeptExists(id);
// 校验是否有子部门
if (deptMapper.selectCountByParentId(id) > 0) {
throw ServiceExceptionUtil.exception(DEPT_EXITS_CHILDREN);
throw exception(DEPT_EXITS_CHILDREN);
}
// 删除部门
deptMapper.deleteById(id);
@@ -133,16 +129,16 @@ public class DeptServiceImpl implements DeptService {
}
@Override
public List<DeptDO> getSimpleDepts(DeptListReqVO reqVO) {
public List<DeptDO> getDeptList(DeptListReqVO reqVO) {
return deptMapper.selectList(reqVO);
}
@Override
public List<DeptDO> getDeptsByParentIdFromCache(Long parentId, boolean recursive) {
public List<DeptDO> getDeptListByParentIdFromCache(Long parentId, boolean recursive) {
if (parentId == null) {
return Collections.emptyList();
}
List<DeptDO> result = new ArrayList<>(); // TODO 芋艿:待优化,新增缓存,避免每次遍历的计算
List<DeptDO> result = new ArrayList<>();
// 递归,简单粗暴
this.getDeptsByParentIdFromCache(result, parentId,
recursive ? Integer.MAX_VALUE : 1, // 如果递归获取,则无限;否则,只递归 1 次
@@ -182,65 +178,65 @@ public class DeptServiceImpl implements DeptService {
recursiveCount - 1, parentDeptMap));
}
private void checkCreateOrUpdate(Long id, Long parentId, String name) {
private void validateForCreateOrUpdate(Long id, Long parentId, String name) {
// 校验自己存在
checkDeptExists(id);
validateDeptExists(id);
// 校验父部门的有效性
checkParentDeptEnable(id, parentId);
validateParentDeptEnable(id, parentId);
// 校验部门名的唯一性
checkDeptNameUnique(id, parentId, name);
validateDeptNameUnique(id, parentId, name);
}
private void checkParentDeptEnable(Long id, Long parentId) {
private void validateParentDeptEnable(Long id, Long parentId) {
if (parentId == null || DeptIdEnum.ROOT.getId().equals(parentId)) {
return;
}
// 不能设置自己为父部门
if (parentId.equals(id)) {
throw ServiceExceptionUtil.exception(DEPT_PARENT_ERROR);
throw exception(DEPT_PARENT_ERROR);
}
// 父岗位不存在
DeptDO dept = deptMapper.selectById(parentId);
if (dept == null) {
throw ServiceExceptionUtil.exception(DEPT_PARENT_NOT_EXITS);
throw exception(DEPT_PARENT_NOT_EXITS);
}
// 父部门被禁用
if (!CommonStatusEnum.ENABLE.getStatus().equals(dept.getStatus())) {
throw ServiceExceptionUtil.exception(DEPT_NOT_ENABLE);
throw exception(DEPT_NOT_ENABLE);
}
// 父部门不能是原来的子部门
List<DeptDO> children = this.getDeptsByParentIdFromCache(id, true);
List<DeptDO> children = this.getDeptListByParentIdFromCache(id, true);
if (children.stream().anyMatch(dept1 -> dept1.getId().equals(parentId))) {
throw ServiceExceptionUtil.exception(DEPT_PARENT_IS_CHILD);
throw exception(DEPT_PARENT_IS_CHILD);
}
}
private void checkDeptExists(Long id) {
private void validateDeptExists(Long id) {
if (id == null) {
return;
}
DeptDO dept = deptMapper.selectById(id);
if (dept == null) {
throw ServiceExceptionUtil.exception(DEPT_NOT_FOUND);
throw exception(DEPT_NOT_FOUND);
}
}
private void checkDeptNameUnique(Long id, Long parentId, String name) {
private void validateDeptNameUnique(Long id, Long parentId, String name) {
DeptDO menu = deptMapper.selectByParentIdAndName(parentId, name);
if (menu == null) {
return;
}
// 如果 id 为空,说明不用比较是否为相同 id 的岗位
if (id == null) {
throw ServiceExceptionUtil.exception(DEPT_NAME_DUPLICATE);
throw exception(DEPT_NAME_DUPLICATE);
}
if (!menu.getId().equals(id)) {
throw ServiceExceptionUtil.exception(DEPT_NAME_DUPLICATE);
throw exception(DEPT_NAME_DUPLICATE);
}
}
@Override
public List<DeptDO> getDepts(Collection<Long> ids) {
public List<DeptDO> getDeptList(Collection<Long> ids) {
return deptMapper.selectBatchIds(ids);
}
@@ -250,13 +246,12 @@ public class DeptServiceImpl implements DeptService {
}
@Override
public void validDepts(Collection<Long> ids) {
public void validateDeptList(Collection<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return;
}
// 获得科室信息
List<DeptDO> depts = deptMapper.selectBatchIds(ids);
Map<Long, DeptDO> deptMap = CollectionUtils.convertMap(depts, DeptDO::getId);
Map<Long, DeptDO> deptMap = getDeptMap(ids);
// 校验
ids.forEach(id -> {
DeptDO dept = deptMap.get(id);
@@ -269,9 +264,4 @@ public class DeptServiceImpl implements DeptService {
});
}
@Override
public List<DeptDO> getSimpleDepts(Collection<Long> ids) {
return deptMapper.selectBatchIds(ids);
}
}

View File

@@ -418,7 +418,7 @@ public class PermissionServiceImpl implements PermissionService {
}
// 情况四DEPT_DEPT_AND_CHILD
if (Objects.equals(role.getDataScope(), DataScopeEnum.DEPT_AND_CHILD.getScope())) {
List<DeptDO> depts = deptService.getDeptsByParentIdFromCache(userDeptIdCache.get(), true);
List<DeptDO> depts = deptService.getDeptListByParentIdFromCache(userDeptIdCache.get(), true);
CollUtil.addAll(result.getDeptIds(), CollectionUtils.convertList(depts, DeptDO::getId));
// 添加本身部门编号
CollUtil.addAll(result.getDeptIds(), userDeptIdCache.get());

View File

@@ -299,7 +299,7 @@ public class AdminUserServiceImpl implements AdminUserService {
if (deptId == null) {
return Collections.emptySet();
}
Set<Long> deptIds = convertSet(deptService.getDeptsByParentIdFromCache(
Set<Long> deptIds = convertSet(deptService.getDeptListByParentIdFromCache(
deptId, true), DeptDO::getId);
deptIds.add(deptId); // 包括自身
return deptIds;
@@ -317,7 +317,7 @@ public class AdminUserServiceImpl implements AdminUserService {
// 校验邮箱唯一
checkEmailUnique(id, email);
// 校验部门处于开启状态
deptService.validDepts(CollectionUtils.singleton(deptId));
deptService.validateDeptList(CollectionUtils.singleton(deptId));
// 校验岗位处于开启状态
postService.validPosts(postIds);
}