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.
244 lines
11 KiB
244 lines
11 KiB
using BBWY.Common.Http;
|
|
using BBWY.Common.Models;
|
|
using BBWY.Server.Model;
|
|
using BBWY.Server.Model.Dto;
|
|
using Microsoft.Extensions.Options;
|
|
using NLog;
|
|
using System.Threading.Tasks;
|
|
using Yitter.IdGenerator;
|
|
using System.Linq;
|
|
using BBWY.Server.Model.Db;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
using FreeSql;
|
|
using System.Threading;
|
|
|
|
namespace BBWY.Server.Business.Sync
|
|
{
|
|
public class ProductSyncBusiness : BaseSyncBusiness, IDenpendency
|
|
{
|
|
private ProductBusiness productBusiness;
|
|
|
|
public ProductSyncBusiness(RestApiService restApiService,
|
|
IOptions<GlobalConfig> options,
|
|
NLogManager nLogManager,
|
|
IFreeSql fsql,
|
|
IIdGenerator idGenerator,
|
|
TaskSchedulerManager taskSchedulerManager,
|
|
VenderBusiness venderBusiness,
|
|
ProductBusiness productBusiness,
|
|
YunDingBusiness yunDingBusiness) : base(restApiService,
|
|
options,
|
|
nLogManager,
|
|
fsql,
|
|
idGenerator,
|
|
taskSchedulerManager,
|
|
venderBusiness,
|
|
yunDingBusiness)
|
|
{
|
|
this.productBusiness = productBusiness;
|
|
}
|
|
|
|
private string GetMainSkuId(IList<ProductSkuResponse> skuList)
|
|
{
|
|
var maxPrice = skuList.Max(s => s.Price);
|
|
var sku = skuList.FirstOrDefault(s => s.Price == maxPrice && s.State == 1);
|
|
return sku?.Id ?? string.Empty;
|
|
}
|
|
|
|
private bool CheckMainSkuState(IList<ProductSkuResponse> skuList, string mainSkuId)
|
|
{
|
|
if (string.IsNullOrEmpty(mainSkuId))
|
|
return false;
|
|
var mainSku = skuList.FirstOrDefault(s => s.Id == mainSkuId);
|
|
if (mainSku == null || mainSku.State != 1)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
private void SyncProduct(ShopResponse shop, ProductListResponse productList)
|
|
{
|
|
var currentProductId = "";
|
|
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)
|
|
{
|
|
Thread.Sleep(200);
|
|
currentProductId = product.Id;
|
|
|
|
#region 检查sku
|
|
var skuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest()
|
|
{
|
|
AppKey = shop.AppKey,
|
|
AppSecret = shop.AppSecret,
|
|
AppToken = shop.AppToken,
|
|
Platform = shop.PlatformId,
|
|
Spu = product.Id,
|
|
IsContainSource = true
|
|
});
|
|
if (skuList == null || skuList.Count() == 0)
|
|
continue;
|
|
|
|
//排查已删除的sku
|
|
var currentDbSkus = dbProductSkus.Where(dbsku => dbsku.ProductId == product.Id).ToList();
|
|
var deletedSkuList = currentDbSkus.Where(dbsku => skuList.Count(s => s.Id == dbsku.Id) == 0).ToList();
|
|
if (deletedSkuList.Count() > 0)
|
|
{
|
|
foreach (var deletedSku in deletedSkuList)
|
|
{
|
|
var update = fsql.Update<ProductSku>(deletedSku.Id).Set(s => s.State, 4);
|
|
updateProductSkuList.Add(update);
|
|
}
|
|
}
|
|
|
|
foreach (var sku in skuList)
|
|
{
|
|
var categoryId = sku.Source.Value<int>("categoryId");
|
|
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,
|
|
CategoryId = categoryId
|
|
});
|
|
}
|
|
else if (dbSku.State != sku.State || dbSku.Price != sku.Price || dbSku.CategoryId != categoryId)
|
|
{
|
|
var update = fsql.Update<ProductSku>(dbSku.Id)
|
|
.Set(s => s.State, sku.State)
|
|
.Set(s => s.Price, sku.Price)
|
|
.Set(s => s.CategoryId, categoryId);
|
|
updateProductSkuList.Add(update);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 检查产品
|
|
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,
|
|
MainSkuId = GetMainSkuId(skuList)
|
|
});
|
|
}
|
|
else if (dbProduct.State != product.State || !CheckMainSkuState(skuList, dbProduct.MainSkuId))
|
|
{
|
|
string newMainSkuId = string.Empty;
|
|
if (!CheckMainSkuState(skuList, dbProduct.MainSkuId))
|
|
newMainSkuId = GetMainSkuId(skuList);
|
|
|
|
var update = fsql.Update<Product>(product.Id).SetIf(dbProduct.State != product.State, p => p.State, product.State)
|
|
.SetIf(!string.IsNullOrEmpty(newMainSkuId), p => p.MainSkuId, newMainSkuId);
|
|
updateProductList.Add(update);
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
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(currentProductId, ex);
|
|
}
|
|
}
|
|
|
|
private void SyncProduct(ShopResponse shop, bool IsSyncAllProduct = false)
|
|
{
|
|
try
|
|
{
|
|
var request = new SearchProductRequest()
|
|
{
|
|
PageSize = 50,
|
|
PageIndex = 1,
|
|
AppKey = shop.AppKey,
|
|
AppSecret = shop.AppSecret,
|
|
AppToken = shop.AppToken,
|
|
Platform = shop.PlatformId
|
|
};
|
|
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++;
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var shopData = JsonConvert.SerializeObject(shop);
|
|
nLogManager.Default().Error(ex, $"SyncProduct ShopData:{shopData}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 同步所有店铺的前50个产品
|
|
/// </summary>
|
|
public void SyncAllShopProduct()
|
|
{
|
|
var shopList = venderBusiness.GetShopList(platform: Enums.Platform.京东);
|
|
//SyncProduct(shopList.FirstOrDefault(s => s.ShopId == "12657364"), true); //布莱特玩具专营店
|
|
foreach (var shop in shopList)
|
|
{
|
|
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.ShopName == "趣弈手机配件专营店"), true);
|
|
foreach (var shop in shopList)
|
|
{
|
|
Task.Factory.StartNew(() => SyncProduct(shop, true), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|