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.
125 lines
4.8 KiB
125 lines
4.8 KiB
3 years ago
|
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 dbProductIds = fsql.Select<Product>().Where(p => p.ShopId == shopId && p.Platform == shop.PlatformId && productIds.Contains(p.Id)).ToList(p => p.Id);
|
||
|
var noExtisProductIds = productIds.Except(dbProductIds).ToList();
|
||
|
if (noExtisProductIds.Count() == 0)
|
||
|
return;
|
||
|
|
||
|
var insertProductList = productList.Items.Where(p => noExtisProductIds.Contains(p.Id)).Select(p => new Product()
|
||
|
{
|
||
|
Id = p.Id,
|
||
|
CreateTime = DateTime.Now,
|
||
|
Platform = shop.PlatformId,
|
||
|
ProductItemNum = p.ProductItemNum,
|
||
|
ShopId = shopId,
|
||
|
Title = p.Title
|
||
|
}).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
|
||
|
}));
|
||
|
}
|
||
|
|
||
|
fsql.Transaction(() =>
|
||
|
{
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|