Browse Source

同步全部产品

qianyi
shanji 3 years ago
parent
commit
74d3ecde2a
  1. 11
      BBWY.Server.API/Controllers/ProductSyncController.cs
  2. 177
      BBWY.Server.Business/Sync/ProductSyncBusiness.cs

11
BBWY.Server.API/Controllers/ProductSyncController.cs

@ -14,12 +14,21 @@ namespace BBWY.Server.API.Controllers
} }
/// <summary> /// <summary>
/// 全店同步产品 /// 全店同步产品(前50)
/// </summary> /// </summary>
[HttpPost] [HttpPost]
public void SyncAllShopProduct() public void SyncAllShopProduct()
{ {
productSyncBusiness.SyncAllShopProduct(); productSyncBusiness.SyncAllShopProduct();
} }
/// <summary>
/// 全店同步产品(全部产品)
/// </summary>
[HttpPost]
public void SyncAllShopAllProduct()
{
productSyncBusiness.SyncAllShopAllProduct();
}
} }
} }

177
BBWY.Server.Business/Sync/ProductSyncBusiness.cs

@ -37,103 +37,113 @@ namespace BBWY.Server.Business.Sync
this.productBusiness = productBusiness; this.productBusiness = productBusiness;
} }
private void SyncProduct(ShopResponse shop) private void SyncProduct(ShopResponse shop, ProductListResponse productList)
{ {
try var shopId = long.Parse(shop.ShopId);
List<Product> insertProductList = new List<Product>();
List<IUpdate<Product>> updateProductList = new List<IUpdate<Product>>();
List<ProductSku> inserSkuList = new List<ProductSku>();
List<IUpdate<ProductSku>> updateProductSkuList = new List<IUpdate<ProductSku>>();
var productIds = productList.Items.Select(p => p.Id);
var dbProducts = fsql.Select<Product>().Where(p => p.ShopId == shopId && productIds.Contains(p.Id)).ToList();
var dbProductSkus = fsql.Select<ProductSku>().Where(s => s.ShopId == shopId && productIds.Contains(s.ProductId)).ToList();
foreach (var product in productList.Items)
{ {
var shopId = long.Parse(shop.ShopId); var dbProduct = dbProducts.FirstOrDefault(dbp => dbp.Id == product.Id);
var productList = productBusiness.GetProductList(new SearchProductRequest() 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>(product.Id).Set(p => p.State, dbProduct.State);
updateProductList.Add(update);
}
var skuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest()
{ {
PageSize = 50,
PageIndex = 1,
AppKey = shop.AppKey, AppKey = shop.AppKey,
AppSecret = shop.AppSecret, AppSecret = shop.AppSecret,
AppToken = shop.AppToken, AppToken = shop.AppToken,
Platform = shop.PlatformId Platform = shop.PlatformId,
Spu = product.Id
}); });
if (productList == null || productList.Count == 0) foreach (var sku in skuList)
return;
List<Product> insertProductList = new List<Product>();
List<IUpdate<Product>> updateProductList = new List<IUpdate<Product>>();
List<ProductSku> inserSkuList = new List<ProductSku>();
List<IUpdate<ProductSku>> updateProductSkuList = new List<IUpdate<ProductSku>>();
var productIds = productList.Items.Select(p => p.Id);
var dbProducts = fsql.Select<Product>().Where(p => p.ShopId == shopId && productIds.Contains(p.Id)).ToList();
var dbProductSkus = fsql.Select<ProductSku>().Where(s => s.ShopId == shopId && productIds.Contains(s.ProductId)).ToList();
foreach (var product in productList.Items)
{ {
var dbProduct = dbProducts.FirstOrDefault(dbp => dbp.Id == product.Id); var dbSku = dbProductSkus.FirstOrDefault(s => s.Id == sku.Id);
if (dbProduct == null) if (dbSku == null)
{ {
insertProductList.Add(new Product() inserSkuList.Add(new ProductSku()
{ {
Id = product.Id, Id = sku.Id,
CreateTime = DateTime.Now, CreateTime = DateTime.Now,
Logo = sku.Logo,
Platform = shop.PlatformId, Platform = shop.PlatformId,
ProductItemNum = product.ProductItemNum, Price = sku.Price,
ProductId = sku.ProductId,
ShopId = shopId, ShopId = shopId,
Title = product.Title, Title = sku.Title,
State = product.State State = sku.State
}); });
} }
else if (dbProduct.State != product.State) else if (dbSku.State != sku.State || dbSku.Price != sku.Price)
{ {
var update = fsql.Update<Product>(product.Id).Set(p => p.State, dbProduct.State); var update = fsql.Update<ProductSku>(dbSku.Id).Set(s => s.State, sku.State).Set(s => s.Price, sku.Price);
updateProductList.Add(update); updateProductSkuList.Add(update);
} }
}
}
var skuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest() fsql.Transaction(() =>
{ {
AppKey = shop.AppKey, fsql.Insert(insertProductList).ExecuteAffrows();
AppSecret = shop.AppSecret, fsql.Insert(inserSkuList).ExecuteAffrows();
AppToken = shop.AppToken, if (updateProductList.Count > 0)
Platform = shop.PlatformId, foreach (var update in updateProductList)
Spu = product.Id update.ExecuteAffrows();
}); if (updateProductSkuList.Count > 0)
foreach (var update in updateProductSkuList)
update.ExecuteAffrows();
foreach (var sku in skuList) });
{ }
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<ProductSku>(dbSku.Id).Set(s => s.State, sku.State).Set(s => s.Price, sku.Price);
updateProductSkuList.Add(update);
}
}
}
fsql.Transaction(() => private void SyncProduct(ShopResponse shop, bool IsSyncAllProduct = false)
{
try
{
var request = new SearchProductRequest()
{ {
fsql.Insert(insertProductList).ExecuteAffrows(); PageSize = 50,
fsql.Insert(inserSkuList).ExecuteAffrows(); PageIndex = 1,
if (updateProductList.Count > 0) AppKey = shop.AppKey,
foreach (var update in updateProductList) AppSecret = shop.AppSecret,
update.ExecuteAffrows(); AppToken = shop.AppToken,
if (updateProductSkuList.Count > 0) Platform = shop.PlatformId
foreach (var update in updateProductSkuList) };
update.ExecuteAffrows(); while (true)
{
}); var productList = productBusiness.GetProductList(request);
if (productList == null || productList.Count == 0)
return;
SyncProduct(shop, productList);
if (productList.Items.Count < 50 || !IsSyncAllProduct)
break;
request.PageIndex++;
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -143,17 +153,30 @@ namespace BBWY.Server.Business.Sync
} }
/// <summary> /// <summary>
/// 同步所有店铺的订单 /// 同步所有店铺的前50个产品
/// </summary> /// </summary>
public void SyncAllShopProduct() public void SyncAllShopProduct()
{ {
var shopList = venderBusiness.GetShopList(); var shopList = venderBusiness.GetShopList();
//SyncProduct(shopList[0]); //SyncProduct(shopList.FirstOrDefault(s => s.ShopId == "10388155"), true); //布莱特玩具专营店
foreach (var shop in shopList) foreach (var shop in shopList)
{ {
Task.Factory.StartNew(() => SyncProduct(shop), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler); Task.Factory.StartNew(() => SyncProduct(shop), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler);
} }
} }
/// <summary>
/// 同步所有店铺的全部产品
/// </summary>
public void SyncAllShopAllProduct()
{
var shopList = venderBusiness.GetShopList();
//SyncProduct(shopList.FirstOrDefault(s => s.ShopId == "10388155"), true); //布莱特玩具专营店
foreach (var shop in shopList)
{
Task.Factory.StartNew(() => SyncProduct(shop, true), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler);
}
}
} }
} }

Loading…
Cancel
Save