Browse Source

产品同步

AddValidOverTime
shanji 2 years ago
parent
commit
45a19f0440
  1. 147
      BBWY.Server.Business/Sync/ProductSyncBusiness.cs

147
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<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()
{
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>(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)
{
@ -231,8 +364,8 @@ namespace BBWY.Server.Business.Sync
/// </summary>
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);

Loading…
Cancel
Save