From 45a19f0440ce34848a7727107c24f15a19b889eb Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Wed, 12 Jul 2023 12:49:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A7=E5=93=81=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sync/ProductSyncBusiness.cs | 147 +++++++++++++++++- 1 file changed, 140 insertions(+), 7 deletions(-) diff --git a/BBWY.Server.Business/Sync/ProductSyncBusiness.cs b/BBWY.Server.Business/Sync/ProductSyncBusiness.cs index dbda2854..d757a63c 100644 --- a/BBWY.Server.Business/Sync/ProductSyncBusiness.cs +++ b/BBWY.Server.Business/Sync/ProductSyncBusiness.cs @@ -185,6 +185,19 @@ namespace BBWY.Server.Business.Sync { try { + var shopId = long.Parse(shop.ShopId); + var dbProductList = fsql.Select().Where(p => p.ShopId == shopId).ToList(); + var dbProductSkuList = fsql.Select().Where(p => p.ShopId == shopId).ToList(); + + List productList = new List(); + List productSkuList = new List(); + + List insertProductList = new List(); + List insertProductSkuList = new List(); + + List> updateProductList = new List>(); + List> updateProductSkuList = new List>(); + var request = new SearchProductRequest() { PageSize = 50, @@ -196,15 +209,135 @@ namespace BBWY.Server.Business.Sync }; while (true) { - var productList = productBusiness.GetProductList(request); - if (productList == null || productList.Count == 0) - return; - SyncProduct(shop, productList); - if (productList.Items.Count < 50 || !IsSyncAllProduct) + var currentProductList = productBusiness.GetProductList(request); + if (currentProductList == null || currentProductList.Count == 0) + break; + productList.AddRange(currentProductList.Items); + + foreach (var product in currentProductList.Items) + { + var currentSkuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest() + { + AppKey = shop.AppKey, + AppSecret = shop.AppSecret, + AppToken = shop.AppToken, + Platform = shop.PlatformId, + Spu = product.Id, + IsContainSource = true + }); + if (currentSkuList == null || currentSkuList.Count() == 0) + continue; + productSkuList.AddRange(currentSkuList); + } + + if (currentProductList.Items.Count < 50 || !IsSyncAllProduct) break; request.PageIndex++; Thread.Sleep(1000); } + + #region 找出新增的产品 + var newProductList = productList.Where(p => !dbProductList.Any(dp => dp.Id == p.Id)).ToList(); + if (newProductList.Count() > 0) + { + insertProductList.AddRange(newProductList.Select(p => new Product() + { + Id = p.Id, + CreateTime = p.CreateTime, + Title = p.Title, + State = p.State, + ShopId = shopId, + Platform = shop.PlatformId, + ProductItemNum = p.ProductItemNum, + MainSkuId = productSkuList.FirstOrDefault(s => s.ProductId == p.Id)?.Id + })); + } + #endregion + + #region 找出状态变化的产品 + var stateChangeProductList = productList.Where(p => dbProductList.Any(dp => dp.Id == p.Id && dp.State != p.State)).ToList(); + if (stateChangeProductList.Count() > 0) + { + foreach (var product in stateChangeProductList) + { + var update = fsql.Update(product.Id).Set(p => p.State, product.State); + updateProductList.Add(update); + } + } + #endregion + + #region 找出接口查不出的产品 + var goneProductList = dbProductList.Where(dp => !productList.Any(p => p.Id == dp.Id)).ToList(); + if (goneProductList.Count() > 0) + { + var goneProductIdList = goneProductList.Select(p => p.Id).ToList(); + var update = fsql.Update().Set(p => p.State, -1).Where(p => goneProductIdList.Contains(p.Id)); + updateProductList.Add(update); + } + #endregion + + #region 找出新增的SKU + var newProductSkuList = productSkuList.Where(p => !dbProductSkuList.Any(dp => dp.Id == p.Id)).ToList(); + if (newProductSkuList.Count() > 0) + { + insertProductSkuList.AddRange(newProductSkuList.Select(p => new ProductSku() + { + Id = p.Id, + CreateTime = p.CreateTime, + Title = p.Title, + State = p.State, + ShopId = shopId, + Platform = shop.PlatformId, + Logo = p.Logo, + ProductId = p.ProductId, + Price = p.Price, + CategoryId = p.Source.Value("categoryId") + })); + } + #endregion + + #region 找出状态变化的SKU + var stateChangeProductSkuList = productSkuList.Where(p => dbProductSkuList.Any(dp => dp.Id == p.Id && dp.State != p.State)).ToList(); + if (stateChangeProductSkuList.Count() > 0) + { + foreach (var productSku in stateChangeProductSkuList) + { + var update = fsql.Update(productSku.Id).Set(p => p.State, productSku.State); + updateProductSkuList.Add(update); + } + } + #endregion + + #region 找出接口查不出的SKU + var goneProductSkuList = dbProductSkuList.Where(dp => !productSkuList.Any(p => p.Id == dp.Id)).ToList(); + if (goneProductSkuList.Count() > 0) + { + var goneProductSkuIdList = goneProductSkuList.Select(p => p.Id).ToList(); + var update = fsql.Update().Set(p => p.State, 4).Where(p => goneProductSkuIdList.Contains(p.Id)); + updateProductSkuList.Add(update); + } + #endregion + + + fsql.Transaction(() => + { + if (insertProductList.Count() > 0) + fsql.Insert(insertProductList).ExecuteAffrows(); + if (insertProductSkuList.Count() > 0) + fsql.Insert(insertProductSkuList).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) { @@ -231,8 +364,8 @@ namespace BBWY.Server.Business.Sync /// public void SyncAllShopAllProduct() { - var shopList = venderBusiness.GetShopList(); - //SyncProduct(shopList.FirstOrDefault(s => s.ShopName == "趣弈手机配件专营店"), true); + var shopList = venderBusiness.GetShopList(platform: Enums.Platform.京东); + //SyncProduct(shopList.FirstOrDefault(s => s.ShopName == "布莱特玩具专营店"), true); foreach (var shop in shopList) { Task.Factory.StartNew(() => SyncProduct(shop, true), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler);