@@ -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 < TenantResponse > _tenants ;
private List < SapPersonDepartmentStatus > _departmentChanges ;
private List < SapPersonJobStatus > _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 < WerkEntity > ( ) . 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 < GainfoEntity > ( ) . 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 < GainfoEntity > ( ) . 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 < SapPersonItem > 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 < GainfoSapEntity > ( )
var existsWorkCodes = await _dbContext . Queryable < GainfoSapEntity > ( )
. 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 < GainfoSapEntity > ( ) . Where ( x = > x . ename = = item . ename ) . First ( ) ;
_dbContext . Updateable < GainfoSapEntity > ( ) . SetColumns ( g = > new GainfoSapEntity
var gainfoSap = await _dbContext . Queryable < GainfoSapEntity > ( ) . Where ( x = > x . ename = = item . ename ) . FirstAsync ( ) ;
await _dbContext . Updateable < GainfoSapEntity > ( ) . 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 < GainfoEntity > ( ) . Where ( x = > x . Code = = item . ename ) . First ( ) ;
var gainfo = await _dbContext . Queryable < GainfoEntity > ( ) . 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 < GainfoEntity > ( ) . SetColumns ( g = > new GainfoEntity
await _dbContext . Updateable < GainfoEntity > ( ) . 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 < GainfoSapEntity > ToGainfoSap ( List < SapPersonItem > data )
private static List < GainfoSapEntity > ToGainfoSap ( List < SapPersonItem > 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 ) )
{