From f3d87a1e52066cb154c832d8f24fcac292931eef Mon Sep 17 00:00:00 2001 From: shanji <18996038927@163.com> Date: Wed, 7 Jun 2023 17:01:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B41688=E5=8F=91=E8=B4=A7?= =?UTF-8?q?=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BBWYB.Server.API/Program.cs | 1 + .../Extensions/PurchaseOrderStateExtension.cs | 12 ++ BBWYB.Server.Business/KuaiDi100Manager.cs | 93 +++++++++++++ .../PurchaseOrder/PurchaseOrderBusiness.cs | 129 +++++++++++++++++- SDKAdapter/ExpressCompanyNameConverter.cs | 5 + .../Client/Base/PP_PlatformClient.cs | 2 +- .../Client/Impl/PP_1688Client.cs | 22 ++- .../Order/PP_QueryOrderLogisticsResponse.cs | 4 + 8 files changed, 257 insertions(+), 11 deletions(-) create mode 100644 BBWYB.Server.Business/Extensions/PurchaseOrderStateExtension.cs create mode 100644 BBWYB.Server.Business/KuaiDi100Manager.cs diff --git a/BBWYB.Server.API/Program.cs b/BBWYB.Server.API/Program.cs index 94be203..7cbd9f9 100644 --- a/BBWYB.Server.API/Program.cs +++ b/BBWYB.Server.API/Program.cs @@ -62,6 +62,7 @@ services.BatchRegisterServices(new Assembly[] { Assembly.Load("BBWYB.Server.Busi services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); +services.AddSingleton(); services.AddMapper(new MappingProfiles()); services.Configure>(configuration.GetSection("ApiVersionRequirements")); diff --git a/BBWYB.Server.Business/Extensions/PurchaseOrderStateExtension.cs b/BBWYB.Server.Business/Extensions/PurchaseOrderStateExtension.cs new file mode 100644 index 0000000..fcc8220 --- /dev/null +++ b/BBWYB.Server.Business/Extensions/PurchaseOrderStateExtension.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BBWYB.Server.Business.Extensions +{ + internal class PurchaseOrderStateExtension + { + } +} diff --git a/BBWYB.Server.Business/KuaiDi100Manager.cs b/BBWYB.Server.Business/KuaiDi100Manager.cs new file mode 100644 index 0000000..5878531 --- /dev/null +++ b/BBWYB.Server.Business/KuaiDi100Manager.cs @@ -0,0 +1,93 @@ +using BBWYB.Common.Http; +using BBWYB.Common.Models; +using Newtonsoft.Json; +using SDKAdapter; + +namespace BBWYB.Server.Business +{ + public class KuaiDi100Manager : IDenpendency + { + private RestApiService restApiService; + private ExpressCompanyNameConverter expressCompanyNameConverter; + + public IList KuaiDi100PushStateList_ZaiTu = new List() { 0, 1001, 1002, 1003 }; + public IList KuaiDi100PushStateList_LanShou = new List() { 1, 101, 102, 103 }; + public IList KuaiDi100PushStateList_QianShou = new List() { 3, 301, 302, 303, 304 }; + public IList KuaiDi100PushStateList_PaiJian = new List() { 5, 501 }; + + public KuaiDi100Manager(RestApiService restApiService, ExpressCompanyNameConverter expressCompanyNameConverter) + { + this.restApiService = restApiService; + this.expressCompanyNameConverter = expressCompanyNameConverter; + } + + private void ThrowSubscribeError(string waybillno, string sourceExpressName, string kuaidi100CompanyCode, string error) + { + throw new Exception($"订阅快递100失败 waybillno:{waybillno} sourceExpressName:{sourceExpressName} kuaidi100CompanyCode:{kuaidi100CompanyCode} error:{error}"); + } + + /// + /// 订阅快递100 + /// + /// + /// + /// + /// + public void SubscribeKuaiDi100(string waybillno, string sourceExpressName, string kuaidi100CompanyCode,string callbackUrl) + { + if (string.IsNullOrEmpty(sourceExpressName) && string.IsNullOrEmpty(kuaidi100CompanyCode)) + ThrowSubscribeError(waybillno, sourceExpressName, kuaidi100CompanyCode, "原始快递公司名称和快递100公司编码必须任意一个有值"); + + if (string.IsNullOrEmpty(kuaidi100CompanyCode)) + kuaidi100CompanyCode = expressCompanyNameConverter.ConverterToKuaiDi100CompanyCode(sourceExpressName); + if (string.IsNullOrEmpty(kuaidi100CompanyCode)) + ThrowSubscribeError(waybillno, sourceExpressName, kuaidi100CompanyCode, "无翻译结果"); + + var paramStr = JsonConvert.SerializeObject(new + { + company = kuaidi100CompanyCode, + number = waybillno, + key = "SdcRPzxo8802", + parameters = new + { + callbackurl = callbackUrl, + salt = Guid.NewGuid(), + resultv2 = "4" + } + }); + var subscribeResult = restApiService.SendRequest("https://poll.kuaidi100.com", "poll", $"schema=json¶m={paramStr}", null, HttpMethod.Post, RestApiService.ContentType_Form); + if (subscribeResult.StatusCode != System.Net.HttpStatusCode.OK) + ThrowSubscribeError(waybillno, sourceExpressName, kuaidi100CompanyCode, subscribeResult.Content); + var subscribeResponse = JsonConvert.DeserializeObject(subscribeResult.Content); + if (!subscribeResponse.result) + ThrowSubscribeError(waybillno, sourceExpressName, kuaidi100CompanyCode, subscribeResponse.message); + } + + /// + /// 获取物流状态 ZaiTu LanShou QianShou PaiJian Unknow + /// + /// + /// + public string GetExpressState(int kuaidi100State) + { + if (KuaiDi100PushStateList_ZaiTu.Contains(kuaidi100State)) + return "ZaiTu"; + if (KuaiDi100PushStateList_LanShou.Contains(kuaidi100State)) + return "LanShou"; + if (KuaiDi100PushStateList_QianShou.Contains(kuaidi100State)) + return "QianShou"; + if (KuaiDi100PushStateList_PaiJian.Contains(kuaidi100State)) + return "PaiJian"; + return "Unknow"; + } + } + + public class KuaiDi100Response + { + public bool result { get; set; } + + public int returnCode { get; set; } + + public string message { get; set; } + } +} diff --git a/BBWYB.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs b/BBWYB.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs index 89ca7f9..e8f9ab5 100644 --- a/BBWYB.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs +++ b/BBWYB.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs @@ -29,6 +29,7 @@ namespace BBWYB.Server.Business private ExpressCompanyNameConverter expressCompanyNameConverter; private RestApiService restApiService; private IList cantPurchaseOrderStateList; + private KuaiDi100Manager kuaiDi100Manager; public PurchaseOrderBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, @@ -39,7 +40,8 @@ namespace BBWYB.Server.Business VenderBusiness venderBusiness, PurchaseSchemeBusiness purchaseSchemeBusiness, ExpressCompanyNameConverter expressCompanyNameConverter, - RestApiService restApiService) : base(fsql, nLogManager, idGenerator) + RestApiService restApiService, + KuaiDi100Manager kuaiDi100Manager) : base(fsql, nLogManager, idGenerator) { this.ppPlatformClientFactory = ppPlatformClientFactory; this.taskSchedulerManager = taskSchedulerManager; @@ -56,6 +58,7 @@ namespace BBWYB.Server.Business Enums.OrderState.待付款, Enums.OrderState.待完结 }; + this.kuaiDi100Manager = kuaiDi100Manager; } /// @@ -594,7 +597,7 @@ namespace BBWYB.Server.Business pss.PurchaseSkuSpecId, pss.SkuPurchaseSchemeId }); - if(purchaseSchemeSkuList.Count()==0) + if (purchaseSchemeSkuList.Count() == 0) throw new BusinessException($"采购单{purchaseOrder.PurchaseOrderId} 采购商Id{purchaserId} 未匹配到采购方案"); #endregion @@ -916,7 +919,127 @@ namespace BBWYB.Server.Business private void DeliveryCallbackFrom1688(JObject jObject) { var purchaseOrderId = jObject["data"].Value("orderId"); - Task.Factory.StartNew(() => DeliveryCallback(purchaseOrderId, null, Enums.Platform.阿里巴巴), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler); + Task.Factory.StartNew(() => DeliveryCallbackFrom1688(purchaseOrderId), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler); + } + + /// + /// 1688发货回调 + /// + /// 采购单 + private void DeliveryCallbackFrom1688(string purchaseOrderId) + { + string orderId = string.Empty; + long? shopId = null; + string currentProgress = string.Empty; + string wayBillNoResponseInfo = string.Empty; + try + { + List insertPurchaseExpressOrderList = new List(); + IList> updateOrderPurchaseSkuList = new List>(); + IUpdate updateOrderPurchase = null; + IUpdate updateOrder = null; + + #region 查询采购单信息 + currentProgress = "查询采购单信息"; + var orderPurchaseInfo = fsql.Select().Where(o => o.PurchaseOrderId == purchaseOrderId && o.IsEnabled == true).ToOne(); + if (orderPurchaseInfo == null) + throw new Exception("未查询到采购单信息"); + orderId = orderPurchaseInfo.OrderId; + shopId = orderPurchaseInfo.ShopId; + #endregion + + #region 查询采购账号 + currentProgress = "查询采购账号"; + var purchaseAccount = fsqlManager.MDSfsql.Select().Where(pa => pa.Id == orderPurchaseInfo.PurchaseAccountId).ToOne(); + if (purchaseAccount == null) + throw new Exception($"未查询到采购账号{orderPurchaseInfo.PurchaseAccountId}"); + #endregion + + #region 获取采购单的物流信息 + currentProgress = "获取采购单的物流信息"; + var client = ppPlatformClientFactory.GetClient(AdapterEnums.PlatformType.阿里巴巴); + var ppQueryOrderLogisticsRequest = new PP_QueryOrderLogisticsRequest() + { + AppKey = purchaseAccount.AppKey, + AppSecret = purchaseAccount.AppSecret, + AppToken = purchaseAccount.AppToken, + OrderId = purchaseOrderId, + Platform = AdapterEnums.PlatformType.阿里巴巴 + }; + var logisticsList = client.QueryOrderLogistics(ppQueryOrderLogisticsRequest); + wayBillNoResponseInfo = JsonConvert.SerializeObject(new { purchaseOrderId, logisticsList }); + #endregion + + #region 查询该笔采购单的sku信息和已有快递单信息 + currentProgress = "查询该笔采购单的sku信息和已有快递单信息"; + var orderPurchaseSkuList = fsql.Select().Where(x => x.PurchaseOrderId == purchaseOrderId).ToList(); + var purchaseExpressOrderList = fsql.Select().Where(x => x.PurchaseOrderId == purchaseOrderId).ToList(); + #endregion + + #region 找出新发货的快递单 + foreach (var logisticsInfo in logisticsList) + { + if (purchaseExpressOrderList.Any(po => po.WaybillNo == logisticsInfo.WayBillNo)) + continue; + + #region 创建快递单 + var purchaseExpressOrder = new PurchaseExpressOrder() + { + OrderId = orderId, + CreateTime = DateTime.Now, + PurchaseOrderId = purchaseOrderId, + ShopId = shopId ?? 0, + SourceExpressId = logisticsInfo.ExpressId, + SourceExpressName = logisticsInfo.ExpressName, + WaybillNo = logisticsInfo.WayBillNo, + ExpressState = kuaiDi100Manager.GetExpressState(1) //快递100发货状态值 默认揽收 + }; + insertPurchaseExpressOrderList.Add(purchaseExpressOrder); + #endregion + + #region 更新采购sku的快递单号 + foreach (var orderEntryId in logisticsInfo.OrderEntryIds) + { + var purchaseSku = orderPurchaseSkuList.FirstOrDefault(x => x.Id == orderEntryId); + if (purchaseSku == null) + continue; + purchaseSku.WaybillNo = logisticsInfo.WayBillNo; + updateOrderPurchaseSkuList.Add(fsql.Update(orderEntryId).Set(ps => ps.WaybillNo, logisticsInfo.WayBillNo)); + } + #endregion + + #region 订阅快递100 + currentProgress = "订阅快递100"; + kuaiDi100Manager.SubscribeKuaiDi100(logisticsInfo.WayBillNo, logisticsInfo.ExpressName, string.Empty,""); + #endregion + } + #endregion + + #region 计算采购单状态 + + #endregion + + #region 计算订单状态 + + #endregion + + #region 通知C端订单状态 + + #endregion + + fsql.Transaction(() => + { + fsql.Insert(insertPurchaseExpressOrderList).ExecuteAffrows(); + foreach (var update in updateOrderPurchaseSkuList) + update.ExecuteAffrows(); + updateOrderPurchase?.ExecuteAffrows(); + updateOrder?.ExecuteAffrows(); + }); + } + catch (Exception ex) + { + nLogManager.Default().Error(ex, $"DeliveryCallback 回调平台1688,订单号{orderId},采购单号{purchaseOrderId},执行进度[{currentProgress}],采购单物流信息:{wayBillNoResponseInfo}"); + } } /// diff --git a/SDKAdapter/ExpressCompanyNameConverter.cs b/SDKAdapter/ExpressCompanyNameConverter.cs index 70297b9..993a4bc 100644 --- a/SDKAdapter/ExpressCompanyNameConverter.cs +++ b/SDKAdapter/ExpressCompanyNameConverter.cs @@ -127,6 +127,11 @@ namespace SDKAdapter } return expressCompany; } + + public string ConverterToKuaiDi100CompanyCode(string sourceName) + { + return string.Empty; + } } public class LogisticsCompanyRelationship diff --git a/SDKAdapter/PurchasePlatform/Client/Base/PP_PlatformClient.cs b/SDKAdapter/PurchasePlatform/Client/Base/PP_PlatformClient.cs index 42b4898..65ed26c 100644 --- a/SDKAdapter/PurchasePlatform/Client/Base/PP_PlatformClient.cs +++ b/SDKAdapter/PurchasePlatform/Client/Base/PP_PlatformClient.cs @@ -53,7 +53,7 @@ namespace SDKAdapter.PurchasePlatform.Client /// /// /// - public virtual PP_QueryOrderLogisticsResponse QueryOrderLogistics(PP_QueryOrderLogisticsRequest request) + public virtual IList QueryOrderLogistics(PP_QueryOrderLogisticsRequest request) { throw new NotImplementedException(); } diff --git a/SDKAdapter/PurchasePlatform/Client/Impl/PP_1688Client.cs b/SDKAdapter/PurchasePlatform/Client/Impl/PP_1688Client.cs index 9897b46..8bd875c 100644 --- a/SDKAdapter/PurchasePlatform/Client/Impl/PP_1688Client.cs +++ b/SDKAdapter/PurchasePlatform/Client/Impl/PP_1688Client.cs @@ -251,7 +251,7 @@ namespace SDKAdapter.PurchasePlatform.Client }; } - public override PP_QueryOrderLogisticsResponse QueryOrderLogistics(PP_QueryOrderLogisticsRequest request) + public override IList QueryOrderLogistics(PP_QueryOrderLogisticsRequest request) { var client = GetSyncAPIClient(request.AppKey, request.AppSecret); RequestPolicy reqPolicy = new RequestPolicy(); @@ -278,14 +278,22 @@ namespace SDKAdapter.PurchasePlatform.Client throw new Exception(result.Value("errorMessage")); //nLogManager.Default().Info($"GetWayBillNoByOrderId QueryOrderWayBillNoRequest {JsonConvert.SerializeObject(queryOrderWayBillNoRequest)} Result {result}"); + /* + new PP_QueryOrderLogisticsResponse() + { + ExpressId = logisticsOrderJToken.Value("logisticsCompanyId"), + ExpressName = logisticsOrderJToken.Value("logisticsCompanyName"), + WayBillNo = logisticsOrderJToken.Value("logisticsBillNo") + } + */ - var firstJToken = result["result"].FirstOrDefault(); - return new PP_QueryOrderLogisticsResponse() + return result["result"].Children().Select(logisticsOrderJToken => new PP_QueryOrderLogisticsResponse() { - ExpressId = firstJToken.Value("logisticsCompanyId"), - ExpressName = firstJToken.Value("logisticsCompanyName"), - WayBillNo = firstJToken.Value("logisticsBillNo") - }; + ExpressId = logisticsOrderJToken.Value("logisticsCompanyId"), + ExpressName = logisticsOrderJToken.Value("logisticsCompanyName"), + WayBillNo = logisticsOrderJToken.Value("logisticsBillNo"), + OrderEntryIds = logisticsOrderJToken.Value("orderEntryIds").Split(',', StringSplitOptions.RemoveEmptyEntries) + }).ToList(); } } diff --git a/SDKAdapter/PurchasePlatform/Models/Response/Order/PP_QueryOrderLogisticsResponse.cs b/SDKAdapter/PurchasePlatform/Models/Response/Order/PP_QueryOrderLogisticsResponse.cs index 848bad1..76c807b 100644 --- a/SDKAdapter/PurchasePlatform/Models/Response/Order/PP_QueryOrderLogisticsResponse.cs +++ b/SDKAdapter/PurchasePlatform/Models/Response/Order/PP_QueryOrderLogisticsResponse.cs @@ -7,5 +7,9 @@ public string ExpressId { get; set; } public string ExpressName { get; set; } + + public IList OrderEntryIds { get; set; } + + public IList SkuIds { get; set; } } }