diff --git a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionAnalyzeService.cs b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionAnalyzeService.cs index c3aa25e9..69f7af2b 100644 --- a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionAnalyzeService.cs +++ b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionAnalyzeService.cs @@ -104,10 +104,36 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService .ToListAsync(); var gainfoGroups = await _dbContext.Queryable().ToListAsync(); - var devicePermissions = await _dbContext.Queryable() - .Where(x => x.IsDeleted == false) - .Select() - .ToListAsync(); + var departments = await _dbContext.Queryable().Select().ToListAsync(); + + var userAssignValues = gainfos + .Select(x => x.No) + .Distinct() + .ToList(); + + var groupAssignValues = gainfoGroupRelationships + .SelectMany(x => RecursionHelper.HandlerGainfoGroupPreviousCodes(x.GroupCode, gainfoGroups)) + .Select(x => x.ToString()) + .Distinct() + .ToList(); + + var departmentAssignValues = gainfos + .SelectMany(x => RecursionHelper.HandlerDepartmentPreviousCodes(x.DepartmentNo, departments) + .Select(departmentNo => $"{x.Factory}:{departmentNo}")) + .Distinct() + .ToList(); + + var tenantAssignValues = gainfos + .Select(x => x.Factory) + .Where(x => x.IsPresent()) + .Distinct() + .ToList(); + + var devicePermissions = new List(); + devicePermissions.AddRange(await GetDevicePermissionsAsync(AssignTypeEnumType.User, userAssignValues)); + devicePermissions.AddRange(await GetDevicePermissionsAsync(AssignTypeEnumType.Group, groupAssignValues)); + devicePermissions.AddRange(await GetDevicePermissionsAsync(AssignTypeEnumType.Department, departmentAssignValues)); + devicePermissions.AddRange(await GetDevicePermissionsAsync(AssignTypeEnumType.Tenant, tenantAssignValues)); var deviceGroups = await _dbContext.Queryable() .Where(x => x.IsDeleted == false) @@ -115,7 +141,6 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService var deviceRelationships = await _dbContext.Queryable() .Where(x => x.IsDeleted == false) .ToListAsync(); - var departments = await _dbContext.Queryable().Select().ToListAsync(); var result = new ConcurrentDictionary>(); @@ -159,6 +184,25 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService return result.ToDictionary(x => x.Key, x => x.Value); } + private async Task> GetDevicePermissionsAsync( + AssignTypeEnumType assignType, + List assignValues) + { + var result = new List(); + foreach (var chunk in assignValues.Distinct().Chunk(1000)) + { + var items = await _dbContext.Queryable() + .Where(x => x.IsDeleted == false) + .Where(x => x.AssignType == assignType) + .Where(x => chunk.Contains(x.AssignValue)) + .Select() + .ToListAsync(); + result.AddRange(items); + } + + return result; + } + private static List GetAssignDeviceCodesPermissions( AnalyzePackDto analyzePack, @@ -326,4 +370,4 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService Factory = x.Factory }).ToListAsync(); } -} \ No newline at end of file +} diff --git a/Src/Dlhis.Basic/Organization/Repositories/IGainfoSapRepository.cs b/Src/Dlhis.Basic/Organization/Repositories/IGainfoSapRepository.cs new file mode 100644 index 00000000..dd9d3034 --- /dev/null +++ b/Src/Dlhis.Basic/Organization/Repositories/IGainfoSapRepository.cs @@ -0,0 +1,8 @@ +using Dlhis.Jip.Api.Dto; + +namespace Dlhis.Basic.Organization.Repositories; + +public interface IGainfoSapRepository : IScopeDependency +{ + Task> GetLatestPersonsAsync(List workCodes); +} diff --git a/Src/Dlhis.Basic/Organization/Repositories/Implements/GainfoSapRepository.cs b/Src/Dlhis.Basic/Organization/Repositories/Implements/GainfoSapRepository.cs new file mode 100644 index 00000000..9ccbdb5d --- /dev/null +++ b/Src/Dlhis.Basic/Organization/Repositories/Implements/GainfoSapRepository.cs @@ -0,0 +1,87 @@ +using Dlhis.Entity.Basic.Entity; +using Dlhis.Jip.Api.Dto; +using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; +using SqlSugar; + +namespace Dlhis.Basic.Organization.Repositories.Implements; + +public class GainfoSapRepository : IGainfoSapRepository +{ + private readonly IDbContext _dbContext; + + public GainfoSapRepository(IDbContext dbContext) + { + _dbContext = dbContext; + } + + public async Task> GetLatestPersonsAsync(List workCodes) + { + var persons = new List(); + foreach (var chunk in workCodes.Chunk(2000)) + { + var items = await _dbContext.Queryable() + .Where(x => chunk.Contains(x.ename)) + .Select(x => new + { + RowNumber = SqlFunc.RowNumber(SqlFunc.Desc(x.logdate), x.ename), + x.gname, + x.gname2, + x.ename, + x.idno, + x.birthday, + x.email, + x.appellation, + x.phone, + x.ZSTATUS, + x.createdate, + x.PERNR, + x.WERKS, + x.KOSTL, + x.ZZ_ZD, + x.Z_ZJ, + x.STELL, + x.STELL_NM, + x.ZLZRQ, + x.MASSN, + x.MNTXT, + x.ZZ_COUNNM, + x.ZZ_GZDJD, + x.ZZ_GZDCQ + }) + .MergeTable() + .Where(x => x.RowNumber == 1) + .Select(x => new SapPersonItem + { + NAME = x.gname, + NAME2 = x.gname2, + ZWORKCODE = x.ename, + ZZ_SFZH = x.idno, + GBDAT = x.birthday, + ZXUELI = x.email, + ORGEH = x.appellation, + ZMOBILE = x.phone, + ZSTATUS = x.ZSTATUS, + ZRZRQ = x.createdate, + PERNR = x.PERNR, + WERKS = x.WERKS, + KOSTL_XJ = x.KOSTL, + ZZ_ZD = x.ZZ_ZD, + Z_ZJ = x.Z_ZJ, + PLANS = x.STELL, + STELL = x.STELL, + STELL_NM = x.STELL_NM, + ZLZRQ = x.ZLZRQ, + MASSN = x.MASSN, + MNTXT = x.MNTXT, + ZZ_COUNNM = x.ZZ_COUNNM, + ZZ_GZDJD = x.ZZ_GZDJD, + ZZ_GZDCQ = x.ZZ_GZDCQ + }) + .ToListAsync(); + + persons.AddRange(items); + } + + return persons; + } +} diff --git a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoUpdateService.cs b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoUpdateService.cs index 6528c05c..15860f92 100644 --- a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoUpdateService.cs +++ b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoUpdateService.cs @@ -1,6 +1,7 @@ using Dlhis.Basic.Common.Message.Requests; using Dlhis.Basic.Common.Message.Services; using Dlhis.Basic.Organization.Comparers; +using Dlhis.Basic.Organization.Repositories; using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Basic.Enums; using Dlhis.Entity.Device.Enums; @@ -20,13 +21,15 @@ public class GainfoUpdateService : IGainfoUpdateService private readonly IGainfoPermissionService _gainfoPermissionService; private readonly IJinkoApiService _dingTalkApi; private readonly IIdentityUserSyncService _identityUserSyncService; + private readonly IGainfoSapRepository _gainfoSapRepository; public GainfoUpdateService(IDbContext dbContext, ILogger logger, IJinkoSapService jinkoSapService, IGainfoPermissionService gainfoPermissionService, IJinkoApiService dingTalkApi, - IIdentityUserSyncService identityUserSyncService) + IIdentityUserSyncService identityUserSyncService, + IGainfoSapRepository gainfoSapRepository) { _dbContext = dbContext; _logger = logger; @@ -34,6 +37,7 @@ public class GainfoUpdateService : IGainfoUpdateService _gainfoPermissionService = gainfoPermissionService; _dingTalkApi = dingTalkApi; _identityUserSyncService = identityUserSyncService; + _gainfoSapRepository = gainfoSapRepository; } @@ -111,16 +115,6 @@ public class GainfoUpdateService : IGainfoUpdateService } } - public async Task UpdatePersonAsync(string userCode) - { - var gainfoSap = await _dbContext.Queryable() - .Where(x => x.ename == userCode) - .Where(x => x.isowner == "F") - .OrderByDescending(x => new { x.logdate, x.id }) - .FirstAsync(); - await UpdatePersonAsync(gainfoSap); - } - private async Task SavePersonAsync(List persons) { @@ -139,15 +133,8 @@ public class GainfoUpdateService : IGainfoUpdateService _logger.LogWarning("SAP分批处理Chunk数量:{count}", searchCodes.Length); var chunkThirdPartPersons = thirdPartPersons.Where(x => searchCodes.Contains(x.ZWORKCODE)).ToList(); - var histories = await _dbContext.Queryable() - .Where(x => searchCodes.Contains(x.ename)) - .ToListAsync(); - - var latestHistoryPersons = histories - .OrderByDescending(x => x.logdate) - .ThenByDescending(x => x.id) - .GroupBy(x => x.ename) - .ToDictionary(x => x.Key, x => ToSapPersonItem(x.First())); + var latestHistoryPersons = (await _gainfoSapRepository.GetLatestPersonsAsync(searchCodes.ToList())) + .ToDictionary(x => x.ZWORKCODE, x => x); foreach (var person in chunkThirdPartPersons) { @@ -455,7 +442,7 @@ public class GainfoUpdateService : IGainfoUpdateService return sapList; } - var last = await LoadDataAsync(workCodes); + var last = await _gainfoSapRepository.GetLatestPersonsAsync(workCodes); if (last.Count <= 0) { return sapList; @@ -521,33 +508,6 @@ public class GainfoUpdateService : IGainfoUpdateService return modifiedPersons; } - private async Task> LoadDataAsync(List workCodes) - { - var gainfos = new List(); - var chunks = workCodes.Chunk(2000); - foreach (var chunk in chunks) - { - var items = await _dbContext.Queryable() - .Where(x => chunk.Contains(x.ename)) - .ToListAsync(); - var latestItems = items - .OrderByDescending(x => x.logdate) - .ThenByDescending(x => x.id) - .GroupBy(x => x.ename) - .Select(x => x.First()) - .ToList(); - gainfos.AddRange(latestItems); - } - - return gainfos.Select(ToSapPersonItem).ToList(); - } - - - private static List ToGainfoSap(List data) - { - return data.Select(ToGainfoSap).ToList(); - } - private static GainfoSapEntity ToGainfoSap(SapPersonItem person) { var entryDate = person.ZRZRQ; @@ -603,37 +563,6 @@ public class GainfoUpdateService : IGainfoUpdateService }; } - private static SapPersonItem ToSapPersonItem(GainfoSapEntity x) - { - return new SapPersonItem - { - NAME = x.gname, - NAME2 = x.gname2, - ZWORKCODE = x.ename, - ZZ_SFZH = x.idno, - GBDAT = x.birthday, - ZXUELI = x.email, - ORGEH = x.appellation, - ZMOBILE = x.phone, - ZSTATUS = x.ZSTATUS, - ZRZRQ = x.createdate, - PERNR = x.PERNR, - WERKS = x.WERKS, - KOSTL_XJ = x.KOSTL, - ZZ_ZD = x.ZZ_ZD, - Z_ZJ = x.Z_ZJ, - PLANS = x.STELL, //x.STELL, 20251129变更 - STELL = x.STELL, - STELL_NM = x.STELL_NM, - ZLZRQ = x.ZLZRQ, - MASSN = x.MASSN, - MNTXT = x.MNTXT, - ZZ_COUNNM = x.ZZ_COUNNM, - ZZ_GZDJD = x.ZZ_GZDJD, - ZZ_GZDCQ = x.ZZ_GZDCQ - }; - } - private static string GetNative(string idNo) { if (string.IsNullOrWhiteSpace(idNo) || idNo.Length < 6) return null;