步步为盈
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.

110 lines
5.1 KiB

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(platform: Enums.Platform.);
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")
});
}
}
}
}