mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-11 06:57:12 +08:00
优化:部门新增类型字段,用于标识部门是系统内置或自定义
1.系统内置部门不允许禁用、删除、修改上级部门 2.抽取 getAncestors 方法,用于复用获取祖级列表 3.删除部门时,自动删除角色和部门关联
This commit is contained in:
@@ -21,6 +21,7 @@ import lombok.Data;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseDO;
|
||||
import top.charles7c.cnadmin.common.enums.DataTypeEnum;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
|
||||
/**
|
||||
@@ -64,4 +65,9 @@ public class DeptDO extends BaseDO {
|
||||
* 状态(1:启用,2:禁用)
|
||||
*/
|
||||
private DisEnableStatusEnum status;
|
||||
|
||||
/**
|
||||
* 类型(1:系统内置,2:自定义)
|
||||
*/
|
||||
private DataTypeEnum type;
|
||||
}
|
||||
|
@@ -43,6 +43,7 @@ public class DeptRequest extends BaseRequest {
|
||||
/**
|
||||
* 上级部门 ID
|
||||
*/
|
||||
@NotNull(message = "上级部门不能为空", groups = Add.class)
|
||||
@Schema(description = "上级部门 ID")
|
||||
private Long parentId;
|
||||
|
||||
|
@@ -26,6 +26,7 @@ import com.baomidou.mybatisplus.annotation.TableField;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseDetailVO;
|
||||
import top.charles7c.cnadmin.common.config.easyexcel.ExcelBaseEnumConverter;
|
||||
import top.charles7c.cnadmin.common.enums.DataTypeEnum;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
|
||||
/**
|
||||
@@ -75,10 +76,22 @@ public class DeptDetailVO extends BaseDetailVO {
|
||||
@ExcelProperty(value = "状态", converter = ExcelBaseEnumConverter.class)
|
||||
private DisEnableStatusEnum status;
|
||||
|
||||
/**
|
||||
* 类型(1:系统内置,2:自定义)
|
||||
*/
|
||||
@Schema(description = "类型(1:系统内置,2:自定义)")
|
||||
@ExcelProperty(value = "类型", converter = ExcelBaseEnumConverter.class)
|
||||
private DataTypeEnum type;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
@Schema(description = "描述")
|
||||
@ExcelProperty(value = "描述")
|
||||
private String description;
|
||||
|
||||
@Override
|
||||
public Boolean getDisabled() {
|
||||
return DataTypeEnum.SYSTEM.equals(type);
|
||||
}
|
||||
}
|
||||
|
@@ -23,6 +23,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import top.charles7c.cnadmin.common.annotation.TreeField;
|
||||
import top.charles7c.cnadmin.common.base.BaseVO;
|
||||
import top.charles7c.cnadmin.common.enums.DataTypeEnum;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
|
||||
/**
|
||||
@@ -63,9 +64,20 @@ public class DeptVO extends BaseVO {
|
||||
@Schema(description = "状态(1:启用,2:禁用)")
|
||||
private DisEnableStatusEnum status;
|
||||
|
||||
/**
|
||||
* 类型(1:系统内置,2:自定义)
|
||||
*/
|
||||
@Schema(description = "类型(1:系统内置,2:自定义)")
|
||||
private DataTypeEnum type;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
@Schema(description = "描述")
|
||||
private String description;
|
||||
|
||||
@Override
|
||||
public Boolean getDisabled() {
|
||||
return DataTypeEnum.SYSTEM.equals(type);
|
||||
}
|
||||
}
|
||||
|
@@ -21,8 +21,6 @@ import lombok.experimental.Accessors;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseVO;
|
||||
import top.charles7c.cnadmin.common.constant.SysConsts;
|
||||
import top.charles7c.cnadmin.common.enums.DataScopeEnum;
|
||||
@@ -77,16 +75,8 @@ public class RoleVO extends BaseVO {
|
||||
@Schema(description = "描述")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 是否禁用修改
|
||||
*/
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private Boolean disabled;
|
||||
|
||||
@Override
|
||||
public Boolean getDisabled() {
|
||||
if (SysConsts.ADMIN_ROLE_CODE.equals(code)) {
|
||||
return true;
|
||||
}
|
||||
return disabled;
|
||||
return SysConsts.ADMIN_ROLE_CODE.equals(code);
|
||||
}
|
||||
}
|
||||
|
@@ -23,8 +23,6 @@ import lombok.experimental.Accessors;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
|
||||
import cn.hutool.core.util.DesensitizedUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseVO;
|
||||
@@ -93,17 +91,9 @@ public class UserVO extends BaseVO {
|
||||
@Schema(description = "描述")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 是否禁用修改
|
||||
*/
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
private Boolean disabled;
|
||||
|
||||
@Override
|
||||
public Boolean getDisabled() {
|
||||
if (Objects.equals(this.getId(), LoginHelper.getUserId())) {
|
||||
return true;
|
||||
}
|
||||
return disabled;
|
||||
return Objects.equals(this.getId(), LoginHelper.getUserId());
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
|
@@ -44,4 +44,12 @@ public interface RoleDeptService {
|
||||
* @return 部门 ID 列表
|
||||
*/
|
||||
List<Long> listDeptIdByRoleId(Long roleId);
|
||||
|
||||
/**
|
||||
* 根据部门 ID 删除
|
||||
*
|
||||
* @param deptIds
|
||||
* 部门 ID 列表
|
||||
*/
|
||||
void deleteByDeptIds(List<Long> deptIds);
|
||||
}
|
@@ -19,6 +19,7 @@ package top.charles7c.cnadmin.system.service.impl;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@@ -28,9 +29,11 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.base.BaseServiceImpl;
|
||||
import top.charles7c.cnadmin.common.constant.SysConsts;
|
||||
import top.charles7c.cnadmin.common.enums.DataTypeEnum;
|
||||
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
|
||||
import top.charles7c.cnadmin.common.util.ExceptionUtils;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
@@ -41,6 +44,7 @@ import top.charles7c.cnadmin.system.model.request.DeptRequest;
|
||||
import top.charles7c.cnadmin.system.model.vo.DeptDetailVO;
|
||||
import top.charles7c.cnadmin.system.model.vo.DeptVO;
|
||||
import top.charles7c.cnadmin.system.service.DeptService;
|
||||
import top.charles7c.cnadmin.system.service.RoleDeptService;
|
||||
import top.charles7c.cnadmin.system.service.UserService;
|
||||
|
||||
/**
|
||||
@@ -54,6 +58,7 @@ import top.charles7c.cnadmin.system.service.UserService;
|
||||
public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO, DeptDetailVO, DeptQuery, DeptRequest>
|
||||
implements DeptService {
|
||||
|
||||
private final RoleDeptService roleDeptService;
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@@ -62,12 +67,10 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
|
||||
public Long add(DeptRequest request) {
|
||||
String name = request.getName();
|
||||
boolean isExists = this.checkNameExists(name, request.getParentId(), null);
|
||||
CheckUtils.throwIf(() -> isExists, String.format("新增失败,'%s'已存在", name));
|
||||
CheckUtils.throwIf(() -> isExists, String.format("新增失败,'%s' 已存在", name));
|
||||
|
||||
request.setAncestors(this.getAncestors(request.getParentId()));
|
||||
request.setStatus(DisEnableStatusEnum.ENABLE);
|
||||
DeptDO parentDept = baseMapper.selectById(request.getParentId());
|
||||
CheckUtils.throwIfNull(parentDept, "上级部门不存在");
|
||||
request.setAncestors(String.format("%s,%s", parentDept.getAncestors(), request.getParentId()));
|
||||
return super.add(request);
|
||||
}
|
||||
|
||||
@@ -76,15 +79,21 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
|
||||
public void update(DeptRequest request, Long id) {
|
||||
String name = request.getName();
|
||||
boolean isExists = this.checkNameExists(name, request.getParentId(), id);
|
||||
CheckUtils.throwIf(() -> isExists, String.format("修改失败,'%s'已存在", name));
|
||||
CheckUtils.throwIf(() -> isExists, String.format("修改失败,'%s' 已存在", name));
|
||||
DeptDO oldDept = this.getById(id);
|
||||
CheckUtils.throwIf(
|
||||
() -> DisEnableStatusEnum.DISABLE.equals(request.getStatus())
|
||||
&& DataTypeEnum.SYSTEM.equals(oldDept.getType()),
|
||||
String.format("'%s' 是系统内置部门,不允许禁用", oldDept.getName()));
|
||||
|
||||
DeptDO oldDept = baseMapper.selectById(id);
|
||||
// 更新祖级列表
|
||||
if (!Objects.equals(oldDept.getParentId(), request.getParentId())) {
|
||||
DeptDO newParentDept = baseMapper.selectById(request.getParentId());
|
||||
CheckUtils.throwIfNull(newParentDept, "上级部门不存在");
|
||||
request.setAncestors(String.format("%s,%s", newParentDept.getAncestors(), request.getParentId()));
|
||||
this.updateChildrenAncestors(id, request.getAncestors(), oldDept.getAncestors());
|
||||
// 变更上级部门
|
||||
if (ObjectUtil.notEqual(oldDept.getParentId(), request.getParentId())) {
|
||||
CheckUtils.throwIf(() -> DataTypeEnum.SYSTEM.equals(oldDept.getType()),
|
||||
String.format("'%s' 是系统内置部门,不允许变更上级部门", oldDept.getName()));
|
||||
// 更新祖级列表
|
||||
String newAncestors = this.getAncestors(request.getParentId());
|
||||
request.setAncestors(newAncestors);
|
||||
this.updateChildrenAncestors(newAncestors, oldDept.getAncestors(), id);
|
||||
}
|
||||
super.update(request, id);
|
||||
}
|
||||
@@ -92,9 +101,19 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(List<Long> ids) {
|
||||
List<DeptDO> list =
|
||||
baseMapper.lambdaQuery().select(DeptDO::getName, DeptDO::getType).in(DeptDO::getId, ids).list();
|
||||
Optional<DeptDO> isSystemData = list.stream().filter(d -> DataTypeEnum.SYSTEM.equals(d.getType())).findFirst();
|
||||
CheckUtils.throwIf(isSystemData::isPresent,
|
||||
String.format("所选部门 '%s' 是系统内置部门,不允许删除", isSystemData.orElseGet(DeptDO::new).getName()));
|
||||
CheckUtils.throwIf(() -> userService.countByDeptIds(ids) > 0, "所选部门存在用户关联,请解除关联后重试");
|
||||
|
||||
// 删除部门
|
||||
super.delete(ids);
|
||||
// 删除子部门
|
||||
baseMapper.lambdaUpdate().in(DeptDO::getParentId, ids).remove();
|
||||
// 删除角色和部门关联
|
||||
roleDeptService.deleteByDeptIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -125,17 +144,30 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO,
|
||||
.ne(id != null, DeptDO::getId, id).exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取祖级列表
|
||||
*
|
||||
* @param parentId
|
||||
* 上级部门
|
||||
* @return 祖级列表
|
||||
*/
|
||||
private String getAncestors(Long parentId) {
|
||||
DeptDO parentDept = baseMapper.selectById(parentId);
|
||||
CheckUtils.throwIfNull(parentDept, "上级部门不存在");
|
||||
return String.format("%s,%s", parentDept.getAncestors(), parentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新子部门祖级列表
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @param newAncestors
|
||||
* 新祖级列表
|
||||
* @param oldAncestors
|
||||
* 原祖级列表
|
||||
* @param id
|
||||
* ID
|
||||
*/
|
||||
private void updateChildrenAncestors(Long id, String newAncestors, String oldAncestors) {
|
||||
private void updateChildrenAncestors(String newAncestors, String oldAncestors, Long id) {
|
||||
List<DeptDO> children =
|
||||
baseMapper.lambdaQuery().apply(String.format("find_in_set(%s, `ancestors`)", id)).list();
|
||||
if (CollUtil.isEmpty(children)) {
|
||||
|
@@ -58,4 +58,9 @@ public class RoleDeptServiceImpl implements RoleDeptService {
|
||||
public List<Long> listDeptIdByRoleId(Long roleId) {
|
||||
return roleDeptMapper.selectDeptIdByRoleId(roleId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteByDeptIds(List<Long> deptIds) {
|
||||
roleDeptMapper.lambdaUpdate().in(RoleDeptDO::getDeptId, deptIds).remove();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user