You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

514 lines
25 KiB

using BBWYB.Common.Log;
using BBWYB.Common.Models;
using BBWYB.Server.Model;
using BBWYB.Server.Model.Db;
using BBWYB.Server.Model.Dto;
using FreeSql;
using SDKAdapter.OperationPlatform.Models;
using Yitter.IdGenerator;
namespace BBWYB.Server.Business.Sync
{
public class ProductSyncBusiness : BaseBusiness, IDenpendency
{
private ProductBusiness productBusiness;
private VenderBusiness venderBusiness;
private TaskSchedulerManager taskSchedulerManager;
public ProductSyncBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, ProductBusiness productBusiness, VenderBusiness venderBusiness, TaskSchedulerManager taskSchedulerManager) : base(fsql, nLogManager, idGenerator)
{
this.productBusiness = productBusiness;
this.venderBusiness = venderBusiness;
this.taskSchedulerManager = taskSchedulerManager;
}
/// <summary>
/// 同步所有店铺的全部产品
/// </summary>
public void SyncAllShopAllProduct()
{
var shopList = venderBusiness.GetShopList(platform: Enums.Platform.);
//var shopList = venderBusiness.GetShopList(shopId: 9);
foreach (var shop in shopList)
{
Task.Factory.StartNew(() => SyncProduct(shop, true), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SyncProductTaskScheduler);
}
}
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);
}
}
private void SyncProduct(ShopResponse shop, bool IsSyncAllProduct = false)
{
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<OP_ProductResponse> productList = new List<OP_ProductResponse>();
List<OP_ProductSkuResponse> productSkuList = 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>>();
var request = new OP_QueryProductRequest()
{
PageSize = 50,
PageIndex = 1,
AppKey = shop.AppKey,
AppSecret = shop.AppSecret,
AppToken = shop.AppToken,
Platform = SDKAdapter.AdapterEnums.PlatformType.
};
var requestSku = new OP_QueryProductSkuRequest()
{
AppKey = shop.AppKey,
AppSecret = shop.AppSecret,
AppToken = shop.AppToken,
Platform = SDKAdapter.AdapterEnums.PlatformType.,
//Spu = product.Id,
PageSize = 50,
PageIndex = 1
};
while (true)
{
Console.WriteLine($"{shop.ShopName} 获取第{request.PageIndex}页产品");
var pListRes = productBusiness.GetProductList(request);
if (pListRes == null || pListRes.Items == null || pListRes.Items.Count == 0)
break;
productList.AddRange(pListRes.Items);
foreach (var product in pListRes.Items)
{
requestSku.Spu = product.Id;
var psListRes = productBusiness.GetProductSkuList(requestSku);
if (psListRes == null || psListRes.Items == null || psListRes.Items.Count == 0)
continue;
productSkuList.AddRange(psListRes.Items);
}
if (pListRes.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,
Logo = p.Logo,
ProductName = p.Title,
ShopId = int.Parse(shop.ShopId),
State = p.State,
SyncTime = DateTime.Now,
UpdateTime = DateTime.Now,
UpperTime = p.CreateTime,
CategoryId = p.CategoryId,
CategoryName = p.CategoryName
}));
}
#endregion
#region 找出变化的产品 (状态,标题)
var stateChangeProductList = productList.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);
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, 0).Where(p => goneProductIdList.Contains(p.Id));
updateProductList.Add(update);
}
#endregion
#region 找出新增的SKU
var newProductSkuList = productSkuList.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 = DateTime.Now,
UpperTime = ps.CreateTime,
CategoryId = ps.CategoryId,
CategoryName = ps.CategoryName
}));
}
#endregion
#region 找出状态变化的SKU
var stateChangeProductSkuList = productSkuList.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(p => p.State, productSku.State)
.Set(p => p.SkuName, productSku.Title)
.Set(p => p.Price, productSku.Price)
.Set(p => p.Logo, productSku.Logo)
.Set(p => p.CategoryId, productSku.CategoryId)
.Set(p => p.CategoryName, productSku.CategoryName);
updateProductSkuList.Add(update);
}
}
#endregion
#region 找出接口查不出的SKU
var goneProductSkuList = dbProductSkuList.Where(dps => !productSkuList.Any(ps => ps.Id == dps.Id)).ToList();
if (goneProductSkuList.Count() > 0)
{
var goneProductSkuIdList = goneProductSkuList.Select(ps => ps.Id).ToList();
var update = fsql.Update<ProductSku>().Set(ps => ps.State, 0).Where(ps => goneProductSkuIdList.Contains(ps.Id));
updateProductSkuList.Add(update);
}
#endregion
fsql.Transaction(() =>
{
if (insertProductList.Count() > 0)
fsql.Insert(insertProductList).ExecuteAffrows();
if (insertProductSkuList.Count() > 0)
fsql.Insert(insertProductSkuList).ExecuteAffrows();
});
{
var tempUpdateList = new List<IUpdate<Product>>();
if (updateProductList.Count() > 0)
{
for (var i = 0; i < updateProductList.Count(); i++)
{
tempUpdateList.Add(updateProductList[i]);
if (tempUpdateList.Count() == 20)
{
fsql.Transaction(() =>
{
foreach (var update in tempUpdateList)
update.ExecuteAffrows();
});
tempUpdateList.Clear();
}
}
if (tempUpdateList.Count() > 0)
{
fsql.Transaction(() =>
{
foreach (var update in tempUpdateList)
update.ExecuteAffrows();
});
tempUpdateList.Clear();
updateProductList.Clear();
}
}
}
{
var tempUpdateList = new List<IUpdate<ProductSku>>();
if (updateProductSkuList.Count() > 0)
{
for (var i = 0; i < updateProductSkuList.Count(); i++)
{
tempUpdateList.Add(updateProductSkuList[i]);
if (tempUpdateList.Count() == 20)
{
fsql.Transaction(() =>
{
foreach (var update in tempUpdateList)
update.ExecuteAffrows();
});
tempUpdateList.Clear();
}
}
if (tempUpdateList.Count() > 0)
{
fsql.Transaction(() =>
{
foreach (var update in tempUpdateList)
update.ExecuteAffrows();
});
tempUpdateList.Clear();
updateProductSkuList.Clear();
}
}
}
Console.WriteLine($"{shop.ShopName}同步完毕,新增spu{insertProductList.Count()}个,新增sku{insertProductSkuList.Count()}个,更新spu{updateProductList.Count()}个,更新sku{updateProductSkuList.Count()}个");
}
catch (Exception ex)
{
nLogManager.Default().Error(ex, $"SyncProduct ShopId:{shop.ShopName}");
}
}
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
{
#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 < spuRequest.PageSize)
break;
spuRequest.PageIndex++;
}
#endregion
#region 下架的spu
spuRequest.PageIndex = 1;
spuRequest.ProductState = SDKAdapter.AdapterEnums.ProuctState.;
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 < spuRequest.PageSize)
break;
spuRequest.PageIndex++;
}
#endregion
}
#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
foreach (var product in opProductList)
{
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,
};
var response = productBusiness.GetProductSkuList(skuRequest);
if (response == null || response.Items == null || response.Items.Count == 0)
continue;
opProductSkuList.AddRange(response.Items);
}
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 = DateTime.Now,
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(p => p.State, productSku.State)
.Set(p => p.SkuName, productSku.Title)
.Set(p => p.Price, productSku.Price)
.Set(p => p.Logo, productSku.Logo)
.Set(p => p.CategoryId, productSku.CategoryId)
.Set(p => p.CategoryName, productSku.CategoryName);
updateProductSkuList.Add(update);
}
}
#endregion
#region 找出接口查不出的SKU
var goneProductSkuList = dbProductSkuList.Where(dps => !opProductSkuList.Any(ps => ps.Id == dps.Id)).ToList();
if (goneProductSkuList.Count() > 0)
{
var goneProductSkuIdList = goneProductSkuList.Select(ps => ps.Id).ToList();
var update = fsql.Update<ProductSku>().Set(ps => ps.State, 0).Where(ps => goneProductSkuIdList.Contains(ps.Id));
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}");
}
}
}
}