diff --git a/BBWY.Server.Business/PlatformSDK/JDBusiness.cs b/BBWY.Server.Business/PlatformSDK/JDBusiness.cs index a81e8463..8b23f19f 100644 --- a/BBWY.Server.Business/PlatformSDK/JDBusiness.cs +++ b/BBWY.Server.Business/PlatformSDK/JDBusiness.cs @@ -182,6 +182,28 @@ namespace BBWY.Server.Business } + public override ProductCategoryResponse GetCategoryInfo(JDQueryCategoryRequest request) + { + var jdClient = GetJdClient(request.AppKey, request.AppSecret); + var req = new CategoryReadFindByIdRequest(); + req.cid = long.Parse(request.CategoryId); + req.field = "fid,id,lev,name"; + + var res = jdClient.Execute(req, request.AppToken, DateTime.Now.ToLocalTime()); + if (res.IsError) + throw new BusinessException(string.IsNullOrEmpty(res.ErrorMsg) ? res.ErrMsg : res.ErrorMsg); + + if (res.Json == null) + res.Json = JObject.Parse(res.Body); + var j = res.Json["jingdong_category_read_findById_responce"]["category"]; + return new ProductCategoryResponse() + { + Fid = j.Value("fid"), + Id = j.Value("id"), + Name = j.Value("name"), + Lev = j.Value("lev") + }; + } public override IList GetSimpleProductSkuList(SearchProductSkuRequest searchProductSkuRequest) { var jdClient = GetJdClient(searchProductSkuRequest.AppKey, searchProductSkuRequest.AppSecret); diff --git a/BBWY.Server.Business/PlatformSDK/PlatformSDKBusiness.cs b/BBWY.Server.Business/PlatformSDK/PlatformSDKBusiness.cs index e97af5bf..7755fffc 100644 --- a/BBWY.Server.Business/PlatformSDK/PlatformSDKBusiness.cs +++ b/BBWY.Server.Business/PlatformSDK/PlatformSDKBusiness.cs @@ -43,6 +43,11 @@ namespace BBWY.Server.Business throw new NotImplementedException(); } + public virtual ProductCategoryResponse GetCategoryInfo(JDQueryCategoryRequest request) + { + throw new NotImplementedException(); + } + public virtual IList GetOrderList(SearchPlatformOrderRequest searchOrderRequest) { throw new NotImplementedException(); diff --git a/BBWY.Server.Business/Product/ProductBusiness.cs b/BBWY.Server.Business/Product/ProductBusiness.cs index eab8beba..763d7690 100644 --- a/BBWY.Server.Business/Product/ProductBusiness.cs +++ b/BBWY.Server.Business/Product/ProductBusiness.cs @@ -40,5 +40,17 @@ namespace BBWY.Server.Business throw new BusinessException(response.Msg) { Code = response.Code }; return response.Data; } + + public ProductCategoryResponse GetCategoyrInfo(JDQueryCategoryRequest request) + { + var relayAPIHost = GetPlatformRelayAPIHost(request.Platform); + var sendResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetCategoryInfo", request, GetYunDingRequestHeader(), HttpMethod.Post); + if (sendResult.StatusCode != System.Net.HttpStatusCode.OK) + throw new BusinessException(sendResult.Content) { Code = (int)sendResult.StatusCode }; + var response = JsonConvert.DeserializeObject>(sendResult.Content); + if (!response.Success) + throw new BusinessException(response.Msg) { Code = response.Code }; + return response.Data; + } } } diff --git a/BBWY.Server.Business/Sync/ProductSyncBusiness.cs b/BBWY.Server.Business/Sync/ProductSyncBusiness.cs index 417a3a21..5cfffc36 100644 --- a/BBWY.Server.Business/Sync/ProductSyncBusiness.cs +++ b/BBWY.Server.Business/Sync/ProductSyncBusiness.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using Newtonsoft.Json; using FreeSql; using System.Threading; +using System.Collections.Concurrent; namespace BBWY.Server.Business.Sync { @@ -20,6 +21,8 @@ namespace BBWY.Server.Business.Sync { private ProductBusiness productBusiness; + private ConcurrentDictionary categoryCache; + public ProductSyncBusiness(RestApiService restApiService, IOptions options, NLogManager nLogManager, @@ -38,6 +41,7 @@ namespace BBWY.Server.Business.Sync yunDingBusiness) { this.productBusiness = productBusiness; + this.categoryCache = new ConcurrentDictionary(); } private string GetMainSkuId(IList skuList) @@ -292,7 +296,8 @@ namespace BBWY.Server.Business.Sync Logo = p.Logo, ProductId = p.ProductId, Price = p.Price, - CategoryId = p.Source.Value("categoryId") + CategoryId = p.Source.Value("categoryId"), + CategoryName = GetCategoryName(shop, p.Source.Value("categoryId")) })); } #endregion @@ -309,6 +314,19 @@ namespace BBWY.Server.Business.Sync } #endregion + #region 找出缺少类目的sku + var noCategoryProductList = productSkuList.Where(p => dbProductSkuList.Any(dp => dp.Id == p.Id && string.IsNullOrEmpty(dp.CategoryName))).ToList(); + if (noCategoryProductList.Count() > 0) + { + foreach (var productSku in noCategoryProductList) + { + var categoryName = GetCategoryName(shop, productSku.Source.Value("categoryId")); + var update = fsql.Update(productSku.Id).Set(p => p.CategoryName, categoryName); + updateProductSkuList.Add(update); + } + } + #endregion + #region 找出接口查不出的SKU var goneProductSkuList = dbProductSkuList.Where(dp => !productSkuList.Any(p => p.Id == dp.Id)).ToList(); if (goneProductSkuList.Count() > 0) @@ -366,12 +384,36 @@ namespace BBWY.Server.Business.Sync public void SyncAllShopAllProduct() { var shopList = venderBusiness.GetShopList(platform: Enums.Platform.京东); - //SyncProduct(shopList.FirstOrDefault(s => s.ShopName == "熊猴玩具专营店"), true); + //SyncProduct(shopList.FirstOrDefault(s => s.ShopName == "披风熊玩具专营店"), true); foreach (var shop in shopList) { Task.Factory.StartNew(() => SyncProduct(shop, true), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler); } } + + private string GetCategoryName(ShopResponse shop, string categoryId) + { + if (categoryCache.TryGetValue(categoryId, out string name)) + return name; + try + { + var res = productBusiness.GetCategoyrInfo(new JDQueryCategoryRequest() + { + AppKey = shop.AppKey, + CategoryId = categoryId, + AppSecret = shop.AppSecret, + AppToken = shop.AppToken, + Platform = shop.PlatformId + }); + categoryCache.TryAdd(categoryId, res.Name); + name = res.Name; + } + catch + { + + } + return name; + } } } diff --git a/BBWY.Server.Model/Db/Product/ProductSku.cs b/BBWY.Server.Model/Db/Product/ProductSku.cs index 11cb529d..b5cd26da 100644 --- a/BBWY.Server.Model/Db/Product/ProductSku.cs +++ b/BBWY.Server.Model/Db/Product/ProductSku.cs @@ -51,6 +51,7 @@ namespace BBWY.Server.Model.Db /// public int? CategoryId { get; set; } + public string CategoryName { get; set; } } } diff --git a/BBWY.Server.Model/Dto/Request/JD/JDQueryCategoryRequest.cs b/BBWY.Server.Model/Dto/Request/JD/JDQueryCategoryRequest.cs new file mode 100644 index 00000000..ce6b209c --- /dev/null +++ b/BBWY.Server.Model/Dto/Request/JD/JDQueryCategoryRequest.cs @@ -0,0 +1,7 @@ +namespace BBWY.Server.Model.Dto +{ + public class JDQueryCategoryRequest:PlatformRequest + { + public string CategoryId { get; set; } + } +} diff --git a/BBWY.Server.Model/Dto/Response/Product/ProductCategoryResponse.cs b/BBWY.Server.Model/Dto/Response/Product/ProductCategoryResponse.cs new file mode 100644 index 00000000..861359d9 --- /dev/null +++ b/BBWY.Server.Model/Dto/Response/Product/ProductCategoryResponse.cs @@ -0,0 +1,13 @@ +namespace BBWY.Server.Model.Dto +{ + public class ProductCategoryResponse + { + public string Id { get; set; } + + public string Name { get; set; } + + public string Fid { get; set; } + + public string Lev { get; set; } + } +} diff --git a/JD.API/Controllers/PlatformSDKController.cs b/JD.API/Controllers/PlatformSDKController.cs index 61f6255e..fc180222 100644 --- a/JD.API/Controllers/PlatformSDKController.cs +++ b/JD.API/Controllers/PlatformSDKController.cs @@ -58,6 +58,17 @@ namespace JD.API.API.Controllers return platformSDKBusinessList.FirstOrDefault(p => p.Platform == searchProductSkuRequest.Platform).GetProductSkuList(searchProductSkuRequest); } + /// + /// 查询商品类目 + /// + /// + /// + [HttpPost] + public ProductCategoryResponse GetCategoryInfo([FromBody] JDQueryCategoryRequest request) + { + return platformSDKBusinessList.FirstOrDefault(p => p.Platform == request.Platform).GetCategoryInfo(request); + } + /// /// 获取简单Sku列表 只包含SkuId和图片 ///