fix 重构数据同步
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.ScheduledTask.SyncSapInfo.Comparers;
|
||||
using Dlhis.ScheduledTask.SyncSapInfo.Dto;
|
||||
using Dlhis.ScheduledTask.SyncSapInfo.Services;
|
||||
using Hangfire;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
|
||||
namespace Dlhis.ScheduledTask.SyncSapInfo.Job;
|
||||
|
||||
/// <summary>
|
||||
/// 部门同步定时任务
|
||||
/// </summary>
|
||||
public class SyncOrganizationJob
|
||||
{
|
||||
private List<SapOrganizationItem> lastUpdatedOrgList = new();
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly IJinkoSapService _jinkoSapService;
|
||||
|
||||
public SyncOrganizationJob(IDbContext dbContext, IJinkoSapService jinkoSapService)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_jinkoSapService = jinkoSapService;
|
||||
}
|
||||
|
||||
public async Task SyncOrgInfo()
|
||||
{
|
||||
var orgResult = await _jinkoSapService.GetSapOrganizationsAsync();
|
||||
|
||||
if (orgResult.Org != null && orgResult.Org.Items.Count > 0)
|
||||
{
|
||||
var modifiedOrgList = GetModifiedOrg(orgResult.Org?.Items);
|
||||
await SaveOrg(modifiedOrgList);
|
||||
}
|
||||
|
||||
var syncPositionJob = BackgroundJob.ContinueJobWith<SyncPositionJob>("SyncOrganizationJob", job => job.SyncPositionInfo(), JobContinuationOptions.OnlyOnSucceededState);
|
||||
BackgroundJob.ContinueJobWith<SyncPersonJob>(syncPositionJob, job => job.SyncPersonInfo(), JobContinuationOptions.OnlyOnSucceededState);
|
||||
}
|
||||
|
||||
private List<SapOrganizationItem> GetModifiedOrg(List<SapOrganizationItem> sapList)
|
||||
{
|
||||
if (lastUpdatedOrgList.Count <= 0)
|
||||
{
|
||||
return sapList;
|
||||
}
|
||||
|
||||
var needRemove = sapList.Intersect(lastUpdatedOrgList, new SapOrganizationComparer());
|
||||
return sapList.Except(needRemove, new SapOrganizationComparer()).ToList();
|
||||
}
|
||||
|
||||
private async Task SaveOrg(List<SapOrganizationItem> modifiedOrgList)
|
||||
{
|
||||
if (modifiedOrgList == null || modifiedOrgList.Count == 0) return;
|
||||
|
||||
foreach (SapOrganizationItem item in modifiedOrgList)
|
||||
{
|
||||
var department = await _dbContext.Queryable<DepartmentEntity>().Where(x => x.No == item.OBJID).FirstAsync();
|
||||
if (department == null)
|
||||
{
|
||||
department = new DepartmentEntity
|
||||
{
|
||||
No = item.OBJID,
|
||||
Name = item.STEXT,
|
||||
PNo = item.SOBID,
|
||||
};
|
||||
|
||||
await _dbContext.InsertAsync(department);
|
||||
}
|
||||
else
|
||||
{
|
||||
department.PNo = item.SOBID;
|
||||
department.Name = item.STEXT;
|
||||
|
||||
await _dbContext.UpdateAsync(department);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user