|
|
@ -37,7 +37,21 @@ namespace BBWYB.Server.Business.Sync |
|
|
|
|
|
|
|
public void SyncAllShopUpdateProduct() |
|
|
|
{ |
|
|
|
var shopList = venderBusiness.GetShopList(platform: Enums.Platform.拳探); |
|
|
|
//var shopList = venderBusiness.GetShopList(shopId: 9);
|
|
|
|
foreach (var shop in shopList) |
|
|
|
{ |
|
|
|
Task.Factory.StartNew(() => SyncUpdateProduct(shop), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SyncProductTaskScheduler); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void SyncOneShopUpdateProduct(long shopId) |
|
|
|
{ |
|
|
|
var shopList = venderBusiness.GetShopList(shopId, platform: Enums.Platform.拳探); |
|
|
|
foreach (var shop in shopList) |
|
|
|
{ |
|
|
|
Task.Factory.StartNew(() => SyncUpdateProduct(shop), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SyncProductTaskScheduler); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -285,7 +299,194 @@ namespace BBWYB.Server.Business.Sync |
|
|
|
|
|
|
|
private void SyncUpdateProduct(ShopResponse shop) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
List<Product> dbProductList; |
|
|
|
List<ProductSku> dbProductSkuList; |
|
|
|
List<OP_ProductResponse> opProductList = new List<OP_ProductResponse>(); |
|
|
|
List<OP_ProductSkuResponse> opProductSkuList = new List<OP_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>>(); |
|
|
|
|
|
|
|
#region 查询变化的spu
|
|
|
|
var spuRequest = new OP_QueryProductRequest() |
|
|
|
{ |
|
|
|
PageSize = 50, |
|
|
|
PageIndex = 1, |
|
|
|
AppKey = shop.AppKey, |
|
|
|
AppSecret = shop.AppSecret, |
|
|
|
AppToken = shop.AppToken, |
|
|
|
Platform = SDKAdapter.AdapterEnums.PlatformType.拳探, |
|
|
|
UpdateStartTime = DateTime.Now.AddHours(-1), |
|
|
|
UpdateEndTime = DateTime.Now |
|
|
|
}; |
|
|
|
while (true) |
|
|
|
{ |
|
|
|
var response = productBusiness.GetProductList(spuRequest); |
|
|
|
if (response == null || response.Items == null || response.Items.Count == 0) |
|
|
|
break; |
|
|
|
|
|
|
|
opProductList.AddRange(response.Items); |
|
|
|
if (response.Items.Count < 50) |
|
|
|
break; |
|
|
|
|
|
|
|
spuRequest.PageIndex++; |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 对比spu
|
|
|
|
|
|
|
|
var spuIdList = opProductList.Select(x => x.Id); |
|
|
|
dbProductList = fsql.Select<Product>(spuIdList).ToList(); |
|
|
|
|
|
|
|
#region 找出新增的产品
|
|
|
|
var newProductList = opProductList.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 = p.UpdateTime, |
|
|
|
UpperTime = p.CreateTime, |
|
|
|
CategoryId = p.CategoryId, |
|
|
|
CategoryName = p.CategoryName |
|
|
|
})); |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 找出变化的产品 (状态,标题,类目)
|
|
|
|
var stateChangeProductList = opProductList.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>(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) |
|
|
|
.Set(p => p.UpdateTime, product.UpdateTime); |
|
|
|
updateProductList.Add(update); |
|
|
|
} |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 查询变化的sku
|
|
|
|
|
|
|
|
var skuRequest = new OP_QueryProductSkuRequest() |
|
|
|
{ |
|
|
|
AppKey = shop.AppKey, |
|
|
|
AppSecret = shop.AppSecret, |
|
|
|
AppToken = shop.AppToken, |
|
|
|
Platform = SDKAdapter.AdapterEnums.PlatformType.拳探, |
|
|
|
//Spu = product.Id,
|
|
|
|
PageSize = 50, |
|
|
|
PageIndex = 1, |
|
|
|
UpdateStartTime = DateTime.Now.AddHours(-1), |
|
|
|
UpdateEndTime = DateTime.Now |
|
|
|
}; |
|
|
|
while (true) |
|
|
|
{ |
|
|
|
var response = productBusiness.GetProductSkuList(skuRequest); |
|
|
|
if (response == null || response.Items == null || response.Items.Count == 0) |
|
|
|
break; |
|
|
|
|
|
|
|
opProductSkuList.AddRange(response.Items); |
|
|
|
if (response.Items.Count < 50) |
|
|
|
break; |
|
|
|
|
|
|
|
skuRequest.PageIndex++; |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 对比sku
|
|
|
|
|
|
|
|
var skuIdList = opProductSkuList.Select(x => x.Id); |
|
|
|
dbProductSkuList = fsql.Select<ProductSku>(skuIdList).ToList(); |
|
|
|
|
|
|
|
#region 找出新增的SKU
|
|
|
|
var newProductSkuList = opProductSkuList.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 = ps.UpdateTime, |
|
|
|
UpperTime = ps.CreateTime, |
|
|
|
CategoryId = ps.CategoryId, |
|
|
|
CategoryName = ps.CategoryName |
|
|
|
})); |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 找出状态变化的SKU
|
|
|
|
var stateChangeProductSkuList = opProductSkuList.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>(productSku.Id).Set(ps => ps.State, productSku.State) |
|
|
|
.Set(ps => ps.SkuName, productSku.Title) |
|
|
|
.Set(ps => ps.Price, productSku.Price) |
|
|
|
.Set(ps => ps.Logo, productSku.Logo) |
|
|
|
.Set(ps => ps.CategoryId, productSku.CategoryId) |
|
|
|
.Set(ps => ps.CategoryName, productSku.CategoryName) |
|
|
|
.Set(ps => ps.UpdateTime, productSku.UpdateTime); |
|
|
|
updateProductSkuList.Add(update); |
|
|
|
} |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 数据库操作
|
|
|
|
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(); |
|
|
|
} |
|
|
|
}); |
|
|
|
#endregion
|
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
nLogManager.Default().Error(ex, $"SyncProduct ShopId:{shop.ShopName}"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|