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>> storeHouseSyncMethodDic; public StoreHouseSyncBusiness(RestApiService restApiService, IOptions 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.京东, 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(); 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>(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(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(); //} fsql.Transaction(() => { fsql.Delete().Where(s => 1 == 1).ExecuteAffrows(); fsql.Insert(storeHouseList).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") }); } } } }