feat(dept): 添加部门树形结构功能和相关API接口
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Yi.Framework.Ddd.Application;
|
||||
@@ -6,6 +7,7 @@ using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||
using Yi.Framework.Rbac.Domain.Entities;
|
||||
using Yi.Framework.Rbac.Domain.Repositories;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
||||
|
||||
namespace Yi.Framework.Rbac.Application.Services.System
|
||||
{
|
||||
@@ -78,5 +80,110 @@ namespace Yi.Framework.Rbac.Application.Services.System
|
||||
throw new UserFriendlyException(DeptConst.Exist);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 多查
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[Route("dept/list")]
|
||||
public async Task<List<DeptGetListOutputDto>> GetAllListAsync(DeptGetListInputVo input)
|
||||
{
|
||||
var entities = await _repository._DbQueryable
|
||||
.WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!))
|
||||
.WhereIF(input.State is not null, u => u.State == input.State)
|
||||
.OrderBy(u => u.OrderNum, OrderByType.Asc)
|
||||
.ToListAsync();
|
||||
|
||||
return await MapToGetListOutputDtosAsync(entities);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取树形结构的部门列表
|
||||
/// </summary>
|
||||
/// <returns>树形结构的部门列表</returns>
|
||||
public async Task<List<DeptTreeDto>> GetTreeAsync()
|
||||
{
|
||||
// 获取所有启用的部门
|
||||
var entities = await _repository._DbQueryable
|
||||
.Where(x => x.State == true)
|
||||
.OrderBy(x => x.OrderNum, OrderByType.Asc)
|
||||
.ToListAsync();
|
||||
return entities.DeptTreeBuild();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取排除指定部门及其子孙部门的部门列表
|
||||
/// </summary>
|
||||
/// <param name="id">要排除的部门ID</param>
|
||||
/// <returns>排除后的部门列表</returns>
|
||||
[HttpGet]
|
||||
[Route("dept/list/exclude/{id}")]
|
||||
public async Task<List<DeptGetListOutputDto>> GetListExcludeAsync(Guid id)
|
||||
{
|
||||
// 获取要排除的部门及其所有子孙部门的ID
|
||||
var excludeIds = await GetAllChildrenIdsAsync(id);
|
||||
excludeIds.Add(id); // 同时排除自己
|
||||
|
||||
// 查询并过滤掉排除的部门
|
||||
var result = await _repository._DbQueryable
|
||||
.Where(x => !excludeIds.Contains(x.Id))
|
||||
.OrderBy(dept => dept.OrderNum, OrderByType.Asc)
|
||||
.Select(dept => new DeptGetListOutputDto
|
||||
{
|
||||
Id = dept.Id,
|
||||
CreationTime = dept.CreationTime,
|
||||
CreatorId = dept.CreatorId,
|
||||
State = dept.State,
|
||||
DeptName = dept.DeptName,
|
||||
DeptCode = dept.DeptCode,
|
||||
Leader = dept.Leader,
|
||||
ParentId = dept.ParentId,
|
||||
Remark = dept.Remark,
|
||||
OrderNum = dept.OrderNum
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 递归获取指定部门的所有子孙部门ID
|
||||
/// </summary>
|
||||
/// <param name="deptId">部门ID</param>
|
||||
/// <returns>所有子孙部门ID列表</returns>
|
||||
private async Task<List<Guid>> GetAllChildrenIdsAsync(Guid deptId)
|
||||
{
|
||||
var result = new List<Guid>();
|
||||
|
||||
// 获取所有部门
|
||||
var allDepts = await _repository._DbQueryable.ToListAsync();
|
||||
|
||||
// 递归获取子孙部门ID
|
||||
GetChildrenIdsRecursive(deptId, allDepts, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 递归辅助方法:获取子孙部门ID
|
||||
/// </summary>
|
||||
/// <param name="parentId">父部门ID</param>
|
||||
/// <param name="allDepts">所有部门列表</param>
|
||||
/// <param name="result">结果列表</param>
|
||||
private void GetChildrenIdsRecursive(Guid parentId, List<DeptAggregateRoot> allDepts, List<Guid> result)
|
||||
{
|
||||
// 查找直接子部门
|
||||
var children = allDepts.Where(x => x.ParentId == parentId).ToList();
|
||||
|
||||
foreach (var child in children)
|
||||
{
|
||||
// 添加子部门ID
|
||||
result.Add(child.Id);
|
||||
|
||||
// 递归获取子部门的子部门
|
||||
GetChildrenIdsRecursive(child.Id, allDepts, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.Collections.Generic;
|
||||
using static Yi.Framework.Core.Helper.TreeHelper;
|
||||
|
||||
namespace Yi.Framework.Rbac.Domain.Shared.Dtos
|
||||
{
|
||||
/// <summary>
|
||||
/// 部门树形DTO
|
||||
/// </summary>
|
||||
public class DeptTreeDto : ITreeModel<DeptTreeDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// 部门ID
|
||||
/// </summary>
|
||||
public Guid Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 父部门ID
|
||||
/// </summary>
|
||||
public Guid ParentId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 排序号
|
||||
/// </summary>
|
||||
public int OrderNum { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 部门名称
|
||||
/// </summary>
|
||||
public string DeptName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
public bool State { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 子部门列表
|
||||
/// </summary>
|
||||
public List<DeptTreeDto>? Children { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ using Volo.Abp;
|
||||
using Volo.Abp.Auditing;
|
||||
using Volo.Abp.Domain.Entities;
|
||||
using Yi.Framework.Core.Data;
|
||||
using Yi.Framework.Core.Helper;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
||||
|
||||
namespace Yi.Framework.Rbac.Domain.Entities
|
||||
{
|
||||
@@ -87,4 +89,40 @@ namespace Yi.Framework.Rbac.Domain.Entities
|
||||
public string? Remark { get; set; }
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 部门实体扩展
|
||||
/// </summary>
|
||||
public static class DeptEntityExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 构建部门树形列表
|
||||
/// </summary>
|
||||
/// <param name="depts">部门列表</param>
|
||||
/// <returns>树形结构的部门列表</returns>
|
||||
public static List<DeptTreeDto> DeptTreeBuild(this List<DeptAggregateRoot> depts)
|
||||
{
|
||||
// 过滤启用的部门
|
||||
var filteredDepts = depts
|
||||
.Where(d => d.State == true)
|
||||
.ToList();
|
||||
|
||||
List<DeptTreeDto> deptTrees = new();
|
||||
foreach (var dept in filteredDepts)
|
||||
{
|
||||
var deptTree = new DeptTreeDto
|
||||
{
|
||||
Id = dept.Id,
|
||||
OrderNum = dept.OrderNum,
|
||||
DeptName = dept.DeptName,
|
||||
State = dept.State,
|
||||
ParentId = dept.ParentId,
|
||||
};
|
||||
|
||||
deptTrees.Add(deptTree);
|
||||
}
|
||||
|
||||
return TreeHelper.SetTree(deptTrees);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user