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.
306 lines
13 KiB
306 lines
13 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.Serialization;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using AutoMapper;
|
|
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.Data;
|
|
using Volo.Abp.Domain.Entities;
|
|
using Volo.Abp.Domain.Repositories;
|
|
using Volo.Abp.MultiTenancy;
|
|
using Volo.Abp.ObjectMapping;
|
|
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.Agent;
|
|
using Win_in.Sfs.Scp.WebApi.Asns;
|
|
using Win_in.Sfs.Scp.WebApi.EntityFrameworkCore;
|
|
using IObjectMapper = Volo.Abp.ObjectMapping.IObjectMapper;
|
|
|
|
namespace Win_in.Sfs.Scp.WebApi
|
|
{
|
|
public class AsnBackgroundWorker : AsyncPeriodicBackgroundWorkerBase
|
|
{
|
|
private readonly AsnOptions _options;
|
|
private readonly IObjectMapper _objectMapper;
|
|
private readonly IPostService _postService;
|
|
|
|
public AsnBackgroundWorker(
|
|
AbpAsyncTimer timer,
|
|
IOptions<AsnOptions> options,
|
|
IServiceScopeFactory serviceScopeFactory,
|
|
IObjectMapper objectMapper,
|
|
IPostService postService
|
|
) : base(timer, serviceScopeFactory)
|
|
{
|
|
_options = options.Value;
|
|
_objectMapper = objectMapper;
|
|
Timer.Period = options.Value.PeriodSeconds * 1000; //default 5 minutes
|
|
_postService = postService;
|
|
}
|
|
|
|
[UnitOfWork]
|
|
protected override async Task DoWorkAsync(PeriodicBackgroundWorkerContext workerContext)
|
|
{
|
|
Logger.LogInformation("Get ASN from SCP: Start");
|
|
if (!_options.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 noPoAsnRepository = workerContext.ServiceProvider.GetRequiredService<IAsnRepository>();
|
|
var tenantStore = workerContext.ServiceProvider.GetRequiredService<ITenantStore>();
|
|
var currentTenant = workerContext.ServiceProvider.GetRequiredService<ICurrentTenant>();
|
|
var dataFilter = workerContext.ServiceProvider.GetRequiredService<IDataFilter>();
|
|
//Do the work
|
|
|
|
foreach (var site in _options.Sites)
|
|
{
|
|
var asnX12List = new List<X12Asn>();
|
|
// var noPoAsnList = new List<AsnMstr>();
|
|
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.BatchSize);
|
|
|
|
Logger.LogInformation($"{siteCode}: {scpAsns.Count} ASNs were Found");
|
|
//var baseUrl = _options.BaseUrl;
|
|
//var username = _options.Username;
|
|
//var password = _options.Password;
|
|
foreach (var asn in scpAsns)
|
|
{
|
|
var barcodes = await scpAsnManager.GetBarcodesAsync(siteCode, asn.AsnBillNum);
|
|
|
|
var noPoAsn = CreateNoPoAsn(asn, barcodes);
|
|
asnX12List.Add(noPoAsn);
|
|
asn.IsPort = 1;
|
|
//var apiUrl = "";
|
|
|
|
var jsonString = noPoAsn.JsonString;
|
|
#region
|
|
//try
|
|
//{
|
|
// var result = await _postService.PostAsync(baseUrl, apiUrl, jsonString, username, password);
|
|
// if (result == null)
|
|
// {
|
|
// noPoAsn.SetError("Boomi", EnumExchangeDataErrorCode.None, "Boomiû�з���ֵ","Boomi�ӿ�ִ��ʧ��");
|
|
// }
|
|
// if (result?.Status == "SUCCESS")
|
|
// {
|
|
// noPoAsn.SetSuccess("Boomi", "Boomi�ӿ�ִ�гɹ�");
|
|
// }
|
|
// else if (result?.Status == "EXCEPTION")
|
|
// {
|
|
// noPoAsn.SetError("Boomi", EnumExchangeDataErrorCode.Exception, "EXCEPTION" + result?.ErrorMessage, "Boomi�ӿ�ִ��ʧ��");
|
|
// }
|
|
// else if (result?.Status == "FAIL")
|
|
// {
|
|
// noPoAsn.SetError("Boomi", EnumExchangeDataErrorCode.FAIL, "FAIL" + result?.ErrorMessage, "Boomi�ӿ�ִ��ʧ��");
|
|
// }
|
|
//}
|
|
//catch (Exception e)
|
|
//{
|
|
// Logger.LogException(e);
|
|
// e = e.GetBaseException();
|
|
// noPoAsn.SetError("Boomi", EnumExchangeDataErrorCode.Exception, "�ӿڳ����쳣"+e.Message, "Boomi�ӿ�ִ��ʧ��");
|
|
//}
|
|
//finally
|
|
//{
|
|
// asnX12List.Add(noPoAsn);
|
|
//}
|
|
#endregion
|
|
if (asn.AsnBillNum.StartsWith(_options.QadAsnPrefix))//ASN to QAD
|
|
{
|
|
//����Ҫ��������Ϣ @20220905
|
|
barcodes.ForEach(p => p.PalletCode = "");
|
|
//����Ҫ��������Ϣ @20220905
|
|
|
|
var asnX12 = CreateX12Asn(site, asn, barcodes);
|
|
asnX12List.Add(asnX12);
|
|
}
|
|
|
|
Logger.LogInformation($"{siteCode}:{asn.Id} {asn.AsnBillNum} was loaded");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogException(ex);
|
|
}
|
|
|
|
await AddX12AsnsAsync(asnX12List, x12AsnRepository);
|
|
// await AddNoPoAsnsAsync(noPoAsnList,noPoAsnRepository);
|
|
}
|
|
|
|
|
|
Logger.LogInformation("Get ASN from SCP: Complete");
|
|
}
|
|
private X12Asn CreateNoPoAsn(TB_ASN asn, List<TS_BARCODE> barcodes)
|
|
{
|
|
|
|
var asnMstr = new AsnMstr()
|
|
{
|
|
UID = asn.Id,
|
|
Company = asn.Site,
|
|
Site = asn.Site,
|
|
AsnNbr = asn.AsnBillNum,
|
|
RpNbr = asn.AskBillNum,
|
|
PoNbr = asn.PoBillNum,
|
|
VendorCode = asn.VendId,
|
|
Contacts = asn.ReceiveUser,
|
|
Phone = "",
|
|
TruckNbr = asn.PlateNumber,
|
|
Whse = "",
|
|
Dock = asn.Dock,
|
|
ShipDate = asn.ShipTime,
|
|
DueDate = asn.ReceiveTime,
|
|
TimeWindow = asn.ReceivedPort,
|
|
Remark = asn.Remark,
|
|
Subsite = asn.WebSubSite,
|
|
};
|
|
if (string.IsNullOrEmpty(asnMstr.TimeWindow))
|
|
{
|
|
asnMstr.TimeWindow = "00:00-23:59";
|
|
}
|
|
foreach (var barcode in barcodes)
|
|
{
|
|
var det = new AsnDet()
|
|
{
|
|
AsnNbr = asn.AsnBillNum,
|
|
PoNbr = asn.PoBillNum,
|
|
PoLine = barcode.PoBillLine,
|
|
PartCode = barcode.PartCode,
|
|
Lot = barcode.Batch,
|
|
Qty = barcode.Qty,
|
|
ProductionDate = barcode.ProduceDate,
|
|
WareClass = "",
|
|
WareCode = "",
|
|
StdPackQty = barcode.PackQty,
|
|
PoUm = barcode.PoUnit,
|
|
LocUm = barcode.LocUnit,
|
|
UmConv = 1,
|
|
PartType = "",
|
|
Supplierlot = barcode.VendBatch,
|
|
Status = barcode.State.ToString(),
|
|
LabelCode = barcode.Extend2,
|
|
EntireBarCode = barcode.FullBarCode,
|
|
PalletLabelCode = barcode.PalletCode,
|
|
Remark = barcode.Remark,
|
|
};
|
|
asnMstr.Details.Add(det);
|
|
}
|
|
var jsonString = JsonSerializer.Serialize(asnMstr);
|
|
var ediString = jsonString;
|
|
var asnX12 = new X12Asn(asn.Id, asn.Site, asn.AsnBillNum, ScpWebApiConsts.NO_PO_ASN, jsonString, ediString,
|
|
asn.ShipTime ?? DateTime.Today,asn.WebSubSite);
|
|
return asnX12;
|
|
}
|
|
private static X12Asn CreateX12Asn(AsnOptions.Site site, TB_ASN asn, List<TS_BARCODE> barcodes)
|
|
{
|
|
var asnFactory = new AsnFactory();
|
|
var asnX128563060 =
|
|
asnFactory.CreateAsnX128563060(site.Receiver, asn, barcodes);
|
|
var jsonString = JsonSerializer.Serialize(asnX128563060);
|
|
var ediString = asnX128563060.ToString();
|
|
var asnX12 = new X12Asn(asn.Id, asn.Site, asn.AsnBillNum, ScpWebApiConsts.ASN, jsonString, ediString,
|
|
asn.ShipTime ?? DateTime.Today, asn.WebSubSite);
|
|
return asnX12;
|
|
}
|
|
|
|
/*
|
|
private async Task AddNoPoAsnsAsync(List<AsnMstr> noPoAsnList, IAsnRepository noPoAsnRepository)
|
|
{
|
|
foreach (var asn in noPoAsnList)
|
|
{
|
|
var exist = await noPoAsnRepository.FirstOrDefaultAsync(
|
|
p => p.UID == asn.UID && p.Site == asn.Site);
|
|
if (exist == null)
|
|
{
|
|
await noPoAsnRepository.InsertAsync(asn);
|
|
}
|
|
else
|
|
{
|
|
exist.RpNbr = asn.RpNbr;
|
|
exist.Contacts = asn.Contacts;
|
|
exist.Phone = asn.Phone;
|
|
exist.TruckNbr = asn.TruckNbr;
|
|
exist.Whse = asn.Whse;
|
|
exist.Dock = asn.Dock;
|
|
exist.ShipDate = asn.ShipDate;
|
|
exist.DueDate = asn.DueDate;
|
|
exist.TimeWindow = asn.TimeWindow;
|
|
exist.Remark = asn.Remark;
|
|
exist.Details = asn.Details;
|
|
exist.SetEffectiveDate(asn.EffectiveDate);
|
|
exist.Reset();
|
|
await noPoAsnRepository.UpdateAsync(exist);
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
|
|
private static async Task AddX12AsnsAsync(List<X12Asn> asnX12List, IX12AsnRepository x12AsnRepository)
|
|
{
|
|
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.EdiString = x12Asn.EdiString;
|
|
if(x12Asn.Status== EnumExchangeDataStatus.Finish)
|
|
{
|
|
exist.SetSuccess(x12Asn.Reader);
|
|
}
|
|
else
|
|
{
|
|
exist.SetError(x12Asn.Reader, (EnumExchangeDataErrorCode) x12Asn.ErrorCode, x12Asn.ErrorMessage, x12Asn.Remark);
|
|
}
|
|
await x12AsnRepository.UpdateAsync(exist);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|