部门添加获取全名称(包含上级部门的名称)的方法

This commit is contained in:
Wang Chen Chen
2024-08-08 11:36:08 +08:00
parent 7f2d6f2757
commit 3dee74397a
6 changed files with 130 additions and 3 deletions

View File

@@ -24,6 +24,13 @@ public interface ApiPmsDeptService {
Map<Long, PmsDeptDTO> mapByDeptIds(Set<Long> deptIds);
/**
* 根据 部门ID 查询 部门的全名称
* 如: Google中国/研发部/web前端组
*/
Map<Long, PmsDeptDTO> mapFullNameByDeptIds(Set<Long> deptIds);
/**
* 获取 子部门
*/

View File

@@ -1,5 +1,13 @@
package com.xaaef.molly.internal.dto;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xaaef.molly.common.consts.RegexConst;
import com.xaaef.molly.common.valid.ValidCreate;
import com.xaaef.molly.common.valid.ValidUpdate;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.*;
import lombok.experimental.Accessors;
@@ -25,41 +33,58 @@ public class PmsDeptDTO implements java.io.Serializable {
/**
* 部门 ID
*/
@Schema(description = "部门ID")
@NotNull(message = "部门ID,必须填写", groups = {ValidUpdate.class})
private Long deptId;
/**
* 部门上级
*/
@Schema(description = "上级部门ID")
@NotNull(message = "上级部门ID,必须填写", groups = {ValidCreate.class})
private Long parentId;
/**
* 部门 名称
*/
@Schema(description = "部门名称")
@NotBlank(message = "部门名称,必须填写", groups = {ValidCreate.class})
private String deptName;
/**
* 部门 领导名称
*/
@Schema(description = "领导名称")
@NotBlank(message = "领导名称,必须填写", groups = {ValidCreate.class})
private String leader;
/**
* 部门 领导手机号
*/
@Schema(description = "领导手机号")
@NotBlank(message = "领导手机号,必须填写", groups = {ValidCreate.class})
@Pattern(regexp = RegexConst.MOBILE, message = "领导手机号,格式错误", groups = {ValidCreate.class, ValidUpdate.class})
private String leaderMobile;
/**
* 排序
*/
@Schema(description = "排序")
@NotNull(message = "部门排序,必须填写", groups = {ValidCreate.class})
private Long sort;
/**
* 部门描述
*/
@Schema(description = "部门描述")
@NotBlank(message = "部门描述,必须填写", groups = {ValidCreate.class})
private String description;
/**
* 祖级列表
*/
@Schema(description = "祖级列表")
@JsonIgnore
private String ancestors;
}

View File

@@ -80,7 +80,7 @@ public class CmsProjectServiceImpl extends BaseServiceImpl<CmsProjectMapper, Cms
if (deptIds.isEmpty()) {
return;
}
var mapDept = apiPmsDeptService.mapByDeptIds(deptIds);
var mapDept = apiPmsDeptService.mapFullNameByDeptIds(deptIds);
list.forEach(p -> {
var dept = mapDept.getOrDefault(p.getDeptId(), null);
p.setDept(dept);

View File

@@ -54,6 +54,19 @@ public class ApiPmsDeptServiceImpl implements ApiPmsDeptService {
}
@Override
public Map<Long, PmsDeptDTO> mapFullNameByDeptIds(Set<Long> deptIds) {
return deptService.listDeptFullName(deptIds)
.stream()
.map(source -> {
var target = new PmsDeptDTO();
BeanUtils.copyProperties(source, target);
return target;
})
.collect(Collectors.toMap(PmsDeptDTO::getDeptId, a -> a));
}
@Override
public Set<Long> listChildIdByDeptId(Long deptId) {
return deptService.listChildIdByDeptId(deptId);

View File

@@ -7,6 +7,7 @@ import com.xaaef.molly.perms.entity.PmsDept;
import com.xaaef.molly.tenant.base.service.BaseService;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
@@ -50,4 +51,17 @@ public interface PmsDeptService extends BaseService<PmsDept> {
Set<PmsDept> listChildByDeptId(Long deptId);
/**
* 根据 部门ID 查询 部门的全名称
* 如: Google中国/研发部/web前端组
*/
Map<Long, String> mapDeptFullName(Set<Long> deptIds);
/**
* 根据 部门ID 查询 部门的全名称
* 如: Google中国/研发部/web前端组
*/
List<PmsDept> listDeptFullName(Set<Long> deptIds);
}

View File

@@ -2,10 +2,12 @@ package com.xaaef.molly.perms.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -26,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
@@ -107,6 +110,71 @@ public class PmsDeptServiceImpl extends BaseServiceImpl<PmsDeptMapper, PmsDept>
}
@Override
public Map<Long, String> mapDeptFullName(Set<Long> deptIds) {
var w1 = new LambdaQueryWrapper<PmsDept>()
.select(List.of(PmsDept::getDeptId, PmsDept::getDeptName, PmsDept::getAncestors))
.in(PmsDept::getDeptId, deptIds);
var deptList = super.list(w1);
includeParentName(deptList);
return deptList.stream()
.collect(
Collectors.toMap(
PmsDept::getDeptId,
PmsDept::getDeptName,
(o1, o2) -> o2, HashMap::new
)
);
}
@Override
public List<PmsDept> listDeptFullName(Set<Long> deptIds) {
var deptList = super.listByIds(deptIds);
includeParentName(deptList);
return deptList;
}
private void includeParentName(Collection<PmsDept> deptList) {
if (CollectionUtil.isNotEmpty(deptList)) {
// 获取全部 祖先ID
var ancestors = deptList.stream()
.filter(a -> StrUtil.isNotBlank(a.getAncestors()))
.map(a -> a.getAncestors().split(StrUtil.COMMA))
.flatMap(Stream::of)
.filter(StrUtil::isNotBlank)
.map(Long::parseLong)
.filter(a -> a > 0)
.collect(Collectors.toSet());
if (!ancestors.isEmpty()) {
var w1 = new LambdaQueryWrapper<PmsDept>()
.select(List.of(PmsDept::getDeptId, PmsDept::getDeptName))
.in(PmsDept::getDeptId, ancestors);
var nameMaps = new HashMap<Long, String>();
deptList.forEach(a -> nameMaps.put(a.getDeptId(), a.getDeptName()));
baseMapper.selectList(w1).forEach(a -> nameMaps.put(a.getDeptId(), a.getDeptName()));
if (!nameMaps.isEmpty()) {
deptList.forEach(d -> {
if (StrUtil.isNotBlank(d.getAncestors())) {
var parentIds = Arrays.stream(d.getAncestors().split(StrUtil.COMMA))
.map(Long::parseLong)
.filter(a -> a > 0)
.collect(Collectors.toCollection(LinkedHashSet::new));
parentIds.add(d.getDeptId());
var fullName = parentIds.stream()
.map(nameMaps::get)
.filter(StrUtil::isNotBlank)
.collect(Collectors.joining(StrUtil.SLASH));
d.setDeptName(fullName);
}
});
}
}
}
}
@Transactional(rollbackFor = Exception.class)
@Override
public boolean save(PmsDept entity) {
@@ -117,7 +185,7 @@ public class PmsDeptServiceImpl extends BaseServiceImpl<PmsDeptMapper, PmsDept>
if (parentDept == null) {
throw new BizException(String.format("上级部门 %d 不存在!", entity.getParentId()));
}
entity.setAncestors(parentDept.getAncestors() + "," + entity.getParentId());
entity.setAncestors(parentDept.getAncestors() + StrUtil.COMMA + entity.getParentId());
}
entity.setDeptId(null);
return super.save(entity);
@@ -155,7 +223,7 @@ public class PmsDeptServiceImpl extends BaseServiceImpl<PmsDeptMapper, PmsDept>
var newParentDept = getById(entity.getParentId());
var oldDept = getById(entity.getDeptId());
if (Objects.nonNull(newParentDept) && Objects.nonNull(oldDept)) {
var newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
var newAncestors = newParentDept.getAncestors() + StrUtil.COMMA + newParentDept.getDeptId();
var oldAncestors = oldDept.getAncestors();
entity.setAncestors(newAncestors);
updateDeptChildren(entity.getDeptId(), newAncestors, oldAncestors);