10 changed files with 373 additions and 4 deletions
@ -0,0 +1,23 @@ |
|||||
|
using BBWYB.Server.Business.Sync; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
|
||||
|
namespace BBWYB.Server.API.Controllers |
||||
|
{ |
||||
|
public class ProductSyncController : BaseApiController |
||||
|
{ |
||||
|
private ProductSyncBusiness productSyncBusiness; |
||||
|
public ProductSyncController(IHttpContextAccessor httpContextAccessor, ProductSyncBusiness productSyncBusiness) : base(httpContextAccessor) |
||||
|
{ |
||||
|
this.productSyncBusiness = productSyncBusiness; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 全店同步产品
|
||||
|
/// </summary>
|
||||
|
[HttpPost] |
||||
|
public void SyncAllShopAllProduct() |
||||
|
{ |
||||
|
productSyncBusiness.SyncAllShopAllProduct(); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,214 @@ |
|||||
|
using BBWYB.Common.Log; |
||||
|
using BBWYB.Common.Models; |
||||
|
using BBWYB.Server.Model.Db; |
||||
|
using BBWYB.Server.Model.Dto; |
||||
|
using FreeSql; |
||||
|
using SDKAdapter.OperationPlatform.Models; |
||||
|
using Yitter.IdGenerator; |
||||
|
using System.Linq; |
||||
|
using Newtonsoft.Json; |
||||
|
using BBWYB.Server.Model; |
||||
|
|
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
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) |
||||
|
{ |
||||
|
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 |
||||
|
})); |
||||
|
} |
||||
|
#endregion
|
||||
|
|
||||
|
#region 找出变化的产品 (状态,标题)
|
||||
|
var stateChangeProductList = productList.Where(p => dbProductList.Any(dp => dp.Id == p.Id && (dp.State != p.State || dp.ProductName != p.Title))).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); |
||||
|
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 |
||||
|
})); |
||||
|
} |
||||
|
#endregion
|
||||
|
|
||||
|
#region 找出状态变化的SKU
|
||||
|
var stateChangeProductSkuList = productSkuList.Where(ps => dbProductSkuList.Any(dps => dps.Id == ps.Id && (dps.State != ps.State || dps.SkuName != ps.Title))).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); |
||||
|
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(); |
||||
|
|
||||
|
if (updateProductList.Count() > 0) |
||||
|
{ |
||||
|
foreach (var update in updateProductList) |
||||
|
update.ExecuteAffrows(); |
||||
|
} |
||||
|
|
||||
|
if (updateProductSkuList.Count() > 0) |
||||
|
{ |
||||
|
foreach (var update in updateProductSkuList) |
||||
|
update.ExecuteAffrows(); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
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}"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,54 @@ |
|||||
|
using FreeSql.DataAnnotations; |
||||
|
|
||||
|
namespace BBWYB.Server.Model.Db |
||||
|
{ |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 商品表(SPU)
|
||||
|
/// </summary>
|
||||
|
[Table(Name = "product", DisableSyncStructure = true)] |
||||
|
public partial class Product |
||||
|
{ |
||||
|
|
||||
|
[Column(StringLength = 50, IsPrimary = true, IsNullable = false)] |
||||
|
public string Id { get; set; } |
||||
|
|
||||
|
|
||||
|
public string Logo { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 商品名称
|
||||
|
/// </summary>
|
||||
|
[Column(StringLength = 200)] |
||||
|
public string ProductName { get; set; } |
||||
|
|
||||
|
[Column(DbType = "bigint")] |
||||
|
public long? ShopId { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 状态(0下架 1上架)
|
||||
|
/// </summary>
|
||||
|
[Column(DbType = "int")] |
||||
|
public int? State { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 同步时间
|
||||
|
/// </summary>
|
||||
|
[Column(DbType = "datetime")] |
||||
|
public DateTime? SyncTime { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 更新时间
|
||||
|
/// </summary>
|
||||
|
[Column(DbType = "datetime")] |
||||
|
public DateTime? UpdateTime { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 最近上架时间
|
||||
|
/// </summary>
|
||||
|
[Column(DbType = "datetime")] |
||||
|
public DateTime? UpperTime { get; set; } |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,59 @@ |
|||||
|
using FreeSql.DataAnnotations; |
||||
|
|
||||
|
namespace BBWYB.Server.Model.Db |
||||
|
{ |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 商品Sku表
|
||||
|
/// </summary>
|
||||
|
[ Table(Name = "productsku", DisableSyncStructure = true)] |
||||
|
public partial class ProductSku { |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// SkuId
|
||||
|
/// </summary>
|
||||
|
[Column(StringLength = 50, IsPrimary = true, IsNullable = false)] |
||||
|
public string Id { get; set; } |
||||
|
|
||||
|
|
||||
|
public string Logo { get; set; } |
||||
|
|
||||
|
[Column(DbType = "decimal(18,2)")] |
||||
|
public decimal? Price { get; set; } = 0.00M; |
||||
|
|
||||
|
[Column(StringLength = 50)] |
||||
|
public string ProductId { get; set; } |
||||
|
|
||||
|
[Column(DbType = "bigint")] |
||||
|
public long? ShopId { get; set; } |
||||
|
|
||||
|
[Column(StringLength = 200)] |
||||
|
public string SkuName { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 0下架 1上架
|
||||
|
/// </summary>
|
||||
|
[Column(DbType = "int")] |
||||
|
public int? State { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 同步时间
|
||||
|
/// </summary>
|
||||
|
[Column(DbType = "datetime")] |
||||
|
public DateTime? SyncTime { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 更新时间
|
||||
|
/// </summary>
|
||||
|
[Column(DbType = "datetime")] |
||||
|
public DateTime? UpdateTime { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 最近上架时间
|
||||
|
/// </summary>
|
||||
|
[Column(DbType = "datetime")] |
||||
|
public DateTime? UpperTime { get; set; } |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue