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
266
267
|
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Caching;
using Volo.Abp.EventBus.Local;
using Volo.Abp.Users;
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.User;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Authorization;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Entities.ValueObjects;
using Yi.Framework.Rbac.Domain.Managers;
using Yi.Framework.Rbac.Domain.Repositories;
using Yi.Framework.Rbac.Domain.Shared.Caches;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.Rbac.Domain.Shared.OperLog;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Services.System
{
/// <summary>
/// User服务实现
/// </summary>
public class UserService : YiCrudAppService<UserAggregateRoot, UserGetOutputDto, UserGetListOutputDto, Guid,
UserGetListInputVo, UserCreateInputVo, UserUpdateInputVo>, IUserService
//IUserService
{
protected ILocalEventBus LocalEventBus => LazyServiceProvider.LazyGetRequiredService<ILocalEventBus>();
public UserService(ISqlSugarRepository<UserAggregateRoot, Guid> repository, UserManager userManager,
IUserRepository userRepository, ICurrentUser currentUser, IDeptService deptService,
ILocalEventBus localEventBus,
IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache) : base(repository)
=>
(_userManager, _userRepository, _currentUser, _deptService, _repository, _localEventBus) =
(userManager, userRepository, currentUser, deptService, repository, localEventBus);
private UserManager _userManager { get; set; }
private ISqlSugarRepository<UserAggregateRoot, Guid> _repository;
private IUserRepository _userRepository { get; set; }
private IDeptService _deptService { get; set; }
private ICurrentUser _currentUser { get; set; }
private ILocalEventBus _localEventBus;
/// <summary>
/// 查询用户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[Permission("system:user:list")]
public override async Task<PagedResultDto<UserGetListOutputDto>> GetListAsync(UserGetListInputVo input)
{
RefAsync<int> total = 0;
List<Guid> deptIds = null;
if (input.DeptId is not null)
{
deptIds = await _deptService.GetChildListAsync(input.DeptId ?? Guid.Empty);
}
List<Guid> ids = input.Ids?.Split(",").Select(x => Guid.Parse(x)).ToList();
var outPut = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.UserName),
x => x.UserName.Contains(input.UserName!))
.WhereIF(input.Phone is not null, x => x.Phone.ToString()!.Contains(input.Phone.ToString()!))
.WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name!.Contains(input.Name!))
.WhereIF(input.State is not null, x => x.State == input.State)
.WhereIF(input.StartTime is not null && input.EndTime is not null,
x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
//这个为过滤当前部门,加入数据权限后,将由数据权限控制
.WhereIF(input.DeptId is not null, x => deptIds.Contains(x.DeptId ?? Guid.Empty))
.WhereIF(ids is not null, x => ids.Contains(x.Id))
.LeftJoin<DeptAggregateRoot>((user, dept) => user.DeptId == dept.Id)
.OrderByDescending(user => user.CreationTime)
.Select((user, dept) => new UserGetListOutputDto(), true)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
var result = new PagedResultDto<UserGetListOutputDto>();
result.Items = outPut;
result.TotalCount = total;
return result;
}
protected override UserAggregateRoot MapToEntity(UserCreateInputVo createInput)
{
var output = base.MapToEntity(createInput);
output.EncryPassword = new EncryPasswordValueObject(createInput.Password);
return output;
}
/// <summary>
/// 添加用户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[OperLog("添加用户", OperEnum.Insert)]
[Permission("system:user:add")]
public async override Task<UserGetOutputDto> CreateAsync(UserCreateInputVo input)
{
var entitiy = await MapToEntityAsync(input);
await _userManager.CreateAsync(entitiy);
await _userManager.GiveUserSetRoleAsync(new List<Guid> { entitiy.Id }, input.RoleIds);
await _userManager.GiveUserSetPostAsync(new List<Guid> { entitiy.Id }, input.PostIds);
var result = await MapToGetOutputDtoAsync(entitiy);
return result;
}
protected override async Task<UserAggregateRoot> MapToEntityAsync(UserCreateInputVo createInput)
{
var entitiy = await base.MapToEntityAsync(createInput);
entitiy.BuildPassword();
return entitiy;
}
/// <summary>
/// 单查
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[Permission("system:user:list")]
public override async Task<UserGetOutputDto> GetAsync(Guid id)
{
//使用导航树形查询
var entity = await _repository._DbQueryable.Includes(u => u.Roles).Includes(u => u.Posts)
.Includes(u => u.Dept).InSingleAsync(id);
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 更新用户
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
[OperLog("更新用户", OperEnum.Update)]
[Permission("system:user:edit")]
public async override Task<UserGetOutputDto> UpdateAsync(Guid id, UserUpdateInputVo input)
{
if (input.UserName == UserConst.Admin || input.UserName == UserConst.TenantAdmin)
{
throw new UserFriendlyException(UserConst.Name_Not_Allowed);
}
if (await _repository.IsAnyAsync(u => input.UserName!.Equals(u.UserName) && !id.Equals(u.Id)))
{
throw new UserFriendlyException(UserConst.Exist);
}
var entity = await _repository.GetByIdAsync(id);
//更新密码,特殊处理
if (!string.IsNullOrWhiteSpace(input.Password))
{
entity.EncryPassword.Password = input.Password;
entity.BuildPassword();
}
await MapToEntityAsync(input, entity);
var res1 = await _repository.UpdateAsync(entity);
await _userManager.GiveUserSetRoleAsync(new List<Guid> { id }, input.RoleIds);
await _userManager.GiveUserSetPostAsync(new List<Guid> { id }, input.PostIds);
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 更新个人中心
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[OperLog("更新个人信息", OperEnum.Update)]
public async Task<UserGetOutputDto> UpdateProfileAsync(ProfileUpdateInputVo input)
{
var entity = await _repository.GetByIdAsync(_currentUser.Id);
ObjectMapper.Map(input, entity);
await _repository.UpdateAsync(entity);
var dto = await MapToGetOutputDtoAsync(entity);
//发布更新昵称任务事件
// if (input.Nick != entity.Icon)
// {
// await this.LocalEventBus.PublishAsync(
// new AssignmentEventArgs(AssignmentRequirementTypeEnum.UpdateNick, _currentUser.GetId(), input.Nick),
// false);
// }
return dto;
}
/// <summary>
/// 更新状态
/// </summary>
/// <param name="id"></param>
/// <param name="state"></param>
/// <returns></returns>
[Route("user/{id}/{state}")]
[OperLog("更新用户状态", OperEnum.Update)]
[Permission("system:user:update")]
public async Task<UserGetOutputDto> 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);
}
[OperLog("删除用户", OperEnum.Delete)]
[Permission("system:user:delete")]
public override async Task DeleteAsync(Guid id)
{
await base.DeleteAsync(id);
}
[Permission("system:user:export")]
public override Task<IActionResult> GetExportExcelAsync(UserGetListInputVo input)
{
return base.GetExportExcelAsync(input);
}
[Permission("system:user:import")]
public override Task PostImportExcelAsync(List<UserCreateInputVo> input)
{
return base.PostImportExcelAsync(input);
}
/// <summary>
/// 获取指定部门及其所有子部门下的用户列表
/// </summary>
/// <param name="deptId">部门ID</param>
/// <returns>用户列表</returns>
[HttpGet]
[Route("user/dept/{deptId}")]
[Permission("system:user:list")]
public async Task<List<UserGetListOutputDto>> GetUsersByDeptAsync(Guid deptId)
{
// 获取当前部门及其所有子部门的ID列表
var deptIds = await _deptService.GetChildListAsync(deptId);
// 将当前部门ID也加入列表
if (!deptIds.Contains(deptId))
{
deptIds.Add(deptId);
}
// 查询所有这些部门下的用户
var users = await _repository._DbQueryable
.Where(u => deptIds.Contains(u.DeptId ?? Guid.Empty))
.LeftJoin<DeptAggregateRoot>((user, dept) => user.DeptId == dept.Id)
.OrderByDescending(user => user.CreationTime)
.Select((user, dept) => new UserGetListOutputDto(), true)
.ToListAsync();
return users;
}
}
}
|