贾荣国
2 years ago
10 changed files with 252 additions and 109 deletions
@ -1,16 +0,0 @@ |
|||||
namespace Win_in.Sfs.Scp.WebApi.Agent |
|
||||
{ |
|
||||
public class AgentOptions |
|
||||
{ |
|
||||
public IncomingOptions IncomingOptions { get; set; } |
|
||||
} |
|
||||
|
|
||||
public class IncomingOptions |
|
||||
{ |
|
||||
public bool Active { get; set; } = false; |
|
||||
public int PeriodSeconds { get; set; } = 5 * 60; |
|
||||
public int RetryTimes { get; set; } = 3; |
|
||||
public int BatchSize { get; set; } = 100; |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -0,0 +1,136 @@ |
|||||
|
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<AsnOptions> _options; |
||||
|
|
||||
|
public AsnBackgroundWorker( |
||||
|
AbpAsyncTimer timer, |
||||
|
IOptions<AsnOptions> 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<IScpAsnManager>(); |
||||
|
var x12AsnRepository = workerContext.ServiceProvider.GetRequiredService<IX12AsnRepository>(); |
||||
|
var tenantStore = workerContext.ServiceProvider.GetRequiredService<ITenantStore>(); |
||||
|
var currentTenant = workerContext.ServiceProvider.GetRequiredService<ICurrentTenant>(); |
||||
|
var dataFilter = workerContext.ServiceProvider.GetRequiredService<IDataFilter>(); |
||||
|
|
||||
|
//Do the work
|
||||
|
var asnX12List = new List<X12Asn>(); |
||||
|
|
||||
|
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<IMultiTenant>()) |
||||
|
{ |
||||
|
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"); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
@ -1,67 +0,0 @@ |
|||||
using System; |
|
||||
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; |
|
||||
|
|
||||
namespace Win_in.Sfs.Scp.WebApi.Agent |
|
||||
{ |
|
||||
public class IncomingDataWorker : AsyncPeriodicBackgroundWorkerBase |
|
||||
{ |
|
||||
private readonly IOptions<AgentOptions> _options; |
|
||||
|
|
||||
public IncomingDataWorker( |
|
||||
AbpAsyncTimer timer, |
|
||||
IOptions<AgentOptions> options, |
|
||||
IServiceScopeFactory serviceScopeFactory |
|
||||
) : base(timer, serviceScopeFactory) |
|
||||
{ |
|
||||
_options = options; |
|
||||
Timer.Period = options.Value.IncomingOptions.PeriodSeconds * 1000; //default 5 minutes
|
|
||||
} |
|
||||
|
|
||||
[UnitOfWork] |
|
||||
protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) |
|
||||
{ |
|
||||
Logger.LogInformation("Starting: Handling Incoming Exchange data..."); |
|
||||
if (!_options.Value.IncomingOptions.Active) |
|
||||
{ |
|
||||
Logger.LogInformation("Incoming Exchange is not active!"); |
|
||||
return; |
|
||||
} |
|
||||
//Resolve dependencies
|
|
||||
var incomingDataManager = workerContext |
|
||||
.ServiceProvider |
|
||||
.GetRequiredService<IIncomingDataManager>(); |
|
||||
//Do the work
|
|
||||
var incomingDataList = await incomingDataManager.GetReadyListAsync(); |
|
||||
|
|
||||
foreach (var incomingData in incomingDataList) |
|
||||
{ |
|
||||
|
|
||||
try |
|
||||
{ |
|
||||
await UpdateWmsAsync(incomingData); |
|
||||
} |
|
||||
catch (Exception e) |
|
||||
{ |
|
||||
e = e.GetBaseException(); |
|
||||
incomingData.SetError(EnumExchangeDataErrorCode.Exception, e.Message); |
|
||||
} |
|
||||
//归档并删除
|
|
||||
await incomingDataManager.FileAndDeleteAsync(incomingData); |
|
||||
|
|
||||
} |
|
||||
|
|
||||
Logger.LogInformation("Completed: Handling Incoming Exchange data..."); |
|
||||
} |
|
||||
|
|
||||
private async Task UpdateWmsAsync(object incomingData) |
|
||||
{ |
|
||||
throw new NotImplementedException(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
@ -1,14 +1,68 @@ |
|||||
{ |
{ |
||||
"ConnectionStrings": { |
"ConnectionStrings": { |
||||
"DataExchange": "Server=127.0.0.1;Database=DataExchange_Test;uid=sa;pwd=Microsoft2008;" |
"Default": "Server=127.0.0.1;Database=Scp_WebApi;User ID=sa;Password=Microsoft2008;connection timeout=600;" |
||||
}, |
}, |
||||
|
"Serilog": { |
||||
|
"Using": [ "Serilog.Sinks.File", "Serilog.Sinks.Async", "Serilog.Sinks.Console", "Serilog.Sinks.MSSqlServer" ], |
||||
|
"MinimumLevel": { |
||||
|
"Default": "Debug", |
||||
|
"Override": { |
||||
|
"Microsoft": "Information", |
||||
|
"Microsoft.EntityFrameworkCore": "Warning" |
||||
|
} |
||||
|
}, |
||||
|
"WriteTo": [ |
||||
|
{ |
||||
|
"Name": "Async", |
||||
|
"Args": { |
||||
|
"configure": [ |
||||
|
{ |
||||
|
"Name": "File", |
||||
|
"Args": { |
||||
|
"path": "Logs\\log.txt", |
||||
|
"rollingInterval": "Day", |
||||
|
"restrictedToMinimumLevel": "Debug" |
||||
|
} |
||||
|
|
||||
"AgentOptions": { |
} |
||||
"IncomingOptions": { |
] |
||||
"Active": false, |
} |
||||
"PeriodSeconds": 300, |
}, |
||||
"RetryTimes": 3, |
{ |
||||
"BatchSize": 100 |
"Name": "Console", |
||||
} |
"Args": { |
||||
|
"restrictedToMinimumLevel": "Debug", |
||||
|
"outputTemplate": "{Timestamp:HH:mm:ss.fff zzz} [{Level}] {Message} {NewLine}{Exception}" |
||||
|
} |
||||
|
}, |
||||
|
{ |
||||
|
"Name": "MSSqlServer", |
||||
|
"Args": { |
||||
|
"connectionString": "Server=127.0.0.1;Database=Scp_WebApi;User ID=sa;Password=Microsoft2008;connection timeout=600;", |
||||
|
"tableName": "AgentLogs", |
||||
|
"autoCreateSqlTable": true |
||||
|
} |
||||
|
} |
||||
|
], |
||||
|
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ] |
||||
|
}, |
||||
|
"AsnOptions": { |
||||
|
"Active": true, |
||||
|
"PeriodSeconds": 10, |
||||
|
"RetryTimes": 3, |
||||
|
"BatchSize": 10, |
||||
|
"MaxCount": 100, |
||||
|
"Receiver": "IACNA_ID", |
||||
|
"Sites": [ |
||||
|
{ |
||||
|
"Code": "T8", |
||||
|
"MinUid": 200 |
||||
|
}, |
||||
|
{ |
||||
|
"Code": "T5", |
||||
|
"MinUid": 100 |
||||
|
} |
||||
|
] |
||||
} |
} |
||||
|
|
||||
} |
} |
||||
|
Loading…
Reference in new issue