perf: 优化人员同步权限和历史查询

This commit is contained in:
2026-07-27 18:29:13 +08:00
parent 66e1ad5a19
commit 9deb1f3dc1
4 changed files with 153 additions and 85 deletions

View File

@@ -104,10 +104,36 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
.ToListAsync(); .ToListAsync();
var gainfoGroups = await _dbContext.Queryable<GainfoGroupEntity>().ToListAsync(); var gainfoGroups = await _dbContext.Queryable<GainfoGroupEntity>().ToListAsync();
var devicePermissions = await _dbContext.Queryable<DevicePermissionEntity>() var departments = await _dbContext.Queryable<DepartmentEntity>().Select<DepartmentDto>().ToListAsync();
.Where(x => x.IsDeleted == false)
.Select<DevicePermissionDto>() var userAssignValues = gainfos
.ToListAsync(); .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<DevicePermissionDto>();
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<DeviceGroupEntity>() var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>()
.Where(x => x.IsDeleted == false) .Where(x => x.IsDeleted == false)
@@ -115,7 +141,6 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
var deviceRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>() var deviceRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
.Where(x => x.IsDeleted == false) .Where(x => x.IsDeleted == false)
.ToListAsync(); .ToListAsync();
var departments = await _dbContext.Queryable<DepartmentEntity>().Select<DepartmentDto>().ToListAsync();
var result = new ConcurrentDictionary<string, List<TenantAssignTypeDeviceCode>>(); var result = new ConcurrentDictionary<string, List<TenantAssignTypeDeviceCode>>();
@@ -159,6 +184,25 @@ public class DevicePermissionAnalyzeService : IDevicePermissionAnalyzeService
return result.ToDictionary(x => x.Key, x => x.Value); return result.ToDictionary(x => x.Key, x => x.Value);
} }
private async Task<List<DevicePermissionDto>> GetDevicePermissionsAsync(
AssignTypeEnumType assignType,
List<string> assignValues)
{
var result = new List<DevicePermissionDto>();
foreach (var chunk in assignValues.Distinct().Chunk(1000))
{
var items = await _dbContext.Queryable<DevicePermissionEntity>()
.Where(x => x.IsDeleted == false)
.Where(x => x.AssignType == assignType)
.Where(x => chunk.Contains(x.AssignValue))
.Select<DevicePermissionDto>()
.ToListAsync();
result.AddRange(items);
}
return result;
}
private static List<TenantAssignTypeDeviceCode> GetAssignDeviceCodesPermissions( private static List<TenantAssignTypeDeviceCode> GetAssignDeviceCodesPermissions(
AnalyzePackDto analyzePack, AnalyzePackDto analyzePack,

View File

@@ -0,0 +1,8 @@
using Dlhis.Jip.Api.Dto;
namespace Dlhis.Basic.Organization.Repositories;
public interface IGainfoSapRepository : IScopeDependency
{
Task<List<SapPersonItem>> GetLatestPersonsAsync(List<string> workCodes);
}

View File

@@ -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<List<SapPersonItem>> GetLatestPersonsAsync(List<string> workCodes)
{
var persons = new List<SapPersonItem>();
foreach (var chunk in workCodes.Chunk(2000))
{
var items = await _dbContext.Queryable<GainfoSapEntity>()
.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;
}
}

View File

@@ -1,6 +1,7 @@
using Dlhis.Basic.Common.Message.Requests; using Dlhis.Basic.Common.Message.Requests;
using Dlhis.Basic.Common.Message.Services; using Dlhis.Basic.Common.Message.Services;
using Dlhis.Basic.Organization.Comparers; using Dlhis.Basic.Organization.Comparers;
using Dlhis.Basic.Organization.Repositories;
using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Basic.Enums; using Dlhis.Entity.Basic.Enums;
using Dlhis.Entity.Device.Enums; using Dlhis.Entity.Device.Enums;
@@ -20,13 +21,15 @@ public class GainfoUpdateService : IGainfoUpdateService
private readonly IGainfoPermissionService _gainfoPermissionService; private readonly IGainfoPermissionService _gainfoPermissionService;
private readonly IJinkoApiService _dingTalkApi; private readonly IJinkoApiService _dingTalkApi;
private readonly IIdentityUserSyncService _identityUserSyncService; private readonly IIdentityUserSyncService _identityUserSyncService;
private readonly IGainfoSapRepository _gainfoSapRepository;
public GainfoUpdateService(IDbContext dbContext, public GainfoUpdateService(IDbContext dbContext,
ILogger<GainfoUpdateService> logger, ILogger<GainfoUpdateService> logger,
IJinkoSapService jinkoSapService, IJinkoSapService jinkoSapService,
IGainfoPermissionService gainfoPermissionService, IGainfoPermissionService gainfoPermissionService,
IJinkoApiService dingTalkApi, IJinkoApiService dingTalkApi,
IIdentityUserSyncService identityUserSyncService) IIdentityUserSyncService identityUserSyncService,
IGainfoSapRepository gainfoSapRepository)
{ {
_dbContext = dbContext; _dbContext = dbContext;
_logger = logger; _logger = logger;
@@ -34,6 +37,7 @@ public class GainfoUpdateService : IGainfoUpdateService
_gainfoPermissionService = gainfoPermissionService; _gainfoPermissionService = gainfoPermissionService;
_dingTalkApi = dingTalkApi; _dingTalkApi = dingTalkApi;
_identityUserSyncService = identityUserSyncService; _identityUserSyncService = identityUserSyncService;
_gainfoSapRepository = gainfoSapRepository;
} }
@@ -111,16 +115,6 @@ public class GainfoUpdateService : IGainfoUpdateService
} }
} }
public async Task UpdatePersonAsync(string userCode)
{
var gainfoSap = await _dbContext.Queryable<GainfoSapEntity>()
.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<SapPersonItem> persons) private async Task SavePersonAsync(List<SapPersonItem> persons)
{ {
@@ -139,15 +133,8 @@ public class GainfoUpdateService : IGainfoUpdateService
_logger.LogWarning("SAP分批处理Chunk数量:{count}", searchCodes.Length); _logger.LogWarning("SAP分批处理Chunk数量:{count}", searchCodes.Length);
var chunkThirdPartPersons = thirdPartPersons.Where(x => searchCodes.Contains(x.ZWORKCODE)).ToList(); var chunkThirdPartPersons = thirdPartPersons.Where(x => searchCodes.Contains(x.ZWORKCODE)).ToList();
var histories = await _dbContext.Queryable<GainfoSapEntity>() var latestHistoryPersons = (await _gainfoSapRepository.GetLatestPersonsAsync(searchCodes.ToList()))
.Where(x => searchCodes.Contains(x.ename)) .ToDictionary(x => x.ZWORKCODE, x => x);
.ToListAsync();
var latestHistoryPersons = histories
.OrderByDescending(x => x.logdate)
.ThenByDescending(x => x.id)
.GroupBy(x => x.ename)
.ToDictionary(x => x.Key, x => ToSapPersonItem(x.First()));
foreach (var person in chunkThirdPartPersons) foreach (var person in chunkThirdPartPersons)
{ {
@@ -455,7 +442,7 @@ public class GainfoUpdateService : IGainfoUpdateService
return sapList; return sapList;
} }
var last = await LoadDataAsync(workCodes); var last = await _gainfoSapRepository.GetLatestPersonsAsync(workCodes);
if (last.Count <= 0) if (last.Count <= 0)
{ {
return sapList; return sapList;
@@ -521,33 +508,6 @@ public class GainfoUpdateService : IGainfoUpdateService
return modifiedPersons; return modifiedPersons;
} }
private async Task<List<SapPersonItem>> LoadDataAsync(List<string> workCodes)
{
var gainfos = new List<GainfoSapEntity>();
var chunks = workCodes.Chunk(2000);
foreach (var chunk in chunks)
{
var items = await _dbContext.Queryable<GainfoSapEntity>()
.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<GainfoSapEntity> ToGainfoSap(List<SapPersonItem> data)
{
return data.Select(ToGainfoSap).ToList();
}
private static GainfoSapEntity ToGainfoSap(SapPersonItem person) private static GainfoSapEntity ToGainfoSap(SapPersonItem person)
{ {
var entryDate = person.ZRZRQ; 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) private static string GetNative(string idNo)
{ {
if (string.IsNullOrWhiteSpace(idNo) || idNo.Length < 6) return null; if (string.IsNullOrWhiteSpace(idNo) || idNo.Length < 6) return null;