DeptService.cs
6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Volo.Abp.Application.Dtos;
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Dept;
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
{
/// <summary>
/// Dept服务实现
/// </summary>
public class DeptService : YiCrudAppService<DeptAggregateRoot, DeptGetOutputDto, DeptGetListOutputDto, Guid,
DeptGetListInputVo, DeptCreateInputVo, DeptUpdateInputVo>, IDeptService
{
private IDeptRepository _repository;
public DeptService(IDeptRepository repository) : base(repository)
{
_repository = repository;
}
[RemoteService(false)]
public async Task<List<Guid>> GetChildListAsync(Guid deptId)
{
return await _repository.GetChildListAsync(deptId);
}
/// <summary>
/// 通过角色id查询该角色全部部门
/// </summary>
/// <returns></returns>
//[Route("{roleId}")]
public async Task<List<DeptGetListOutputDto>> GetRoleIdAsync(Guid roleId)
{
var entities = await _repository.GetListRoleIdAsync(roleId);
return await MapToGetListOutputDtosAsync(entities);
}
/// <summary>
/// 多查
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<PagedResultDto<DeptGetListOutputDto>> GetListAsync(DeptGetListInputVo input)
{
RefAsync<int> total = 0;
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 new PagedResultDto<DeptGetListOutputDto>
{
Items = await MapToGetListOutputDtosAsync(entities),
TotalCount = total
};
}
protected override async Task CheckCreateInputDtoAsync(DeptCreateInputVo input)
{
var isExist =
await _repository.IsAnyAsync(x => x.DeptCode == input.DeptCode);
if (isExist)
{
throw new UserFriendlyException(DeptConst.Exist);
}
}
protected override async Task CheckUpdateInputDtoAsync(DeptAggregateRoot entity, DeptUpdateInputVo input)
{
var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id)
.AnyAsync(x => x.DeptCode == input.DeptCode);
if (isExist)
{
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);
}
}
}
}