From b4b7e088a9d218723a5d7d887d5dbdf8985e0837 Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Sun, 31 Jul 2022 18:38:29 +0800 Subject: [PATCH] 1 --- .../Sync/ProductSyncBusiness.cs | 85 +++++++++++++------ 1 file changed, 60 insertions(+), 25 deletions(-) diff --git a/BBWY.Server.Business/Sync/ProductSyncBusiness.cs b/BBWY.Server.Business/Sync/ProductSyncBusiness.cs index e85d0f5b..03bce1e2 100644 --- a/BBWY.Server.Business/Sync/ProductSyncBusiness.cs +++ b/BBWY.Server.Business/Sync/ProductSyncBusiness.cs @@ -11,6 +11,7 @@ using BBWY.Server.Model.Db; using System; using System.Collections.Generic; using Newtonsoft.Json; +using FreeSql; namespace BBWY.Server.Business.Sync { @@ -54,22 +55,38 @@ namespace BBWY.Server.Business.Sync if (productList == null || productList.Count == 0) return; + 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(); - var insertProductList = productList.Items.Select(p => new Product() + foreach (var product in productList.Items) { - Id = p.Id, - CreateTime = DateTime.Now, - Platform = shop.PlatformId, - ProductItemNum = p.ProductItemNum, - ShopId = shopId, - Title = p.Title, - State = p.State - }).ToList(); + var dbProduct = dbProducts.FirstOrDefault(dbp => dbp.Id == product.Id); + if (dbProduct == null) + { + insertProductList.Add(new Product() + { + Id = product.Id, + 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 inserSkuList = new List(); - foreach (var product in insertProductList) - { var skuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest() { AppKey = shop.AppKey, @@ -78,26 +95,44 @@ namespace BBWY.Server.Business.Sync Platform = shop.PlatformId, Spu = product.Id }); - inserSkuList.AddRange(skuList.Select(s => new ProductSku() + + foreach (var sku in skuList) { - Id = s.Id, - CreateTime = DateTime.Now, - Logo = s.Logo, - Platform = shop.PlatformId, - Price = s.Price, - ProductId = s.ProductId, - ShopId = shopId, - Title = s.Title, - State = s.State - })); + var dbSku = dbProductSkus.FirstOrDefault(s => s.Id == sku.Id); + if (dbSku == null) + { + inserSkuList.Add(new ProductSku() + { + Id = sku.Id, + 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.Delete().Where(p => p.ShopId == shopId && productIds.Contains(p.Id)).ExecuteAffrows(); - fsql.Delete().Where(s => s.ShopId == shopId && productIds.Contains(s.ProductId)).ExecuteAffrows(); 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)