fix 尝试处理iserviceprovider问题
This commit is contained in:
@@ -6,20 +6,19 @@ using Dlhis.Entity.Basic.Enums;
|
||||
using Dlhis.ScheduledTask.SyncSapInfo.Dto;
|
||||
using Dlhis.ScheduledTask.SyncSapInfo.Comparers;
|
||||
using Dlhis.ScheduledTask.SyncSapInfo.Services;
|
||||
using Kurisu.AspNetCore.Scope;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Dlhis.ScheduledTask.SyncSapInfo.Job;
|
||||
|
||||
public class SyncPersonJob
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IJinkoSapService _jinkoSapService;
|
||||
private List<PositionEntity> _positions;
|
||||
|
||||
public SyncPersonJob(IDbContext dbContext, ILogger<SyncPersonJob> logger, IJinkoSapService jinkoSapService)
|
||||
public SyncPersonJob(ILogger<SyncPersonJob> logger, IJinkoSapService jinkoSapService)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
_jinkoSapService = jinkoSapService;
|
||||
}
|
||||
@@ -32,13 +31,17 @@ public class SyncPersonJob
|
||||
_logger.LogInformation("获取SAP人员数量为:{count}", persons.Count);
|
||||
if (persons.Count > 0)
|
||||
{
|
||||
_positions = await _dbContext.Queryable<PositionEntity>().ToListAsync();
|
||||
var modifiedPersons = await GetModifiedPersonAsync(persons.OrderByDescending(x => x.ZSTATUS).ThenBy(x => x.ZRZRQ).ToList());
|
||||
_logger.LogInformation("SAP人员差异数量为:{count}", modifiedPersons.Count);
|
||||
if (modifiedPersons.Count > 0)
|
||||
await Scoped.Temp.Value.CreateAsync(async sp =>
|
||||
{
|
||||
var failedRecords = SavePerson(modifiedPersons);
|
||||
}
|
||||
var dbContext = sp.GetService<IDbContext>();
|
||||
_positions = await dbContext.Queryable<PositionEntity>().ToListAsync();
|
||||
var modifiedPersons = await GetModifiedPersonAsync(dbContext, persons.OrderByDescending(x => x.ZSTATUS).ThenBy(x => x.ZRZRQ).ToList());
|
||||
_logger.LogInformation("SAP人员差异数量为:{count}", modifiedPersons.Count);
|
||||
if (modifiedPersons.Count > 0)
|
||||
{
|
||||
var failedRecords = SavePerson(modifiedPersons);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,61 +51,67 @@ public class SyncPersonJob
|
||||
var workCodes = persons.Select(x => x.ZWORKCODE).ToList();
|
||||
var chunks = workCodes.Chunk(4000).ToList();
|
||||
|
||||
foreach (var searchCodes in chunks)
|
||||
await Scoped.Temp.Value.CreateAsync(async sp =>
|
||||
{
|
||||
_logger.LogInformation("SAP分批处理Chunk数量:{count}", searchCodes.Count());
|
||||
var dbContext = sp.GetService<IDbContext>();
|
||||
|
||||
var existsWorkCodes = await _dbContext.Queryable<GainfoSapEntity>()
|
||||
.Where(x => searchCodes.Contains(x.ename))
|
||||
.Select(x => x.ename)
|
||||
.ToListAsync();
|
||||
|
||||
var originUpdates = persons.Where(x => existsWorkCodes.Contains(x.ZWORKCODE)).ToList();
|
||||
var existsUpdateWorkCodes = originUpdates.Select(x => x.ZWORKCODE).ToList();
|
||||
var originInserts = persons.Where(x => searchCodes.Contains(x.ZWORKCODE)).Where(x => !existsUpdateWorkCodes.Contains(x.ZWORKCODE)).ToList();
|
||||
|
||||
|
||||
int i = 0;
|
||||
var currentTotal = originUpdates.Count + originInserts.Count;
|
||||
|
||||
var updates = ToGainfoSap(originUpdates);
|
||||
_logger.LogInformation("SAP分批更新数:{count}", updates.Count);
|
||||
var inserts = ToGainfoSap(originInserts);
|
||||
_logger.LogInformation("SAP分批新增数:{count}", inserts.Count);
|
||||
foreach (var item in inserts)
|
||||
foreach (var searchCodes in chunks)
|
||||
{
|
||||
i++;
|
||||
try
|
||||
_logger.LogInformation("SAP分批处理Chunk数量:{count}", searchCodes.Count());
|
||||
|
||||
var existsWorkCodes = await dbContext.Queryable<GainfoSapEntity>()
|
||||
.Where(x => searchCodes.Contains(x.ename))
|
||||
.Select(x => x.ename)
|
||||
.ToListAsync();
|
||||
|
||||
var originUpdates = persons.Where(x => existsWorkCodes.Contains(x.ZWORKCODE)).ToList();
|
||||
var existsUpdateWorkCodes = originUpdates.Select(x => x.ZWORKCODE).ToList();
|
||||
var originInserts = persons.Where(x => searchCodes.Contains(x.ZWORKCODE)).Where(x => !existsUpdateWorkCodes.Contains(x.ZWORKCODE)).ToList();
|
||||
|
||||
|
||||
int i = 0;
|
||||
var currentTotal = originUpdates.Count + originInserts.Count;
|
||||
|
||||
var updates = ToGainfoSap(originUpdates);
|
||||
_logger.LogInformation("SAP分批更新数:{count}", updates.Count);
|
||||
var inserts = ToGainfoSap(originInserts);
|
||||
_logger.LogInformation("SAP分批新增数:{count}", inserts.Count);
|
||||
foreach (var item in inserts)
|
||||
{
|
||||
await SavePerson(item);
|
||||
i++;
|
||||
try
|
||||
{
|
||||
await SavePerson(dbContext, item);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
failure++;
|
||||
_logger.LogError(ex, "执行新增失败:{message}", ex.Message);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
foreach (var item in updates)
|
||||
{
|
||||
failure++;
|
||||
_logger.LogError(ex, "执行新增失败:{message}", ex.Message);
|
||||
i++;
|
||||
try
|
||||
{
|
||||
await UpdatePerson(dbContext, item);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
failure++;
|
||||
_logger.LogError(ex, "执行更新失败:{message}", ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
foreach (var item in updates)
|
||||
{
|
||||
i++;
|
||||
try
|
||||
{
|
||||
await UpdatePerson(item);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
failure++;
|
||||
_logger.LogError(ex, "执行更新失败:{message}", ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private async Task SavePerson(GainfoSapEntity item)
|
||||
private async Task SavePerson(IDbContext db, GainfoSapEntity item)
|
||||
{
|
||||
var info = new GainfoEntity
|
||||
{
|
||||
@@ -130,12 +139,13 @@ public class SyncPersonJob
|
||||
Werks = item.WERKS,
|
||||
Employed = item.isowner,
|
||||
};
|
||||
await _dbContext.InsertAsync(info);
|
||||
|
||||
await db.InsertAsync(info);
|
||||
}
|
||||
private async Task UpdatePerson(GainfoSapEntity item)
|
||||
private async Task UpdatePerson(IDbContext db, GainfoSapEntity item)
|
||||
{
|
||||
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.IdNo == item.idno).FirstAsync();
|
||||
await _dbContext.Updateable<GainfoEntity>().SetColumns(g => new GainfoEntity
|
||||
var gainfo = await db.Queryable<GainfoEntity>().Where(x => x.IdNo == item.idno).FirstAsync();
|
||||
await db.Updateable<GainfoEntity>().SetColumns(g => new GainfoEntity
|
||||
{
|
||||
SapNo = item.PERNR,
|
||||
Name = item.gname,
|
||||
@@ -152,9 +162,9 @@ public class SyncPersonJob
|
||||
.ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
private async Task<List<SapPersonItem>> GetModifiedPersonAsync(List<SapPersonItem> sapList)
|
||||
private async Task<List<SapPersonItem>> GetModifiedPersonAsync(IDbContext db, List<SapPersonItem> sapList)
|
||||
{
|
||||
var last = await LoadDataAsync();
|
||||
var last = await LoadDataAsync(db);
|
||||
if (last.Count <= 0)
|
||||
{
|
||||
return sapList;
|
||||
@@ -165,9 +175,9 @@ public class SyncPersonJob
|
||||
}
|
||||
|
||||
|
||||
private async Task<List<SapPersonItem>> LoadDataAsync()
|
||||
private async Task<List<SapPersonItem>> LoadDataAsync(IDbContext db)
|
||||
{
|
||||
var gainfo = await _dbContext.Queryable<GainfoSapEntity>().ToListAsync();
|
||||
var gainfo = await db.Queryable<GainfoSapEntity>().ToListAsync();
|
||||
|
||||
return gainfo.Select(x => new SapPersonItem
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user