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.
123 lines
4.8 KiB
123 lines
4.8 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;
|
|
|
|
namespace BBWY.Server.Business.Sync
|
|
{
|
|
public class ProductSyncBusiness : BaseSyncBusiness, IDenpendency
|
|
{
|
|
private ProductBusiness productBusiness;
|
|
|
|
public ProductSyncBusiness(RestApiService restApiService,
|
|
IOptions<GlobalConfig> options,
|
|
ILogger logger,
|
|
IFreeSql fsql,
|
|
IIdGenerator idGenerator,
|
|
TaskSchedulerManager taskSchedulerManager,
|
|
VenderBusiness venderBusiness,
|
|
ProductBusiness productBusiness) : base(restApiService,
|
|
options,
|
|
logger,
|
|
fsql,
|
|
idGenerator,
|
|
taskSchedulerManager,
|
|
venderBusiness)
|
|
{
|
|
this.productBusiness = productBusiness;
|
|
}
|
|
|
|
private void SyncProduct(ShopResponse shop)
|
|
{
|
|
try
|
|
{
|
|
var shopId = long.Parse(shop.ShopId);
|
|
var productList = productBusiness.GetProductList(new SearchProductRequest()
|
|
{
|
|
PageSize = 50,
|
|
PageIndex = 1,
|
|
AppKey = shop.AppKey,
|
|
AppSecret = shop.AppSecret,
|
|
AppToken = shop.AppToken,
|
|
Platform = shop.PlatformId
|
|
});
|
|
|
|
if (productList == null || productList.Count == 0)
|
|
return;
|
|
|
|
var productIds = productList.Items.Select(p => p.Id);
|
|
|
|
var insertProductList = productList.Items.Select(p => new Product()
|
|
{
|
|
Id = p.Id,
|
|
CreateTime = DateTime.Now,
|
|
Platform = shop.PlatformId,
|
|
ProductItemNum = p.ProductItemNum,
|
|
ShopId = shopId,
|
|
Title = p.Title,
|
|
State = p.State
|
|
}).ToList();
|
|
|
|
var inserSkuList = new List<ProductSku>();
|
|
foreach (var product in insertProductList)
|
|
{
|
|
var skuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest()
|
|
{
|
|
AppKey = shop.AppKey,
|
|
AppSecret = shop.AppSecret,
|
|
AppToken = shop.AppToken,
|
|
Platform = shop.PlatformId,
|
|
Spu = product.Id
|
|
});
|
|
inserSkuList.AddRange(skuList.Select(s => new ProductSku()
|
|
{
|
|
Id = s.Id,
|
|
CreateTime = DateTime.Now,
|
|
Logo = s.Logo,
|
|
Platform = shop.PlatformId,
|
|
Price = s.Price,
|
|
ProductId = s.ProductId,
|
|
ShopId = shopId,
|
|
Title = s.Title,
|
|
State = s.State
|
|
}));
|
|
}
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
fsql.Delete<Product>().Where(p => p.ShopId == shopId && productIds.Contains(p.Id)).ExecuteAffrows();
|
|
fsql.Delete<ProductSku>().Where(s => s.ShopId == shopId && productIds.Contains(s.ProductId)).ExecuteAffrows();
|
|
fsql.Insert(insertProductList).ExecuteAffrows();
|
|
fsql.Insert(inserSkuList).ExecuteAffrows();
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var shopData = JsonConvert.SerializeObject(shop);
|
|
logger.Error(ex, $"SyncProduct ShopData:{shopData}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 同步所有店铺的订单
|
|
/// </summary>
|
|
public void SyncAllShopProduct()
|
|
{
|
|
var shopList = venderBusiness.GetShopList();
|
|
foreach (var shop in shopList)
|
|
{
|
|
Task.Factory.StartNew(() => SyncProduct(shop), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.ProductSyncTaskScheduler);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|