using System; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Volo.Abp.BackgroundWorkers; using Volo.Abp.Threading; using Volo.Abp.Uow; using Win_in.Sfs.Wms.DataExchange.Domain.Shared; using Win_in.Sfs.Wms.DataExchange.Fawtyg.TyrpAgent.Incoming; namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.TyrpAgent; public class TyrpIncomingBackgroundWorker : AsyncPeriodicBackgroundWorkerBase { private readonly string Incoming = "TYRP Incoming"; private readonly IOptions _options; public TyrpIncomingBackgroundWorker( AbpAsyncTimer timer, IOptions options, IServiceScopeFactory serviceScopeFactory ) : base(timer, serviceScopeFactory) { _options = options; Timer.Period = options.Value.IncomingOptions.PeriodSeconds * 1000; //default 10 minutes common.isupdate = true; common.updatedt = DateTime.Now.AddDays(-1); } [UnitOfWork] protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) { Logger.LogInformation($"Starting: Handling {Incoming}"); if (!_options.Value.IncomingOptions.Active) { Logger.LogInformation($"{Incoming} is not active!"); return; } //var repository = workerContext.ServiceProvider.GetRequiredService(); //var list = await repository.GetListAsync(); //foreach (var part in list) //{ // Console.WriteLine(part); // part.Name = DateTime.Now.Ticks.ToString(); // await repository.UpdateAsync(part); //} if (DateTime.Now.Day > common.updatedt.Day) { if (common.isupdate) { common.updatedt = DateTime.Now; common.isupdate = false; //Logger.LogInformation($"Read ItemBasic");//零件 //var itemBasicReader = workerContext.ServiceProvider.GetRequiredService(); //var itemBasicConverter = workerContext.ServiceProvider.GetRequiredService(); ////读取并保存itemBasic //var itemBasicOutsFromExternalList = await itemBasicReader.ReadAsync().ConfigureAwait(false); ////转换itemBasic //await itemBasicConverter.ConvertAsync(itemBasicOutsFromExternalList).ConfigureAwait(false); Logger.LogInformation($"Read User");//用户和部门 var userReader = workerContext.ServiceProvider.GetRequiredService(); var userConverter = workerContext.ServiceProvider.GetRequiredService(); var departmentConverter = workerContext.ServiceProvider.GetRequiredService(); //读取并保存user var outsFromExternalList = await userReader.ReadAsync().ConfigureAwait(false); var userOutsFromExternalList = outsFromExternalList.Where(r => r.DataType == EnumIncomingDataType.User.ToString()).ToList(); var departmentOutsFromExternalList = outsFromExternalList.Where(r => r.DataType == EnumIncomingDataType.Department.ToString()).ToList(); //转换user await userConverter.ConvertAsync(userOutsFromExternalList).ConfigureAwait(false); //转换department await departmentConverter.ConvertAsync(departmentOutsFromExternalList).ConfigureAwait(false); Logger.LogInformation($"Read InterfaceCalendar");//账期 var interfaceCalendarReader = workerContext.ServiceProvider.GetRequiredService(); var interfaceCalendarConverter = workerContext.ServiceProvider.GetRequiredService(); //读取并保存InterfaceCalendar var interfaceCalendarOutsFromExternalList = await interfaceCalendarReader.ReadAsync().ConfigureAwait(false); //转换InterfaceCalendar await interfaceCalendarConverter.ConvertAsync(interfaceCalendarOutsFromExternalList).ConfigureAwait(false); Logger.LogInformation($"Read Dict");//系统代码 var dictReader = workerContext.ServiceProvider.GetRequiredService(); var dictConverter = workerContext.ServiceProvider.GetRequiredService(); //读取并保存Dict var dictOutsFromExternalList = await dictReader.ReadAsync().ConfigureAwait(false); //转换Dict await dictConverter.ConvertAsync(dictOutsFromExternalList).ConfigureAwait(false); //Logger.LogInformation($"Read Bom"); //var BomReader = workerContext.ServiceProvider.GetRequiredService(); //var BomConverter = workerContext.ServiceProvider.GetRequiredService(); ////读取并保存Bom //var bomsFromExternalList = await BomReader.ReadAsync().ConfigureAwait(false); ////转换Bom //await BomConverter.ConvertAsync(bomsFromExternalList).ConfigureAwait(false); Logger.LogInformation($"Read Supplier");//供应商 var SupplierReader = workerContext.ServiceProvider.GetRequiredService(); var SupplierConverter = workerContext.ServiceProvider.GetRequiredService(); //读取并保存Supplier var suppliersFromExternalList = await SupplierReader.ReadAsync().ConfigureAwait(false); //转换Supplier await SupplierConverter.ConvertAsync(suppliersFromExternalList).ConfigureAwait(false); Logger.LogInformation($"Read Dictpj");//专案代码 var DictpjReader = workerContext.ServiceProvider.GetRequiredService(); //读取并保存Dictpj var dictpjOutsFromExternalList = await DictpjReader.ReadAsync().ConfigureAwait(false); //转换Dictpj await dictConverter.ConvertAsync(dictOutsFromExternalList).ConfigureAwait(false); Logger.LogInformation($"Read Customer");//客户资料 var CustomerReader = workerContext.ServiceProvider.GetRequiredService(); var CustomerConverter = workerContext.ServiceProvider.GetRequiredService(); //读取并保存Customer var customersFromExternalList = await CustomerReader.ReadAsync().ConfigureAwait(false); //转换Customer await CustomerConverter.ConvertAsync(customersFromExternalList).ConfigureAwait(false); } else { common.isupdate = true; } } //Logger.LogInformation($"Read BackFlush");//耗用单 //var BackFlushReader = workerContext.ServiceProvider.GetRequiredService(); //var BackFlushConverter = workerContext.ServiceProvider.GetRequiredService(); ////读取并保存Customer //var backFlushsFromExternalList = await BackFlushReader.ReadAsync().ConfigureAwait(false); ////转换Customer //await BackFlushConverter.ConvertAsync(backFlushsFromExternalList).ConfigureAwait(false); Logger.LogInformation($"Completed: Handling {Incoming}"); } }