|
|
@ -37,88 +37,98 @@ namespace BBWY.Server.Business.Sync |
|
|
|
this.productBusiness = productBusiness; |
|
|
|
} |
|
|
|
|
|
|
|
private void SyncProduct(ShopResponse shop, ProductListResponse productList) |
|
|
|
private void SyncProduct(ShopResponse shop, ProductListResponse productList, out string productId) |
|
|
|
{ |
|
|
|
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) |
|
|
|
productId = ""; |
|
|
|
try |
|
|
|
{ |
|
|
|
var dbProduct = dbProducts.FirstOrDefault(dbp => dbp.Id == product.Id); |
|
|
|
if (dbProduct == null) |
|
|
|
{ |
|
|
|
insertProductList.Add(new Product() |
|
|
|
{ |
|
|
|
Id = product.Id, |
|
|
|
CreateTime = product.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 shopId = long.Parse(shop.ShopId); |
|
|
|
List<Product> insertProductList = new List<Product>(); |
|
|
|
List<IUpdate<Product>> updateProductList = new List<IUpdate<Product>>(); |
|
|
|
|
|
|
|
var skuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest() |
|
|
|
{ |
|
|
|
AppKey = shop.AppKey, |
|
|
|
AppSecret = shop.AppSecret, |
|
|
|
AppToken = shop.AppToken, |
|
|
|
Platform = shop.PlatformId, |
|
|
|
Spu = product.Id |
|
|
|
}); |
|
|
|
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 sku in skuList) |
|
|
|
foreach (var product in productList.Items) |
|
|
|
{ |
|
|
|
var dbSku = dbProductSkus.FirstOrDefault(s => s.Id == sku.Id); |
|
|
|
if (dbSku == null) |
|
|
|
productId = product.Id; |
|
|
|
var dbProduct = dbProducts.FirstOrDefault(dbp => dbp.Id == product.Id); |
|
|
|
if (dbProduct == null) |
|
|
|
{ |
|
|
|
inserSkuList.Add(new ProductSku() |
|
|
|
insertProductList.Add(new Product() |
|
|
|
{ |
|
|
|
Id = sku.Id, |
|
|
|
CreateTime = sku.CreateTime ?? DateTime.Now, |
|
|
|
Logo = sku.Logo, |
|
|
|
Id = product.Id, |
|
|
|
CreateTime = product.CreateTime ?? DateTime.Now, |
|
|
|
Platform = shop.PlatformId, |
|
|
|
Price = sku.Price, |
|
|
|
ProductId = sku.ProductId, |
|
|
|
ProductItemNum = product.ProductItemNum, |
|
|
|
ShopId = shopId, |
|
|
|
Title = sku.Title, |
|
|
|
State = sku.State |
|
|
|
Title = product.Title, |
|
|
|
State = product.State |
|
|
|
}); |
|
|
|
} |
|
|
|
else if (dbSku.State != sku.State || dbSku.Price != sku.Price) |
|
|
|
else if (dbProduct.State != product.State) |
|
|
|
{ |
|
|
|
var update = fsql.Update<ProductSku>(dbSku.Id).Set(s => s.State, sku.State).Set(s => s.Price, sku.Price); |
|
|
|
updateProductSkuList.Add(update); |
|
|
|
var update = fsql.Update<Product>(product.Id).Set(p => p.State, dbProduct.State); |
|
|
|
updateProductList.Add(update); |
|
|
|
} |
|
|
|
|
|
|
|
var skuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest() |
|
|
|
{ |
|
|
|
AppKey = shop.AppKey, |
|
|
|
AppSecret = shop.AppSecret, |
|
|
|
AppToken = shop.AppToken, |
|
|
|
Platform = shop.PlatformId, |
|
|
|
Spu = product.Id |
|
|
|
}); |
|
|
|
if (skuList == null || skuList.Count() == 0) |
|
|
|
continue; |
|
|
|
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 = sku.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(() => |
|
|
|
{ |
|
|
|
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(); |
|
|
|
fsql.Transaction(() => |
|
|
|
{ |
|
|
|
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) |
|
|
|
{ |
|
|
|
throw new Exception(productId, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void SyncProduct(ShopResponse shop, bool IsSyncAllProduct = false) |
|
|
@ -139,7 +149,7 @@ namespace BBWY.Server.Business.Sync |
|
|
|
var productList = productBusiness.GetProductList(request); |
|
|
|
if (productList == null || productList.Count == 0) |
|
|
|
return; |
|
|
|
SyncProduct(shop, productList); |
|
|
|
SyncProduct(shop, productList, out string productId); |
|
|
|
if (productList.Items.Count < 50 || !IsSyncAllProduct) |
|
|
|
break; |
|
|
|
request.PageIndex++; |
|
|
@ -171,11 +181,11 @@ namespace BBWY.Server.Business.Sync |
|
|
|
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); |
|
|
|
} |
|
|
|
SyncProduct(shopList.FirstOrDefault(s => s.ShopName == "瑞源玩具专营店"), true); //瑞源玩具专营店
|
|
|
|
//foreach (var shop in shopList)
|
|
|
|
//{
|
|
|
|
// Task.Factory.StartNew(() => SyncProduct(shop, true), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler);
|
|
|
|
//}
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|