using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Volo.Abp; using Volo.Abp.BackgroundWorkers; using Volo.Abp.Data; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; using Volo.Abp.MultiTenancy; using Volo.Abp.TenantManagement; using Volo.Abp.Threading; using Volo.Abp.Uow; using Win_in.Sfs.Scp.v1.Domain; using Win_in.Sfs.Scp.v1.Domain.Asns; using Win_in.Sfs.Scp.WebApi.Asns; namespace Win_in.Sfs.Scp.WebApi { public class AsnBackgroundWorker : AsyncPeriodicBackgroundWorkerBase { private readonly IOptions _options; public AsnBackgroundWorker( AbpAsyncTimer timer, IOptions options, IServiceScopeFactory serviceScopeFactory ) : base(timer, serviceScopeFactory) { _options = options; Timer.Period = options.Value.PeriodSeconds * 1000; //default 5 minutes } [UnitOfWork] protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) { Logger.LogInformation("Get ASN from SCP: Start"); if (!_options.Value.Active) { Logger.LogInformation("Get ASN from SCP: Switch is closed!"); return; } //Resolve dependencies var scpAsnManager = workerContext.ServiceProvider.GetRequiredService(); var x12AsnRepository = workerContext.ServiceProvider.GetRequiredService(); var tenantStore = workerContext.ServiceProvider.GetRequiredService(); var currentTenant = workerContext.ServiceProvider.GetRequiredService(); var dataFilter = workerContext.ServiceProvider.GetRequiredService(); //Do the work var asnX12List = new List(); foreach (var site in _options.Value.Sites) { var siteCode = site.Code; var siteMinUid = site.MinUid; try { var tenant = await tenantStore.FindAsync(siteCode); using (currentTenant.Change(tenant.Id, tenant.Name)) { using (dataFilter.Disable()) { long lastUid = 0; var x12 = await x12AsnRepository .Where(p => p.Site == siteCode) .OrderByDescending(p => p.UID) .FirstOrDefaultAsync(); lastUid = x12?.UID ?? 0; if (lastUid < siteMinUid) { lastUid = siteMinUid; } Logger.LogInformation($"{siteCode}: Last UID is {lastUid}"); var scpAsns = await scpAsnManager.GetUnreadAsnsAsync(siteCode, lastUid, _options.Value.BatchSize); Logger.LogInformation($"{siteCode}: {scpAsns.Count} ASNs were Found"); foreach (var asn in scpAsns) { var barcodes = await scpAsnManager.GetBarcodesAsync(siteCode, asn.AsnBillNum); var asnFactory = new AsnFactory(); var asnX128563060 = asnFactory.CreateAsnX128563060(_options.Value.Receiver, asn, barcodes); var jsonString = JsonSerializer.Serialize(asnX128563060); var ediString = asnX128563060.ToString(); var asnX12 = new X12Asn(asn.Id, asn.Site, asn.AsnBillNum, jsonString, ediString, asn.ShipTime ?? DateTime.Today); asnX12List.Add(asnX12); Logger.LogInformation($"{siteCode}:{asn.Id} {asn.AsnBillNum} was loaded"); } } } } catch (Exception ex) { Logger.LogException(ex); } } foreach (var x12Asn in asnX12List) { var exist = await x12AsnRepository.FirstOrDefaultAsync( p => p.UID == x12Asn.UID && p.Site == x12Asn.Site); if (exist == null) { await x12AsnRepository.InsertAsync(x12Asn); } else { exist.JsonString = x12Asn.JsonString; exist.Reset(); await x12AsnRepository.UpdateAsync(exist); } } await x12AsnRepository.InsertManyAsync(asnX12List, true); Logger.LogInformation("Get ASN from SCP: Complete"); } } }