From a5a6c7a515795a7785b2f5cf01067e400c72e2a3 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Wed, 17 Jul 2024 16:48:30 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E7=85=A7=E7=89=87=E4=B8=8A=E4=BC=A0sap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Src/Dlhis.Basic.Api/IDevicePermissionApi.cs | 9 --- .../Services/Implements/GainfoFaceService.cs | 21 +++++- Src/Dlhis.Entity/Sap/SapDataSyncEntity.cs | 14 ++++ Src/Dlhis.Scheduled.Api/IHangfireApi.cs | 9 +-- .../Request/FaceUploadToSapRequest.cs | 7 ++ .../Common/Response/SapFaceSyncResponse.cs | 11 +++ .../Common/Services/IHangfireService.cs | 5 +- .../Common/Services/IJinkoJIPService.cs | 12 +++- .../Services/Implements/HangfireService.cs | 14 ++-- .../Services/Implements/JinkoJIPService.cs | 19 ++++++ Src/Dlhis.ScheduledTask/Jobs/SyncFaceJob.cs | 51 ++++++++++++++ .../Controllers/HangfireController.cs | 68 ++----------------- .../Request/FaceUploadToSapRequest.cs | 7 ++ 13 files changed, 160 insertions(+), 87 deletions(-) create mode 100644 Src/Dlhis.Entity/Sap/SapDataSyncEntity.cs create mode 100644 Src/Dlhis.Scheduled.Api/Request/FaceUploadToSapRequest.cs create mode 100644 Src/Dlhis.ScheduledTask/Common/Response/SapFaceSyncResponse.cs create mode 100644 Src/Dlhis.ScheduledTask/Jobs/SyncFaceJob.cs create mode 100644 Src/Dlhis.ScheduledTask/TaskAssignment/Request/FaceUploadToSapRequest.cs diff --git a/Src/Dlhis.Basic.Api/IDevicePermissionApi.cs b/Src/Dlhis.Basic.Api/IDevicePermissionApi.cs index 5fcb07cf..09b76de1 100644 --- a/Src/Dlhis.Basic.Api/IDevicePermissionApi.cs +++ b/Src/Dlhis.Basic.Api/IDevicePermissionApi.cs @@ -22,13 +22,4 @@ public interface IDevicePermissionApi /// [Post("/api/devicepermissions/re-permission")] Task> RePermissionUsersInfoAsync(PermissionUsersInfoDto beforeDto); - - - /// - /// 任务生成 - /// - /// - /// - [Post("/api/devicepermissions/assignment-generate")] - Task GeneratePermissionsAsync(GeneratePermissionIdRequest 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 3138ea9d..5ac85a27 100644 --- a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoFaceService.cs +++ b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoFaceService.cs @@ -4,6 +4,8 @@ using Dlhis.Basic.Organization.Response; using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Basic.Enums; using Dlhis.Entity.Device.Enums; +using Dlhis.Scheduled.Api; +using Dlhis.Scheduled.Api.Request; using Kurisu.AspNetCore.Authentication.Abstractions; using Kurisu.AspNetCore.CustomClass; using Kurisu.AspNetCore.DataAccess.Extensions; @@ -22,6 +24,7 @@ public class GainfoFaceService : IGainfoFaceService private readonly ILogger _logger; private readonly IConfiguration _configuration; private readonly ICurrentUser _currentUser; + private readonly IHangfireApi _hangfireApi; private readonly IDevicePermissionService _devicePermissionService; private readonly IDataProtectionProvider _dataProtectionProvider; @@ -30,6 +33,7 @@ public class GainfoFaceService : IGainfoFaceService ILogger logger, IConfiguration configuration, ICurrentUser currentUser, + IHangfireApi hangfireApi, IDevicePermissionService devicePermissionService, IDataProtectionProvider dataProtectionProvider) { @@ -39,6 +43,7 @@ public class GainfoFaceService : IGainfoFaceService _logger = logger; _configuration = configuration; _currentUser = currentUser; + _hangfireApi = hangfireApi; _devicePermissionService = devicePermissionService; _dataProtectionProvider = dataProtectionProvider; } @@ -216,9 +221,21 @@ public class GainfoFaceService : IGainfoFaceService await _faceDbContext.UpdateAsync(face); } - var prefix = gainfo.Factory == "501" ? "jschuneng" : "jingke"; - var url = $"https://pay.aizhuhotel.com/{prefix}/api/face"; + try + { + await _hangfireApi.SyncFaceToSapAsync(new FaceUploadToSapRequest + { + UserCode = face.UserCode, + Base64 = face.Face + }); + } + catch (Exception e) + { + _logger.LogError(e, "上传face数据到sap失败:{message}", e.Message); + } + // var prefix = gainfo.Factory == "501" ? "jschuneng" : "jingke"; + // var url = $"https://pay.aizhuhotel.com/{prefix}/api/face"; //生成任务 await _devicePermissionService.GenerateDeviceTasksAsync(gainfo.No, taskType); diff --git a/Src/Dlhis.Entity/Sap/SapDataSyncEntity.cs b/Src/Dlhis.Entity/Sap/SapDataSyncEntity.cs new file mode 100644 index 00000000..018288eb --- /dev/null +++ b/Src/Dlhis.Entity/Sap/SapDataSyncEntity.cs @@ -0,0 +1,14 @@ +using Kurisu.AspNetCore.DataAccess.SqlSugar; +using SqlSugar; + +namespace Dlhis.Entity.Sap; + +[SugarTable(TableName = "SapDataSync")] +public class SapDataSyncEntity : SugarBaseEntity +{ + public string Key { get; set; } + + public string Type { get; set; } + + public bool IsSync { get; set; } +} \ No newline at end of file diff --git a/Src/Dlhis.Scheduled.Api/IHangfireApi.cs b/Src/Dlhis.Scheduled.Api/IHangfireApi.cs index 7cac8284..1c5b332e 100644 --- a/Src/Dlhis.Scheduled.Api/IHangfireApi.cs +++ b/Src/Dlhis.Scheduled.Api/IHangfireApi.cs @@ -15,8 +15,9 @@ public interface IHangfireApi /// [Post("api/hangfire/sap/person/no")] Task SyncSapPersonAsync(SapNoRequest request); - - [Auth] - [Post("api/hangfire/assignment-generate")] - Task GeneratePermissionsAsync(GeneratePermissionIdRequest request); + + + + [Post("api/hangfire/sap/face")] + Task SyncFaceToSapAsync(FaceUploadToSapRequest request); } \ No newline at end of file diff --git a/Src/Dlhis.Scheduled.Api/Request/FaceUploadToSapRequest.cs b/Src/Dlhis.Scheduled.Api/Request/FaceUploadToSapRequest.cs new file mode 100644 index 00000000..5ef8be37 --- /dev/null +++ b/Src/Dlhis.Scheduled.Api/Request/FaceUploadToSapRequest.cs @@ -0,0 +1,7 @@ +namespace Dlhis.Scheduled.Api.Request; + +public class FaceUploadToSapRequest +{ + public string UserCode { get; set; } + public string Base64 { get; set; } +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Response/SapFaceSyncResponse.cs b/Src/Dlhis.ScheduledTask/Common/Response/SapFaceSyncResponse.cs new file mode 100644 index 00000000..77e8ba2f --- /dev/null +++ b/Src/Dlhis.ScheduledTask/Common/Response/SapFaceSyncResponse.cs @@ -0,0 +1,11 @@ +namespace Dlhis.ScheduledTask.Common.Response; + +public class SapFaceSyncResponse +{ + /// + /// S 成功 + /// + public string OLX { get; set; } + + public string OMS { get; set; } +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Services/IHangfireService.cs b/Src/Dlhis.ScheduledTask/Common/Services/IHangfireService.cs index c90a87a5..074af7b4 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/IHangfireService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/IHangfireService.cs @@ -1,5 +1,6 @@ using Dlhis.Basic.Api.Request; using Dlhis.ScheduledTask.Common.Request; +using Dlhis.ScheduledTask.TaskAssignment.Request; namespace Dlhis.ScheduledTask.Common.Services; @@ -22,6 +23,6 @@ public interface IHangfireService : IScopeDependency /// Task CreateSapWerkSyncBackgroundJobAsync(SapNoRequest request); - - void GeneratePermissions(GeneratePermissionIdRequest request); + + Task FaceUploadToSapAsync(FaceUploadToSapRequest request); } \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Services/IJinkoJIPService.cs b/Src/Dlhis.ScheduledTask/Common/Services/IJinkoJIPService.cs index fa504a74..50d3567c 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/IJinkoJIPService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/IJinkoJIPService.cs @@ -19,8 +19,18 @@ public interface IJinkoJIPService : ISingletonDependency /// Task GetTravelInfosByDepartmentsAsync(DateTime begin, DateTime end, List departmentNos); - + /// + /// 获取出差信息 + /// + /// + /// + /// + /// Task GetTravelInfosByUserCodeAsync(DateTime begin, DateTime end, string userCode); + + Task SyncFaceToSapAsync(string userCode, string base64); + + string IssueToken(); } \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Services/Implements/HangfireService.cs b/Src/Dlhis.ScheduledTask/Common/Services/Implements/HangfireService.cs index 7508f760..c2ff4d70 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/Implements/HangfireService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/Implements/HangfireService.cs @@ -1,7 +1,6 @@ -using Dlhis.Basic.Api; -using Dlhis.Basic.Api.Request; -using Dlhis.Entity.Basic.Entity; +using Dlhis.Entity.Basic.Entity; using Dlhis.ScheduledTask.Common.Request; +using Dlhis.ScheduledTask.TaskAssignment.Request; using Hangfire; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.Utils; @@ -13,11 +12,13 @@ public class HangfireService : IHangfireService { private readonly RedisCache _redisCache; private readonly IDbContext _dbContext; + private readonly IJinkoJIPService _jinkoJipService; - public HangfireService(RedisCache redisCache, IDbContext dbContext) + public HangfireService(RedisCache redisCache, IDbContext dbContext, IJinkoJIPService jinkoJipService) { _redisCache = redisCache; _dbContext = dbContext; + _jinkoJipService = jinkoJipService; } public async Task CreateSapPersonSyncBackgroundJobAsync(SapNoRequest request) @@ -47,9 +48,8 @@ public class HangfireService : IHangfireService } } - /// - public void GeneratePermissions(GeneratePermissionIdRequest request) + public async Task FaceUploadToSapAsync(FaceUploadToSapRequest request) { - BackgroundJob.Enqueue(x => x.GeneratePermissionsAsync(request)); + await _jinkoJipService.SyncFaceToSapAsync(request.UserCode, request.Base64); } } \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoJIPService.cs b/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoJIPService.cs index 1cab2eb0..2f336f97 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoJIPService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoJIPService.cs @@ -158,6 +158,25 @@ public class JinkoJIPService : IJinkoJIPService return jsonResponse.ToObject(); } + public async Task SyncFaceToSapAsync(string userCode, string base64) + { + const string apiUrl = "/JIP/HCM/HCM_I_PHOTO"; + var obj = new + { + I_WORKCODE = userCode, + I_PHOTO_STR = base64 + }; + + var body = obj.ToJson(); + + var content = new StringContent(body, Encoding.UTF8, "application/json"); + var response = await GetHttpClient().PostAsync(apiUrl, content); + response.EnsureSuccessStatusCode(); + + var jsonResponse = await response.Content.ReadAsStringAsync(); + return jsonResponse.ToObject(); + } + public string IssueToken() { diff --git a/Src/Dlhis.ScheduledTask/Jobs/SyncFaceJob.cs b/Src/Dlhis.ScheduledTask/Jobs/SyncFaceJob.cs new file mode 100644 index 00000000..ef19e6d1 --- /dev/null +++ b/Src/Dlhis.ScheduledTask/Jobs/SyncFaceJob.cs @@ -0,0 +1,51 @@ +using Dlhis.Entity.Basic.Entity; +using Dlhis.Entity.Sap; +using Dlhis.ScheduledTask.Attributes; +using Dlhis.ScheduledTask.Common.Services; + +namespace Dlhis.ScheduledTask.Jobs; + +[RecurringJob(nameof(SyncFaceJob), "0 0/15 * * * ?", Name = "face数据同步sap")] +public class SyncFaceJob : BaseJob +{ + private readonly IJinkoJIPService _jinkoJipService; + + /// + public SyncFaceJob(IServiceProvider serviceProvider, IJinkoJIPService jinkoJipService) : base(serviceProvider) + { + _jinkoJipService = jinkoJipService; + } + + /// + protected override async Task RunAsync(IServiceProvider serviceProvider) + { + var faceDbContext = DbContext.ChangeDb("face"); + + var existsData = await DbContext.Queryable().Where(x => x.IsSync).Select(x => x.Key).ToListAsync(); + + var userFaces = await faceDbContext.Queryable() + .Where(x => !existsData.Contains(x.UserCode)) + .ToPageListAsync(1, 5000); + + foreach (var data in userFaces) + { + try + { + var response = await _jinkoJipService.SyncFaceToSapAsync(data.UserCode, data.Face); + if (response.OLX == "S") + { + await DbContext.InsertAsync(new SapDataSyncEntity + { + Key = data.UserCode, + Type = "Face", + IsSync = true + }); + } + } + catch (Exception e) + { + Logger.LogError(e, "上传face数据失败:{message}", e.Message); + } + } + } +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/HangfireController.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/HangfireController.cs index c44e46d2..6bc80341 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/HangfireController.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/HangfireController.cs @@ -1,14 +1,9 @@ -using Dlhis.Basic.Api.Request; -using Dlhis.Entity.Basic.Entity; -using Dlhis.Entity.Canteen.Entities; -using Dlhis.Entity.Canteen.Enums; -using Dlhis.ScheduledTask.Common.Request; +using Dlhis.ScheduledTask.Common.Request; using Dlhis.ScheduledTask.Common.Services; -using Kurisu.AspNetCore.DataAccess.SqlSugar.Aop; +using Dlhis.ScheduledTask.TaskAssignment.Request; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using SqlSugar; namespace Dlhis.ScheduledTask.TaskAssignment.Controllers; @@ -52,61 +47,10 @@ public class HangfireController : ControllerBase await _hangfireService.CreateSapWerkSyncBackgroundJobAsync(request); } - - [HttpPost("assignment-generate")] - public void GeneratePermissionsAsync(GeneratePermissionIdRequest request) + [AllowAnonymous] + [HttpPost("sap/face")] + public async Task FaceUploadToSapAsync(FaceUploadToSapRequest request) { - _hangfireService.GeneratePermissions(request); - } - - - [IgnoreTenant] - [HttpPost("recalc")] - public async Task ReCalc() - { - // select UserCode,Remark, count(1) count from balancerecord - // where Status = 'Valid' - // and TenantId !='1201' - // and IsDeleted = false - // group by UserCode,Remark - // having ifnull(Remark,'')!='' and count > 1 and Remark !='用户取消' and Remark !='正常发放'; - - var ls = await _dbContext.Queryable().Where(x => x.Status == BalanceRecordStatusEnumType.Valid) - .Select(x => new { x.UserCode, x.Remark, count = SqlFunc.AggregateCount(x.Id) }) - .GroupBy(x => new { x.UserCode, x.Remark }) - .Having(x => SqlFunc.IsNull(x.Remark, "") != "" && SqlFunc.AggregateCount(1) > 1 && x.Remark != "用户取消" && x.Remark != "正常发放") - .ToListAsync(); - - foreach (var row in ls) - { - var data = await _dbContext.Queryable() - .Where(x => x.UserCode == row.UserCode) - .Where(x => x.ApplicationType == CanteenApplicationTypeEnumType.MealSupplement) - .Where(x => x.Remark == row.Remark) - .ToListAsync(); - - if (data.Count <= 1) - { - continue; - } - - var f = data.First(); - var others = data.Where(x => x.Id != f.Id).ToList(); - - others.ForEach(x => x.Status = BalanceRecordStatusEnumType.Obsolute); - await _dbContext.UpdateAsync(others); - - var sum = await _dbContext.Queryable().Where(x => x.UserCode == f.UserCode) - .Where(x => x.Status == BalanceRecordStatusEnumType.Valid) - .SumAsync(x => x.Amount); - - var mealInfo = await _dbContext.Queryable().Where(x => x.UserCode == f.UserCode).SingleAsync(); - await _dbContext.Updateable() - .SetColumns(x => x.Balance == sum) - .SetColumns(x => x.Version == x.Version + 1) - .SetColumns(x => x.ModifiedTime == DateTime.Now) - .Where(x => x.UserCode == f.UserCode && x.Version == mealInfo.Version) - .ExecuteCommandAsync(); - } + await _hangfireService.FaceUploadToSapAsync(request); } } \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Request/FaceUploadToSapRequest.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Request/FaceUploadToSapRequest.cs new file mode 100644 index 00000000..6912f14c --- /dev/null +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Request/FaceUploadToSapRequest.cs @@ -0,0 +1,7 @@ +namespace Dlhis.ScheduledTask.TaskAssignment.Request; + +public class FaceUploadToSapRequest +{ + public string UserCode { get; set; } + public string Base64 { get; set; } +} \ No newline at end of file