You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
8.7 KiB
212 lines
8.7 KiB
using System;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using IdentityModel.Client;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Volo.Abp.BackgroundWorkers;
|
|
using Volo.Abp.IdentityServer.Clients;
|
|
using Volo.Abp.Threading;
|
|
using Volo.Abp.Uow;
|
|
using Win_in.Sfs.Wms.DataExchange.Domain;
|
|
using Win_in.Sfs.Wms.DataExchange.Domain.Shared;
|
|
using Win_in.Sfs.Wms.Store.Application.Contracts;
|
|
using static IdentityServer4.Models.IdentityResources;
|
|
|
|
namespace Win_in.Sfs.Wms.DataExchange.Agent;
|
|
|
|
public class IncomingToWmsWorker : AsyncPeriodicBackgroundWorkerBase
|
|
{
|
|
private readonly IOptions<DataExchangeOptions> _options;
|
|
private readonly IUnitOfWorkManager _unitOfWorkManager;
|
|
private readonly HttpClient _httpClient;
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
public IncomingToWmsWorker(
|
|
AbpAsyncTimer timer,
|
|
IOptions<DataExchangeOptions> options,
|
|
IServiceScopeFactory serviceScopeFactory, IUnitOfWorkManager unitOfWorkManager, HttpClient httpClient, IHttpClientFactory httpClientFactory) : base(timer, serviceScopeFactory)
|
|
{
|
|
_options = options;
|
|
_unitOfWorkManager = unitOfWorkManager;
|
|
_httpClient = httpClient;
|
|
_httpClientFactory = httpClientFactory;
|
|
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;
|
|
}
|
|
|
|
await HandleIncomingDataAsync(workerContext).ConfigureAwait(false);
|
|
|
|
Logger.LogInformation("Completed: Handling Incoming Exchange data...");
|
|
}
|
|
|
|
private async Task HandleIncomingDataAsync(PeriodicBackgroundWorkerContext workerContext)
|
|
{
|
|
//Resolve dependencies
|
|
var incomingToWmsManager = workerContext.ServiceProvider.GetRequiredService<IIncomingToWmsManager>();
|
|
//Do the work
|
|
var incomingToWmsList = await incomingToWmsManager.GetToBeProcessedListAsync().ConfigureAwait(false);
|
|
|
|
foreach (var incomingToWms in incomingToWmsList)
|
|
{
|
|
try
|
|
{
|
|
await AddOrUpdateWmsAsync(workerContext, incomingToWms).ConfigureAwait(false);
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
e = e.GetBaseException();
|
|
incomingToWms.SetError(EnumExchangeDataErrorCode.Exception, e.Message);
|
|
}
|
|
|
|
//归档并删除
|
|
await incomingToWmsManager.ArchiveAsync(incomingToWms).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public async Task AddOrUpdateWmsAsync(PeriodicBackgroundWorkerContext workerContext,
|
|
IncomingToWms incomingToWms)
|
|
{
|
|
|
|
if (!Enum.TryParse(incomingToWms.DataType, true, out EnumIncomingDataType dataType))
|
|
{
|
|
incomingToWms.SetError(EnumExchangeDataErrorCode.UnknownDataType, "无法识别的数据类型");
|
|
return;
|
|
}
|
|
|
|
//TODO 完成全部接口
|
|
switch (dataType)
|
|
{
|
|
case EnumIncomingDataType.Department:
|
|
await incomingToWms.HandleDepartmentsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.User:
|
|
await incomingToWms.HandleUsersAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.Item:
|
|
await incomingToWms.HandleItemsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.Location:
|
|
await incomingToWms.HandleErpLocationsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.Bom:
|
|
await incomingToWms.HandleBomsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.Dict:
|
|
await incomingToWms.HandleDictsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.Supplier:
|
|
await incomingToWms.HandleSuppliersAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.SupplierItem:
|
|
await incomingToWms.HandleSupplierItemsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.ItemPack:
|
|
await incomingToWms.HandleItemPacksAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.Customer:
|
|
await incomingToWms.HandleCustomersAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.CustomerItem:
|
|
await incomingToWms.HandleCustomerItemsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.InterfaceCalendar:
|
|
await incomingToWms.HandleInterfaceCalendarsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.PurchaseOrder:
|
|
await incomingToWms.HandlePurchaseOrdersAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.SaleOrder:
|
|
await incomingToWms.HandleSaleOrdersAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.SupplierAsn:
|
|
await incomingToWms.HandleAsnsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.ProductReceipt:
|
|
await SendProductReceipt(workerContext, incomingToWms);
|
|
break;
|
|
case EnumIncomingDataType.MaterialRequest:
|
|
await incomingToWms.HandleMaterialRequestsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.Scrap:
|
|
await incomingToWms.HandleScrapsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.IssueConfirm:
|
|
await incomingToWms.HandleIssueNoteConfirmAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.PurchaseLabel:
|
|
await incomingToWms.HandleInventoryLabelsAsync(workerContext).ConfigureAwait(false);
|
|
break;
|
|
case EnumIncomingDataType.BackFlush:
|
|
await incomingToWms.HandleBackFlushsAsync(workerContext).ConfigureAwait(false);
|
|
|
|
break;
|
|
case EnumIncomingDataType.None:
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
|
|
incomingToWms.SetSuccess();
|
|
}
|
|
|
|
private async Task SendProductReceipt(PeriodicBackgroundWorkerContext workerContext, IncomingToWms incomingToWms)
|
|
{ // 定义请求的 URL
|
|
string apiUrl = "http://10.164.113.31:60085/api/wms/store/product-receipt-note";
|
|
|
|
var productReceiptJson = JsonSerializer.Deserialize<ProductReceiptNoteEditInput>(incomingToWms.DataContent);
|
|
try
|
|
{
|
|
|
|
// 将参数转换为 JSON 格式
|
|
string jsonContent = JsonSerializer.Serialize(productReceiptJson);
|
|
|
|
// 创建请求的内容
|
|
StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
|
|
|
|
|
|
// 发送 POST 请求
|
|
HttpResponseMessage response = await _httpClient.PostAsync(apiUrl, content);
|
|
|
|
// 确保响应成功
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
// 读取响应内容并返回
|
|
string responseBody = await response.Content.ReadAsStringAsync();
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
// 处理请求异常
|
|
// 可以记录日志或者抛出异常
|
|
Console.WriteLine(ex.Message);
|
|
//incomingToWms.SetError(EnumExchangeDataErrorCode.Exception, ex.Message);
|
|
}
|
|
//finally
|
|
//{
|
|
// //Resolve dependencies
|
|
// var incomingToWmsManager = workerContext.ServiceProvider.GetRequiredService<IIncomingToWmsManager>();
|
|
|
|
// //归档并删除
|
|
// await incomingToWmsManager.ArchiveAutoSaveAsync(incomingToWms).ConfigureAwait(false);
|
|
|
|
//}
|
|
|
|
}
|
|
}
|
|
|