UserService.cs
21.2 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Code;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.FriendlyException;
using NCC.Extend.Interfaces.User;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NCC.System.Entitys.Permission;
using NCC.Extend.Entitys.Dto.User;
using NCC.Common.Helper;
using NCC.JsonSerialization;
using NCC.Common.Model.NPOI;
using NCC.Common.Configuration;
using NCC.DataEncryption;
using NCC.ClayObject;
using NCC.Extend.Entitys.lq_jinsanjiao_user;
using NCC.Extend.Entitys.lq_mdxx;
using NCC.Extend.Entitys.lq_employee_profile;
using NCC.Extend.Entitys.Enum;
using NCC.System.Entitys.System;
namespace NCC.Extend.User
{
/// <summary>
/// 用户服务
/// </summary>
[ApiDescriptionSettings(Tag = "绿纤用户服务", Name = "User", Order = 200)]
[Route("api/Extend/[controller]")]
public class UserService : IUserService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<UserEntity> _userRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
/// <summary>
/// 初始化一个<see cref="UserService"/>类型的新实例
/// </summary>
public UserService(
ISqlSugarRepository<UserEntity> userRepository,
IUserManager userManager)
{
_userRepository = userRepository;
_db = _userRepository.Context;
_userManager = userManager;
}
private static readonly HashSet<string> CrossStoreGwExact = new HashSet<string>(StringComparer.Ordinal)
{
"科技老师",
"总经理",
"经理",
"董事长",
"总裁",
};
/// <summary>
/// 跨店/总部岗位在未传 mdid 时不按当前登录门店自动过滤(如科技老师供各门店选择)
/// </summary>
private static bool ShouldAutoFilterByCurrentStoreMdid(string gw)
{
if (string.IsNullOrWhiteSpace(gw))
return false;
return !CrossStoreGwExact.Contains(gw.Trim());
}
#region 获取用户列表
/// <summary>
/// 获取用户列表
/// </summary>
/// <remarks>
/// 员工 App「员工信息」与各端选人共用。列表含报道时间 reportDate。
/// crossStore=1 时不自动补当前登录门店;若同时传 mdid 则按该门店筛选。
///
/// 示例请求:
/// GET /api/Extend/User?currentPage=1&pageSize=20&crossStore=1&mdid=门店ID&onlyOnJob=1
/// </remarks>
/// <param name="input">分页与筛选(mdid / gw / organizeId / onlyOnJob / crossStore 等)</param>
/// <returns>分页用户列表</returns>
/// <response code="200">成功返回列表</response>
[HttpGet("")]
public async Task<dynamic> GetUserList([FromQuery] UserListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
DateTime? monthStart = null;
DateTime? monthLast = null;
if (!string.IsNullOrWhiteSpace(input.onJobMonth))
{
var parts = input.onJobMonth.Split('-');
if (parts.Length == 2 && int.TryParse(parts[0], out var year) && int.TryParse(parts[1], out var month))
{
if (month >= 1 && month <= 12)
{
monthStart = new DateTime(year, month, 1);
monthLast = new DateTime(year, month, DateTime.DaysInMonth(year, month), 23, 59, 59);
}
}
}
if (input.crossStore != 1
&& string.IsNullOrWhiteSpace(input.mdid)
&& !string.IsNullOrWhiteSpace(input.gw)
&& ShouldAutoFilterByCurrentStoreMdid(input.gw))
{
var currentUser = await _userManager.GetUserInfo();
if (currentUser != null && !string.IsNullOrWhiteSpace(currentUser.mdid))
{
input.mdid = currentUser.mdid;
}
}
var gwFilter = string.IsNullOrWhiteSpace(input.gw) ? null : input.gw.Trim();
var data = await _db.Queryable<UserEntity, OrganizeEntity, PositionEntity, RoleEntity, LqMdxxEntity>((u, o, p, r, m) => new JoinQueryInfos(
JoinType.Left, o.Id == SqlFunc.ToString(u.OrganizeId),
JoinType.Left, p.Id == SqlFunc.ToString(u.PositionId),
JoinType.Left, r.Id == SqlFunc.ToString(u.RoleId),
JoinType.Left, m.Id == u.Mdid))
.WhereIF(!string.IsNullOrEmpty(input.id), u => u.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.account), u => u.Account.Contains(input.account))
.WhereIF(!string.IsNullOrEmpty(input.realName), u => u.RealName.Contains(input.realName))
.WhereIF(!string.IsNullOrEmpty(input.mobilePhone), u => u.MobilePhone.Contains(input.mobilePhone))
.WhereIF(!string.IsNullOrEmpty(input.mdid), u => u.Mdid == input.mdid)
.WhereIF(!string.IsNullOrEmpty(input.zw), u => u.Zw.Contains(input.zw))
.WhereIF(!string.IsNullOrEmpty(gwFilter), u => u.Gw == gwFilter)
.WhereIF(!string.IsNullOrEmpty(input.gwfl), u => u.Gwfl.Contains(input.gwfl))
.WhereIF(!string.IsNullOrEmpty(input.fyft), u => u.Fyft.Contains(input.fyft))
.WhereIF(!string.IsNullOrEmpty(input.organizeId), u => u.OrganizeId == input.organizeId)
.WhereIF(!string.IsNullOrEmpty(input.positionId), u => u.PositionId == input.positionId)
.WhereIF(!string.IsNullOrEmpty(input.roleId), u => u.RoleId == input.roleId)
.WhereIF(input.enabledMark.HasValue, u => u.EnabledMark == input.enabledMark)
.WhereIF(input.onlyOnJob == 1, u => SqlFunc.IsNull(u.IsOnJob, 1) == 1)
.Where(u => u.DeleteMark == null) // 排除已删除的用户
.WhereIF(monthStart.HasValue && monthLast.HasValue, u =>
u.EntryDate != null && u.EntryDate <= monthLast.Value &&
(u.LeaveDate == null || u.LeaveDate >= monthStart.Value))
.Select((u, o, p, r, m) => new UserListOutput
{
id = u.Id,
account = u.Account,
realName = u.RealName,
nickName = u.NickName,
headIcon = u.HeadIcon,
gender = u.Gender,
birthday = u.Birthday,
mobilePhone = u.MobilePhone,
telePhone = u.TelePhone,
email = u.Email,
urgentContacts = u.UrgentContacts,
urgentTelePhone = u.UrgentTelePhone,
postalAddress = u.PostalAddress,
signature = u.Signature,
firstLogTime = u.FirstLogTime,
prevLogTime = u.PrevLogTime,
isAdministrator = u.IsAdministrator,
description = u.Description,
sortCode = u.SortCode,
managerId = u.ManagerId,
organizeId = u.OrganizeId,
organizeName = o.FullName,
positionId = u.PositionId,
positionName = p.FullName,
roleId = u.RoleId,
roleName = r.FullName,
portalId = u.PortalId,
extensionStr = u.ExtensionStr,
openId = u.OpenId,
mdid = u.Mdid,
mdmc = m.Dm,
zw = u.Zw,
fyft = u.Fyft,
gwfl = u.Gwfl,
gw = u.Gw,
enabledMark = u.EnabledMark,
creatorTime = u.CreatorTime,
creatorUserId = u.CreatorUserId,
entryDate = u.EntryDate,
leaveDate = u.LeaveDate,
reportDate = u.ReportDate
})
.MergeTable()
.OrderBy(sidx + " " + input.sort)
.ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<UserListOutput>.SqlSugarPageResult(data);
}
#endregion
#region 获取用户详细信息
/// <summary>
/// 获取用户详细信息(含档案扩展字段,敏感信息脱敏)
/// </summary>
[HttpGet("{id}")]
public async Task<UserEmployeeDetailOutput> GetUserInfo(string id)
{
var row = await _db.Queryable<UserEntity, OrganizeEntity, PositionEntity, RoleEntity, LqMdxxEntity>((u, o, p, r, m) => new JoinQueryInfos(
JoinType.Left, o.Id == SqlFunc.ToString(u.OrganizeId),
JoinType.Left, p.Id == SqlFunc.ToString(u.PositionId),
JoinType.Left, r.Id == SqlFunc.ToString(u.RoleId),
JoinType.Left, m.Id == u.Mdid))
.Where(u => u.Id == id && u.DeleteMark == null)
.Select((u, o, p, r, m) => new
{
user = u,
organizeName = o.FullName,
positionName = p.FullName,
roleName = r.FullName,
mdmc = m.Dm
})
.FirstAsync();
if (row?.user == null)
{
throw NCCException.Oh("用户不存在");
}
var profile = await _db.Queryable<LqEmployeeProfileEntity>()
.Where(x => x.UserId == id)
.FirstAsync();
var output = MapEmployeeDetail(row.user, row.organizeName, row.positionName, row.roleName, row.mdmc, profile);
await EnrichEmployeeDetailAsync(output, row.user);
return output;
}
#endregion
#region 获取用户下拉选择数据
/// <summary>
/// 获取用户下拉选择数据
/// </summary>
/// <param name="mdid">门店ID</param>
/// <param name="zw">职位</param>
/// <param name="gw">岗位</param>
/// <param name="onlyOnJob">是否仅在职:1=仅在职</param>
[HttpGet("Selector")]
public async Task<dynamic> GetUserSelector([FromQuery] string mdid = null, [FromQuery] string zw = null, [FromQuery] string gw = null, [FromQuery] int? onlyOnJob = null)
{
var data = await _db.Queryable<UserEntity>()
.WhereIF(!string.IsNullOrEmpty(mdid), u => u.Mdid == mdid)
.WhereIF(!string.IsNullOrEmpty(zw), u => u.Zw != null && u.Zw.Contains(zw))
.WhereIF(!string.IsNullOrEmpty(gw), u => u.Gw == gw)
.WhereIF(onlyOnJob == 1, u => SqlFunc.IsNull(u.IsOnJob, 1) == 1)
.Where(u => u.EnabledMark == 1 && u.DeleteMark == null)
.Select(u => new
{
id = u.Id,
account = u.Account,
realName = u.RealName,
mobilePhone = u.MobilePhone,
mdid = u.Mdid,
zw = u.Zw,
gw = u.Gw
})
.OrderBy(u => u.realName)
.ToListAsync();
return data;
}
#endregion
#region 获取总经理、经理下拉选择数据
/// <summary>
/// 获取用户下拉选择数据
/// </summary>
[HttpGet("ManagerSelector")]
public async Task<dynamic> GetManagerSelector()
{
var data = await _db.Queryable<UserEntity>()
.Where(u => u.Gw == "总经理" || u.Gw == "经理")
.Where(u => u.EnabledMark == 1 && u.DeleteMark == null)
.Select(u => new
{
id = u.Id,
account = u.Account,
realName = u.RealName,
mobilePhone = u.MobilePhone,
mdid = u.Mdid,
zw = u.Zw,
gw = u.Gw
})
.OrderBy(u => u.realName)
.ToListAsync();
return data;
}
#endregion
#region 员工筛选选项
/// <summary>
/// 获取员工列表筛选选项(岗位 / 组织 / 门店)
/// </summary>
/// <remarks>
/// 供员工 App「员工信息」筛选使用:返回有效用户岗位去重列表、组织部门列表、开店门店列表。
///
/// 示例请求:
/// GET /api/Extend/User/FilterOptions
/// </remarks>
/// <returns>gwList、organizeList、storeList</returns>
/// <response code="200">成功返回筛选选项</response>
[HttpGet("FilterOptions")]
public async Task<dynamic> GetFilterOptions()
{
var gwList = await _db.Queryable<UserEntity>()
.Where(u => u.DeleteMark == null && !string.IsNullOrEmpty(u.Gw))
.Select(u => u.Gw)
.Distinct()
.ToListAsync();
var organizeList = await _db.Queryable<OrganizeEntity>()
.Where(o => o.DeleteMark == null)
.OrderBy(o => o.SortCode)
.Select(o => new
{
id = o.Id,
fullName = o.FullName,
parentId = o.ParentId,
category = o.Category
})
.ToListAsync();
var storeList = await _db.Queryable<LqMdxxEntity>()
.Where(it => it.Zxzt == "开店")
.Where(p => p.Status == null || p.Status != (int)StatusEnum.无效)
.OrderBy(it => it.Dm)
.Select(it => new
{
id = it.Id,
fullName = it.Dm
})
.ToListAsync();
return new
{
gwList = (gwList ?? new List<string>())
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x => x.Trim())
.Distinct()
.OrderBy(x => x)
.ToList(),
organizeList,
storeList
};
}
#endregion
private static UserEmployeeDetailOutput MapEmployeeDetail(
UserEntity user,
string organizeName,
string positionName,
string roleName,
string mdmc,
LqEmployeeProfileEntity profile)
{
var bankPlain = profile == null
? null
: EmployeeProfileSensitiveHelper.DecryptSensitive(profile.BankCardCipher);
var output = new UserEmployeeDetailOutput
{
id = user.Id,
account = user.Account,
realName = user.RealName,
nickName = user.NickName,
headIcon = user.HeadIcon,
gender = user.Gender,
birthday = user.Birthday,
mobilePhone = user.MobilePhone,
telePhone = user.TelePhone,
email = user.Email,
urgentContacts = user.UrgentContacts,
urgentTelePhone = user.UrgentTelePhone,
postalAddress = user.PostalAddress,
signature = user.Signature,
firstLogTime = user.FirstLogTime,
prevLogTime = user.PrevLogTime,
isAdministrator = user.IsAdministrator,
description = user.Description,
sortCode = user.SortCode,
managerId = user.ManagerId,
organizeId = user.OrganizeId,
organizeName = organizeName,
positionId = user.PositionId,
positionName = positionName,
roleId = user.RoleId,
roleName = roleName,
portalId = user.PortalId,
extensionStr = user.ExtensionStr,
openId = user.OpenId,
mdid = user.Mdid,
mdmc = mdmc,
zw = user.Zw,
fyft = user.Fyft,
gwfl = user.Gwfl,
gw = user.Gw,
enabledMark = user.EnabledMark,
creatorTime = user.CreatorTime,
creatorUserId = user.CreatorUserId,
entryDate = user.EntryDate,
leaveDate = user.LeaveDate,
reportDate = user.ReportDate,
nation = user.Nation,
nativePlace = user.NativePlace,
education = user.Education,
age = EmployeeProfileSensitiveHelper.CalcAgeFromIdCard(user.CertificatesNumber, user.Birthday),
tenureText = EmployeeProfileSensitiveHelper.CalcTenureText(user.EntryDate),
idCardNo = user.CertificatesNumber,
idCardNoMasked = user.CertificatesNumber,
bankCardNoMasked = EmployeeProfileSensitiveHelper.MaskBankCard(bankPlain)
};
if (profile == null)
{
return output;
}
output.maritalStatusName = profile.MaritalStatus.HasValue
? EnumHelper.GetEnumDesc<EmployeeMaritalStatusEnum>(profile.MaritalStatus.Value) : null;
output.politicalStatusName = profile.PoliticalStatus.HasValue
? EnumHelper.GetEnumDesc<EmployeePoliticalStatusEnum>(profile.PoliticalStatus.Value) : null;
output.householdTypeName = profile.HouseholdType.HasValue
? EnumHelper.GetEnumDesc<EmployeeHouseholdTypeEnum>(profile.HouseholdType.Value) : null;
output.currentAddress = profile.CurrentAddress;
output.idCardAddress = profile.IdCardAddress;
output.idCardFrontUrl = profile.IdCardFrontUrl;
output.idCardBackUrl = profile.IdCardBackUrl;
output.employeeNo = profile.EmployeeNo;
output.employeeTypeName = profile.EmployeeType.HasValue
? EnumHelper.GetEnumDesc<EmployeeTypeEnum>(profile.EmployeeType.Value) : null;
output.employeeStatusName = profile.EmployeeStatus.HasValue
? EnumHelper.GetEnumDesc<EmployeeProfileStatusEnum>(profile.EmployeeStatus.Value) : null;
output.recruitmentTypeName = profile.RecruitmentType.HasValue
? EnumHelper.GetEnumDesc<EmployeeRecruitmentTypeEnum>(profile.RecruitmentType.Value) : null;
output.regularizationDate = profile.RegularizationDate;
output.graduateSchool = profile.GraduateSchool;
output.enrollmentDate = profile.EnrollmentDate;
output.graduationDate = profile.GraduationDate;
output.major = profile.Major;
output.emergencyContactRelation = profile.EmergencyContactRelation;
output.emergencyContactAddress = profile.EmergencyContactAddress;
output.bankName = profile.BankName;
output.bankAccountName = profile.BankAccountName;
return output;
}
/// <summary>
/// 解析字典项、直属上级等展示字段
/// </summary>
private async Task EnrichEmployeeDetailAsync(UserEmployeeDetailOutput output, UserEntity user)
{
if (output == null || user == null)
{
return;
}
var dictIds = new[] { user.Nation, user.Education, user.CertificatesType }
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x => x.Trim())
.Distinct()
.ToList();
var dictionaryNameDict = dictIds.Count == 0
? new Dictionary<string, string>()
: (await _db.Queryable<DictionaryDataEntity>()
.Where(d => dictIds.Contains(d.Id))
.Select(d => new { d.Id, d.FullName })
.ToListAsync())
.GroupBy(x => x.Id)
.ToDictionary(g => g.Key, g => g.First().FullName ?? string.Empty);
output.nation = ResolveDictionaryDisplayName(user.Nation, dictionaryNameDict);
output.education = ResolveDictionaryDisplayName(user.Education, dictionaryNameDict);
if (!string.IsNullOrWhiteSpace(user.ManagerId))
{
output.managerName = await _db.Queryable<UserEntity>()
.Where(u => u.Id == user.ManagerId && u.DeleteMark == null)
.Select(u => u.RealName)
.FirstAsync();
}
}
private static string ResolveDictionaryDisplayName(string value, Dictionary<string, string> dictionaryNameDict)
{
if (string.IsNullOrWhiteSpace(value))
{
return null;
}
var key = value.Trim();
if (dictionaryNameDict.TryGetValue(key, out var name) && !string.IsNullOrWhiteSpace(name))
{
return name;
}
return key;
}
}
}