From ac0447f5c274413294f3e2b58143ad8c6069197e Mon Sep 17 00:00:00 2001 From: shanji <18996038927@163.com> Date: Mon, 14 Mar 2022 20:19:16 +0800 Subject: [PATCH] =?UTF-8?q?1688=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/PurchaseOrderController.cs | 10 ++ BBWY.Server.Business/MDSBusiness.cs | 52 +++++++++++ BBWY.Server.Business/Order/OrderBusiness.cs | 49 +++------- .../PurchaseOrder/PurchaseOrderBusiness.cs | 93 ++++++++++++++++++- BBWY.Server.Business/TaskSchedulerManager.cs | 3 + .../Db/PurchaseScheme/PurchaseAccount.cs | 61 ++++++++++++ .../Dto/Response/Vender/ShopResponse.cs | 30 ++++++ 7 files changed, 261 insertions(+), 37 deletions(-) create mode 100644 BBWY.Server.Business/MDSBusiness.cs create mode 100644 BBWY.Server.Model/Db/PurchaseScheme/PurchaseAccount.cs create mode 100644 BBWY.Server.Model/Dto/Response/Vender/ShopResponse.cs diff --git a/BBWY.Server.API/Controllers/PurchaseOrderController.cs b/BBWY.Server.API/Controllers/PurchaseOrderController.cs index c16f3d42..7b39553e 100644 --- a/BBWY.Server.API/Controllers/PurchaseOrderController.cs +++ b/BBWY.Server.API/Controllers/PurchaseOrderController.cs @@ -62,5 +62,15 @@ namespace BBWY.Server.API.Controllers { purchaseOrderBusiness.FastCreateOrder(createOnlinePurchaseOrderRequest); } + + /// + /// 1688发货回调 + /// + /// + [HttpPost] + public void DeliveryCallbackFrom1688([FromBody]object param) + { + purchaseOrderBusiness.DeliveryCallbackFrom1688(param); + } } } diff --git a/BBWY.Server.Business/MDSBusiness.cs b/BBWY.Server.Business/MDSBusiness.cs new file mode 100644 index 00000000..74679bed --- /dev/null +++ b/BBWY.Server.Business/MDSBusiness.cs @@ -0,0 +1,52 @@ +using BBWY.Common.Http; +using BBWY.Common.Models; +using BBWY.Server.Model; +using BBWY.Server.Model.Dto; +using Microsoft.Extensions.Options; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; + +namespace BBWY.Server.Business +{ + public class MDSBusiness : IDenpendency + { + private RestApiService restApiService; + private GlobalConfig globalConfig; + private IDictionary mdsApiHeader; + + public MDSBusiness(RestApiService restApiService, IOptions options) + { + this.restApiService = restApiService; + this.globalConfig = options.Value; + mdsApiHeader = new Dictionary() { + { "qy","qy"} + }; + } + + public ShopResponse GetShopInfoByShopId(long shopId) + { + var shopResult = restApiService.SendRequest(globalConfig.MdsApi, "/TaskList/Shop/GetShopsByShopId", $"shopId={shopId}", mdsApiHeader, HttpMethod.Get, enableRandomTimeStamp: true); + if (shopResult.StatusCode != System.Net.HttpStatusCode.OK) + throw new Exception($"SyncOrder 获取店铺信息失败 shopId:{shopId} httpCode:{shopResult.StatusCode} httpContent:{shopResult.Content}"); + + var shopResponseJToken = JToken.Parse(shopResult.Content); + if (shopResponseJToken.Value("Success") != true) + throw new Exception($"SyncOrder 获取店铺信息失败 shopId:{shopId} ErrorMsg:{shopResponseJToken.Value("Msg")}"); + + var shopJToken = shopResponseJToken["Data"].FirstOrDefault(); + return new ShopResponse() + { + AppKey = shopJToken.Value("AppKey"), + AppSecret = shopJToken.Value("AppSecret"), + AppToken = shopJToken.Value("AppToken"), + Platform = (Enums.Platform)shopJToken.Value("PlatformId"), + VenderType = shopJToken.Value("ShopType"), + ShopId = shopId, + Name = shopJToken.Value("ShopName") + }; + } + } +} diff --git a/BBWY.Server.Business/Order/OrderBusiness.cs b/BBWY.Server.Business/Order/OrderBusiness.cs index 1cd91861..9e1c4f00 100644 --- a/BBWY.Server.Business/Order/OrderBusiness.cs +++ b/BBWY.Server.Business/Order/OrderBusiness.cs @@ -6,7 +6,6 @@ using BBWY.Server.Model; using BBWY.Server.Model.Db; using BBWY.Server.Model.Dto; using FreeSql; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Options; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -26,10 +25,10 @@ namespace BBWY.Server.Business private IFreeSql fsql; private IDictionary> syncOrderMethodDic; private IIdGenerator idGenerator; - private IDictionary mdsApiHeader; private TaskSchedulerManager taskSchedulerManager; + private MDSBusiness mdsBusiness; - public OrderBusiness(RestApiService restApiService, IConfiguration configuration, ILogger logger, IFreeSql fsql, IIdGenerator idGenerator, IOptions options, TaskSchedulerManager taskSchedulerManager) : base(restApiService, options) + public OrderBusiness(RestApiService restApiService, ILogger logger, IFreeSql fsql, IIdGenerator idGenerator, IOptions options, TaskSchedulerManager taskSchedulerManager, MDSBusiness mdsBusiness) : base(restApiService, options) { this.logger = logger; this.fsql = fsql; @@ -39,9 +38,7 @@ namespace BBWY.Server.Business { { Enums.Platform.京东, SyncJDOrder } }; - mdsApiHeader = new Dictionary() { - { "qy","qy"} - }; + this.mdsBusiness = mdsBusiness; } public OrderListResponse GetOrderList(SearchOrderRequest searchOrderRequest) @@ -666,44 +663,24 @@ namespace BBWY.Server.Business /// public void SyncOrder(long shopId, string orderId, DateTime? startTime = null, DateTime? endTime = null) { - #region 获取店铺信息; - var shopResult = restApiService.SendRequest(globalConfig.MdsApi, "/TaskList/Shop/GetShopsByShopId", $"shopId={shopId}", mdsApiHeader, HttpMethod.Get, enableRandomTimeStamp: true); - if (shopResult.StatusCode != System.Net.HttpStatusCode.OK) - throw new Exception($"SyncOrder 获取店铺信息失败 shopId:{shopId} httpCode:{shopResult.StatusCode} httpContent:{shopResult.Content}"); - - var shopResponseJToken = JToken.Parse(shopResult.Content); - if (shopResponseJToken.Value("Success") != true) - throw new Exception($"SyncOrder 获取店铺信息失败 shopId:{shopId} ErrorMsg:{shopResponseJToken.Value("Msg")}"); - #endregion - - var shopJToken = shopResponseJToken["Data"].FirstOrDefault(); + var shop = mdsBusiness.GetShopInfoByShopId(shopId); try { - var appKey = shopJToken.Value("AppKey"); - var appSecret = shopJToken.Value("AppSecret"); - var appToken = shopJToken.Value("AppToken"); - var platformId = shopJToken.Value("PlatformId"); - var shopType = shopJToken.Value("ShopType"); - if (string.IsNullOrEmpty(appKey) || string.IsNullOrEmpty(appSecret) || string.IsNullOrEmpty(appToken) || platformId == null) - throw new Exception("缺少店铺必要信息"); - - var platform = (Enums.Platform)platformId; - - if (!syncOrderMethodDic.ContainsKey(platform)) + if (!syncOrderMethodDic.ContainsKey(shop.Platform)) throw new Exception("不支持的平台"); - var relayAPIHost = GetPlatformRelayAPIHost((Enums.Platform)platformId); + var relayAPIHost = GetPlatformRelayAPIHost(shop.Platform); var orderListApiResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetOrderList", new SearchPlatformOrderRequest() { StartDate = startTime ?? DateTime.Now.AddHours(-3), EndDate = endTime ?? DateTime.Now, - AppKey = appKey, - AppSecret = appSecret, - AppToken = appToken, + AppKey = shop.AppKey, + AppSecret = shop.AppSecret, + AppToken = shop.AppToken, PageIndex = 1, PageSize = 100, - Platform = platform, - JDColType = string.IsNullOrEmpty(shopType) ? "0" : shopType, + Platform = shop.Platform, + JDColType = string.IsNullOrEmpty(shop.VenderType) ? "0" : shop.VenderType, SaveResponseLog = true, OrderId = orderId }, null, HttpMethod.Post); @@ -717,11 +694,11 @@ namespace BBWY.Server.Business if (orderListResponse.Data == null || orderListResponse.Data.Count == 0) return; - syncOrderMethodDic[platform](orderListResponse.Data, shopId, relayAPIHost, appKey, appSecret, appToken); + syncOrderMethodDic[shop.Platform](orderListResponse.Data, shopId, relayAPIHost, shop.AppKey, shop.AppSecret, shop.AppToken); } catch (Exception ex) { - var shopData = JsonConvert.SerializeObject(shopJToken); + var shopData = JsonConvert.SerializeObject(shop); logger.Error(ex, $"SyncOrder ShopData:{shopData}"); } } diff --git a/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs b/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs index c717fe86..791f5e28 100644 --- a/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs +++ b/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs @@ -1,11 +1,17 @@ using BBWY.Common.Extensions; using BBWY.Common.Models; +using BBWY.Server.Model; using BBWY.Server.Model.Db; using BBWY.Server.Model.Dto; using FreeSql; +using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; +using System.Threading; +using System.Threading.Tasks; using Yitter.IdGenerator; namespace BBWY.Server.Business @@ -13,9 +19,17 @@ namespace BBWY.Server.Business public class PurchaseOrderBusiness : BaseBusiness, IDenpendency { private IEnumerable platformSDKBusinessList; - public PurchaseOrderBusiness(IFreeSql fsql, NLog.ILogger logger, IIdGenerator idGenerator, IEnumerable platformSDKBusinessList) : base(fsql, logger, idGenerator) + private TaskSchedulerManager taskSchedulerManager; + private OrderBusiness orderBusiness; + private MDSBusiness mdsBusiness; + + + public PurchaseOrderBusiness(IFreeSql fsql, NLog.ILogger logger, IIdGenerator idGenerator, IEnumerable platformSDKBusinessList, TaskSchedulerManager taskSchedulerManager, OrderBusiness orderBusiness, MDSBusiness mdsBusiness) : base(fsql, logger, idGenerator) { this.platformSDKBusinessList = platformSDKBusinessList; + this.taskSchedulerManager = taskSchedulerManager; + this.orderBusiness = orderBusiness; + this.mdsBusiness = mdsBusiness; } public void AddPurchaseOrder(AddPurchaseOrderRequest addPurchaseOrderRequest) @@ -187,5 +201,82 @@ namespace BBWY.Server.Business .ExecuteAffrows(); }); } + + public void DeliveryCallbackFrom1688(object param) + { + var orderJObject = JObject.Parse(param.ToString()); + var purchaseOrderId = orderJObject.Value("orderId"); + Task.Factory.StartNew(() => DeliveryCallback(purchaseOrderId, Enums.Platform.阿里巴巴), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler); + } + + public void DeliveryCallbackFromPDD(object param) + { + var orderJObject = JObject.Parse(param.ToString()); + var purchaseOrderId = orderJObject.Value("orderId"); + Task.Factory.StartNew(() => DeliveryCallback(purchaseOrderId, Enums.Platform.拼多多), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler); + } + + /// + /// 采购平台回调核心流程 + /// + /// + /// + private void DeliveryCallback(string purchaseOrderId, Enums.Platform callbackPlatform) + { + string currentProgress = string.Empty; + try + { + #region 查询采购单 + currentProgress = "查询采购单"; + var orderDropshipping = fsql.Select().Where(o => o.PurchaseOrderId == purchaseOrderId).ToOne(); + if (orderDropshipping == null) + throw new Exception("未查询到采购单号"); + #endregion + + #region 查询采购账号 + currentProgress = "查询采购账号"; + var purchaseAccount = fsql.Select().Where(pa => pa.AccountName == orderDropshipping.BuyerAccount && + pa.PurchasePlatformId == callbackPlatform).ToOne(); + if (purchaseAccount == null) + throw new Exception($"未查询到采购账号{orderDropshipping.BuyerAccount}"); + #endregion + + #region 获取采购单的物流信息 + currentProgress = "获取采购单的物流信息"; + + #endregion + + #region 查询采购账号的归属店铺 + currentProgress = "查询采购账号归属店铺"; + var shop = mdsBusiness.GetShopInfoByShopId(purchaseAccount.ShopId.Value); + #endregion + + #region 获取目标平台的物流公司列表 + currentProgress = "获取目标平台物流公司列表"; + #endregion + + #region 物流公司翻译 + currentProgress = "将采购单的物流公司翻译为店铺平台的物流公司"; + #endregion + + #region 店铺平台订单出库 + currentProgress = "店铺平台订单出库"; + orderBusiness.OutStock(new OutStockRequest() + { + AppKey = shop.AppKey, + AppSecret = shop.AppSecret, + AppToken = shop.AppToken, + OrderId = orderDropshipping.OrderId, + Platform = shop.Platform, + WayBillNo = "", + LogisticsId = "" + }); + #endregion + } + catch (Exception ex) + { + logger.Error(ex, $"回调平台{callbackPlatform},采购单号{purchaseOrderId},执行进度[{currentProgress}]"); + } + } } } diff --git a/BBWY.Server.Business/TaskSchedulerManager.cs b/BBWY.Server.Business/TaskSchedulerManager.cs index cadc99d8..27ce9200 100644 --- a/BBWY.Server.Business/TaskSchedulerManager.cs +++ b/BBWY.Server.Business/TaskSchedulerManager.cs @@ -6,9 +6,12 @@ namespace BBWY.Server.Business { public LimitedConcurrencyLevelTaskScheduler SyncOrderTaskScheduler { get; private set; } + public LimitedConcurrencyLevelTaskScheduler PurchaseOrderCallbackTaskScheduler { get; private set; } + public TaskSchedulerManager() { SyncOrderTaskScheduler = new LimitedConcurrencyLevelTaskScheduler(10); + PurchaseOrderCallbackTaskScheduler = new LimitedConcurrencyLevelTaskScheduler(10); } } } diff --git a/BBWY.Server.Model/Db/PurchaseScheme/PurchaseAccount.cs b/BBWY.Server.Model/Db/PurchaseScheme/PurchaseAccount.cs new file mode 100644 index 00000000..720ef179 --- /dev/null +++ b/BBWY.Server.Model/Db/PurchaseScheme/PurchaseAccount.cs @@ -0,0 +1,61 @@ +using FreeSql.DataAnnotations; +using System; + +namespace BBWY.Server.Model.Db +{ + + /// + /// 采购账号表 + /// + [Table(Name = "purchaseaccount", DisableSyncStructure = true)] + public partial class PurchaseAccount + { + + /// + /// 主键 + /// + [Column(StringLength = 50, IsPrimary = true, IsNullable = false)] + public string Id { get; set; } + + + public string AccountName { get; set; } + + + public string AppKey { get; set; } + + + public string AppSecret { get; set; } + + + public string AppToken { get; set; } + + /// + /// 创建时间 + /// + [Column(DbType = "datetime")] + public DateTime CreateTime { get; set; } + + /// + /// 创建人Id + /// + [Column(StringLength = 50)] + public string CreatorId { get; set; } + + /// + /// 否已删除 + /// + [Column(DbType = "tinyint(4)")] + public sbyte Deleted { get; set; } + + [Column(DbType = "int(1)", MapType = typeof(int?))] + public Enums.Platform? PurchasePlatformId { get; set; } + + /// + /// 采购账号归属店铺ID + /// + [Column(DbType = "bigint(1)")] + public long? ShopId { get; set; } + + } + +} diff --git a/BBWY.Server.Model/Dto/Response/Vender/ShopResponse.cs b/BBWY.Server.Model/Dto/Response/Vender/ShopResponse.cs new file mode 100644 index 00000000..b9ef03d6 --- /dev/null +++ b/BBWY.Server.Model/Dto/Response/Vender/ShopResponse.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Text; +using static BBWY.Server.Model.Enums; + +namespace BBWY.Server.Model.Dto +{ + public class ShopResponse + { + public long ShopId { get; set; } + + public string Name { get; set; } + + /// + /// 商家类型 + /// + public string VenderType { get; set; } + + /// + /// 店铺平台 + /// + public Platform Platform { get; set; } + + public string AppKey { get; set; } + + public string AppSecret { get; set; } + + public string AppToken { get; set; } + } +}