feat: 部门树

dev
岑溪 2026-07-07 23:54:01 +08:00
parent b58642a276
commit e0617333b3
7 changed files with 103 additions and 10 deletions

View File

@ -17,6 +17,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* Controller
@ -61,4 +62,10 @@ public class OrgDepartmentController {
public PageResponse<OrgDepartmentCO> page(@Validated OrgDepartmentPageQuery query) {
return orgDepartmentApi.page(query);
}
@ApiOperation("部门树列表")
@GetMapping("/tree")
public SingleResponse<List<OrgDepartmentCO>> tree(@Validated OrgDepartmentPageQuery query) {
return orgDepartmentApi.tree(query);
}
}

View File

@ -17,6 +17,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* App
*
@ -77,7 +83,6 @@ public class OrgDepartmentExecutor implements OrgDepartmentApi {
domainQuery.setPageSize(query.getSize());
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(ThreadLocalUserInfoAdapter.getOrgId());
domainQuery.setParentId(query.getParentId());
domainQuery.setDeptName(query.getDeptName());
PageResult<OrgDepartmentEntity> pageResult = orgDepartmentDomainService.page(domainQuery);
@ -89,6 +94,58 @@ public class OrgDepartmentExecutor implements OrgDepartmentApi {
pageResult.getTotal());
}
@Override
public SingleResponse<List<OrgDepartmentCO>> tree(OrgDepartmentPageQuery query) {
OrgDepartmentQuery domainQuery = new OrgDepartmentQuery();
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
domainQuery.setOrgId(query.getOrgId() == null ? ThreadLocalUserInfoAdapter.get() : query.getOrgId());
domainQuery.setDeptName(query.getDeptName());
List<OrgDepartmentEntity> matched = orgDepartmentDomainService.list(domainQuery);
Map<Long, OrgDepartmentEntity> nodeMap = new LinkedHashMap<>();
for (OrgDepartmentEntity entity : matched) {
collectWithAncestors(entity, nodeMap);
}
return SingleResponse.success(buildTree(nodeMap.values()));
}
private void collectWithAncestors(OrgDepartmentEntity entity, Map<Long, OrgDepartmentEntity> nodeMap) {
OrgDepartmentEntity current = entity;
while (current != null && current.getId() != null) {
if (nodeMap.containsKey(current.getId())) {
return;
}
nodeMap.put(current.getId(), current);
Long parentId = current.getParentId();
if (parentId == null || parentId == 0L) {
return;
}
current = orgDepartmentDomainService.get(parentId);
}
}
private List<OrgDepartmentCO> buildTree(Collection<OrgDepartmentEntity> entities) {
Map<String, OrgDepartmentCO> idToCo = new LinkedHashMap<>();
for (OrgDepartmentEntity entity : entities) {
idToCo.put(String.valueOf(entity.getId()), toCO(entity));
}
List<OrgDepartmentCO> roots = new ArrayList<>();
for (OrgDepartmentCO co : idToCo.values()) {
String parentId = co.getParentId();
if (parentId == null || "0".equals(parentId) || !idToCo.containsKey(parentId)) {
roots.add(co);
continue;
}
OrgDepartmentCO parent = idToCo.get(parentId);
if (parent.getChildren() == null) {
parent.setChildren(new ArrayList<>());
}
parent.getChildren().add(co);
}
return roots;
}
private OrgDepartmentCO toCO(OrgDepartmentEntity entity) {
if (entity == null) {
return null;

View File

@ -7,6 +7,8 @@ import org.qinan.safetyeval.client.dto.OrgDepartmentAddCmd;
import org.qinan.safetyeval.client.dto.OrgDepartmentModifyCmd;
import org.qinan.safetyeval.client.dto.OrgDepartmentPageQuery;
import java.util.List;
/**
* Client
*
@ -23,4 +25,6 @@ public interface OrgDepartmentApi {
SingleResponse<Void> delete(Long id);
PageResponse<OrgDepartmentCO> page(OrgDepartmentPageQuery query);
SingleResponse<List<OrgDepartmentCO>> tree(OrgDepartmentPageQuery query);
}

View File

@ -3,6 +3,8 @@ package org.qinan.safetyeval.client.co;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* COClient Object
*
@ -34,4 +36,7 @@ public class OrgDepartmentCO {
@ApiModelProperty(value = "租户ID")
private String tenantId;
@ApiModelProperty(value = "子部门")
private List<OrgDepartmentCO> children;
}

View File

@ -4,6 +4,8 @@ import org.qinan.safetyeval.domain.entity.OrgDepartmentEntity;
import org.qinan.safetyeval.domain.query.OrgDepartmentQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import java.util.List;
/**
* GatewayDomain
*
@ -20,4 +22,6 @@ public interface OrgDepartmentGateway {
void delete(Long id);
PageResult<OrgDepartmentEntity> page(OrgDepartmentQuery query);
List<OrgDepartmentEntity> list(OrgDepartmentQuery query);
}

View File

@ -7,6 +7,8 @@ import org.qinan.safetyeval.domain.gateway.OrgDepartmentGateway;
import org.qinan.safetyeval.domain.query.OrgDepartmentQuery;
import org.qinan.safetyeval.domain.query.PageResult;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@ -48,4 +50,8 @@ public class OrgDepartmentDomainService {
public PageResult<OrgDepartmentEntity> page(OrgDepartmentQuery query) {
return orgDepartmentGateway.page(query);
}
public List<OrgDepartmentEntity> list(OrgDepartmentQuery query) {
return orgDepartmentGateway.list(query);
}
}

View File

@ -63,6 +63,24 @@ public class OrgDepartmentGatewayImpl implements OrgDepartmentGateway {
@Override
public PageResult<OrgDepartmentEntity> page(OrgDepartmentQuery query) {
Page<OrgDepartmentDO> page = new Page<>(query.getPageNum(), query.getPageSize());
IPage<OrgDepartmentDO> result = orgDepartmentMapper.selectPage(page, buildQueryWrapper(query));
List<OrgDepartmentEntity> entities = result.getRecords().stream()
.map(this::toEntity)
.collect(Collectors.toList());
return PageResult.of(entities, result.getTotal(), result.getCurrent(), result.getSize());
}
@Override
public List<OrgDepartmentEntity> list(OrgDepartmentQuery query) {
return orgDepartmentMapper.selectList(buildQueryWrapper(query)).stream()
.map(this::toEntity)
.collect(Collectors.toList());
}
private LambdaQueryWrapper<OrgDepartmentDO> buildQueryWrapper(OrgDepartmentQuery query) {
LambdaQueryWrapper<OrgDepartmentDO> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(OrgDepartmentDO::getDeleteEnum, "false");
if (query.getOrgId() != null) {
@ -74,15 +92,7 @@ public class OrgDepartmentGatewayImpl implements OrgDepartmentGateway {
if (StringUtils.hasText(query.getDeptName())) {
wrapper.like(OrgDepartmentDO::getDeptName, query.getDeptName());
}
Page<OrgDepartmentDO> page = new Page<>(query.getPageNum(), query.getPageSize());
IPage<OrgDepartmentDO> result = orgDepartmentMapper.selectPage(page, wrapper);
List<OrgDepartmentEntity> entities = result.getRecords().stream()
.map(this::toEntity)
.collect(Collectors.toList());
return PageResult.of(entities, result.getTotal(), result.getCurrent(), result.getSize());
return wrapper;
}
private OrgDepartmentDO toDO(OrgDepartmentEntity entity) {