using Dlhis.Basic.Api;
using Dlhis.Basic.Api.Response;
using Dlhis.Entity.Basic.Entity;
using Dlhis.ScheduledTask.Attributes;
namespace Dlhis.ScheduledTask.Jobs;
///
/// SAP组织架构数据同步
///
[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 modifiedOrgList)
{
foreach (SapOrganizationItem item in modifiedOrgList)
{
var department = await DbContext.Queryable().Where(x => x.No == item.OBJID).FirstAsync();
if (department == null)
{
department = new DepartmentEntity
{
No = item.OBJID,
Name = item.STEXT,
PNo = item.SOBID,
DepartmentNoFirst = item.ORGID03,
DepartmentNoSecond = item.ORGID04
};
await DbContext.InsertAsync(department);
}
else
{
department.PNo = item.SOBID;
department.Name = item.STEXT;
department.DepartmentNoFirst = item.ORGID03;
department.DepartmentNoSecond = item.ORGID04;
await DbContext.UpdateAsync(department);
}
}
}
}