From ce6eee3ca74a0b8bf92933f9dc3e1928fa3587a7 Mon Sep 17 00:00:00 2001 From: wufan Date: Fri, 17 Jan 2025 13:25:25 +0800 Subject: [PATCH] :sparkles: --- .../FedexApi/Services/RequestService.cs | 2 + TradeManageNew/APIClients/MemoryCacheKeys.cs | 4 +- TradeManageNew/DD_OrderServiceNew.asmx.cs | 400 ++++++++++++++++-- TradeUsedSale/Repositories/ErpDbContext.cs | 1 + .../Models/DT_TrackCodeApplyLog.cs | 15 + TradeUsedSale/TradeUsedSale.csproj | 1 + 6 files changed, 375 insertions(+), 48 deletions(-) create mode 100644 TradeUsedSale/Repositories/Models/DT_TrackCodeApplyLog.cs diff --git a/TradeManageNew/APIClients/FedexApi/Services/RequestService.cs b/TradeManageNew/APIClients/FedexApi/Services/RequestService.cs index ffb561f..e075df0 100644 --- a/TradeManageNew/APIClients/FedexApi/Services/RequestService.cs +++ b/TradeManageNew/APIClients/FedexApi/Services/RequestService.cs @@ -3,6 +3,8 @@ using System.CodeDom.Compiler; using System.Collections.Generic; using System.IO; using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; using System.Runtime.Caching; using System.Text; using System.Threading; diff --git a/TradeManageNew/APIClients/MemoryCacheKeys.cs b/TradeManageNew/APIClients/MemoryCacheKeys.cs index bef7dd1..d5690ea 100644 --- a/TradeManageNew/APIClients/MemoryCacheKeys.cs +++ b/TradeManageNew/APIClients/MemoryCacheKeys.cs @@ -13,8 +13,8 @@ public const string XPO_ACCESS_TOKEN_KEY = "XPOAccessToken"; /// - /// Fedex API access token key. + /// Fedex API access token key prefix. /// - public const string FEDEX_ACCESS_TOKEN_KEY = "FedexAccessToken"; + public const string FEDEX_ACCESS_TOKEN_PREFIX = "FedexAccessToken"; } } \ No newline at end of file diff --git a/TradeManageNew/DD_OrderServiceNew.asmx.cs b/TradeManageNew/DD_OrderServiceNew.asmx.cs index 4b0cfe6..59f23b9 100644 --- a/TradeManageNew/DD_OrderServiceNew.asmx.cs +++ b/TradeManageNew/DD_OrderServiceNew.asmx.cs @@ -1,44 +1,35 @@ using System; using System.Collections.Generic; +using System.Data; +using System.IO; using System.Linq; +using System.Net; +using System.Net.Http; +using System.Security.Cryptography; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; using System.Web; +using System.Web.Script.Serialization; using System.Web.Services; -using TradeData; -using NetLibrary.Data; -using System.Data; -using TradeModel; -using NetLibrary.ReportPrint; +using ApiNew; +using LinqToDB; using NetLibrary; +using NetLibrary.Data; using NetLibrary.Express; -using System.Text.RegularExpressions; -using NetLibrary.OnlineTrade; -using System.Web.Script.Serialization; -using System.Text; using NetLibrary.Log; -using System.Threading; -using ApiNew; -using Aspose.Pdf; -using System.Net; -using System.Runtime.ConstrainedExecution; -using System.IO; -using System.Net.Http; +using NetLibrary.OnlineTrade; +using NetLibrary.ReportPrint; using Newtonsoft.Json; -using Org.BouncyCastle.Asn1.Pkcs; -using TradeManage; -using System.Security.Cryptography; -using NetLibrary.WebReference; -using FastJSON; -using System.Runtime.CompilerServices; using Newtonsoft.Json.Serialization; -using NPOI.HSSF.Record.Formula.Functions; -using static NPOI.HSSF.Util.HSSFColor; -using System.Linq.Expressions; +using TradeData; +using TradeManage; +using TradeManageNew.APIClients.FedexApi; +using TradeManageNew.APIClients.FedexApi.Models.Ship; using TradeManageNew.APIClients.ShopifyAPI; -using LinqToDB; -using Match = System.Text.RegularExpressions.Match; -using TradeManageNew.APIClients.FedexApi.Tests; -using TradeManageNew.APIClients.FedexAPI.Models.RatesAndTransitTimes; using TradeManageNew.Models.ShopifyUsedSale; +using TradeModel; +using Match = System.Text.RegularExpressions.Match; namespace TradeManageNew { @@ -8723,23 +8714,7 @@ namespace TradeManageNew return rmodelNew; } - - - - - - [WebMethod(EnableSession = true)] - public RateAndTransitTimesResp GetFedexFeeTest() - { - var ratesAndTransitTimesTests = new RatesAndTransitTimesTests(); - return ratesAndTransitTimesTests.Should_RateAsync_IsReturn(); - - - } - - - - + #region 修改允许发货 [WebMethod(EnableSession = true)] public void UpdatePostState2(List list) @@ -29423,6 +29398,339 @@ namespace TradeManageNew }; } + + public APIReturnModel GetOrderUsedSalePlatformLabel(int Id) + { + // PagesNew.Login(base.Session); + using (var db = new TradeUsedSale.Repositories.ErpDbContext()) + { + var orderUsedSalePlatform = db.DT_OrderUsedSalePlatform.FirstOrDefault(x => + x.Id == Id && x.Status == TradeUsedSale.Enums.UsedSalePlatformStatus.PendingPickup); + + if (orderUsedSalePlatform is null) + { + return new APIReturnModel + { + Code = 0, + Message = $"不存在Id为{Id}的二手售卖商品", + Datas = null + }; + } + + var product = db.HW_GoodsInfo.FirstOrDefault(x => x.GoodsId == orderUsedSalePlatform.ProductId); + if (product is null) + { + return new APIReturnModel + { + Code = 0, + Message = $"系统中不存在Id为{orderUsedSalePlatform.ProductId}的产品信息", + Datas = null + }; + } + + //毛重 + if (product.Weight is null || product.Weight <= 0) + { + return new APIReturnModel + { + Code = 0, + Message = $"产品[{product.GoodsCode}]无毛重信息,无法发货", + Datas = null + }; + } + + //长宽高 + if (product.Long is null || product.Long <= 0 || + product.Width is null || product.Width <= 0 || + product.Height is null || product.Height <= 0) + { + return new APIReturnModel + { + Code = 0, + Message = $"产品[{product.GoodsCode}]外箱尺寸信息不完整,无法发货", + Datas = null + }; + } + + var sku = db.HW_GoodsDetail.FirstOrDefault(x => x.DetailId == orderUsedSalePlatform.SkuId); + if (sku is null) + { + return new APIReturnModel + { + Code = 0, + Message = $"系统中不存在Id为{orderUsedSalePlatform.SkuId}的SKU信息", + Datas = null + }; + } + + if (orderUsedSalePlatform.Status != TradeUsedSale.Enums.UsedSalePlatformStatus.PendingPickup) + { + return new APIReturnModel + { + Code = 0, + Message = $"二手售卖商品[{orderUsedSalePlatform.SkuCode}]不处于待发货状态", + Datas = null + }; + } + + //Shopify订单信息 + var shopifyUsedSaleOrderItem = db.DT_ShopifyUsedSaleOrderItem.FirstOrDefault(x => x.ProductCode == orderUsedSalePlatform.BarCode); + if(shopifyUsedSaleOrderItem is null) + { + return new APIReturnModel + { + Code = 0, + Message = $"未获取到条码为[{ orderUsedSalePlatform.BarCode}]的Shopify订单信息", + Datas = null + }; + } + + var shopifyUsedSaleOrder = db.DT_ShopifyUsedSaleOrder.FirstOrDefault(x => x.Id == shopifyUsedSaleOrderItem.DtShopifyUsedSaleOrderId); + if(shopifyUsedSaleOrder is null) + { + return new APIReturnModel + { + Code = 0, + Message = $"未获取到条码为[{ orderUsedSalePlatform.BarCode}]的Shopify订单信息", + Datas = null + }; + } + + //获取Fedex运费和面单 + var fedexCredential = new FedexCredential(isDebugMode: true) { ReadResponseAsString = true }; + if (orderUsedSalePlatform.WarehouseLocation == TradeUsedSale.Enums.WarehouseLocation.East) + { + fedexCredential.SetEastSandBoxCredential(); + } + else + { + fedexCredential.SetWestSandBoxCredential(); + } + + var serviceTypes = new List() + { + RequestedShipmentServiceType.GROUND_HOME_DELIVERY, + RequestedShipmentServiceType.SMART_POST, + RequestedShipmentServiceType.FEDEX_GROUND, + }; + + var fedexClient = new FedexAPIClient(fedexCredential); + + #region 发件人 + + var shipper = new ShipperParty(); + + if (orderUsedSalePlatform.WarehouseLocation == TradeUsedSale.Enums.WarehouseLocation.East) + { + shipper.Contact = new PartyContact + { + PersonName = "Karmas Far Inc", + PhoneNumber = "9045971260", + }; + + shipper.Address = new PartyAddress_2 + { + StreetLines = SplitAddress("2615 port industrial Dr. suites 408", 35), + City = "Jacksonville", + StateOrProvinceCode = "FL", + PostalCode = "32226", + CountryCode = "US", + }; + } + else + { + shipper.Contact = new PartyContact + { + PersonName = "Karmas Far Inc", + PhoneNumber = "9093726955", + }; + + shipper.Address = new PartyAddress_2 + { + StreetLines = SplitAddress("11190 White Birch Dr Suite 100", 35), + City = "RanchoCucamonga", + StateOrProvinceCode = "CA", + PostalCode = "91730", + CountryCode = "US", + }; + } + + #endregion + + #region 收件人 + + var recipient = new RecipientsParty + { + Address = new PartyAddress_2 + { + StreetLines = SplitAddress(shopifyUsedSaleOrder.Full, 35), + City = shopifyUsedSaleOrder.City, + StateOrProvinceCode = shopifyUsedSaleOrder.ProvinceCode, + PostalCode = shopifyUsedSaleOrder.Zip, + CountryCode = shopifyUsedSaleOrder.CountryCode, + Residential = null + }, + Contact = new PartyContact + { + PersonName = shopifyUsedSaleOrder.Name, + PhoneNumber = shopifyUsedSaleOrder.Phone, + } + }; + + #endregion + + #region 商品信息 + + var requestedPackageLineItems = new List() + { + new RequestedPackageLineItem() + { + SequenceNumber = 1, + Weight = new Weight() + { + Value = product.Weight.Value/1000, + Units = WeightUnits.KG + }, + CustomerReferences = new List() + { + new CustomerReference_1 + { + CustomerReferenceType = CustomerReference_1CustomerReferenceType + .CUSTOMER_REFERENCE, + Value = orderUsedSalePlatform.BarCode, + } + }, + PackageSpecialServices = new PackageSpecialServicesRequested + { + SignatureOptionType = + PackageSpecialServicesRequestedSignatureOptionType.SERVICE_DEFAULT + }, + Dimensions = new Dimensions + { + Length = Convert.ToInt32(product.Long), + Width = Convert.ToInt32(product.Width), + Height = Convert.ToInt32(product.Height), + Units = DimensionsUnits.CM + } + } + }; + + #endregion + + #region 标签信息 + + var labelSpecification = new LabelSpecification() + { + ImageType = LabelSpecificationImageType.PDF, + LabelFormatType = LabelSpecificationLabelFormatType.COMMON2D, + LabelStockType = LabelSpecificationLabelStockType.PAPER_4X8, + }; + + #endregion + + foreach (var serviceType in serviceTypes) + { + var apiRequestInfo = new StringBuilder(); + + try + { + //判断地址类型来跳过部分服务并且收获地址添加是否住宅 + var createShipmentParameter = new CreateShipmentReq + { + LabelResponseOptions = LABELRESPONSEOPTIONS.LABEL, + RequestedShipment = new RequestedShipment() + { + Shipper = shipper, + Recipients = new List() + { + recipient + }, + ServiceType = serviceType, + PackagingType = RequestedShipmentPackageType.YOUR_PACKAGING, + PickupType = RequestedShipmentPickupType.DROPOFF_AT_FEDEX_LOCATION, + BlockInsightVisibility = false, + ShippingChargesPayment = new Payment() + { + PaymentType = PaymentType.SENDER + }, + LabelSpecification = labelSpecification, + RequestedPackageLineItems = requestedPackageLineItems + }, + AccountNumber = new ShipperAccountNumber() + { + Value = fedexCredential.AccountNumber + } + }; + + //记录请求参数 + apiRequestInfo.AppendLine($"请求参数:{JsonConvert.SerializeObject(createShipmentParameter)}"); + + var response = fedexClient.Ship.CreateShipmentAsync(createShipmentParameter) + .ConfigureAwait(false) + .GetAwaiter() + .GetResult(); + + //记录返回结果 + apiRequestInfo.AppendLine($"返回结果:{JsonConvert.SerializeObject(response)}"); + } + catch (Exception ex) + { + var apiRequestStr = apiRequestInfo.ToString(); + if (!string.IsNullOrWhiteSpace(apiRequestStr)) + { + db.InsertWithInt32Identity(new TradeUsedSale.Repositories.Models.DT_TrackCodeApplyLog + { + UsedSaleBarCode = orderUsedSalePlatform.BarCode, + ServiceType = serviceType.ToString(), + Remark = apiRequestStr, + CreationTime = DateTime.Now + }); + } + + db.InsertWithInt32Identity(new TradeUsedSale.Repositories.Models.DT_TrackCodeApplyLog + { + UsedSaleBarCode = orderUsedSalePlatform.BarCode, + ServiceType = serviceType.ToString(), + Remark = $"请求失败:{ex.Message}", + CreationTime = DateTime.Now + }); + } + } + } + + return null; + } + + static string[] SplitAddress(string address, int maxLength) + { + List addressLines = new List(); + while (address.Length > maxLength) + { + int splitIndex = address.LastIndexOf(' ', maxLength); + if (splitIndex == -1) splitIndex = maxLength; // 若无空格,则强行截断 + + addressLines.Add(address.Substring(0, splitIndex).Trim()); + address = address.Substring(splitIndex).Trim(); + } + + // 添加最后一部分 + if (!string.IsNullOrEmpty(address)) + { + addressLines.Add(address); + } + + return addressLines.ToArray(); + } + + public APIReturnModel OrderUsedSalePlatformSendOut(int Id ,TradeUsedSale.Enums.ShippingMethod shippingMethod ) + { + // PagesNew.Login(base.Session); + using (var db = new TradeUsedSale.Repositories.ErpDbContext()) + { + return null; + } + } + #endregion diff --git a/TradeUsedSale/Repositories/ErpDbContext.cs b/TradeUsedSale/Repositories/ErpDbContext.cs index 22f81c1..c1274ad 100644 --- a/TradeUsedSale/Repositories/ErpDbContext.cs +++ b/TradeUsedSale/Repositories/ErpDbContext.cs @@ -32,5 +32,6 @@ namespace TradeUsedSale.Repositories public ITable HW_PostFee => this.GetTable(); public ITable DT_ShopifyUsedSaleOrder => this.GetTable(); public ITable DT_ShopifyUsedSaleOrderItem => this.GetTable(); + public ITable DT_TrackCodeApplyLog => this.GetTable(); } } diff --git a/TradeUsedSale/Repositories/Models/DT_TrackCodeApplyLog.cs b/TradeUsedSale/Repositories/Models/DT_TrackCodeApplyLog.cs new file mode 100644 index 0000000..9498fb1 --- /dev/null +++ b/TradeUsedSale/Repositories/Models/DT_TrackCodeApplyLog.cs @@ -0,0 +1,15 @@ +using System; +using LinqToDB.Mapping; + +namespace TradeUsedSale.Repositories.Models +{ + public class DT_TrackCodeApplyLog + { + [PrimaryKey, Identity] public int Id { get; set; } + + public string UsedSaleBarCode { get; set; } + public string ServiceType { get; set; } + public string Remark { get; set; } + public DateTime CreationTime { get; set; } + } +} \ No newline at end of file diff --git a/TradeUsedSale/TradeUsedSale.csproj b/TradeUsedSale/TradeUsedSale.csproj index d2ca6a4..6108269 100644 --- a/TradeUsedSale/TradeUsedSale.csproj +++ b/TradeUsedSale/TradeUsedSale.csproj @@ -60,6 +60,7 @@ +