|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
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;
|
|
|
|
using Volo.Abp.BackgroundWorkers;
|
|
|
|
using Volo.Abp.IdentityServer.Clients;
|
|
|
|
using Volo.Abp.Threading;
|
|
|
|
using Volo.Abp.Uow;
|
|
|
|
using Volo.Abp.Validation;
|
|
|
|
using Win_in.Sfs.Wms.DataExchange.Domain;
|
|
|
|
using Win_in.Sfs.Wms.DataExchange.Domain.Shared;
|
|
|
|
using Win_in.Sfs.Wms.DataExchange.Wms;
|
|
|
|
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.OrderBy(r=>r.CreationTime))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await AddOrUpdateWmsAsync(workerContext, incomingToWms).ConfigureAwait(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (AbpValidationException ex)
|
|
|
|
{
|
|
|
|
string Verification = "";
|
|
|
|
if (ex.ValidationErrors.Count > 0)
|
|
|
|
{
|
|
|
|
Verification = ex.ValidationErrors
|
|
|
|
.Select(err => err.ToString())
|
|
|
|
.Aggregate(string.Empty, (current, next) => string.Format("{0} {1}", current, next));
|
|
|
|
}
|
|
|
|
//throw new UserFriendlyException(Verification + ex.Message + Convert.ToString(ex.InnerException), null, ex.Message);
|
|
|
|
incomingToWms.SetError(EnumExchangeDataErrorCode.Exception, Verification + ex.Message + Convert.ToString(ex.InnerException));
|
|
|
|
}
|
|
|
|
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.ErpLocation:
|
|
|
|
await incomingToWms.HandleErpLocationsAsync(workerContext).ConfigureAwait(false);
|
|
|
|
break;
|
|
|
|
case EnumIncomingDataType.ErpLocationItem:
|
|
|
|
await incomingToWms.HandleErpLocationItemsAsync(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.PurchasePrice:
|
|
|
|
await incomingToWms.HandlePurchasePricesAsync(workerContext).ConfigureAwait(false);
|
|
|
|
break;
|
|
|
|
case EnumIncomingDataType.SalePrice:
|
|
|
|
await incomingToWms.HandleSalePricesAsync(workerContext).ConfigureAwait(false);
|
|
|
|
break;
|
|
|
|
case EnumIncomingDataType.StdCostPrice:
|
|
|
|
await incomingToWms.HandleStdCostPricesAsync(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 incomingToWms.HandleProductReceiptsAsync(workerContext).ConfigureAwait(false);
|
|
|
|
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);
|
|
|
|
// await SendBackFlush(workerContext, incomingToWms).ConfigureAwait(false);
|
|
|
|
break;
|
|
|
|
case EnumIncomingDataType.TransferNote:
|
|
|
|
await incomingToWms.HandleTransferNoteAsync(workerContext).ConfigureAwait(false);
|
|
|
|
break;
|
|
|
|
case EnumIncomingDataType.MesNote:
|
|
|
|
await incomingToWms.HandleMesNoteAsync(workerContext).ConfigureAwait(false);
|
|
|
|
break;
|
|
|
|
case EnumIncomingDataType.Delivery:
|
|
|
|
await incomingToWms.HandleDeliveryRequestAsync(workerContext).ConfigureAwait(false);
|
|
|
|
break;
|
|
|
|
case EnumIncomingDataType.CallMtl:
|
|
|
|
await incomingToWms.HandleInjectionIssueRequestAsync(workerContext).ConfigureAwait(false);
|
|
|
|
break;
|
|
|
|
case EnumIncomingDataType.None:
|
|
|
|
default:
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
}
|
|
|
|
if (incomingToWms.Status != EnumExchangeDataStatus.Error)
|
|
|
|
{
|
|
|
|
incomingToWms.SetSuccess();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private async Task SendBackFlush(PeriodicBackgroundWorkerContext workerContext, IncomingToWms incomingToWms)
|
|
|
|
{ // 定义请求的 URL
|
|
|
|
// string apiUrl = "http://10.164.113.31:60085/api/wms/store/product-receipt-note";
|
|
|
|
|
|
|
|
string apiUrl = _options.Value.IncomingOptions.apiUrl + "api/wms/store/backFlush-note/create-many";
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(_options.Value.IncomingOptions.apiUrl))
|
|
|
|
{
|
|
|
|
var backflushJson = JsonSerializer.Deserialize<BackFlushNoteEditInput>(incomingToWms.DataContent);
|
|
|
|
List<BackFlushNoteEditInput> backflush = new List<BackFlushNoteEditInput>();
|
|
|
|
backflush.Add(backflushJson);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
|
|
|
// 将参数转换为 JSON 格式
|
|
|
|
string jsonContent = JsonSerializer.Serialize(backflush);
|
|
|
|
|
|
|
|
// 创建请求的内容
|
|
|
|
StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
|
|
|
|
|
|
|
|
|
|
|
|
// 发送 POST 请求
|
|
|
|
HttpResponseMessage response = await _httpClient.PostAsync(apiUrl, content).ConfigureAwait(false);
|
|
|
|
|
|
|
|
// 确保响应成功
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
// 读取响应内容并返回
|
|
|
|
string responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (HttpRequestException ex)
|
|
|
|
{
|
|
|
|
// 处理请求异常
|
|
|
|
// 可以记录日志或者抛出异常
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
incomingToWms.SetError(EnumExchangeDataErrorCode.Exception, ex.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.WriteLine("apiUrl未配置!");
|
|
|
|
incomingToWms.SetError(EnumExchangeDataErrorCode.Exception, "apiUrl未配置!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private async Task SendProductReceipt(PeriodicBackgroundWorkerContext workerContext, IncomingToWms incomingToWms)
|
|
|
|
{ // 定义请求的 URL
|
|
|
|
// string apiUrl = "http://10.164.113.31:60085/api/wms/store/product-receipt-note";
|
|
|
|
|
|
|
|
string apiUrl = _options.Value.IncomingOptions.apiUrl + "api/wms/store/product-receipt-note";
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(_options.Value.IncomingOptions.apiUrl))
|
|
|
|
{
|
|
|
|
var productReceiptJson = JsonSerializer.Deserialize<ProductReceiptNoteEditInput>(incomingToWms.DataContent);
|
|
|
|
var memos = productReceiptJson.Details.Select(r => r.Remark).ToList();
|
|
|
|
string memostr = String.Join(", ", memos);
|
|
|
|
productReceiptJson.Worker = "Mes";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
|
|
|
// 将参数转换为 JSON 格式
|
|
|
|
string jsonContent = JsonSerializer.Serialize(productReceiptJson);
|
|
|
|
|
|
|
|
// 创建请求的内容
|
|
|
|
StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
|
|
|
|
|
|
|
|
|
|
|
|
// 发送 POST 请求
|
|
|
|
HttpResponseMessage response = await _httpClient.PostAsync(apiUrl, content).ConfigureAwait(false);
|
|
|
|
|
|
|
|
// 确保响应成功
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
// 读取响应内容并返回
|
|
|
|
string responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
|
|
|
Logger.LogInformation("标签" + memostr);
|
|
|
|
}
|
|
|
|
catch (HttpRequestException ex)
|
|
|
|
{
|
|
|
|
// 处理请求异常
|
|
|
|
// 可以记录日志或者抛出异常
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
incomingToWms.SetError(EnumExchangeDataErrorCode.Exception, ex.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.WriteLine("apiUrl未配置!");
|
|
|
|
incomingToWms.SetError(EnumExchangeDataErrorCode.Exception, "apiUrl未配置!");
|
|
|
|
}
|
|
|
|
//finally
|
|
|
|
//{
|
|
|
|
// //Resolve dependencies
|
|
|
|
// var incomingToWmsManager = workerContext.ServiceProvider.GetRequiredService<IIncomingToWmsManager>();
|
|
|
|
|
|
|
|
// //归档并删除
|
|
|
|
// await incomingToWmsManager.ArchiveAutoSaveAsync(incomingToWms).ConfigureAwait(false);
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|