From 51b0397763819eb457e418df3971a4f2c42f744a Mon Sep 17 00:00:00 2001 From: liguoyang Date: Tue, 11 Jun 2024 12:32:31 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=A1=A3=E6=A1=88=E4=BA=BA=E8=84=B8?= =?UTF-8?q?=E5=88=86=E9=A1=B5=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/GainfoFacesController.cs | 35 +++++++ .../Request/GainfoFacePageRequest.cs | 15 +++ .../Response/GainfoFacePageResponse.cs | 32 +++++++ .../Services/IGainfoFaceService.cs | 5 +- .../Services/Implements/GainfoFaceService.cs | 91 +++++++++++-------- .../Controllers/SummariesController.cs | 10 -- .../Summary/Services/ISummaryService.cs | 2 - .../Services/Implements/SummaryService.cs | 17 ---- .../Services/Implements/JinkoSapService.cs | 4 +- 9 files changed, 140 insertions(+), 71 deletions(-) create mode 100644 Src/Dlhis.Basic/Organization/Controllers/GainfoFacesController.cs create mode 100644 Src/Dlhis.Basic/Organization/Request/GainfoFacePageRequest.cs create mode 100644 Src/Dlhis.Basic/Organization/Response/GainfoFacePageResponse.cs diff --git a/Src/Dlhis.Basic/Organization/Controllers/GainfoFacesController.cs b/Src/Dlhis.Basic/Organization/Controllers/GainfoFacesController.cs new file mode 100644 index 00000000..843ecbe9 --- /dev/null +++ b/Src/Dlhis.Basic/Organization/Controllers/GainfoFacesController.cs @@ -0,0 +1,35 @@ +using Dlhis.Basic.Organization.Request; +using Dlhis.Basic.Organization.Response; +using Dlhis.Basic.Organization.Services; +using Kurisu.AspNetCore.DataAccess.Dto; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace Dlhis.Basic.Organization.Controllers; + +/// +/// 档案人脸 +/// +[Authorize] +[Route("api/[controller]")] +[ApiController] +public class GainfoFacesController : ControllerBase +{ + private readonly IGainfoFaceService _gainfoFaceService; + + public GainfoFacesController(IGainfoFaceService gainfoFaceService) + { + _gainfoFaceService = gainfoFaceService; + } + + /// + /// 人脸档案分页 + /// + /// + /// + [HttpPost("page")] + public async Task GetPageAsync([FromQuery] GainfoFacePageRequest request) + { + return await _gainfoFaceService.GetPageAsync(request); + } +} \ No newline at end of file diff --git a/Src/Dlhis.Basic/Organization/Request/GainfoFacePageRequest.cs b/Src/Dlhis.Basic/Organization/Request/GainfoFacePageRequest.cs new file mode 100644 index 00000000..9f9c325d --- /dev/null +++ b/Src/Dlhis.Basic/Organization/Request/GainfoFacePageRequest.cs @@ -0,0 +1,15 @@ +using System.ComponentModel.DataAnnotations; +using Kurisu.AspNetCore.DataAccess.Dto; + +namespace Dlhis.Basic.Organization.Request; + +public class GainfoFacePageRequest : PageDto +{ + public bool? Exists { get; set; } + + /// + /// 厂区 + /// + [Required(ErrorMessage = "厂区不可为空")] + public string Factory { get; set; } +} \ No newline at end of file diff --git a/Src/Dlhis.Basic/Organization/Response/GainfoFacePageResponse.cs b/Src/Dlhis.Basic/Organization/Response/GainfoFacePageResponse.cs new file mode 100644 index 00000000..c105aa40 --- /dev/null +++ b/Src/Dlhis.Basic/Organization/Response/GainfoFacePageResponse.cs @@ -0,0 +1,32 @@ +using Kurisu.AspNetCore.DataAccess.Dto; + +namespace Dlhis.Basic.Organization.Response; + +public class GainfoFacePageSummaryResponse +{ + public int Total { get; set; } + + public int Exists { get; set; } + + public int Left => Total - Exists; + + public Pagination Page { get; set; } +} + +public class GainfoFacePageResponse +{ + /// + /// 工号 + /// + public string UserCode { get; set; } + + /// + /// 姓名 + /// + public string UserName { get; set; } + + /// + /// 是否存在人脸 + /// + public bool Exists { get; set; } +} \ No newline at end of file diff --git a/Src/Dlhis.Basic/Organization/Services/IGainfoFaceService.cs b/Src/Dlhis.Basic/Organization/Services/IGainfoFaceService.cs index e4653c21..255fbaa4 100644 --- a/Src/Dlhis.Basic/Organization/Services/IGainfoFaceService.cs +++ b/Src/Dlhis.Basic/Organization/Services/IGainfoFaceService.cs @@ -1,10 +1,13 @@ using Dlhis.Basic.Organization.Request; +using Dlhis.Basic.Organization.Response; using Microsoft.AspNetCore.Mvc; namespace Dlhis.Basic.Organization.Services; public interface IGainfoFaceService : IScopeDependency { + Task GetPageAsync(GainfoFacePageRequest request); + Task UploadExternalFaceAsync(GainfoUploadFaceRequest request); Task GetExternalFaceAsync(string idno); @@ -18,4 +21,4 @@ public interface IGainfoFaceService : IScopeDependency Task GetFaceByUserCodeAsync(string userCode); Task> GetFaceByUserCodesAsync(GetFaceByUserCodesRequest request); -} +} \ No newline at end of file diff --git a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoFaceService.cs b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoFaceService.cs index 0482febb..36d48369 100644 --- a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoFaceService.cs +++ b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoFaceService.cs @@ -1,12 +1,13 @@ using Dlhis.Basic.Device.Services; using Dlhis.Basic.Organization.Request; +using Dlhis.Basic.Organization.Response; using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Basic.Enums; using Dlhis.Entity.Device.Enums; using Kurisu.AspNetCore.Authentication.Abstractions; using Kurisu.AspNetCore.CustomClass; +using Kurisu.AspNetCore.DataAccess.Extensions; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; -using Kurisu.AspNetCore.Utils; using Kurisu.AspNetCore.Utils.Extensions; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Mvc; @@ -17,9 +18,9 @@ public class GainfoFaceService : IGainfoFaceService { private readonly IGainfoStore _gainfoStore; private readonly IDbContext _dbContext; + private readonly IDbContext _faceDbContext; private readonly ILogger _logger; private readonly IConfiguration _configuration; - private readonly RedisCache _redisCache; private readonly ICurrentUser _currentUser; private readonly IDevicePermissionService _devicePermissionService; private readonly IDataProtectionProvider _dataProtectionProvider; @@ -28,21 +29,49 @@ public class GainfoFaceService : IGainfoFaceService IDbContext dbContext, ILogger logger, IConfiguration configuration, - RedisCache redisCache, ICurrentUser currentUser, IDevicePermissionService devicePermissionService, IDataProtectionProvider dataProtectionProvider) { _gainfoStore = gainfoStore; _dbContext = dbContext; + _faceDbContext = dbContext.ChangeDb("face"); _logger = logger; _configuration = configuration; - _redisCache = redisCache; _currentUser = currentUser; _devicePermissionService = devicePermissionService; _dataProtectionProvider = dataProtectionProvider; } + + public async Task GetPageAsync(GainfoFacePageRequest request) + { + var userCodes = await _dbContext.Queryable().Where(x => x.Factory == request.Factory).Select(x => x.Code).ToListAsync(); + var existsFaceCodes = await _faceDbContext.Queryable().Where(x => userCodes.Contains(x.UserCode)).Select(x => x.UserCode).ToListAsync(); + + var list = await _dbContext.Queryable() + .Where(x => x.Factory == request.Factory) + .WhereIF(request.Exists.HasValue && request.Exists.Value, x => existsFaceCodes.Contains(x.Code)) + .WhereIF(request.Exists.HasValue && !request.Exists.Value, x => !existsFaceCodes.Contains(x.Code)) + .Select(x => new GainfoFacePageResponse + { + UserCode = x.Code, + UserName = x.Name, + Exists = false + }) + .ToListAsync(); + + var page = list.ToPage(request); + page.Data.ForEach(x => { x.Exists = existsFaceCodes.Contains(x.UserCode); }); + + return new GainfoFacePageSummaryResponse + { + Total = userCodes.Count, + Exists = existsFaceCodes.Count, + Page = page + }; + } + public async Task UploadExternalFaceAsync(GainfoUploadFaceRequest request) { var protector = _dataProtectionProvider.CreateProtector("gainfo:external:idno"); @@ -60,8 +89,7 @@ public class GainfoFaceService : IGainfoFaceService .FirstAsync(); gainfo.ThrowIfNull("人员信息不存在"); - var faceContext = _dbContext.ChangeDb("face"); - var face = await faceContext.Queryable().Where(x => x.UserCode == gainfo.Code).SingleAsync(); + var face = await _faceDbContext.Queryable().Where(x => x.UserCode == gainfo.Code).SingleAsync(); if (face == null) { @@ -75,7 +103,7 @@ public class GainfoFaceService : IGainfoFaceService ModifiedBy = 1 }; - await faceContext.InsertAsync(face); + await _faceDbContext.InsertAsync(face); } else { @@ -83,7 +111,7 @@ public class GainfoFaceService : IGainfoFaceService face.ModifiedBy = 1; face.ModifiedTime = DateTime.Now; - await faceContext.UpdateAsync(face); + await _faceDbContext.UpdateAsync(face); } } @@ -99,10 +127,9 @@ public class GainfoFaceService : IGainfoFaceService throw new UserFriendlyException("凭证有误,无法获取人脸数"); } - var dbContext = _dbContext.ChangeDb("face"); - var face = await dbContext.Queryable() - .Where(x => x.UserCode == idno) - .SingleAsync(); + var face = await _faceDbContext.Queryable() + .Where(x => x.UserCode == idno) + .SingleAsync(); face.ThrowIfNull("人脸照片尚未录入"); @@ -126,18 +153,10 @@ public class GainfoFaceService : IGainfoFaceService var password = timeLimitedDataProtector.Unprotect(token); var userCode = password.Split(':')[1]; - var faceCache = await _redisCache.HashGetAsync("faces", userCode); - if (faceCache.HasValue) - { - return new FileContentResult(Convert.FromBase64String(faceCache), "image/png"); - } - - var dbContext = _dbContext.ChangeDb("face"); - var face = await dbContext.Queryable().Where(x => x.UserCode == userCode).SingleAsync(); + var face = await _faceDbContext.Queryable().Where(x => x.UserCode == userCode).SingleAsync(); if (face == null) { - _logger.LogWarning("学工号:{userCode}不存在人脸照片", userCode); return new NotFoundResult(); } @@ -157,12 +176,10 @@ public class GainfoFaceService : IGainfoFaceService var userId = _currentUser.GetIntSubjectId(); - var faceContext = _dbContext.ChangeDb("face"); - var face = await faceContext.Queryable().Where(x => x.UserCode == gainfo.Code).SingleAsync(); + var face = await _faceDbContext.Queryable().Where(x => x.UserCode == gainfo.Code).SingleAsync(); var taskType = face == null ? DeviceTaskEnumType.Add : DeviceTaskEnumType.Update; - await _redisCache.HashSetAsync("faces", gainfo.Code, request.Face); if (face == null) { face = new GainfoFaceEntity @@ -175,7 +192,7 @@ public class GainfoFaceService : IGainfoFaceService ModifiedBy = userId }; - await faceContext.InsertAsync(face); + await _faceDbContext.InsertAsync(face); } else { @@ -188,7 +205,7 @@ public class GainfoFaceService : IGainfoFaceService face.Face = request.Face; face.ModifiedBy = userId; face.ModifiedTime = DateTime.Now; - await faceContext.UpdateAsync(face); + await _faceDbContext.UpdateAsync(face); } //生成任务 @@ -204,12 +221,10 @@ public class GainfoFaceService : IGainfoFaceService gainfo.ThrowIfNull("人员档案信息不存在"); - var dbContext = _dbContext.ChangeDb("face"); - var face = await dbContext.Queryable().Where(x => x.UserCode == gainfo.Code).SingleAsync(); + var face = await _faceDbContext.Queryable().Where(x => x.UserCode == gainfo.Code).SingleAsync(); var taskType = face == null ? DeviceTaskEnumType.Add : DeviceTaskEnumType.Update; - await _redisCache.HashSetAsync("faces", gainfo.Code, request.Face); if (face == null) { face = new GainfoFaceEntity @@ -218,7 +233,7 @@ public class GainfoFaceService : IGainfoFaceService Face = request.Face }; - await dbContext.InsertAsync(face); + await _faceDbContext.InsertAsync(face); } else { @@ -229,7 +244,7 @@ public class GainfoFaceService : IGainfoFaceService } face.Face = request.Face; - await dbContext.UpdateAsync(face); + await _faceDbContext.UpdateAsync(face); } //生成任务 @@ -240,13 +255,9 @@ public class GainfoFaceService : IGainfoFaceService public async Task GetFaceByUserCodeAsync(string userCode) { - var gainfo = await _dbContext.Queryable().Where(x => x.Code == userCode).SingleAsync(); - gainfo.ThrowIfNull($"{userCode}人员信息不存在"); - - var dbContext = _dbContext.ChangeDb("face"); - var face = await dbContext.Queryable() - .Where(x => x.UserCode == userCode) - .SingleAsync(); + var face = await _faceDbContext.Queryable() + .Where(x => x.UserCode == userCode) + .SingleAsync(); face.ThrowIfNull("人脸照片尚未录入"); @@ -274,8 +285,10 @@ public class GainfoFaceService : IGainfoFaceService } catch (Exception) { + //ignore } } + return faceDictionary; } -} +} \ No newline at end of file diff --git a/Src/Dlhis.Basic/Summary/Controllers/SummariesController.cs b/Src/Dlhis.Basic/Summary/Controllers/SummariesController.cs index 924f8d19..bfb8f69f 100644 --- a/Src/Dlhis.Basic/Summary/Controllers/SummariesController.cs +++ b/Src/Dlhis.Basic/Summary/Controllers/SummariesController.cs @@ -46,14 +46,4 @@ public class SummariesController : ControllerBase { return await _summaryService.GetDoorDataAsync(); } - - /// - /// - /// - /// - [HttpGet("faceRecordDetail")] - public async Task GetCurrentFactoryFaceDetailAsync() - { - return await _summaryService.GetCurrentFactoryFaceDetailAsync(); - } } \ No newline at end of file diff --git a/Src/Dlhis.Basic/Summary/Services/ISummaryService.cs b/Src/Dlhis.Basic/Summary/Services/ISummaryService.cs index 12efd370..dfb99fa3 100644 --- a/Src/Dlhis.Basic/Summary/Services/ISummaryService.cs +++ b/Src/Dlhis.Basic/Summary/Services/ISummaryService.cs @@ -9,6 +9,4 @@ public interface ISummaryService : IScopeDependency Task GetDoorHomeDataAsync(); Task GetDoorDataAsync(); - - Task GetCurrentFactoryFaceDetailAsync(); } \ No newline at end of file diff --git a/Src/Dlhis.Basic/Summary/Services/Implements/SummaryService.cs b/Src/Dlhis.Basic/Summary/Services/Implements/SummaryService.cs index 3affb075..221aad79 100644 --- a/Src/Dlhis.Basic/Summary/Services/Implements/SummaryService.cs +++ b/Src/Dlhis.Basic/Summary/Services/Implements/SummaryService.cs @@ -13,7 +13,6 @@ namespace Dlhis.Basic.Summary.Services.Implements; public class SummaryService : ISummaryService { private readonly IDbContext _dbContext; - private readonly IDbContext _faceDbContext; private readonly IGainfoStore _gainfoStore; private readonly ICurrentUser _currentUser; @@ -22,8 +21,6 @@ public class SummaryService : ISummaryService _dbContext = dbContext; _gainfoStore = gainfoStore; _currentUser = currentUser; - - _faceDbContext = dbContext.ChangeDb("face"); } public async Task GetAttendanceSummaryAsync() @@ -152,20 +149,6 @@ public class SummaryService : ISummaryService }; } - /// - public async Task GetCurrentFactoryFaceDetailAsync() - { - var currentTenantId = _currentUser.GetStringTenantId(); - var userCodes = await _dbContext.Queryable().Where(x => x.Factory == currentTenantId).Select(x => x.Code).ToListAsync(); - var total = userCodes.Count; - var exists = await _faceDbContext.Queryable().Where(x => userCodes.Contains(x.UserCode)).CountAsync(); - - return new - { - total, exists - }; - } - private static List FixTrend(IEnumerable dtos, DateTime start, DateTime end) { var result = dtos.ToList(); diff --git a/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs b/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs index 06db2208..703407e3 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs @@ -14,7 +14,7 @@ public class JinkoSapService : IJinkoSapService private readonly IDbContext _dbContext; private readonly IHttpClientFactory _httpClientFactory; - public JinkoSapService(IHttpClientFactory httpClientFactory, ILogger logger, IDbContext dbContext) + public JinkoSapService(IHttpClientFactory httpClientFactory, IDbContext dbContext) { _dbContext = dbContext; _httpClientFactory = httpClientFactory; @@ -26,7 +26,7 @@ public class JinkoSapService : IJinkoSapService httpClient.BaseAddress = new Uri("http://sappipap01.jinkosolar.cn:50100"); httpClient.Timeout = TimeSpan.FromMinutes(10); - var credentials = $"{"EHS_USER"}:{"y3R1hd2h*RICAC"}"; + const string credentials = $"{"EHS_USER"}:{"y3R1hd2h*RICAC"}"; var headerValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", headerValue);