//---------------------------------------------------------------- //Module Name: CacheManager //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime Author Description //---------------------------------------------------------------- //2014-03-19 deng.yinping Create //---------------------------------------------------------------- using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Model.GoodRules; using Muchinfo.MTPClient.Data.Model.Sale; using Muchinfo.MTPClient.Data.Quote; using Muchinfo.PC.Common.Bosn; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Data.Model.News; using Muchinfo.MTPClient.Infrastructure.Comparers; using Muchinfo.MTPClient.Infrastructure.Enums; using Muchinfo.MTPClient.Infrastructure.Utilities; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using Muchinfo.MTPClient.Data.Model.Account; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Data.Model.DBTableMsg; using Muchinfo.MTPClient.Data.Model.Delivery; namespace Muchinfo.MTPClient.Infrastructure.Cache { public delegate void DataDownloadingEventHandler(int process); public static class CacheManager { #region Fields private static DateTime _startTime; private static Dictionary _caches = new Dictionary(); private static AutoResetEvent _autoResetEvent = new AutoResetEvent(false); private static Dictionary> _saleGoodses = new Dictionary>(); private static Dictionary> _goodsTradeTimes = new Dictionary>(); ///商品开收市时间 private static Dictionary> _holdingSummaries = new Dictionary>(); ///当前资金帐号的持仓汇总信息 private static Dictionary _tradeDayPlan = new Dictionary(); ///日结算计划 #endregion Fields #region Methods #region Public Static Methods #region 挂牌点选缓存属性 private static eTradeProperty _tradeProperty_Listing; /// /// 当前商品交易属性 /// public static eTradeProperty TradeProperty_Listing { get { return _tradeProperty_Listing; } set { _tradeProperty_Listing = value; } } private static bool _isHasListingData =false; /// /// 当前买卖方向是否有委托数据 /// public static bool IsHasListingData { get { return _isHasListingData; } set { _isHasListingData = value; } } private static decimal _orderLot_Listing; /// /// 挂牌点选-选择商品-》委托数量 /// public static decimal OrderLot_Listing { get { return _orderLot_Listing; } set { _orderLot_Listing = value; } } private static long _orderId_Listing =0; /// /// 挂牌点选-选择商品-》委托单号 /// public static long OrderId_Listing { get { return _orderId_Listing; } set { _orderId_Listing = value; } } private static Direction _buyOrSell_Listing = Direction.Bid; /// /// //挂牌点选-选择商品-》买卖方向 /// public static Direction BuyOrSell_Listing { get { return _buyOrSell_Listing; } set { _buyOrSell_Listing = value; } } private static decimal _orderPrice_Listing =Decimal.Zero; /// /// //挂牌点选-选择商品-》委托单号 /// public static decimal OrderPrice_Listing { get { return _orderPrice_Listing; } set { _orderPrice_Listing = value; } } private static eOrderFormType _fromType = eOrderFormType.Commom; /// /// 一般下单/挂牌下单 /// public static eOrderFormType FromType { get { return _fromType; } set { _fromType = value; } } /// /// 重置挂牌下单属性 /// public static void ResetListingDefaultData() { OrderId_Listing = 0; BuyOrSell_Listing = Direction.Bid; OrderPrice_Listing = Decimal.Zero; FromType = eOrderFormType.Commom; } #endregion #region 本地缓存数据属性 /// /// 平仓使用资金账号ID /// public static ulong FundsAccountId { get; set; } /// ///缓存登陆后可用的市场列表 MTP2.0 /// public static List CacheAvailableMarketList { get; set; } public static List CacheDeliveryGoodsInfo { get; set; } /// ///缓存最后更新时间戳 MTP2.0 /// public static List CacheLastUpdateTimeList { get; set; } /// /// 缓存资金账户列表信息 /// public static List CacheTAAcountList { get; set; } /// /// 缓存商品个性化列表信息 /// public static List CacheGoodsRuleAndFeeList { get; set; } /// /// 缓存实物持仓列表【全额持仓】 MTP2.0 /// public static List CacheRealGoodsPositionList { get; set; } /// /// 缓存持仓头寸列表【保证金持仓】 MTP2.0 /// public static List CacheGoodsPositionList { get; set; } /// /// 缓存买/卖可用持仓头寸数量 MTP2.0 /// /// public static uint GetGoodsPositionAvailableQty(Direction dir, uint goodsId, eTradeProperty TradeProperty) { switch (TradeProperty) { case eTradeProperty.TRADEPROPERTY_NOSHORT://全额: if (CacheRealGoodsPositionList != null) { return CacheRealGoodsPositionList.Where(x => x.GoodsId == goodsId).Select(x => x.HolderQty - x.FreezeQty-x.limitCount).FirstOrDefault(); } return 0; default://保证金[查询相反方向的持仓数量] if (CacheGoodsPositionList != null && dir == Direction.Bid) { return CacheGoodsPositionList.Where(x => x.GoodsId == goodsId).Select(x => x.BuyCurPositionQty - x.BuyFrozenQty - x.BuyOtherFrozenQty).FirstOrDefault(); } else if (CacheGoodsPositionList != null && dir == Direction.Ask) { return CacheGoodsPositionList.Where(x => x.GoodsId == goodsId).Select(x => x.SellCurPositionQty - x.SellFrozenQty - x.SellOtherFrozenQty).FirstOrDefault(); } return 0; } } /// /// 获取缓存持仓全额、保证金列表 MTP2.0 /// /// public static List GetGoodsPositionList(Direction buyOrSell, uint goodsId, eTradeMode tradeMode) { return CacheGoodsPositionList.Where(x => x.GoodsId == goodsId).ToList(); //switch (tradeMode) //{ // case eTradeMode.Full_Transfer://全额: // if (CacheRealGoodsPositionList != null) // { // return CacheRealGoodsPositionList.Where(x => x.GoodsId == goodsId).ToList(); // } // return null; // default://保证金 // if (CacheGoodsPositionList != null) // { // return CacheGoodsPositionList.Where(x => x.GoodsId == goodsId).ToList(); // } // return null; //} } /// /// 获取缓存持仓全额、保证金列表 MTP2.0 /// /// public static TradePositionModel GetHoldingSummary(uint goodsId, string currentFundId) { return HoldingSummaries[UInt64.Parse(currentFundId)].Where(x => x.GoodsId == goodsId).FirstOrDefault(); } ///缓存最大通知id MTP2.0 public static long CacheServerLastNoticeId { get; set; } public static List CacheExchanges { get { var emptyResult = new List(); if (!_caches.ContainsKey(CacheType.Exchange) || _caches[CacheType.Exchange].CacheList == null) return emptyResult; var result = _caches[CacheType.Exchange].CacheList.OfType().Where(z => z != null).Distinct(new ExchangeDataModelComparer()); return result.ToList(); } } public static List CacheGoodsGroups { get { var emptyResult = new List(); if (!_caches.ContainsKey(CacheType.GoodsGroup) || _caches[CacheType.GoodsGroup].CacheList == null) return emptyResult; var result = _caches[CacheType.GoodsGroup].CacheList.OfType().Where(z => z != null); return result.ToList(); } } /// /// 缓存市场组MTP2.0 /// public static List CacheMarketsGroups { get { var emptyResult = new List(); if (!_caches.ContainsKey(CacheType.MarketGroup) || _caches[CacheType.MarketGroup].CacheList == null) return emptyResult; var result = _caches[CacheType.MarketGroup].CacheList.OfType().Where(z => z != null); return result.ToList(); } } /// /// 缓存商品信息MTP2.0 /// public static List CacheGoodsBaseInfos { get { var emptyResult = new List(); if (!_caches.ContainsKey(CacheType.GoodsBaseInfo) || _caches[CacheType.GoodsBaseInfo].CacheList == null) return emptyResult; //var result = _caches[CacheType.GoodsBaseInfo].CacheList.OfType().Where(z => z != null && z.IsValid); var result = _caches[CacheType.GoodsBaseInfo].CacheList.OfType().Where(z => z != null); return result.ToList(); } } /// /// 商品开收市时间 /// public static Dictionary> GoodsTradeTimes { get { return _goodsTradeTimes; } } /// /// 日结算计划 /// public static Dictionary TradeDayPlan { get { return _tradeDayPlan; } } /// /// 资金账户对应的持仓信息-MTP2.0无用 /// public static Dictionary> HoldingSummaries { get { return _holdingSummaries; } } /// /// 发售商品组,商品 /// public static Dictionary> SaleGoodses { get { return _saleGoodses; } } public static List CacheNewsCategory { get { var emptyResult = new List(); if (!_caches.ContainsKey(CacheType.NewsCategory) || _caches[CacheType.NewsCategory].CacheList == null) return emptyResult; var result = _caches[CacheType.NewsCategory].CacheList.OfType().Where(z => z != null); return result.ToList(); } } private static Dictionary _tradeGoodsFeeRules=new Dictionary(); /// /// /交易商品的费用计算方式,与交易规则 /// public static Dictionary TradeGoodsFeeRules { get { return _tradeGoodsFeeRules; } set { _tradeGoodsFeeRules = value; } } private static Dictionary _marketStatus = new Dictionary(); /// /// 保存市场类型状态 /// public static Dictionary MarketStatus { get { return _marketStatus; } set { _marketStatus = value; } } private static List _marketRunsNew = new List(); /// /// 缓存市场状态 /// /// The market runs. public static List MarketRuns { get { return _marketRunsNew; } set { _marketRunsNew = value; } } #region 获取服务交易日 /// /// 获取服务运行状态 /// /// The marketid. /// MarketRunModelNew. public static MarketRunModelNew GetMarketRunById(int marketid) { if (MarketRuns == null || !MarketRuns.Any()) return null; var model = MarketRuns.FirstOrDefault(z => z.MarketId == marketid); return model; } /// /// 更新服务运行状态 /// /// The models. public static void UpdateMarketRun(List models) { if (models == null || !models.Any()) return; foreach (var model in models) { var item = GetMarketRunById(model.MarketId); if (item != null)MarketRuns.Remove(item); MarketRuns.Add(model); } } #endregion #region 交易商品的合约单位缓存 private static List _tradeGoodsUnit = new List(); /// /// 交易商品的合约单位 /// public static List TradeGoodsUnit { get { return _tradeGoodsUnit; } set { _tradeGoodsUnit = value; } } #endregion #region p+x交割商品信息LIST缓存 private static List _allDeliveryRelationList = new List(); /// /// p+x交割商品信息LIST缓存 /// public static List AllDeliveryRelationList { get { return _allDeliveryRelationList; } set { _allDeliveryRelationList = value; } } #endregion /// /// 获取指定的更新时间戳 /// /// public static ulong GetLastUpdateTimeBy(LastUpdateTimeType currentType) { ulong defalutValue = 0; if (CacheLastUpdateTimeList != null && CacheLastUpdateTimeList.Any()) { string currentValue =CacheLastUpdateTimeList.Where(x => x.TimeKey == currentType).Select(x => x.TimeValue).FirstOrDefault(); UInt64.TryParse(currentValue,out defalutValue); } return defalutValue; } /// ///获取交易规则,优先使用个人的,可能返回空 /// /// /// /// public static GoodsFeeType GetTradeRule(int goodId, int ruleId) { GoodsFeeType resultRule = null; if (TradeGoodsFeeRules!=null&&TradeGoodsFeeRules.ContainsKey(goodId)) { if (TradeGoodsFeeRules[goodId].AccountGoodsRules.ContainsKey(ruleId)) { resultRule= TradeGoodsFeeRules[goodId].AccountGoodsRules[ruleId]; } else if (TradeGoodsFeeRules[goodId].GoodsRules.ContainsKey(ruleId)) { resultRule = TradeGoodsFeeRules[goodId].GoodsRules[ruleId]; } } return resultRule; } /// /// 获取商品费用信息 /// /// 商品ID /// 费用类型 /// 是否商品的 /// public static GoodsFeeType GetGoodsTradeFeeType(int goodsId, int feeId, bool isGoods = true) { GoodsFeeType feeRule = null; if (TradeGoodsFeeRules != null && TradeGoodsFeeRules.ContainsKey(goodsId)) { if (isGoods) { if (TradeGoodsFeeRules[goodsId].GoodsFees.ContainsKey(feeId)) { feeRule = TradeGoodsFeeRules[goodsId].GoodsFees[feeId]; } } else { if (TradeGoodsFeeRules[goodsId].AccountGoodsFees.ContainsKey(feeId)) { feeRule = TradeGoodsFeeRules[goodsId].AccountGoodsFees[feeId]; } } } return feeRule; } #endregion ///// ///// 开多任务 加载交易信息 ///// //public static void LoadBaseInfo() //{ // TaskSuppleTable(); // Task.Factory.TryStartNew(GetExchangeListAndMerage, () => { }); // Task.Factory.TryStartNew(GetMarketDetailListAndMerage, () => { }); //} /// /// 缓存登陆后基本信息MTP2.0 /// /// public static void LoadLoginBaseInfo(AccountBaseInfoModel accountInfoEntity) { if (null != accountInfoEntity && accountInfoEntity.ServerLastNoticeId !=0) ////最后更新时间列表 { //登录取到的ID为最后的ID,注释此代码-dyp //CacheServerLastNoticeId = accountInfoEntity.ServerLastNoticeId; } if (null != accountInfoEntity && accountInfoEntity.LastUpdateTimeList.Any() && accountInfoEntity.LastUpdateTimeList.Count > 0) ////最后更新时间列表 { CacheLastUpdateTimeList = accountInfoEntity.LastUpdateTimeList; } if (null != accountInfoEntity && accountInfoEntity.TAAccountList.Any() && accountInfoEntity.TAAccountList.Count > 0) ////用户资金账户列表信息 { CacheTAAcountList = accountInfoEntity.TAAccountList; if (CacheAvailableMarketList == null) CacheAvailableMarketList = new List(); else CacheAvailableMarketList.Clear(); HoldingSummaries.Clear(); foreach (var account in accountInfoEntity.TAAccountList) { HoldingSummaries.Add(account.FundsAccounts.AccountId, account.GoodsPositionList); CacheAvailableMarketList.AddRange(account.MarketsIdList); } CacheAvailableMarketList = CacheAvailableMarketList.Distinct().ToList(); } if (null != accountInfoEntity && accountInfoEntity.GoodsRuleAndFeeList.Any() && accountInfoEntity.GoodsRuleAndFeeList.Count > 0) { CacheGoodsRuleAndFeeList = accountInfoEntity.GoodsRuleAndFeeList; } } /// /// 缓存市场信息MTP2.0 /// /// public static void LoadMarketInfoList(List marketsEntity) { TaskSuppleTable(); if (null != marketsEntity && marketsEntity.Any()) ////市场信息 { marketsEntity = UpdateMarketsGroup(marketsEntity, GoodsFromScr.Trade); ////更新市场组 var lastModifyTime = ApplicationParameter.BasicDateTime; UpdateTable(CacheType.MarketGroup, lastModifyTime, marketsEntity.ToArray()); } } /// /// 缓存商品GOODS信息MTP2.0 /// /// public static void LoadGoodsInfoList(List goodsEntity, GoodsFromScr goodsScr) { TaskSuppleTable(); if (null != goodsEntity && goodsEntity.Any() && CacheAvailableMarketList != null) ////交易商品 { //商品市场权限过滤,MTP2.0NEW var result = goodsEntity.Distinct(new QuoteGoodsComparer()).Where(x => CacheAvailableMarketList.Contains(x.MarketID)).ToList(); result = UpdateGoods(result, goodsScr); ////设置商品来源 var lastModifyTime = ApplicationParameter.BasicDateTime; UpdateTable(CacheType.GoodsBaseInfo, lastModifyTime, result.ToArray()); } } /// /// 商品、市场,交易所信息缓存 /// /// 用户有权限的交易信息 public static void LoadGoodsBaseInfo(AccountInfoEntity accountInfoEntity) { LoadGoodsBaseInfo(accountInfoEntity.QuoteGoodses, accountInfoEntity.GoodsGroups, GoodsFromScr.Trade); } public static void LoadExchangeInfo(List Exchanges) { if (null != Exchanges && Exchanges.Any()) ////交易所信息 { var lastModifyTime = ApplicationParameter.BasicDateTime;// allExchanges.Max(z => z.LastModifiedTime); UpdateTable(CacheType.Exchange, lastModifyTime, Exchanges.ToArray()); } } /// /// 缓存商品组 /// /// public static void LoadGoodsGroupList(List goodsGroupsEntity) { TaskSuppleTable(); if (null != goodsGroupsEntity && goodsGroupsEntity.Any()) ////市场信息 { var newEntity = UpdateGoodsGroup(goodsGroupsEntity, GoodsFromScr.Trade); ////更新市场组 var lastModifyTime = ApplicationParameter.BasicDateTime; UpdateTable(CacheType.GoodsGroup, lastModifyTime, newEntity.ToArray()); } } /// /// 商品、市场,交易所信息缓存 /// public static void LoadGoodsBaseInfo(List goodses, List goodsGroups, GoodsFromScr goodsScr) { TaskSuppleTable(); //if (null != Exchanges && Exchanges.Any()) ////交易所信息 //{ // var lastModifyTime = ApplicationParameter.BasicDateTime;// allExchanges.Max(z => z.LastModifiedTime); // UpdateTable(CacheType.Exchange, lastModifyTime, Exchanges.ToArray()); //} if (null != goodsGroups && goodsGroups.Any()) ////市场信息 { goodsGroups = UpdateGoodsGroup(goodsGroups, goodsScr); ////更新商品组 var lastModifyTime = ApplicationParameter.BasicDateTime;//allExchanges.Max(z => z.LastModifiedTime); UpdateTable(CacheType.GoodsGroup, lastModifyTime, goodsGroups.ToArray()); } if (null != goodses && goodses.Any()) ////交易商品 { var result = goodses.Distinct(new QuoteGoodsComparer()).ToList(); result = UpdateGoods(result, goodsScr); ////设置商品来源 var lastModifyTime = ApplicationParameter.BasicDateTime; //allExchanges.Max(z => z.LastModifiedTime); UpdateTable(CacheType.GoodsBaseInfo, lastModifyTime, result.ToArray()); } } /// /// 更新商品并设置商品来源 /// /// 商品列表 /// 商品来源 /// private static List UpdateGoods(List goodses, GoodsFromScr goodsScr) { if (CacheGoodsBaseInfos == null || !CacheGoodsBaseInfos.Any()) ////当前没有商品添加 { foreach (var quoteGoodse in goodses) { quoteGoodse.ContainsGoodsSrc = (int) goodsScr; ////设置商品来源 quoteGoodse.TradeMode = CacheMarketsGroups.Where(x => x.MarketID == quoteGoodse.MarketID).Select(x=>x.TradeMode).FirstOrDefault(); quoteGoodse.TradeProperty = CacheMarketsGroups.Where(x => x.MarketID == quoteGoodse.MarketID).Select(x => x.TradeProperty).FirstOrDefault(); } return goodses; } else { var temGoodses = new List(); var list = CacheGoodsBaseInfos; foreach (var quoteGoodse in goodses) { var existGoods = CacheGoodsBaseInfos.FirstOrDefault((goods) => goods.Symbol == quoteGoodse.Symbol); quoteGoodse.TradeMode = CacheMarketsGroups.Where(x => x.MarketID == quoteGoodse.MarketID).Select(x => x.TradeMode).FirstOrDefault(); if (existGoods == null) ////商品不存在需添加 { quoteGoodse.ContainsGoodsSrc = (int) goodsScr; temGoodses.Add(quoteGoodse); } else { if (goodsScr == GoodsFromScr.Trade) ////如果存在更新从交易更新的。则先移除旧的 { list.Remove(existGoods); quoteGoodse.ContainsGoodsSrc = (int) GoodsFromScr.Trade | (int) existGoods.ContainsGoodsSrc; ////或运算 temGoodses.Add(quoteGoodse); } else { existGoods.ContainsGoodsSrc = existGoods.ContainsGoodsSrc | (int)goodsScr; //// 商品包括多个来源 ////或运算 } } } list.AddRange(temGoodses); return list; } } /// /// 更新商品组 /// /// 商品组列表 /// 商品组更新来源 /// private static List UpdateGoodsGroup(List goodsGroups, GoodsFromScr goodsScr) { if (CacheGoodsGroups == null || !CacheGoodsGroups.Any()) ////当前没有商品添加 { foreach (var goodsGroup in goodsGroups) { MarketStatus[goodsGroup.MarketTypeID] = 0; } return goodsGroups; } else { var temGroups = new List(); var cacheGroups = CacheGoodsGroups; foreach (var goodsGroup in goodsGroups) { var groupItem= cacheGroups.FirstOrDefault((item) => item.GoodsGroupid == goodsGroup.GoodsGroupid); if (groupItem != null) { if (goodsScr == GoodsFromScr.Trade) ////如果存在更新从交易更新的。则先移除旧的 { cacheGroups.Remove(groupItem); temGroups.Add(goodsGroup); MarketStatus[goodsGroup.MarketTypeID] = 0; } } else { temGroups.Add(goodsGroup); } } cacheGroups.AddRange(temGroups); return cacheGroups; } } /// /// 更新市场组MTP2.0 /// /// 市场组列表 /// 市场组更新来源 /// private static List UpdateMarketsGroup(List marketGroups, GoodsFromScr goodsScr) { if (CacheMarketsGroups == null || !CacheMarketsGroups.Any()) ////当前没有市场添加 { foreach (var marketGroup in marketGroups) { MarketStatus[(int)marketGroup.MarketID] = 0; } return marketGroups; } else { var temGroups = new List(); var cacheGroups = CacheMarketsGroups; foreach (var marketGroup in marketGroups) { var groupItem = cacheGroups.FirstOrDefault((item) => item.MarketID == marketGroup.MarketID); if (groupItem != null) { if (goodsScr == GoodsFromScr.Trade) ////如果存在更新从交易更新的。则先移除旧的 { cacheGroups.Remove(groupItem); temGroups.Add(marketGroup); MarketStatus[(int)marketGroup.MarketID] = 0; } } else { temGroups.Add(marketGroup); } } cacheGroups.AddRange(temGroups); return cacheGroups; } } #endregion Public Static Methods #region Private Static Methods private static List GetAllCacheTypes() { var result = new List(); var values = Enum.GetValues(typeof(CacheType)); foreach (var value in values) { result.Add((CacheType)value); } return result; } ///// ///// 多任务使用查询交易所信息 ///// //private static void GetExchangeListAndMerage() //{ // int errorCode = 0; // var tradeAccount = UserManager.CurrentTradeAccount; // if (tradeAccount == null) return; // var allExchanges = new List(); // var basicDataService = SimpleIoc.Default.GetInstance(); // try // { // var exchanges = basicDataService.GetExchanges(new List() { tradeAccount.ExchangeId.ToString() }); // if (exchanges == null || !exchanges.Any()) // { // throw new Exception(Client_Resource.Infrastructure_TradingFailurePleaseTryAgain); // } // allExchanges.AddRange(exchanges); // foreach (var exchange in allExchanges) // { // //等待服务联调 // basicDataService.GetExchangesParamer(exchange); // } // } // catch (MuchinfoException muchinfo) // { // errorCode = muchinfo.ReturnCode; // } // catch (Exception ex) // { // errorCode = ExceptionManager.QueryExchangeError; // } // MessengerHelper.DefaultSend(new DisplayMessage() // { // ErrorCode = errorCode, // Result = errorCode == 0, // QueryType = QueryType.Exchange, // }, MessengerTokens.LoadBaseInfo); // if (allExchanges.Any()) // { // var lastModifyTime = ApplicationParameter.BasicDateTime;// allExchanges.Max(z => z.LastModifiedTime); // UpdateTable(CacheType.Exchange, lastModifyTime, allExchanges.ToArray()); // } //} ///// ///// 多任务使用查询市场信息 ///// ///// //private static void GetMarketDetailListAndMerage() //{ // int errorCode = 0; // var tradeAccount = UserManager.CurrentTradeAccount; // if (tradeAccount == null) return; // var allMarkets = new List(); // var basicDataService = SimpleIoc.Default.GetInstance(); // try // { // var markets = basicDataService.GetMarkets(tradeAccount.TradeCode); // if (markets == null || !markets.Any()) // { // throw new Exception(Client_Resource.Infrastructure_MatketFailurePleaseTrayAgain); // } // allMarkets.AddRange(markets); // } // catch (MuchinfoException muchinfo) // { // errorCode = muchinfo.ReturnCode; // } // catch (Exception ex) // { // errorCode = ExceptionManager.QueryMarketsError; // } // MessengerHelper.DefaultSend(new DisplayMessage() // { // ErrorCode = errorCode, // Result = errorCode == 0, // QueryType = QueryType.Markets, // }, MessengerTokens.LoadBaseInfo); // if (allMarkets.Any()) // { // var lastModifyTime = ApplicationParameter.BasicDateTime;//allExchanges.Max(z => z.LastModifiedTime); // UpdateTable(CacheType.MarketDetail, lastModifyTime, allMarkets.ToArray()); // } // _autoResetEvent.Set(); //} private static List ConvertCacheList(CacheObject cacheObject) where T : class { if (cacheObject == null || cacheObject.CacheList == null || !cacheObject.CacheList.Any()) return null; var models = cacheObject.CacheList.Select(z => { var jObject = z as JObject; return jObject != null ? jObject.ToObject() : default(T); }); return models.ToList(); } /// /// Loads the table. /// /// Type of the cache. private static void LoadTable(CacheType cacheType) { if (!_caches.ContainsKey(cacheType)) { var cacheObject = LoadCacheObject(cacheType); if (_caches.ContainsKey(cacheObject.CacheType)) { _caches.Remove(cacheObject.CacheType); } _caches.Add(cacheObject.CacheType, cacheObject); } } private static CacheObject LoadCacheObject(CacheType cacheType) { string cacheObjectFile = Path.Combine(UserManager.UserDataFolder, cacheType.ToString() + UserManager.LocalDataExtensionName); var cacheObject = BsonHelper.LoadData(cacheObjectFile).FirstOrDefault(); if (cacheObject == null) cacheObject = new CacheObject(cacheType) { CacheTime = DateTime.MinValue }; if (cacheObject.CacheList != null && cacheObject.CacheList.Count > 0) { switch (cacheObject.CacheType) { case CacheType.Exchange: cacheObject.CacheList = ConvertCacheList(cacheObject); break; //case CacheType.ExchangeChange: // cacheObject.CacheList = ConvertCacheList(cacheObject); // break; case CacheType.GoodsBaseInfo: cacheObject.CacheList = ConvertCacheList(cacheObject); break; case CacheType.MarketGroup: cacheObject.CacheList = ConvertCacheList(cacheObject); break; case CacheType.GoodsGroup: cacheObject.CacheList = ConvertCacheList(cacheObject); break; //case CacheType.QuoteHead: // cacheObject.CacheList = ConvertCacheList(cacheObject); // break; //case CacheType.QuoteHeadDetail: // cacheObject.CacheList = ConvertCacheList(cacheObject); // break; //case CacheType.Sector: // cacheObject.CacheList = ConvertCacheList(cacheObject); // break; //case CacheType.Warning: // cacheObject.CacheList = ConvertCacheList(cacheObject); // break; case CacheType.NewsCategory: cacheObject.CacheList = ConvertCacheList(cacheObject); break; } } return cacheObject; } ///// ///// Saves the table. ///// ///// Type of the cache. //private static void SaveTable(CacheType cacheType) //{ // var tableFile = Path.Combine(UserManager.UserDataFolder, cacheType.ToString() + UserManager.LocalDataExtensionName); // var table = _caches[cacheType]; // BsonHelper.SaveData(tableFile, table, null, true); //} /// /// 多任务添加缓存类型 /// private static void TaskSuppleTable() { var typeList = GetAllCacheTypes(); foreach (CacheType type in typeList) { switch (type) { case CacheType.Other: case CacheType.Finished: break; //case CacheType.Warning: // LoadTable(CacheType.Warning); // break; default: LoadTable(type); break; } } } private static void UpdateTable(CacheType cacheType, DateTime lastModifyTime, T[] newRecords, bool isSave = true) { if (!_caches.ContainsKey(cacheType)) return; if (null == newRecords || newRecords.Length == 0) return; var data = _caches[cacheType] ?? new CacheObject(cacheType); if (data.CacheList == null) data.CacheList = new List(); var newList = new List(); foreach (var newRecord in newRecords) { newList.Add(newRecord); } //test data.CacheList.Clear(); //end data.CacheList.AddRange(newList); //更新缓存时间 data.CacheTime = lastModifyTime; _caches[cacheType] = data; } #endregion Private Static Methods #endregion Methods #region 存储外部交易所、商品组、商品的关系 public static void LoadExChangeGroupGoods(ExChangeCheckModel exChangeCheckModel) { TaskSuppleTable(); if (null != exChangeCheckModel) // { var lastModifyTime = ApplicationParameter.BasicDateTime; List list = new List() {exChangeCheckModel}; UpdateTable(CacheType.ExChangeGroupGoods, lastModifyTime, list.ToArray()); } } public static List CacheExChangeGroupGoods { get { if (!_caches.ContainsKey(CacheType.ExChangeGroupGoods) || _caches[CacheType.ExChangeGroupGoods].CacheList == null) return null; //var result = _caches[CacheType.GoodsBaseInfo].CacheList.OfType().Where(z => z != null && z.IsValid); IEnumerable result = _caches[CacheType.ExChangeGroupGoods].CacheList.OfType().Where(z => z != null); return result.ToList(); } } /// /// 终端启动时通过通用查许从quotesourcegroup表获取行情交易日 /// public static List QuoteSourceGroupMarketDatas { get; set; } #endregion } }