using BBWYB.Common.Log; using BBWYB.Common.Models; using BBWYB.Server.Model; using BBWYB.Server.Model.Db; using BBWYB.Server.Model.Dto; using FreeSql; using SDKAdapter.OperationPlatform.Models; using Yitter.IdGenerator; namespace BBWYB.Server.Business.Sync { public class ProductSyncBusiness : BaseBusiness, IDenpendency { private ProductBusiness productBusiness; private VenderBusiness venderBusiness; private TaskSchedulerManager taskSchedulerManager; public ProductSyncBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, ProductBusiness productBusiness, VenderBusiness venderBusiness, TaskSchedulerManager taskSchedulerManager) : base(fsql, nLogManager, idGenerator) { this.productBusiness = productBusiness; this.venderBusiness = venderBusiness; this.taskSchedulerManager = taskSchedulerManager; } /// /// 同步所有店铺的全部产品 /// public void SyncAllShopAllProduct() { var shopList = venderBusiness.GetShopList(platform: Enums.Platform.拳探); //var shopList = venderBusiness.GetShopList(shopId: 9); foreach (var shop in shopList) { Task.Factory.StartNew(() => SyncProduct(shop, true), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SyncProductTaskScheduler); } } public void SyncAllShopUpdateProduct() { } private void SyncProduct(ShopResponse shop, bool IsSyncAllProduct = false) { 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 OP_QueryProductRequest() { PageSize = 50, PageIndex = 1, AppKey = shop.AppKey, AppSecret = shop.AppSecret, AppToken = shop.AppToken, Platform = SDKAdapter.AdapterEnums.PlatformType.拳探 }; var requestSku = new OP_QueryProductSkuRequest() { AppKey = shop.AppKey, AppSecret = shop.AppSecret, AppToken = shop.AppToken, Platform = SDKAdapter.AdapterEnums.PlatformType.拳探, //Spu = product.Id, PageSize = 50, PageIndex = 1 }; while (true) { Console.WriteLine($"{shop.ShopName} 获取第{request.PageIndex}页产品"); var pListRes = productBusiness.GetProductList(request); if (pListRes == null || pListRes.Items == null || pListRes.Items.Count == 0) break; productList.AddRange(pListRes.Items); foreach (var product in pListRes.Items) { requestSku.Spu = product.Id; var psListRes = productBusiness.GetProductSkuList(requestSku); if (psListRes == null || psListRes.Items == null || psListRes.Items.Count == 0) continue; productSkuList.AddRange(psListRes.Items); } if (pListRes.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, Logo = p.Logo, ProductName = p.Title, ShopId = int.Parse(shop.ShopId), State = p.State, SyncTime = DateTime.Now, UpdateTime = DateTime.Now, UpperTime = p.CreateTime, CategoryId = p.CategoryId, CategoryName = p.CategoryName })); } #endregion #region 找出变化的产品 (状态,标题) var stateChangeProductList = productList.Where(p => dbProductList.Any(dp => dp.Id == p.Id && (dp.State != p.State || dp.ProductName != p.Title || dp.CategoryId != p.CategoryId))).ToList(); if (stateChangeProductList.Count() > 0) { foreach (var product in stateChangeProductList) { var update = fsql.Update(product.Id).Set(p => p.State, product.State) .Set(p => p.ProductName, product.Title) .Set(p => p.CategoryId, product.CategoryId) .Set(p => p.CategoryName, product.CategoryName); 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, 0).Where(p => goneProductIdList.Contains(p.Id)); updateProductList.Add(update); } #endregion #region 找出新增的SKU var newProductSkuList = productSkuList.Where(ps => !dbProductSkuList.Any(dps => dps.Id == ps.Id)).ToList(); if (newProductSkuList.Count() > 0) { insertProductSkuList.AddRange(newProductSkuList.Select(ps => new ProductSku() { Id = ps.Id, Logo = ps.Logo, Price = ps.Price, ProductId = ps.ProductId, ShopId = int.Parse(shop.ShopId), SkuName = ps.Title, State = ps.State, SyncTime = DateTime.Now, UpdateTime = DateTime.Now, UpperTime = ps.CreateTime, CategoryId = ps.CategoryId, CategoryName = ps.CategoryName })); } #endregion #region 找出状态变化的SKU var stateChangeProductSkuList = productSkuList.Where(ps => dbProductSkuList.Any(dps => dps.Id == ps.Id && (dps.State != ps.State || dps.SkuName != ps.Title || dps.Price != ps.Price || dps.Logo != ps.Logo || dps.CategoryId != ps.CategoryId))).ToList(); if (stateChangeProductSkuList.Count() > 0) { foreach (var productSku in stateChangeProductSkuList) { var update = fsql.Update(productSku.Id).Set(p => p.State, productSku.State) .Set(p => p.SkuName, productSku.Title) .Set(p => p.Price, productSku.Price) .Set(p => p.Logo, productSku.Logo) .Set(p => p.CategoryId, productSku.CategoryId) .Set(p => p.CategoryName, productSku.CategoryName); updateProductSkuList.Add(update); } } #endregion #region 找出接口查不出的SKU var goneProductSkuList = dbProductSkuList.Where(dps => !productSkuList.Any(ps => ps.Id == dps.Id)).ToList(); if (goneProductSkuList.Count() > 0) { var goneProductSkuIdList = goneProductSkuList.Select(ps => ps.Id).ToList(); var update = fsql.Update().Set(ps => ps.State, 0).Where(ps => goneProductSkuIdList.Contains(ps.Id)); updateProductSkuList.Add(update); } #endregion fsql.Transaction(() => { if (insertProductList.Count() > 0) fsql.Insert(insertProductList).ExecuteAffrows(); if (insertProductSkuList.Count() > 0) fsql.Insert(insertProductSkuList).ExecuteAffrows(); }); { var tempUpdateList = new List>(); if (updateProductList.Count() > 0) { for (var i = 0; i < updateProductList.Count(); i++) { tempUpdateList.Add(updateProductList[i]); if (tempUpdateList.Count() == 20) { fsql.Transaction(() => { foreach (var update in tempUpdateList) update.ExecuteAffrows(); }); tempUpdateList.Clear(); } } if (tempUpdateList.Count() > 0) { fsql.Transaction(() => { foreach (var update in tempUpdateList) update.ExecuteAffrows(); }); tempUpdateList.Clear(); updateProductList.Clear(); } } } { var tempUpdateList = new List>(); if (updateProductSkuList.Count() > 0) { for (var i = 0; i < updateProductSkuList.Count(); i++) { tempUpdateList.Add(updateProductSkuList[i]); if (tempUpdateList.Count() == 20) { fsql.Transaction(() => { foreach (var update in tempUpdateList) update.ExecuteAffrows(); }); tempUpdateList.Clear(); } } if (tempUpdateList.Count() > 0) { fsql.Transaction(() => { foreach (var update in tempUpdateList) update.ExecuteAffrows(); }); tempUpdateList.Clear(); updateProductSkuList.Clear(); } } } Console.WriteLine($"{shop.ShopName}同步完毕,新增spu{insertProductList.Count()}个,新增sku{insertProductSkuList.Count()}个,更新spu{updateProductList.Count()}个,更新sku{updateProductSkuList.Count()}个"); } catch (Exception ex) { nLogManager.Default().Error(ex, $"SyncProduct ShopId:{shop.ShopName}"); } } private void SyncUpdateProduct(ShopResponse shop) { } } }