|
@ -185,6 +185,19 @@ namespace BBWY.Server.Business.Sync |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
|
|
|
var shopId = long.Parse(shop.ShopId); |
|
|
|
|
|
var dbProductList = fsql.Select<Product>().Where(p => p.ShopId == shopId).ToList(); |
|
|
|
|
|
var dbProductSkuList = fsql.Select<ProductSku>().Where(p => p.ShopId == shopId).ToList(); |
|
|
|
|
|
|
|
|
|
|
|
List<ProductResponse> productList = new List<ProductResponse>(); |
|
|
|
|
|
List<ProductSkuResponse> productSkuList = new List<ProductSkuResponse>(); |
|
|
|
|
|
|
|
|
|
|
|
List<Product> insertProductList = new List<Product>(); |
|
|
|
|
|
List<ProductSku> insertProductSkuList = new List<ProductSku>(); |
|
|
|
|
|
|
|
|
|
|
|
List<IUpdate<Product>> updateProductList = new List<IUpdate<Product>>(); |
|
|
|
|
|
List<IUpdate<ProductSku>> updateProductSkuList = new List<IUpdate<ProductSku>>(); |
|
|
|
|
|
|
|
|
var request = new SearchProductRequest() |
|
|
var request = new SearchProductRequest() |
|
|
{ |
|
|
{ |
|
|
PageSize = 50, |
|
|
PageSize = 50, |
|
@ -196,15 +209,135 @@ namespace BBWY.Server.Business.Sync |
|
|
}; |
|
|
}; |
|
|
while (true) |
|
|
while (true) |
|
|
{ |
|
|
{ |
|
|
var productList = productBusiness.GetProductList(request); |
|
|
var currentProductList = productBusiness.GetProductList(request); |
|
|
if (productList == null || productList.Count == 0) |
|
|
if (currentProductList == null || currentProductList.Count == 0) |
|
|
return; |
|
|
break; |
|
|
SyncProduct(shop, productList); |
|
|
productList.AddRange(currentProductList.Items); |
|
|
if (productList.Items.Count < 50 || !IsSyncAllProduct) |
|
|
|
|
|
|
|
|
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; |
|
|
break; |
|
|
request.PageIndex++; |
|
|
request.PageIndex++; |
|
|
Thread.Sleep(1000); |
|
|
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>(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<Product>().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<int>("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>(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<ProductSku>().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) |
|
|
catch (Exception ex) |
|
|
{ |
|
|
{ |
|
@ -231,8 +364,8 @@ namespace BBWY.Server.Business.Sync |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public void SyncAllShopAllProduct() |
|
|
public void SyncAllShopAllProduct() |
|
|
{ |
|
|
{ |
|
|
var shopList = venderBusiness.GetShopList(); |
|
|
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) |
|
|
foreach (var shop in shopList) |
|
|
{ |
|
|
{ |
|
|
Task.Factory.StartNew(() => SyncProduct(shop, true), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler); |
|
|
Task.Factory.StartNew(() => SyncProduct(shop, true), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler); |
|
|