diff --git a/BBWY.Server.Business/PlatformSDK/JDBusiness.cs b/BBWY.Server.Business/PlatformSDK/JDBusiness.cs index 3a5c35e1..71e6f2ab 100644 --- a/BBWY.Server.Business/PlatformSDK/JDBusiness.cs +++ b/BBWY.Server.Business/PlatformSDK/JDBusiness.cs @@ -131,6 +131,15 @@ namespace BBWY.Server.Business var rep_skuList = jdClient.Execute(req_skuList, searchProductSkuRequest.AppToken, DateTime.Now.ToLocalTime()); if (rep_skuList.IsError) throw new BusinessException(string.IsNullOrEmpty(rep_skuList.ErrorMsg) ? rep_skuList.ErrMsg : rep_skuList.ErrorMsg); + + if (rep_skuList.Json == null || + !rep_skuList.Json.ContainsKey("jingdong_sku_read_searchSkuList_responce") || + rep_skuList.Json["jingdong_sku_read_searchSkuList_responce"]["page"] == null || + rep_skuList.Json["jingdong_sku_read_searchSkuList_responce"]["page"]["data"] == null) + { + return new ProductSkuResponse[0]; + } + return ((JArray)rep_skuList.Json["jingdong_sku_read_searchSkuList_responce"]["page"]["data"]).Select(s => new ProductSkuResponse() { Id = s.Value("skuId"), diff --git a/BBWY.Server.Business/Sync/ProductSyncBusiness.cs b/BBWY.Server.Business/Sync/ProductSyncBusiness.cs index 64bf51e3..fafba1bd 100644 --- a/BBWY.Server.Business/Sync/ProductSyncBusiness.cs +++ b/BBWY.Server.Business/Sync/ProductSyncBusiness.cs @@ -37,88 +37,98 @@ namespace BBWY.Server.Business.Sync this.productBusiness = productBusiness; } - private void SyncProduct(ShopResponse shop, ProductListResponse productList) + private void SyncProduct(ShopResponse shop, ProductListResponse productList, out string productId) { - var shopId = long.Parse(shop.ShopId); - List insertProductList = new List(); - List> updateProductList = new List>(); - - List inserSkuList = new List(); - List> updateProductSkuList = new List>(); - - var productIds = productList.Items.Select(p => p.Id); - var dbProducts = fsql.Select().Where(p => p.ShopId == shopId && productIds.Contains(p.Id)).ToList(); - var dbProductSkus = fsql.Select().Where(s => s.ShopId == shopId && productIds.Contains(s.ProductId)).ToList(); - - foreach (var product in productList.Items) + productId = ""; + try { - var dbProduct = dbProducts.FirstOrDefault(dbp => dbp.Id == product.Id); - if (dbProduct == null) - { - insertProductList.Add(new Product() - { - Id = product.Id, - CreateTime = product.CreateTime ?? DateTime.Now, - Platform = shop.PlatformId, - ProductItemNum = product.ProductItemNum, - ShopId = shopId, - Title = product.Title, - State = product.State - }); - } - else if (dbProduct.State != product.State) - { - var update = fsql.Update(product.Id).Set(p => p.State, dbProduct.State); - updateProductList.Add(update); - } + var shopId = long.Parse(shop.ShopId); + List insertProductList = new List(); + List> updateProductList = new List>(); - var skuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest() - { - AppKey = shop.AppKey, - AppSecret = shop.AppSecret, - AppToken = shop.AppToken, - Platform = shop.PlatformId, - Spu = product.Id - }); + List inserSkuList = new List(); + List> updateProductSkuList = new List>(); + + var productIds = productList.Items.Select(p => p.Id); + var dbProducts = fsql.Select().Where(p => p.ShopId == shopId && productIds.Contains(p.Id)).ToList(); + var dbProductSkus = fsql.Select().Where(s => s.ShopId == shopId && productIds.Contains(s.ProductId)).ToList(); - foreach (var sku in skuList) + foreach (var product in productList.Items) { - var dbSku = dbProductSkus.FirstOrDefault(s => s.Id == sku.Id); - if (dbSku == null) + productId = product.Id; + var dbProduct = dbProducts.FirstOrDefault(dbp => dbp.Id == product.Id); + if (dbProduct == null) { - inserSkuList.Add(new ProductSku() + insertProductList.Add(new Product() { - Id = sku.Id, - CreateTime = sku.CreateTime ?? DateTime.Now, - Logo = sku.Logo, + Id = product.Id, + CreateTime = product.CreateTime ?? DateTime.Now, Platform = shop.PlatformId, - Price = sku.Price, - ProductId = sku.ProductId, + ProductItemNum = product.ProductItemNum, ShopId = shopId, - Title = sku.Title, - State = sku.State + Title = product.Title, + State = product.State }); } - else if (dbSku.State != sku.State || dbSku.Price != sku.Price) + else if (dbProduct.State != product.State) { - var update = fsql.Update(dbSku.Id).Set(s => s.State, sku.State).Set(s => s.Price, sku.Price); - updateProductSkuList.Add(update); + var update = fsql.Update(product.Id).Set(p => p.State, dbProduct.State); + updateProductList.Add(update); + } + + var skuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest() + { + AppKey = shop.AppKey, + AppSecret = shop.AppSecret, + AppToken = shop.AppToken, + Platform = shop.PlatformId, + Spu = product.Id + }); + if (skuList == null || skuList.Count() == 0) + continue; + foreach (var sku in skuList) + { + var dbSku = dbProductSkus.FirstOrDefault(s => s.Id == sku.Id); + if (dbSku == null) + { + inserSkuList.Add(new ProductSku() + { + Id = sku.Id, + CreateTime = sku.CreateTime ?? DateTime.Now, + Logo = sku.Logo, + Platform = shop.PlatformId, + Price = sku.Price, + ProductId = sku.ProductId, + ShopId = shopId, + Title = sku.Title, + State = sku.State + }); + } + else if (dbSku.State != sku.State || dbSku.Price != sku.Price) + { + var update = fsql.Update(dbSku.Id).Set(s => s.State, sku.State).Set(s => s.Price, sku.Price); + updateProductSkuList.Add(update); + } } } - } - fsql.Transaction(() => - { - fsql.Insert(insertProductList).ExecuteAffrows(); - fsql.Insert(inserSkuList).ExecuteAffrows(); - if (updateProductList.Count > 0) - foreach (var update in updateProductList) - update.ExecuteAffrows(); - if (updateProductSkuList.Count > 0) - foreach (var update in updateProductSkuList) - update.ExecuteAffrows(); + fsql.Transaction(() => + { + fsql.Insert(insertProductList).ExecuteAffrows(); + fsql.Insert(inserSkuList).ExecuteAffrows(); + if (updateProductList.Count > 0) + foreach (var update in updateProductList) + update.ExecuteAffrows(); + if (updateProductSkuList.Count > 0) + foreach (var update in updateProductSkuList) + update.ExecuteAffrows(); - }); + }); + } + catch (Exception ex) + { + throw new Exception(productId, ex); + } } private void SyncProduct(ShopResponse shop, bool IsSyncAllProduct = false) @@ -139,7 +149,7 @@ namespace BBWY.Server.Business.Sync var productList = productBusiness.GetProductList(request); if (productList == null || productList.Count == 0) return; - SyncProduct(shop, productList); + SyncProduct(shop, productList, out string productId); if (productList.Items.Count < 50 || !IsSyncAllProduct) break; request.PageIndex++; @@ -171,11 +181,11 @@ namespace BBWY.Server.Business.Sync public void SyncAllShopAllProduct() { var shopList = venderBusiness.GetShopList(); - //SyncProduct(shopList.FirstOrDefault(s => s.ShopId == "10388155"), true); //布莱特玩具专营店 - foreach (var shop in shopList) - { - Task.Factory.StartNew(() => SyncProduct(shop, true), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler); - } + SyncProduct(shopList.FirstOrDefault(s => s.ShopName == "瑞源玩具专营店"), true); //瑞源玩具专营店 + //foreach (var shop in shopList) + //{ + // Task.Factory.StartNew(() => SyncProduct(shop, true), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler); + //} } } } diff --git a/BBWY.Server.Business/Sync/RefundOrderSyncBusiness.cs b/BBWY.Server.Business/Sync/RefundOrderSyncBusiness.cs index 08036882..31a7d260 100644 --- a/BBWY.Server.Business/Sync/RefundOrderSyncBusiness.cs +++ b/BBWY.Server.Business/Sync/RefundOrderSyncBusiness.cs @@ -156,11 +156,11 @@ namespace BBWY.Server.Business public void SyncAllShopRefundOrder() { var shopList = venderBusiness.GetShopList(); - SyncRefundOrder(shopList.FirstOrDefault(s => s.ShopName == "赟娅墨森专卖店"), string.Empty, isAuto: true); - //foreach (var shop in shopList) - //{ - // Task.Factory.StartNew(() => SyncRefundOrder(shop, string.Empty, isAuto: true), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SyncRefundOrderTaskScheduler); - //} + //SyncRefundOrder(shopList.FirstOrDefault(s => s.ShopName == "赟娅墨森专卖店"), string.Empty, isAuto: true); + foreach (var shop in shopList) + { + Task.Factory.StartNew(() => SyncRefundOrder(shop, string.Empty, isAuto: true), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SyncRefundOrderTaskScheduler); + } } } } diff --git a/BBWY.Test/Program.cs b/BBWY.Test/Program.cs index d1669bdb..ddeecfe9 100644 --- a/BBWY.Test/Program.cs +++ b/BBWY.Test/Program.cs @@ -2,6 +2,7 @@ using Jd.Api.Request; using Jd.Api.Response; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; namespace BBWY.Test { @@ -14,6 +15,7 @@ namespace BBWY.Test static void Main(string[] args) { + var appKey = "120EA9EC65AB017567D78CC1139EEEA5"; var appSecret = "866a9877f5f24b03b537483b4defe75d"; var token = "2ace3023200c4ea9aa682bbf8bffee18jztm";