using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; 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.Basedata.Application.Contracts; using Win_in.Sfs.Shared.Domain; using Win_in.Sfs.Wms.DataExchange.Application.Contracts; using Win_in.Sfs.Wms.DataExchange.Domain.Shared; using Win_in.Sfs.Wms.Store.Application.Contracts; namespace Win_in.Sfs.Wms.DataExchange.MesAgent { public class MesL7PartsInfoWorker : AsyncPeriodicBackgroundWorkerBase { private readonly OutgoingOptions _options; private readonly IPostService _postService; private readonly IMesProductL7PartsNoteAppService _mesProductL7PartsNoteAppService; private readonly IProductL7PartsNoteAppService _productL7PartsNoteAppService; private readonly IItemBasicAppService _itemBasicAppService; public MesL7PartsInfoWorker( AbpAsyncTimer timer, IOptions options, IServiceScopeFactory serviceScopeFactory, IProductL7PartsNoteAppService productL7PartsNoteAppService, IMesProductL7PartsNoteAppService mesProductL7PartsNoteAppService, IItemBasicAppService itemBasicAppService, IPostService postService ) : base(timer, serviceScopeFactory) { _options = options.Value.OutgoingOptions; _postService = postService; _itemBasicAppService = itemBasicAppService; _productL7PartsNoteAppService = productL7PartsNoteAppService; _mesProductL7PartsNoteAppService = mesProductL7PartsNoteAppService; Timer.Period = options.Value.OutgoingOptions.PeriodSeconds * 1000; //default 5 minutes } [UnitOfWork] protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext) { Logger.LogInformation("Starting: Handling Outgoing Exchange data..."); if (!_options.Active) { Logger.LogInformation("Outgoing Exchange is not active!"); return; } ////Resolve dependencies //var outgoingDataManager = workerContext // .ServiceProvider // .GetRequiredService(); ////Do the work //var batchSize = _options.BatchSize; //var retryTimes = _options.RetryTimes; //var outgoingDataList = await outgoingDataManager.GetReadyListAsync(batchSize, retryTimes); //Logger.LogInformation($"{outgoingDataList.Count} Outgoing Exchange records were Found"); //foreach (var outgoingData in outgoingDataList) //{ // try // { // //定时主动推送数据到外部系统 // await PostToOtherSystemAsync(outgoingData); // //归档并删除 // await outgoingDataManager.FileAndDeleteAsync(outgoingData); // } // catch (Exception e) // { // Logger.LogException(e); // e = e.GetBaseException(); // outgoingData.SetError(EnumExchangeDataErrorCode.Exception, e.Message); // await outgoingDataManager.UpdateAsync(outgoingData); // } //} Logger.LogInformation("Completed: Handling Outgoing Exchange data..."); } //protected virtual async Task PostToOtherSystemAsync(OutgoingData outgoingData) //{ // //if (Enum.TryParse(outgoingData.DataType, true, out EnumOutgoingDataType dataType)) // //{ // // var baseUrl = _options.BaseUrl; // // var apiUrl = GetApiUrl(dataType, _options); // // var dataContent = outgoingData.DataContent; // // if (!TryDeserialize(dataType, dataContent)) // // { // // outgoingData.SetError(EnumExchangeDataErrorCode.WrongDataFormat, dataType.ToString()); // // } // // var username = _options.Username; // // var password = _options.Password; // // await _postService.PostAsync(baseUrl, apiUrl, dataContent, username, password); // // outgoingData.SetSuccess(); // //} // //else // //{ // // outgoingData.SetError(EnumExchangeDataErrorCode.UnknownDataType, "UnknownDataType"); // //} //} private bool TryDeserialize(EnumOutgoingDataType dataType, string dataContent) { //try //{ // switch (dataType) // { // case EnumOutgoingDataType.Receipt: // var tryReceipt = JsonSerializer.Deserialize(dataContent); // break; // case EnumOutgoingDataType.Return: // var tryReturn = JsonSerializer.Deserialize(dataContent); // break; // case EnumOutgoingDataType.Transfer: // var tryTransfer = JsonSerializer.Deserialize(dataContent); // break; // case EnumOutgoingDataType.BackFlush: // var tryBackFlush = JsonSerializer.Deserialize(dataContent); // break; // case EnumOutgoingDataType.Rework: // var tryRework = JsonSerializer.Deserialize(dataContent); // break; // case EnumOutgoingDataType.PreShipper: // var tryPreShipper = JsonSerializer.Deserialize(dataContent); // break; // case EnumOutgoingDataType.Count: // var tryCount = JsonSerializer.Deserialize(dataContent); // break; // case EnumOutgoingDataType.UnplannedReceipt: // var tryUnplannedReceipt = JsonSerializer.Deserialize(dataContent); // break; // case EnumOutgoingDataType.UnplannedDeliver: // var tryUnplannedDeliver = JsonSerializer.Deserialize(dataContent); // break; // default: // throw new ArgumentOutOfRangeException(nameof(dataType), dataType, null); // } //} //catch (Exception) //{ // return false; //} return true; } private string GetApiUrl(EnumOutgoingDataType dataType, OutgoingOptions options) { var apiUrl = dataType switch { EnumOutgoingDataType.Receipt => options.ApiUrls.Receipt,//采购收货 EnumOutgoingDataType.Return => options.ApiUrls.Return,//采购退货 EnumOutgoingDataType.Transfer => options.ApiUrls.Transfer,//库存转移 EnumOutgoingDataType.BackFlush => options.ApiUrls.BackFlush, //生产回冲 EnumOutgoingDataType.Rework => options.ApiUrls.Rework, //返修 EnumOutgoingDataType.PreShipper => options.ApiUrls.PreShipper, //预发运 EnumOutgoingDataType.UnplannedReceipt => options.ApiUrls.UnplannedReceipt, //计划外入库 EnumOutgoingDataType.UnplannedDeliver => options.ApiUrls.UnplannedDeliver, //计划外出库 EnumOutgoingDataType.Count => options.ApiUrls.Count, //盘点 _ => throw new ArgumentOutOfRangeException(nameof(dataType), dataType, null) }; return apiUrl; } } }