56 lines
1.6 KiB
C#
56 lines
1.6 KiB
C#
using Dlhis.Basic.Api;
|
|
using Dlhis.Basic.Api.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.ScheduledTask.Attributes;
|
|
|
|
namespace Dlhis.ScheduledTask.Jobs;
|
|
|
|
/// <summary>
|
|
/// SAP组织架构数据同步
|
|
/// </summary>
|
|
[RecurringJob(nameof(SyncOrganizationJob), "0 0 0/1 * * ? ", Name = "SAP组织架构数据同步")]
|
|
public class SyncOrganizationJob : BaseJob
|
|
{
|
|
private readonly ISyncApi _syncApi;
|
|
|
|
public SyncOrganizationJob(IServiceProvider serviceProvider, ISyncApi syncApi) : base(serviceProvider)
|
|
{
|
|
_syncApi = syncApi;
|
|
}
|
|
|
|
protected override async Task RunAsync(IServiceProvider serviceProvider)
|
|
{
|
|
var orgResult = await _syncApi.SyncOrganizationsAsync();
|
|
|
|
if (orgResult.Org != null && orgResult.Org.Items.Count > 0)
|
|
{
|
|
await SaveOrg(orgResult.Org.Items);
|
|
}
|
|
}
|
|
|
|
private async Task SaveOrg(List<SapOrganizationItem> modifiedOrgList)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
} |