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

View File

@ -59,13 +59,6 @@ public class Role extends BaseEntity implements Serializable {
@ApiModelProperty(value = "菜单", hidden = true)
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
@ApiModelProperty(value = "名称", hidden = true)
private String name;

View File

@ -29,19 +29,6 @@ import java.util.Set;
*/
public interface DeptRepository extends JpaRepository<Dept, Long>, JpaSpecificationExecutor<Dept> {
/**
* PID
* @param id pid
* @return /
*/
List<Dept> findByPid(Long id);
/**
*
* @return /
*/
List<Dept> findByPidIsNull();
/**
* 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)
Set<Dept> findByRoleId(Long roleId);
/**
*
* @param pid /
* @return /
*/
int countByPid(Long pid);
/**
* IDsub_count
* @param count /

View File

@ -74,11 +74,6 @@ public class DeptController {
DeptDto deptDto = deptService.findById(id);
List<DeptDto> depts = deptService.getSuperior(deptDto, new ArrayList<>());
if(exclude){
for (DeptDto dept : depts) {
if(dept.getId().equals(deptDto.getPid())) {
dept.setSubCount(dept.getSubCount() - 1);
}
}
// 编辑部门时不显示自己以及自己下级的数据避免出现PID数据环形问题
depts = depts.stream().filter(i -> !ids.contains(i.getId())).collect(Collectors.toList());
}
@ -114,13 +109,6 @@ public class DeptController {
@PreAuthorize("@el.check('dept:del')")
public ResponseEntity<Object> deleteDept(@RequestBody Set<Long> ids){
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.delete(deptDtos);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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