remove pid

pull/882/head
chanhengseang 2025-05-18 17:08:17 -07:00
parent bd9a0cf243
commit 5e76a671c6
10 changed files with 6 additions and 125 deletions

View File

@ -15,7 +15,6 @@
*/ */
package me.zhengjie.modules.system.domain; package me.zhengjie.modules.system.domain;
import com.alibaba.fastjson2.annotation.JSONField;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -25,7 +24,6 @@ import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
/** /**
* @author Zheng Jie * @author Zheng Jie
@ -44,11 +42,6 @@ public class Dept extends BaseEntity implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
@JSONField(serialize = false)
@ManyToMany(mappedBy = "depts")
@ApiModelProperty(value = "角色")
private Set<Role> roles;
@ApiModelProperty(value = "排序") @ApiModelProperty(value = "排序")
private Integer deptSort; private Integer deptSort;
@ -60,9 +53,6 @@ public class Dept extends BaseEntity implements Serializable {
@ApiModelProperty(value = "是否启用") @ApiModelProperty(value = "是否启用")
private Boolean enabled; private Boolean enabled;
@ApiModelProperty(value = "上级部门")
private Long pid;
@ApiModelProperty(value = "子节点数目", hidden = true) @ApiModelProperty(value = "子节点数目", hidden = true)
private Integer subCount = 0; private Integer subCount = 0;

View File

@ -59,13 +59,6 @@ public class Role extends BaseEntity implements Serializable {
@ApiModelProperty(value = "菜单", hidden = true) @ApiModelProperty(value = "菜单", hidden = true)
private Set<Menu> menus; private Set<Menu> menus;
@ManyToMany
@JoinTable(name = "sys_roles_depts",
joinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "role_id")},
inverseJoinColumns = {@JoinColumn(name = "dept_id",referencedColumnName = "dept_id")})
@ApiModelProperty(value = "部门", hidden = true)
private Set<Dept> depts;
@NotBlank @NotBlank
@ApiModelProperty(value = "名称", hidden = true) @ApiModelProperty(value = "名称", hidden = true)
private String name; private String name;

View File

@ -29,19 +29,6 @@ import java.util.Set;
*/ */
public interface DeptRepository extends JpaRepository<Dept, Long>, JpaSpecificationExecutor<Dept> { public interface DeptRepository extends JpaRepository<Dept, Long>, JpaSpecificationExecutor<Dept> {
/**
* PID
* @param id pid
* @return /
*/
List<Dept> findByPid(Long id);
/**
*
* @return /
*/
List<Dept> findByPidIsNull();
/** /**
* ID * ID
* @param roleId ID * @param roleId ID
@ -51,13 +38,6 @@ public interface DeptRepository extends JpaRepository<Dept, Long>, JpaSpecificat
"d.dept_id = r.dept_id and r.role_id = ?1", nativeQuery = true) "d.dept_id = r.dept_id and r.role_id = ?1", nativeQuery = true)
Set<Dept> findByRoleId(Long roleId); Set<Dept> findByRoleId(Long roleId);
/**
*
* @param pid /
* @return /
*/
int countByPid(Long pid);
/** /**
* IDsub_count * IDsub_count
* @param count / * @param count /

View File

@ -74,11 +74,6 @@ public class DeptController {
DeptDto deptDto = deptService.findById(id); DeptDto deptDto = deptService.findById(id);
List<DeptDto> depts = deptService.getSuperior(deptDto, new ArrayList<>()); List<DeptDto> depts = deptService.getSuperior(deptDto, new ArrayList<>());
if(exclude){ if(exclude){
for (DeptDto dept : depts) {
if(dept.getId().equals(deptDto.getPid())) {
dept.setSubCount(dept.getSubCount() - 1);
}
}
// 编辑部门时不显示自己以及自己下级的数据避免出现PID数据环形问题 // 编辑部门时不显示自己以及自己下级的数据避免出现PID数据环形问题
depts = depts.stream().filter(i -> !ids.contains(i.getId())).collect(Collectors.toList()); depts = depts.stream().filter(i -> !ids.contains(i.getId())).collect(Collectors.toList());
} }
@ -114,13 +109,6 @@ public class DeptController {
@PreAuthorize("@el.check('dept:del')") @PreAuthorize("@el.check('dept:del')")
public ResponseEntity<Object> deleteDept(@RequestBody Set<Long> ids){ public ResponseEntity<Object> deleteDept(@RequestBody Set<Long> ids){
Set<DeptDto> deptDtos = new HashSet<>(); Set<DeptDto> deptDtos = new HashSet<>();
for (Long id : ids) {
List<Dept> deptList = deptService.findByPid(id);
deptDtos.add(deptService.findById(id));
if(CollectionUtil.isNotEmpty(deptList)){
deptDtos = deptService.getDeleteDepts(deptList, deptDtos);
}
}
// 验证是否被角色或用户关联 // 验证是否被角色或用户关联
deptService.verification(deptDtos); deptService.verification(deptDtos);
deptService.delete(deptDtos); deptService.delete(deptDtos);

View File

@ -82,9 +82,7 @@ public class UserController {
if (!ObjectUtils.isEmpty(criteria.getDeptId())) { if (!ObjectUtils.isEmpty(criteria.getDeptId())) {
criteria.getDeptIds().add(criteria.getDeptId()); criteria.getDeptIds().add(criteria.getDeptId());
// 先查找是否存在子节点 // 先查找是否存在子节点
List<Dept> data = deptService.findByPid(criteria.getDeptId());
// 然后把子节点的ID都加入到集合中 // 然后把子节点的ID都加入到集合中
criteria.getDeptIds().addAll(deptService.getDeptChildren(data));
} }
// 数据权限 // 数据权限
List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername())); List<Long> dataScopes = dataService.getDeptIds(userService.findByName(SecurityUtils.getCurrentUsername()));

View File

@ -64,13 +64,6 @@ public interface DeptService {
*/ */
void delete(Set<DeptDto> deptDtos); void delete(Set<DeptDto> deptDtos);
/**
* PID
* @param pid /
* @return /
*/
List<Dept> findByPid(long pid);
/** /**
* ID * ID
* @param id / * @param id /

View File

@ -46,9 +46,6 @@ public class DeptDto extends BaseDTO implements Serializable {
@ApiModelProperty(value = "子部门") @ApiModelProperty(value = "子部门")
private List<DeptDto> children; private List<DeptDto> children;
@ApiModelProperty(value = "上级部门")
private Long pid;
@ApiModelProperty(value = "子部门数量", hidden = true) @ApiModelProperty(value = "子部门数量", hidden = true)
private Integer subCount; private Integer subCount;

View File

@ -87,10 +87,6 @@ public class DataServiceImpl implements DataService {
Set<Dept> depts = deptService.findByRoleId(role.getId()); Set<Dept> depts = deptService.findByRoleId(role.getId());
for (Dept dept : depts) { for (Dept dept : depts) {
deptIds.add(dept.getId()); deptIds.add(dept.getId());
List<Dept> deptChildren = deptService.findByPid(dept.getId());
if (CollUtil.isNotEmpty(deptChildren)) {
deptIds.addAll(deptService.getDeptChildren(deptChildren));
}
} }
return deptIds; return deptIds;
} }

View File

@ -60,11 +60,8 @@ public class DeptServiceImpl implements DeptService {
Sort sort = Sort.by(Sort.Direction.ASC, "deptSort"); Sort sort = Sort.by(Sort.Direction.ASC, "deptSort");
String dataScopeType = SecurityUtils.getDataScopeType(); String dataScopeType = SecurityUtils.getDataScopeType();
if (isQuery) { if (isQuery) {
if(dataScopeType.equals(DataScopeEnum.ALL.getValue())){
criteria.setPidIsNull(true);
}
List<Field> fields = QueryHelp.getAllFields(criteria.getClass(), new ArrayList<>()); List<Field> fields = QueryHelp.getAllFields(criteria.getClass(), new ArrayList<>());
List<String> fieldNames = new ArrayList<String>(){{ add("pidIsNull");add("enabled");}}; List<String> fieldNames = new ArrayList<String>(){{ add("enabled");}};
for (Field field : fields) { for (Field field : fields) {
//设置对象的访问权限保证对private的属性的访问 //设置对象的访问权限保证对private的属性的访问
field.setAccessible(true); field.setAccessible(true);
@ -73,7 +70,6 @@ public class DeptServiceImpl implements DeptService {
continue; continue;
} }
if (ObjectUtil.isNotNull(val)) { if (ObjectUtil.isNotNull(val)) {
criteria.setPidIsNull(null);
break; break;
} }
} }
@ -98,11 +94,6 @@ public class DeptServiceImpl implements DeptService {
return deptMapper.toDto(dept); return deptMapper.toDto(dept);
} }
@Override
public List<Dept> findByPid(long pid) {
return deptRepository.findByPid(pid);
}
@Override @Override
public Set<Dept> findByRoleId(Long id) { public Set<Dept> findByRoleId(Long id) {
return deptRepository.findByRoleId(id); return deptRepository.findByRoleId(id);
@ -112,30 +103,17 @@ public class DeptServiceImpl implements DeptService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void create(Dept resources) { public void create(Dept resources) {
deptRepository.save(resources); deptRepository.save(resources);
// 计算子节点数目
resources.setSubCount(0);
// 清理缓存 // 清理缓存
updateSubCnt(resources.getPid()); delCaches(resources.getId());
// 清理自定义角色权限的datascope缓存
delCaches(resources.getPid());
} }
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void update(Dept resources) { public void update(Dept resources) {
// 旧的部门
Long oldPid = findById(resources.getId()).getPid();
Long newPid = resources.getPid();
if(resources.getPid() != null && resources.getId().equals(resources.getPid())) {
throw new BadRequestException("上级不能为自己");
}
Dept dept = deptRepository.findById(resources.getId()).orElseGet(Dept::new); Dept dept = deptRepository.findById(resources.getId()).orElseGet(Dept::new);
ValidationUtil.isNull( dept.getId(),"Dept","id",resources.getId()); ValidationUtil.isNull( dept.getId(),"Dept","id",resources.getId());
resources.setId(dept.getId()); resources.setId(dept.getId());
deptRepository.save(resources); deptRepository.save(resources);
// 更新父节点中子节点数目
updateSubCnt(oldPid);
updateSubCnt(newPid);
// 清理缓存 // 清理缓存
delCaches(resources.getId()); delCaches(resources.getId());
} }
@ -147,7 +125,6 @@ public class DeptServiceImpl implements DeptService {
// 清理缓存 // 清理缓存
delCaches(deptDto.getId()); delCaches(deptDto.getId());
deptRepository.deleteById(deptDto.getId()); deptRepository.deleteById(deptDto.getId());
updateSubCnt(deptDto.getPid());
} }
} }
@ -168,10 +145,6 @@ public class DeptServiceImpl implements DeptService {
public Set<DeptDto> getDeleteDepts(List<Dept> menuList, Set<DeptDto> deptDtos) { public Set<DeptDto> getDeleteDepts(List<Dept> menuList, Set<DeptDto> deptDtos) {
for (Dept dept : menuList) { for (Dept dept : menuList) {
deptDtos.add(deptMapper.toDto(dept)); deptDtos.add(deptMapper.toDto(dept));
List<Dept> depts = deptRepository.findByPid(dept.getId());
if(CollUtil.isNotEmpty(depts)){
getDeleteDepts(depts, deptDtos);
}
} }
return deptDtos; return deptDtos;
} }
@ -181,10 +154,6 @@ public class DeptServiceImpl implements DeptService {
List<Long> list = new ArrayList<>(); List<Long> list = new ArrayList<>();
deptList.forEach(dept -> { deptList.forEach(dept -> {
if (dept!=null && dept.getEnabled()) { if (dept!=null && dept.getEnabled()) {
List<Dept> depts = deptRepository.findByPid(dept.getId());
if (CollUtil.isNotEmpty(depts)) {
list.addAll(getDeptChildren(depts));
}
list.add(dept.getId()); list.add(dept.getId());
} }
} }
@ -194,12 +163,7 @@ public class DeptServiceImpl implements DeptService {
@Override @Override
public List<DeptDto> getSuperior(DeptDto deptDto, List<Dept> depts) { public List<DeptDto> getSuperior(DeptDto deptDto, List<Dept> depts) {
if(deptDto.getPid() == null){ return deptMapper.toDto(depts);
depts.addAll(deptRepository.findByPidIsNull());
return deptMapper.toDto(depts);
}
depts.addAll(deptRepository.findByPid(deptDto.getPid()));
return getSuperior(findById(deptDto.getPid()), depts);
} }
@Override @Override
@ -210,22 +174,12 @@ public class DeptServiceImpl implements DeptService {
boolean isChild; boolean isChild;
for (DeptDto deptDTO : deptDtos) { for (DeptDto deptDTO : deptDtos) {
isChild = false; isChild = false;
if (deptDTO.getPid() == null) { trees.add(deptDTO);
trees.add(deptDTO);
}
for (DeptDto it : deptDtos) { for (DeptDto it : deptDtos) {
if (it.getPid() != null && deptDTO.getId().equals(it.getPid())) { isChild = true;
isChild = true;
if (deptDTO.getChildren() == null) {
deptDTO.setChildren(new ArrayList<>());
}
deptDTO.getChildren().add(it);
}
} }
if(isChild) { if(isChild) {
depts.add(deptDTO); depts.add(deptDTO);
} else if(deptDTO.getPid() != null && !deptNames.contains(findById(deptDTO.getPid()).getName())) {
depts.add(deptDTO);
} }
} }
@ -249,19 +203,12 @@ public class DeptServiceImpl implements DeptService {
} }
} }
private void updateSubCnt(Long deptId){
if(deptId != null){
int count = deptRepository.countByPid(deptId);
deptRepository.updateSubCntById(count, deptId);
}
}
private List<DeptDto> deduplication(List<DeptDto> list) { private List<DeptDto> deduplication(List<DeptDto> list) {
List<DeptDto> deptDtos = new ArrayList<>(); List<DeptDto> deptDtos = new ArrayList<>();
for (DeptDto deptDto : list) { for (DeptDto deptDto : list) {
boolean flag = true; boolean flag = true;
for (DeptDto dto : list) { for (DeptDto dto : list) {
if (dto.getId().equals(deptDto.getPid())) { if (dto.getId().equals(deptDto.getId())) {
flag = false; flag = false;
break; break;
} }

View File

@ -113,7 +113,6 @@ public class RoleServiceImpl implements RoleService {
role.setName(resources.getName()); role.setName(resources.getName());
role.setDescription(resources.getDescription()); role.setDescription(resources.getDescription());
role.setDataScope(resources.getDataScope()); role.setDataScope(resources.getDataScope());
role.setDepts(resources.getDepts());
role.setLevel(resources.getLevel()); role.setLevel(resources.getLevel());
roleRepository.save(role); roleRepository.save(role);
// 更新相关缓存 // 更新相关缓存