515fceeb
“wangming”
框架初始化
|
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Uow;
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Role;
using Yi.Framework.Rbac.Application.Contracts.Dtos.User;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Managers;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.Rbac.Domain.Shared.Enums;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Services.System
{
/// <summary>
/// Role服务实现
/// </summary>
public class RoleService : YiCrudAppService<RoleAggregateRoot, RoleGetOutputDto, RoleGetListOutputDto, Guid,
RoleGetListInputVo, RoleCreateInputVo, RoleUpdateInputVo>,
IRoleService
{
public RoleService(RoleManager roleManager, ISqlSugarRepository<RoleDeptEntity> roleDeptRepository,
ISqlSugarRepository<UserRoleEntity> userRoleRepository,
ISqlSugarRepository<RoleAggregateRoot, Guid> repository,
ISqlSugarRepository<MenuAggregateRoot, Guid> menuRepository,
ISqlSugarRepository<DeptAggregateRoot, Guid> deptRepository) : base(repository)
{
(_roleManager, _roleDeptRepository, _userRoleRepository, _repository, _menuRepository, _deptRepository) =
(roleManager, roleDeptRepository, userRoleRepository, repository, menuRepository, deptRepository);
}
private ISqlSugarRepository<RoleAggregateRoot, Guid> _repository;
private RoleManager _roleManager { get; set; }
private ISqlSugarRepository<RoleDeptEntity> _roleDeptRepository;
private ISqlSugarRepository<UserRoleEntity> _userRoleRepository;
private ISqlSugarRepository<MenuAggregateRoot, Guid> _menuRepository;
private ISqlSugarRepository<DeptAggregateRoot, Guid> _deptRepository;
public async Task UpdateDataScopeAsync(UpdateDataScopeInput input)
{
//只有自定义的需要特殊处理
if (input.DataScope == DataScopeEnum.CUSTOM)
{
await _roleDeptRepository.DeleteAsync(x => x.RoleId == input.RoleId);
var insertEntities = input.DeptIds.Select(x => new RoleDeptEntity { DeptId = x, RoleId = input.RoleId })
.ToList();
await _roleDeptRepository.InsertRangeAsync(insertEntities);
}
var entity = new RoleAggregateRoot() { DataScope = input.DataScope };
EntityHelper.TrySetId(entity, () => input.RoleId);
await _repository._Db.Updateable(entity).UpdateColumns(x => x.DataScope).ExecuteCommandAsync();
}
public override async Task<PagedResultDto<RoleGetListOutputDto>> GetListAsync(RoleGetListInputVo input)
{
RefAsync<int> total = 0;
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.RoleCode),
x => x.RoleCode.Contains(input.RoleCode!))
.WhereIF(!string.IsNullOrEmpty(input.RoleName), x => x.RoleName.Contains(input.RoleName!))
.WhereIF(input.State is not null, x => x.State == input.State)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<RoleGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
}
/// <summary>
/// 添加角色
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<RoleGetOutputDto> CreateAsync(RoleCreateInputVo input)
{
var isExist =
await _repository.IsAnyAsync(x => x.RoleCode == input.RoleCode || x.RoleName == input.RoleName);
if (isExist)
{
throw new UserFriendlyException(RoleConst.Exist);
}
var entity = await MapToEntityAsync(input);
await _repository.InsertAsync(entity);
var outputDto = await MapToGetOutputDtoAsync(entity);
return outputDto;
}
/// <summary>
/// 修改角色
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<RoleGetOutputDto> UpdateAsync(Guid id, RoleUpdateInputVo input)
{
var entity = await _repository.GetByIdAsync(id);
var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id).AnyAsync(x => x.RoleCode == input.RoleCode || x.RoleName == input.RoleName);
if (isExist)
{
throw new UserFriendlyException(RoleConst.Exist);
}
await MapToEntityAsync(input, entity);
await _repository.UpdateAsync(entity);
await _roleManager.GiveRoleSetMenuAsync(new List<Guid> { id }, input.MenuIds);
var dto = await MapToGetOutputDtoAsync(entity);
return dto;
}
/// <summary>
/// 更新状态
/// </summary>
/// <param name="id"></param>
/// <param name="state"></param>
/// <returns></returns>
[Route("role/{id}/{state}")]
public async Task<RoleGetOutputDto> UpdateStateAsync([FromRoute] Guid id, [FromRoute] bool state)
{
var entity = await _repository.GetByIdAsync(id);
if (entity is null)
{
throw new ApplicationException("角色未存在");
}
entity.State = state;
await _repository.UpdateAsync(entity);
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 获取角色下的用户
/// </summary>
/// <param name="roleId"></param>
/// <param name="input"></param>
/// <param name="isAllocated">是否在该角色下</param>
/// <returns></returns>
[Route("role/auth-user/{roleId}/{isAllocated}")]
public async Task<PagedResultDto<UserGetListOutputDto>> GetAuthUserByRoleIdAsync([FromRoute] Guid roleId,
[FromRoute] bool isAllocated, [FromQuery] RoleAuthUserGetListInput input)
{
PagedResultDto<UserGetListOutputDto> output;
//角色下已授权用户
if (isAllocated == true)
{
output = await GetAllocatedAuthUserByRoleIdAsync(roleId, input);
}
//角色下未授权用户
else
{
output = await GetNotAllocatedAuthUserByRoleIdAsync(roleId, input);
}
return output;
}
private async Task<PagedResultDto<UserGetListOutputDto>> GetAllocatedAuthUserByRoleIdAsync(Guid roleId,
RoleAuthUserGetListInput input)
{
RefAsync<int> total = 0;
var output = await _userRoleRepository._DbQueryable
.LeftJoin<UserAggregateRoot>((ur, u) => ur.UserId == u.Id && ur.RoleId == roleId)
.Where((ur, u) => ur.RoleId == roleId)
.WhereIF(!string.IsNullOrEmpty(input.UserName), (ur, u) => u.UserName.Contains(input.UserName))
.WhereIF(input.Phone is not null, (ur, u) => u.Phone.ToString().Contains(input.Phone.ToString()))
.Select((ur, u) => new UserGetListOutputDto { Id = u.Id }, true)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<UserGetListOutputDto>(total, output);
}
private async Task<PagedResultDto<UserGetListOutputDto>> GetNotAllocatedAuthUserByRoleIdAsync(Guid roleId,
RoleAuthUserGetListInput input)
{
RefAsync<int> total = 0;
var entities = await _userRoleRepository._Db.Queryable<UserAggregateRoot>()
.Where(u => SqlFunc.Subqueryable<UserRoleEntity>().Where(x => x.RoleId == roleId)
.Where(x => x.UserId == u.Id).NotAny())
.WhereIF(!string.IsNullOrEmpty(input.UserName), u => u.UserName.Contains(input.UserName))
.WhereIF(input.Phone is not null, u => u.Phone.ToString().Contains(input.Phone.ToString()))
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
var output = entities.Adapt<List<UserGetListOutputDto>>();
return new PagedResultDto<UserGetListOutputDto>(total, output);
}
/// <summary>
/// 批量给用户授权
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task CreateAuthUserAsync([FromBody] RoleAuthUserCreateOrDeleteInput input)
{
var userRoleEntities = input.UserIds.Select(u => new UserRoleEntity { RoleId = input.RoleId, UserId = u })
.ToList();
await _userRoleRepository.InsertRangeAsync(userRoleEntities);
}
/// <summary>
/// 批量取消授权
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task DeleteAuthUserAsync([FromBody] RoleAuthUserCreateOrDeleteInput input)
{
await _userRoleRepository._Db.Deleteable<UserRoleEntity>().Where(x => x.RoleId == input.RoleId)
.Where(x => input.UserIds.Contains(x.UserId))
.ExecuteCommandAsync();
;
}
/// <summary>
/// 获取角色菜单树
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public async Task<ActionResult> GetMenuTreeAsync(Guid roleId)
{
var checkedKeys = await _menuRepository._DbQueryable
.Where(m => SqlFunc.Subqueryable<RoleMenuEntity>().Where(rm => rm.RoleId == roleId && rm.MenuId == m.Id).Any())
.Select(x => x.Id).ToListAsync();
var menus = await _menuRepository._DbQueryable.ToListAsync();
var menuTrees = menus.TreeDtoBuild();
return new JsonResult(new
{
checkedKeys,
menus = menuTrees
});
}
/// <summary>
/// 获取角色部门树
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public async Task<ActionResult> GetDeptTreeAsync(Guid roleId)
{
var checkedKeys = await _deptRepository._DbQueryable
.Where(d => SqlFunc.Subqueryable<RoleDeptEntity>().Where(rd => rd.RoleId == roleId && rd.DeptId == d.Id).Any())
.Select(x => x.Id).ToListAsync();
var deptList = await _deptRepository._DbQueryable
.Where(x => x.State == true)
.OrderBy(x => x.OrderNum, OrderByType.Asc)
.ToListAsync();
return new JsonResult(new
{
checkedKeys,
depts = deptList.DeptTreeBuild()
});
}
}
}
|