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.
112 lines
5.2 KiB
112 lines
5.2 KiB
using BBWY.Common.Models;
|
|
using BBWY.Server.Model;
|
|
using BBWY.Server.Model.Db;
|
|
using BBWY.Server.Model.Dto;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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(NLogManager nLogManager,
|
|
IFreeSql fsql,
|
|
IIdGenerator idGenerator,
|
|
TaskSchedulerManager taskSchedulerManager,
|
|
VenderBusiness venderBusiness,
|
|
IEnumerable<PlatformSDKBusiness> platformSDKBusinessList) : base(
|
|
nLogManager,
|
|
fsql,
|
|
idGenerator,
|
|
taskSchedulerManager,
|
|
platformSDKBusinessList,
|
|
venderBusiness)
|
|
{
|
|
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(platform: Enums.Platform.京东);
|
|
var storeHouseList = new List<Storehouse>();
|
|
var p = GetPlatformSDKBusiness(Enums.Platform.京东);
|
|
foreach (var shop in shopList)
|
|
{
|
|
Thread.Sleep(1000);
|
|
try
|
|
{
|
|
var response = p.GetStoreHouseList(new PlatformRequest()
|
|
{
|
|
AppKey = shop.AppKey,
|
|
AppSecret = shop.AppSecret,
|
|
AppToken = shop.AppToken,
|
|
Platform = shop.PlatformId,
|
|
SaveResponseLog = false
|
|
});
|
|
//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 == null || response.Count() == 0)
|
|
continue;
|
|
//ResolveJDStoreHouse(response.Data, shop, storeHouseList);
|
|
storeHouseSyncMethodDic[shop.PlatformId](response, 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")
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|