From 28a347e04aa004abc522d94b0744dbed764b99b2 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Wed, 3 Apr 2024 15:29:03 +0800 Subject: [PATCH] =?UTF-8?q?fix=20sap=E4=BA=BA=E5=91=98=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=8B=86=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Src/Dlhis.Basic.Api/IDevicePermissionApi.cs | 5 +- .../DevicePermissionsController.cs | 6 +- .../Services/IDevicePermissionService.cs | 2 +- .../Implements/DevicePermissionService.cs | 9 +- Src/Dlhis.Entity/Basic/Entity/WerkEntity.cs | 43 ++++++ .../Common/Services/IGainfoUpdateService.cs | 4 +- .../Common/Services/IJinkoSapService.cs | 4 +- .../Implements/GainfoUpdateService.cs | 141 ++++++++++++------ .../Services/Implements/JinkoSapService.cs | 14 +- Src/Dlhis.ScheduledTask/Jobs/SyncPersonJob.cs | 24 ++- Src/Dlhis.Sys.Api/Dlhis.Sys.Api.csproj | 2 +- 11 files changed, 184 insertions(+), 70 deletions(-) diff --git a/Src/Dlhis.Basic.Api/IDevicePermissionApi.cs b/Src/Dlhis.Basic.Api/IDevicePermissionApi.cs index d1150a7f..18bb8a88 100644 --- a/Src/Dlhis.Basic.Api/IDevicePermissionApi.cs +++ b/Src/Dlhis.Basic.Api/IDevicePermissionApi.cs @@ -11,13 +11,12 @@ public interface IDevicePermissionApi [Get("/api/devicepermissions/canteen/uniqueNos")] Task>> GetCanteenDeviceUniqueNosByUserNoAsync(string userNo); - /// /// 获取所有人的权限信息 /// /// - [Get("/api/devicepermissions/all-permission")] - Task> GetPermissionUsersInfoAsync(); + [Post("/api/devicepermissions/all-permission")] + Task> GetPermissionUsersInfoAsync(List userNos); /// /// 重新生成权限 diff --git a/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs b/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs index b021c5db..c2215c57 100644 --- a/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs +++ b/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs @@ -74,11 +74,11 @@ public class DevicePermissionsController : ControllerBase /// /// [AllowAnonymous] - [HttpGet("all-permission")] + [HttpPost("all-permission")] [IgnoreTenant] - public async Task GetPermissionUsersInfoAsync() + public async Task GetPermissionUsersInfoAsync(List userNos) { - return await _devicePermissionService.GetPermissionUsersInfoAsync(); + return await _devicePermissionService.GetPermissionUsersInfoAsync(userNos); } /// diff --git a/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs b/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs index 3d8ea3ec..245c2b90 100644 --- a/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs +++ b/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs @@ -68,7 +68,7 @@ public interface IDevicePermissionService : IScopeDependency - Task GetPermissionUsersInfoAsync(); + Task GetPermissionUsersInfoAsync(List userNos); Task ReGeneratePermissionUsersInfoAsync(PermissionUsersInfoDto beforeDto); diff --git a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs index dc8038c1..1142af9d 100644 --- a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs +++ b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs @@ -162,10 +162,11 @@ public class DevicePermissionService : IDevicePermissionService } } - public async Task GetPermissionUsersInfoAsync() + public async Task GetPermissionUsersInfoAsync(List userNos) { - //获取所有人的权限 - var userNos = await GetPermissionUserNosAsync(); + if (userNos.IsEmpty()) + userNos = await GetPermissionUserNosAsync(); + var permissions = await GetAssignDeviceCodesInternalAsync(userNos); return new PermissionUsersInfoDto @@ -489,7 +490,7 @@ public class DevicePermissionService : IDevicePermissionService return result; } - private static List GetAssignDeviceCodesInternal( + private static List GetAssignDeviceCodesInternal( GainfoEntity gainfo, List gainfoGroupRelationships, List devicePermissions, diff --git a/Src/Dlhis.Entity/Basic/Entity/WerkEntity.cs b/Src/Dlhis.Entity/Basic/Entity/WerkEntity.cs index 46607582..cc462666 100644 --- a/Src/Dlhis.Entity/Basic/Entity/WerkEntity.cs +++ b/Src/Dlhis.Entity/Basic/Entity/WerkEntity.cs @@ -17,8 +17,51 @@ public class WerkEntity : SugarBaseEntity /// /// 名称 /// + [SugarColumn(IsNullable = false, ColumnDescription = "名称")] public string Name { get; set; } + /// + /// 后台任务key + /// + [SugarColumn(IsNullable = true, ColumnDescription = "后台任务key")] + public string BackgroundJobKey { get; set; } + + /// + /// 获取总人数 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "获取总人数")] + public int Total { get; set; } + + /// + /// 差异数 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "差异数")] + public int DiffNumber { get; set; } + + /// + /// 权限变更数 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "权限变更数")] + public int PermissionChangeNumber { get; set; } + + /// + /// 最近同步时间 + /// + [SugarColumn(IsNullable = true, ColumnDescription = "最近同步时间")] + public DateTime? LastSyncStartTime { get; set; } + + /// + /// 最近同步时间 + /// + [SugarColumn(IsNullable = true, ColumnDescription = "最近同步时间")] + public DateTime? LastSyncEndTime { get; set; } + + /// + /// 结果 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "结果")] + public string HandleMessage { get; set; } + /// /// 是否启用 /// diff --git a/Src/Dlhis.ScheduledTask/Common/Services/IGainfoUpdateService.cs b/Src/Dlhis.ScheduledTask/Common/Services/IGainfoUpdateService.cs index 07b38d92..fab210dc 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/IGainfoUpdateService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/IGainfoUpdateService.cs @@ -2,5 +2,7 @@ public interface IGainfoUpdateService : IScopeDependency { - Task TodoAsync(string code); + Task TodoByWerkAsync(string werk); + + Task TodoByCodeAsync(string code); } diff --git a/Src/Dlhis.ScheduledTask/Common/Services/IJinkoSapService.cs b/Src/Dlhis.ScheduledTask/Common/Services/IJinkoSapService.cs index 736def0e..daae853f 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/IJinkoSapService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/IJinkoSapService.cs @@ -8,7 +8,7 @@ public interface IJinkoSapService : IScopeDependency Task GetPositionsAsync(); - Task GetPersonsAsync(); + Task GetPersonsByWerkAsync(string werk); - Task GetPersonsAsync(string code); + Task GetPersonsByCodeAsync(string code); } diff --git a/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs b/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs index 83fa60a5..57e958af 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/Implements/GainfoUpdateService.cs @@ -1,5 +1,4 @@ - -using Dlhis.Basic.Api; +using Dlhis.Basic.Api; using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Basic.Enums; using System.Text; @@ -20,7 +19,6 @@ public class GainfoUpdateService : IGainfoUpdateService private readonly IDevicePermissionApi _devicePermissionApi; private readonly IJinkoSapService _jinkoSapService; - private List _tenants; private List _departmentChanges; private List _jobChanges; @@ -32,54 +30,117 @@ public class GainfoUpdateService : IGainfoUpdateService IJinkoSapService jinkoSapService) { _dbContext = dbContext; + _dbContext.Client.QueryFilter.Clear(); _logger = logger; _tenantApi = tenantApi; _devicePermissionApi = devicePermissionApi; _jinkoSapService = jinkoSapService; } - public async Task TodoAsync(string code) - { - _logger.LogWarning("开始获取SAP人员"); - _dbContext.Client.QueryFilter.ClearAndBackup(); - var personResp = await _jinkoSapService.GetPersonsAsync(code); + public async Task TodoByWerkAsync(string werk) + { + var werkInfo = await _dbContext.Queryable().Where(x => x.Werk == werk).SingleAsync(); + werkInfo.LastSyncStartTime = DateTime.Now; + werkInfo.HandleMessage = "任务执行中"; + await _dbContext.UpdateAsync(werkInfo); + + try + { + var personResp = await _jinkoSapService.GetPersonsByWerkAsync(werk); + var persons = personResp.Person.Items; + _departmentChanges = personResp.DepartmentInfo.Items; + _jobChanges = personResp.JobStatusInfo.Items; + + werkInfo.HandleMessage = "SAP人员数量" + persons.Count; + werkInfo.Total = persons.Count; + await _dbContext.UpdateAsync(werkInfo); + + if (persons.Count > 0) + { + var modifiedPersons = GetModifiedPerson(persons.OrderByDescending(x => x.ZSTATUS).ThenBy(x => x.ZRZRQ).ToList()); + + werkInfo.HandleMessage = "SAP人员差异数量" + modifiedPersons.Count; + werkInfo.DiffNumber = modifiedPersons.Count; + await _dbContext.UpdateAsync(werkInfo); + + if (modifiedPersons.Count > 0) + { + var tenantResult = await _tenantApi.GetTenantsInternalAsync(); + _tenants = tenantResult.Data; + + var codes = modifiedPersons.Select(x => x.ZWORKCODE).ToList(); + var userNos = await _dbContext.Queryable().Where(x => codes.Contains(x.Code)).Select(x => x.No).ToListAsync(); + var dtoResult = await _devicePermissionApi.GetPermissionUsersInfoAsync(userNos); + var dto = dtoResult.Data; + + await SavePersonAsync(modifiedPersons); + + var rResult = await _devicePermissionApi.RePermissionUsersInfoAsync(dto); + var r = rResult.Data; + + werkInfo.PermissionChangeNumber = r; + await _dbContext.UpdateAsync(werkInfo); + } + } + } + finally + { + werkInfo.HandleMessage = "同步结束"; + werkInfo.LastSyncEndTime = DateTime.Now; + werkInfo.BackgroundJobKey = string.Empty; + await _dbContext.UpdateAsync(werkInfo); + } + } + + + public async Task TodoByCodeAsync(string code) + { + _logger.LogInformation("SAP根据学工号同步开始:{code}", code); + + var personResp = await _jinkoSapService.GetPersonsByCodeAsync(code); var persons = personResp.Person.Items; _departmentChanges = personResp.DepartmentInfo.Items; _jobChanges = personResp.JobStatusInfo.Items; - _logger.LogWarning("获取SAP人员数量为:{count}", persons.Count); + _logger.LogInformation("获取SAP人员数量为:{count}", persons.Count); if (persons.Count > 0) { var modifiedPersons = GetModifiedPerson(persons.OrderByDescending(x => x.ZSTATUS).ThenBy(x => x.ZRZRQ).ToList()); - _logger.LogWarning("SAP人员差异数量为:{count}", modifiedPersons.Count); + _logger.LogInformation("SAP人员差异数量为:{count}", modifiedPersons.Count); if (modifiedPersons.Count > 0) { + var tenantResult = await _tenantApi.GetTenantsInternalAsync(); + _tenants = tenantResult.Data; + + var codes = modifiedPersons.Select(x => x.ZWORKCODE).ToList(); + var userNos = await _dbContext.Queryable().Where(x => codes.Contains(x.Code)).Select(x => x.No).ToListAsync(); + var dtoResult = await _devicePermissionApi.GetPermissionUsersInfoAsync(userNos); + var dto = dtoResult.Data; + await SavePersonAsync(modifiedPersons); + + var rResult = await _devicePermissionApi.RePermissionUsersInfoAsync(dto); + var r = rResult.Data; } } - _logger.LogWarning("同步SAP人员数据结束"); + _logger.LogInformation("SAP根据学工号同步结束:{code}", code); } + private async Task SavePersonAsync(List persons) { - var tenantResult = await _tenantApi.GetTenantsInternalAsync(); - _tenants = tenantResult.Data; - var workCodes = persons.Select(x => x.ZWORKCODE).ToList(); var chunks = workCodes.Chunk(4000).ToList(); - var dtoResult = await _devicePermissionApi.GetPermissionUsersInfoAsync(); - var dto = dtoResult.Data; - foreach (var searchCodes in chunks) { _logger.LogWarning("SAP分批处理Chunk数量:{count}", searchCodes.Length); - var existsWorkCodes = _dbContext.Queryable() + var existsWorkCodes = await _dbContext.Queryable() .Where(x => searchCodes.Contains(x.ename)) .Select(x => x.ename) - .ToList(); + .ToListAsync(); var originUpdates = persons.Where(x => existsWorkCodes.Contains(x.ZWORKCODE)).ToList(); var existsUpdateWorkCodes = originUpdates.Select(x => x.ZWORKCODE).ToList(); @@ -97,14 +158,13 @@ public class GainfoUpdateService : IGainfoUpdateService i++; try { - InsertPerson(item); + await InsertPersonAsync(item); _logger.LogWarning("执行新增成功 工号:{code},姓名:{name}", item.ename, item.gname); } catch (Exception ex) { _logger.LogError(ex, "执行新增失败:{message}", ex.Message); } - } foreach (var item in updates) @@ -112,7 +172,7 @@ public class GainfoUpdateService : IGainfoUpdateService i++; try { - UpdatePerson(item); + await UpdatePersonAsync(item); _logger.LogWarning("执行更新成功 工号:{code},姓名:{name}", item.ename, item.gname); } catch (Exception ex) @@ -121,26 +181,21 @@ public class GainfoUpdateService : IGainfoUpdateService } } } - - var rResult = await _devicePermissionApi.RePermissionUsersInfoAsync(dto); - var r = rResult.Data; - - _logger.LogWarning("生成下发任务数量:{count}", r); } - private void InsertPerson(GainfoSapEntity item) + private async Task InsertPersonAsync(GainfoSapEntity item) { - _dbContext.Client.Insertable(item).ExecuteCommand(); + await _dbContext.InsertAsync(item); var info = GainfoSapToGainfo(item); if (item.isowner == "T") { - _dbContext.Client.Insertable(info).ExecuteCommand(); + await _dbContext.InsertAsync(info); } else { - _dbContext.Client.Insertable(info).AS("GainfoLeave").ExecuteCommand(); + await _dbContext.Client.Insertable(info).AS("GainfoLeave").ExecuteCommandAsync(); } } @@ -177,10 +232,10 @@ public class GainfoUpdateService : IGainfoUpdateService return info; } - private void UpdatePerson(GainfoSapEntity item) + private async Task UpdatePersonAsync(GainfoSapEntity item) { - var gainfoSap = _dbContext.Queryable().Where(x => x.ename == item.ename).First(); - _dbContext.Updateable().SetColumns(g => new GainfoSapEntity + var gainfoSap = await _dbContext.Queryable().Where(x => x.ename == item.ename).FirstAsync(); + await _dbContext.Updateable().SetColumns(g => new GainfoSapEntity { PERNR = item.PERNR, gname = item.gname, @@ -199,16 +254,16 @@ public class GainfoUpdateService : IGainfoUpdateService ZZ_GZDCQNM = item.ZZ_GZDCQNM, }) .Where(x => x.ename == item.ename) - .ExecuteCommand(); + .ExecuteCommandAsync(); - var gainfo = _dbContext.Queryable().Where(x => x.Code == item.ename).First(); + var gainfo = await _dbContext.Queryable().Where(x => x.Code == item.ename).FirstAsync(); if (item.isowner == "T") { if (gainfo != null) { var workplace = _tenants.FirstOrDefault(x => x.Name == item.ZZ_GZDJDNM)?.Code ?? item.ZZ_GZDJDNM; var factory = _tenants.FirstOrDefault(x => x.Name == item.ZZ_GZDCQNM)?.Code ?? item.ZZ_GZDCQNM; - _dbContext.Updateable().SetColumns(g => new GainfoEntity + await _dbContext.Updateable().SetColumns(g => new GainfoEntity { SapNo = item.PERNR, Name = item.gname, @@ -225,21 +280,21 @@ public class GainfoUpdateService : IGainfoUpdateService Factory = factory }) .Where(x => x.Code == item.ename) - .ExecuteCommand(); + .ExecuteCommandAsync(); } else { var info = GainfoSapToGainfo(item); - _dbContext.Client.Insertable(info).ExecuteCommand(); + await _dbContext.InsertAsync(info); } } else { if (gainfo != null) { - _dbContext.Client.Deleteable(gainfo).ExecuteCommand(); + await _dbContext.DeleteReallyAsync(gainfo); gainfo.Id = 0; - _dbContext.Client.Insertable(gainfo).AS("GainfoLeave").ExecuteCommand(); + await _dbContext.Client.Insertable(gainfo).AS("GainfoLeave").ExecuteCommandAsync(); } } } @@ -318,7 +373,7 @@ public class GainfoUpdateService : IGainfoUpdateService } - private List ToGainfoSap(List data) + private static List ToGainfoSap(List data) { return data.Select(person => { @@ -421,7 +476,7 @@ public class GainfoUpdateService : IGainfoUpdateService return null; } - private string SubString(string s, int len) + private static string SubString(string s, int len) { if (string.IsNullOrWhiteSpace(s)) { diff --git a/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs b/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs index 512bee47..07638119 100644 --- a/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs +++ b/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs @@ -62,11 +62,14 @@ public class JinkoSapService : IJinkoSapService return result; } - public async Task GetPersonsAsync() + public async Task GetPersonsByWerkAsync(string werk) { var apiUrl = "/RESTAdapter/Rest/EHR_PERSONNEL/"; - var werks = await _dbContext.Queryable().Where(x => x.Enable).ToListAsync(); + var werks = await _dbContext.Queryable() + .Where(x => x.Enable) + .WhereIF(werk.IsPresent(), x => x.Werk == werk) + .ToListAsync(); List persons = new(); List jobInfos = new(); @@ -139,13 +142,8 @@ public class JinkoSapService : IJinkoSapService }; } - public async Task GetPersonsAsync(string code) + public async Task GetPersonsByCodeAsync(string code) { - if (code.IsEmpty()) - { - return await GetPersonsAsync(); - } - var apiUrl = "/RESTAdapter/Rest/EHR_PERSONNEL/"; List persons = new(); diff --git a/Src/Dlhis.ScheduledTask/Jobs/SyncPersonJob.cs b/Src/Dlhis.ScheduledTask/Jobs/SyncPersonJob.cs index 5d050e21..8af9a27e 100644 --- a/Src/Dlhis.ScheduledTask/Jobs/SyncPersonJob.cs +++ b/Src/Dlhis.ScheduledTask/Jobs/SyncPersonJob.cs @@ -1,11 +1,14 @@ -using Dlhis.ScheduledTask.Common.Services; +using Dlhis.Entity.Basic.Entity; +using Dlhis.ScheduledTask.Common.Services; +using Hangfire; +using Kurisu.AspNetCore.Utils.Extensions; using Kurisu.Scheduled; using Kurisu.Scheduled.Attributes; namespace Dlhis.ScheduledTask.Jobs; /// -/// SAP人员信息同步 +/// SAP人员信息同步(按人事范围拆分) /// [RecurringJob(nameof(SyncPersonJob), "0 0 3,12,18 * * ?", Name = "SAP人员信息同步")] public class SyncPersonJob : BaseJob @@ -16,7 +19,20 @@ public class SyncPersonJob : BaseJob public override async Task RunAsync(IServiceProvider serviceProvider) { - var gainfoUpdate = serviceProvider.GetService(); - await gainfoUpdate.TodoAsync(null); + var werks = await DbContext.Queryable() + .Where(x => x.Enable) + .ToListAsync(); + + foreach (var werk in werks) + { + if (werk.BackgroundJobKey.IsEmpty()) + { + var jobKey = BackgroundJob.Enqueue(x => x.TodoByWerkAsync(werk.Werk)); + werk.BackgroundJobKey = jobKey; + await DbContext.UpdateAsync(werk); + + Logger.LogInformation("SAP创建人事范围{werk}后台调度任务", werk.Werk); + } + } } } \ No newline at end of file diff --git a/Src/Dlhis.Sys.Api/Dlhis.Sys.Api.csproj b/Src/Dlhis.Sys.Api/Dlhis.Sys.Api.csproj index 50a74ddd..45601c93 100644 --- a/Src/Dlhis.Sys.Api/Dlhis.Sys.Api.csproj +++ b/Src/Dlhis.Sys.Api/Dlhis.Sys.Api.csproj @@ -7,7 +7,7 @@ - +