feat: 部门树
parent
b58642a276
commit
e0617333b3
|
|
@ -17,6 +17,7 @@ import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门适配层(Controller)
|
* 部门适配层(Controller)
|
||||||
|
|
@ -61,4 +62,10 @@ public class OrgDepartmentController {
|
||||||
public PageResponse<OrgDepartmentCO> page(@Validated OrgDepartmentPageQuery query) {
|
public PageResponse<OrgDepartmentCO> page(@Validated OrgDepartmentPageQuery query) {
|
||||||
return orgDepartmentApi.page(query);
|
return orgDepartmentApi.page(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation("部门树列表")
|
||||||
|
@GetMapping("/tree")
|
||||||
|
public SingleResponse<List<OrgDepartmentCO>> tree(@Validated OrgDepartmentPageQuery query) {
|
||||||
|
return orgDepartmentApi.tree(query);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,12 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.stereotype.Service;
|
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层)
|
* 部门执行器(App层)
|
||||||
*
|
*
|
||||||
|
|
@ -77,7 +83,6 @@ public class OrgDepartmentExecutor implements OrgDepartmentApi {
|
||||||
domainQuery.setPageSize(query.getSize());
|
domainQuery.setPageSize(query.getSize());
|
||||||
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
|
domainQuery.setTenantId(AuthUserContextAdapter.getCurrentTenantId());
|
||||||
domainQuery.setOrgId(ThreadLocalUserInfoAdapter.getOrgId());
|
domainQuery.setOrgId(ThreadLocalUserInfoAdapter.getOrgId());
|
||||||
domainQuery.setParentId(query.getParentId());
|
|
||||||
domainQuery.setDeptName(query.getDeptName());
|
domainQuery.setDeptName(query.getDeptName());
|
||||||
|
|
||||||
PageResult<OrgDepartmentEntity> pageResult = orgDepartmentDomainService.page(domainQuery);
|
PageResult<OrgDepartmentEntity> pageResult = orgDepartmentDomainService.page(domainQuery);
|
||||||
|
|
@ -89,6 +94,58 @@ public class OrgDepartmentExecutor implements OrgDepartmentApi {
|
||||||
pageResult.getTotal());
|
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) {
|
private OrgDepartmentCO toCO(OrgDepartmentEntity entity) {
|
||||||
if (entity == null) {
|
if (entity == null) {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ import org.qinan.safetyeval.client.dto.OrgDepartmentAddCmd;
|
||||||
import org.qinan.safetyeval.client.dto.OrgDepartmentModifyCmd;
|
import org.qinan.safetyeval.client.dto.OrgDepartmentModifyCmd;
|
||||||
import org.qinan.safetyeval.client.dto.OrgDepartmentPageQuery;
|
import org.qinan.safetyeval.client.dto.OrgDepartmentPageQuery;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门接口定义(Client层)
|
* 部门接口定义(Client层)
|
||||||
*
|
*
|
||||||
|
|
@ -23,4 +25,6 @@ public interface OrgDepartmentApi {
|
||||||
SingleResponse<Void> delete(Long id);
|
SingleResponse<Void> delete(Long id);
|
||||||
|
|
||||||
PageResponse<OrgDepartmentCO> page(OrgDepartmentPageQuery query);
|
PageResponse<OrgDepartmentCO> page(OrgDepartmentPageQuery query);
|
||||||
|
|
||||||
|
SingleResponse<List<OrgDepartmentCO>> tree(OrgDepartmentPageQuery query);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ package org.qinan.safetyeval.client.co;
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门CO(Client Object)
|
* 部门CO(Client Object)
|
||||||
*
|
*
|
||||||
|
|
@ -34,4 +36,7 @@ public class OrgDepartmentCO {
|
||||||
|
|
||||||
@ApiModelProperty(value = "租户ID")
|
@ApiModelProperty(value = "租户ID")
|
||||||
private String tenantId;
|
private String tenantId;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "子部门")
|
||||||
|
private List<OrgDepartmentCO> children;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ import org.qinan.safetyeval.domain.entity.OrgDepartmentEntity;
|
||||||
import org.qinan.safetyeval.domain.query.OrgDepartmentQuery;
|
import org.qinan.safetyeval.domain.query.OrgDepartmentQuery;
|
||||||
import org.qinan.safetyeval.domain.query.PageResult;
|
import org.qinan.safetyeval.domain.query.PageResult;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 部门Gateway接口(Domain层定义)
|
* 部门Gateway接口(Domain层定义)
|
||||||
*
|
*
|
||||||
|
|
@ -20,4 +22,6 @@ public interface OrgDepartmentGateway {
|
||||||
void delete(Long id);
|
void delete(Long id);
|
||||||
|
|
||||||
PageResult<OrgDepartmentEntity> page(OrgDepartmentQuery query);
|
PageResult<OrgDepartmentEntity> page(OrgDepartmentQuery query);
|
||||||
|
|
||||||
|
List<OrgDepartmentEntity> list(OrgDepartmentQuery query);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ import org.qinan.safetyeval.domain.gateway.OrgDepartmentGateway;
|
||||||
import org.qinan.safetyeval.domain.query.OrgDepartmentQuery;
|
import org.qinan.safetyeval.domain.query.OrgDepartmentQuery;
|
||||||
import org.qinan.safetyeval.domain.query.PageResult;
|
import org.qinan.safetyeval.domain.query.PageResult;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
|
@ -48,4 +50,8 @@ public class OrgDepartmentDomainService {
|
||||||
public PageResult<OrgDepartmentEntity> page(OrgDepartmentQuery query) {
|
public PageResult<OrgDepartmentEntity> page(OrgDepartmentQuery query) {
|
||||||
return orgDepartmentGateway.page(query);
|
return orgDepartmentGateway.page(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<OrgDepartmentEntity> list(OrgDepartmentQuery query) {
|
||||||
|
return orgDepartmentGateway.list(query);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,24 @@ public class OrgDepartmentGatewayImpl implements OrgDepartmentGateway {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageResult<OrgDepartmentEntity> page(OrgDepartmentQuery query) {
|
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<>();
|
LambdaQueryWrapper<OrgDepartmentDO> wrapper = new LambdaQueryWrapper<>();
|
||||||
wrapper.eq(OrgDepartmentDO::getDeleteEnum, "false");
|
wrapper.eq(OrgDepartmentDO::getDeleteEnum, "false");
|
||||||
if (query.getOrgId() != null) {
|
if (query.getOrgId() != null) {
|
||||||
|
|
@ -74,15 +92,7 @@ public class OrgDepartmentGatewayImpl implements OrgDepartmentGateway {
|
||||||
if (StringUtils.hasText(query.getDeptName())) {
|
if (StringUtils.hasText(query.getDeptName())) {
|
||||||
wrapper.like(OrgDepartmentDO::getDeptName, query.getDeptName());
|
wrapper.like(OrgDepartmentDO::getDeptName, query.getDeptName());
|
||||||
}
|
}
|
||||||
|
return wrapper;
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private OrgDepartmentDO toDO(OrgDepartmentEntity entity) {
|
private OrgDepartmentDO toDO(OrgDepartmentEntity entity) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue