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>> storeHouseSyncMethodDic; public StoreHouseSyncBusiness(NLogManager nLogManager, IFreeSql fsql, IIdGenerator idGenerator, TaskSchedulerManager taskSchedulerManager, VenderBusiness venderBusiness, IEnumerable platformSDKBusinessList) : base( nLogManager, fsql, idGenerator, taskSchedulerManager, platformSDKBusinessList, venderBusiness) { storeHouseSyncMethodDic = new Dictionary>>() { { 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(); 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>(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(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 storeHouseList) { foreach (var storeHouseJToken in jarray) { var seq_num = storeHouseJToken.Value("seq_num"); if (storeHouseList.Count(s => s.Id == seq_num) > 0) continue; storeHouseList.Add(new Storehouse() { Id = seq_num, Name = storeHouseJToken.Value("name"), Platform = shop.PlatformId, CreateTime = DateTime.Now, Status = (Enums.StockStatus)storeHouseJToken.Value("use_flag"), Type = (Enums.StockType)storeHouseJToken.Value("type") }); } } } }