5 changed files with 237 additions and 0 deletions
@ -0,0 +1,25 @@ |
|||
using BBWY.Server.Business.Sync; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
|
|||
namespace BBWY.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 SyncAllShopProduct() |
|||
{ |
|||
productSyncBusiness.SyncAllShopProduct(); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,124 @@ |
|||
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); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
@ -0,0 +1,39 @@ |
|||
using FreeSql.DataAnnotations; |
|||
using System; |
|||
|
|||
namespace BBWY.Server.Model.Db |
|||
{ |
|||
|
|||
[Table(Name = "product", DisableSyncStructure = true)] |
|||
public partial class Product { |
|||
|
|||
/// <summary>
|
|||
/// SPU
|
|||
/// </summary>
|
|||
[Column(StringLength = 50, IsPrimary = true, IsNullable = false)] |
|||
public string Id { get; set; } |
|||
|
|||
[Column(DbType = "datetime")] |
|||
public DateTime? CreateTime { get; set; } |
|||
|
|||
[Column(DbType = "int(1)", MapType = typeof(int))] |
|||
public Enums.Platform Platform { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 货号
|
|||
/// </summary>
|
|||
[Column(StringLength = 100)] |
|||
public string ProductItemNum { get; set; } |
|||
|
|||
|
|||
public long? ShopId { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 标题
|
|||
/// </summary>
|
|||
|
|||
public string Title { get; set; } |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,46 @@ |
|||
using FreeSql.DataAnnotations; |
|||
using System; |
|||
|
|||
namespace BBWY.Server.Model.Db |
|||
{ |
|||
|
|||
[Table(Name = "productsku", DisableSyncStructure = true)] |
|||
public partial class ProductSku |
|||
{ |
|||
|
|||
/// <summary>
|
|||
/// SKU
|
|||
/// </summary>
|
|||
[Column(StringLength = 50, IsPrimary = true, IsNullable = false)] |
|||
public string Id { get; set; } |
|||
|
|||
[Column(DbType = "datetime")] |
|||
public DateTime? CreateTime { get; set; } |
|||
|
|||
|
|||
public string Logo { get; set; } |
|||
|
|||
[Column(DbType = "int(1)", MapType = typeof(int))] |
|||
public Enums.Platform Platform { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 售价
|
|||
/// </summary>
|
|||
[Column(DbType = "decimal(18,2)")] |
|||
public decimal? Price { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// SPU
|
|||
/// </summary>
|
|||
[Column(StringLength = 50)] |
|||
public string ProductId { get; set; } |
|||
|
|||
|
|||
public long? ShopId { get; set; } |
|||
|
|||
|
|||
public string Title { get; set; } |
|||
|
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue