11 changed files with 257 additions and 70 deletions
@ -0,0 +1,24 @@ |
|||
using BBWY.Server.Business.Sync; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
|
|||
namespace BBWY.Server.API.Controllers |
|||
{ |
|||
public class StoreHouseSyncController : BaseApiController |
|||
{ |
|||
private StoreHouseSyncBusiness storeHouseSyncBusiness; |
|||
public StoreHouseSyncController(IHttpContextAccessor httpContextAccessor, StoreHouseSyncBusiness storeHouseSyncBusiness) : base(httpContextAccessor) |
|||
{ |
|||
this.storeHouseSyncBusiness = storeHouseSyncBusiness; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 同步全店仓库信息
|
|||
/// </summary>
|
|||
[HttpGet] |
|||
public void StartSyncAllShopStoreHouse() |
|||
{ |
|||
storeHouseSyncBusiness.StartSyncAllShopStoreHouse(); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,110 @@ |
|||
using BBWY.Common.Http; |
|||
using BBWY.Common.Models; |
|||
using BBWY.Server.Model; |
|||
using BBWY.Server.Model.Db; |
|||
using BBWY.Server.Model.Dto; |
|||
using Microsoft.Extensions.Options; |
|||
using Newtonsoft.Json; |
|||
using Newtonsoft.Json.Linq; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Net.Http; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Yitter.IdGenerator; |
|||
|
|||
namespace BBWY.Server.Business.Sync |
|||
{ |
|||
public class StoreHouseSyncBusiness : BaseSyncBusiness, IDenpendency |
|||
{ |
|||
private IDictionary<Enums.Platform, Action<JArray, ShopResponse, IList<Storehouse>>> storeHouseSyncMethodDic; |
|||
|
|||
public StoreHouseSyncBusiness(RestApiService restApiService, |
|||
IOptions<GlobalConfig> options, |
|||
NLogManager nLogManager, |
|||
IFreeSql fsql, |
|||
IIdGenerator idGenerator, |
|||
TaskSchedulerManager taskSchedulerManager, |
|||
VenderBusiness venderBusiness, |
|||
YunDingBusiness yunDingBusiness) : base(restApiService, |
|||
options, |
|||
nLogManager, |
|||
fsql, |
|||
idGenerator, |
|||
taskSchedulerManager, |
|||
venderBusiness, |
|||
yunDingBusiness) |
|||
{ |
|||
storeHouseSyncMethodDic = new Dictionary<Enums.Platform, Action<JArray, ShopResponse, IList<Storehouse>>>() |
|||
{ |
|||
{ Enums.Platform.京东, ResolveJDStoreHouse} |
|||
}; |
|||
} |
|||
|
|||
public void StartSyncAllShopStoreHouse() |
|||
{ |
|||
Task.Factory.StartNew(SyncAllShopStoreHouse, CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.StoreHouseTaskScheduler); |
|||
} |
|||
|
|||
public void SyncAllShopStoreHouse() |
|||
{ |
|||
var shopList = venderBusiness.GetShopList(); |
|||
var storeHouseList = new List<Storehouse>(); |
|||
foreach (var shop in shopList) |
|||
{ |
|||
Thread.Sleep(1000); |
|||
try |
|||
{ |
|||
var restApiResult = restApiService.SendRequest(GetPlatformRelayAPIHost(shop.PlatformId), "api/PlatformSDK/GetStoreHouseList", new PlatformRequest() |
|||
{ |
|||
AppKey = shop.AppKey, |
|||
AppSecret = shop.AppSecret, |
|||
AppToken = shop.AppToken, |
|||
Platform = shop.PlatformId, |
|||
SaveResponseLog = false |
|||
}, GetYunDingRequestHeader(), HttpMethod.Post); |
|||
if (restApiResult.StatusCode != System.Net.HttpStatusCode.OK) |
|||
throw new Exception(restApiResult.Content); |
|||
var response = JsonConvert.DeserializeObject<ApiResponse<JArray>>(restApiResult.Content); |
|||
if (response.Data == null || response.Data.Count() == 0) |
|||
continue; |
|||
//ResolveJDStoreHouse(response.Data, shop, storeHouseList);
|
|||
storeHouseSyncMethodDic[shop.PlatformId](response.Data, shop, storeHouseList); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
nLogManager.Default().Error(ex, $"{shop.ShopName} 获取仓库列表失败"); |
|||
} |
|||
} |
|||
|
|||
var storeHouseIds = storeHouseList.Select(s => s.Id).ToArray(); |
|||
var dbStoreIds = fsql.Select<Storehouse>(storeHouseIds).ToList(s => s.Id); |
|||
var exceptIds = storeHouseIds.Except(dbStoreIds); |
|||
if (exceptIds.Count() > 0) |
|||
{ |
|||
var insertList = storeHouseList.Where(s => exceptIds.Contains(s.Id)).ToList(); |
|||
fsql.Insert(insertList).ExecuteAffrows(); |
|||
} |
|||
} |
|||
|
|||
private void ResolveJDStoreHouse(JArray jarray, ShopResponse shop, IList<Storehouse> storeHouseList) |
|||
{ |
|||
foreach (var storeHouseJToken in jarray) |
|||
{ |
|||
var seq_num = storeHouseJToken.Value<string>("seq_num"); |
|||
if (storeHouseList.Count(s => s.Id == seq_num) > 0) |
|||
continue; |
|||
storeHouseList.Add(new Storehouse() |
|||
{ |
|||
Id = seq_num, |
|||
Name = storeHouseJToken.Value<string>("name"), |
|||
Platform = shop.PlatformId, |
|||
CreateTime = DateTime.Now, |
|||
Status = (Enums.StockStatus)storeHouseJToken.Value<int>("use_flag"), |
|||
Type = (Enums.StockType)storeHouseJToken.Value<int>("type") |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,31 @@ |
|||
using FreeSql.DataAnnotations; |
|||
using System; |
|||
|
|||
namespace BBWY.Server.Model.Db |
|||
{ |
|||
|
|||
[Table(Name = "storehouse", DisableSyncStructure = true)] |
|||
public partial class Storehouse |
|||
{ |
|||
|
|||
[Column(DbType = "datetime")] |
|||
public DateTime? CreateTime { get; set; } |
|||
|
|||
[Column(StringLength = 50, IsPrimary = true, IsNullable = false)] |
|||
public string Id { get; set; } |
|||
|
|||
[Column(StringLength = 50)] |
|||
public string Name { get; set; } |
|||
|
|||
[Column(MapType = typeof(int))] |
|||
public Enums.Platform Platform { get; set; } |
|||
|
|||
[Column(MapType = typeof(int))] |
|||
public Enums.StockStatus Status { get; set; } |
|||
|
|||
[Column(MapType = typeof(int))] |
|||
public Enums.StockType Type { get; set; } |
|||
|
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue