| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139 |
- using System.Collections;
- using GalaSoft.MvvmLight.Ioc;
- using Muchinfo.MTPClient.Data;
- using Muchinfo.MTPClient.Data.Enums;
- using Muchinfo.MTPClient.Data.Model;
- using Muchinfo.MTPClient.Data.Model.Account;
- using Muchinfo.MTPClient.Data.Model.Config;
- using Muchinfo.MTPClient.Data.Model.Delivery;
- using Muchinfo.MTPClient.Data.Model.Listing;
- using Muchinfo.MTPClient.Data.Model.Sale;
- using Muchinfo.MTPClient.Infrastructure.Enums;
- using Muchinfo.MTPClient.Infrastructure.Helpers;
- using Muchinfo.MTPClient.IService;
- using Muchinfo.PC.Common.Bosn;
- using Muchinfo.PC.Common.Helpers;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Windows;
- using Muchinfo.MTPClient.Data.DTO;
- using Muchinfo.MTPClient.Infrastructure.Cache;
- namespace Muchinfo.MTPClient.Infrastructure.Utilities
- {
- /// <summary>
- /// UserManager类
- /// </summary>
- public class UserManager
- {
- #region 字段
- private static object _lockObject = new object();
- /// <summary>
- /// 本地数据文件后缀名
- /// </summary>
- public const string LocalDataExtensionName = ".dll";
- #endregion
- #region 公有属性
- /// <summary>
- /// 当前列表显示的商品
- /// </summary>
- /// <value>The current goods i ds.</value>
- public static List<int> CurrentGoodsIds { get; set; }
- /// <summary>
- /// 交易面板选中的当前Id
- /// </summary>
- public static int CurrentGoodsId { get; set; }
- #region 当前商品是否有商品
- private static bool _currentGroupHasGoods=false;
- /// <summary>
- /// 当前商品是否有商品
- /// </summary>
- public static bool CurrentGroupHasGoods
- {
- get { return _currentGroupHasGoods; }
- set { _currentGroupHasGoods = value; }
- }
- #endregion
- /// <summary>
- /// 主界面是否被解锁 //todo:什么用?
- /// </summary>
- public static bool MainPanelIsUnLock
- {
- get;
- set;
- }
- /// <summary>
- /// 行情窗口类型-List/Board
- /// </summary>
- /// <value>The type of the quotation.</value>
- public static TabContetMode QuotationWindowType { get; set; }
- /// <summary>
- /// 当前用户
- /// </summary>
- public static UserInfo CurrentInfo;
- #endregion
- #region 用户缓存信息
- private static List<TradeAccount> _tradeAccounts = new List<TradeAccount>();
- /// <summary>
- /// 登录的交易账号集合
- /// </summary>
- public static List<TradeAccount> TradeAccounts
- {
- get
- {
- return _tradeAccounts;
- }
- }
- private static TradeAccount _currentTradeAccount;
-
- /// <summary>
- /// 当前选中的交易账号
- /// </summary>
- public static TradeAccount CurrentTradeAccount
- {
- get { return _currentTradeAccount; }
- set
- {
- _currentTradeAccount = value;
- //当时账号更新的交易Header需更新
- MessengerHelper.DefaultSend(_currentTradeAccount, MessengerTokens.UpdateTradeAccount);
- }
- }
-
- /// <summary>
- /// 是否为游客登录
- /// </summary>
- public static bool IsAccountLogin
- {
- get;
- set;
- }
-
- /// <summary>
- /// 关注商品列表
- /// </summary>
- private static List<QuoteGoods> _foucusGoodsList;
- /// <summary>
- /// 游客登录查询的商品
- /// </summary>
- public static List<SaleGoods> BrownSaleGoodses { get; set; }
- /// <summary>
- /// 用户名可操作的菜单
- /// </summary>
- public static List<SysMenuItem> AccessFuncMenus { get; set; }
- /// <summary>
- /// 本地固定菜单
- /// </summary>
- public static List<SysMenuItem> FixedFuncMenus { get; set; }
- /// <summary>
- /// 缓冲本地菜单
- /// </summary>
- public static Dictionary<MenuCommandType, string> LocalFuncMenus { get; set; }
- /// <summary>
- /// 获取菜单类型
- /// </summary>
- /// <param name="FuncMenuCode"></param>
- /// <returns></returns>
- public static MenuCommandType GetMenuCommandType(string FuncMenuCode)
- {
- return UserManager.LocalFuncMenus.Where(x => x.Value == FuncMenuCode).Select(x => x.Key).FirstOrDefault();
- }
- /// <summary>
- /// 获取菜单对应ResourceCode
- /// </summary>
- /// <returns></returns>
- public static string GetMenuCommandValue(MenuCommandType typeKey)
- {
- return UserManager.LocalFuncMenus.Where(x => x.Key == typeKey).Select(x => x.Value).FirstOrDefault();
- }
- private static List<SysMenuItem> _tradeMenuItems=new List<SysMenuItem>();
- /// <summary>
- /// 交易菜单
- /// </summary>
- public static List<SysMenuItem> TradeFuncMenus
- {
- get
- {
- return _tradeMenuItems;
- }
- set
- {
- _tradeMenuItems=value;
- }
- }
- /// <summary>
- /// Gets or sets the trade rate temporary configuration.
- /// </summary>
- /// <value>The trade rate temporary configuration.</value>
- public static List<TradeRateTmpConfig> TradeRateTmpConfigs { get; set; }
- /// <summary>
- /// 外部汇率模版配置表
- /// </summary>
- /// <value>The trade rate temporary configuration.</value>
- public static List<TradeRateTmpConfig> ExtenalTradeRateTmpConfigs { get; set; }
- /// <summary>
- /// 资金账户汇率模版配置
- /// </summary>
- /// <value>The trade rate temporary configuration.</value>
- public static List<TradeRateTmpConfig> TaAccountExchangeRateConfigs { get; set; }
- #endregion
- #region 用户数据路径
- /// <summary>
- /// 系统配置路径
- /// </summary>
- public static string SysConfigFolderPath
- {
- get
- {
- return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config");
- }
- }
- /// <summary>
- /// 获取登录用户或游客配置的文件夹
- /// </summary>
- /// <value>The user settings folder.</value>
- public static string UserSettingsFolder
- {
- get
- {
- var usersFolder = UsersSettingsFolder;
- var folder = CurrentTradeAccount == null || CurrentTradeAccount.LoginID==uint.MaxValue ?
- Path.Combine(usersFolder, "Guest") : Path.Combine(usersFolder, CurrentTradeAccount.LoginID.ToString());
- if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
- return folder;
- }
- }
- /// <summary>
- /// 获取程序目录的"Users"文件夹
- /// </summary>
- public static string UsersSettingsFolder
- {
- get
- {
- var userConfigRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Users");
- if (!Directory.Exists(userConfigRoot)) Directory.CreateDirectory(userConfigRoot);
- return userConfigRoot;
- }
- }
- /// <summary>
- /// 用户基本数据文件夹
- /// </summary>
- public static string UserDataFolder
- {
- get
- {
- var usersFolder = UserSettingsFolder;
- var folder = Path.Combine(usersFolder, "Data");
- if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
- return folder;
- }
- }
- #endregion
- #region 登录账号读写 - BSON存储
- /// <summary>
- /// 获取登录的交易账号集合
- /// </summary>
- /// <returns>List{LoginAccount}.</returns>
- public static List<UserInfo> GetTradeAccounts()
- {
- var filePath = Path.Combine(UsersSettingsFolder, "LoginAccounts" + LocalDataExtensionName);
- if (!File.Exists(filePath)) return null;
- try
- {
- var result = BsonHelper.LoadData<UserInfo>(filePath);
- if (result == null||!result.Any()) return null;
- return result.OrderByDescending(z => z.LoginTime).ToList();
- }
- catch (Exception ex)
- {
- LogHelper.WriteError(typeof(UserManager), "get LoginAccount Error:" + ex.ToString());
- File.Delete(filePath);
- return null;
- }
- }
- /// <summary>
- /// 获取用户保存的配置
- /// </summary>
- /// <param name="userName"></param>
- /// <returns></returns>
- public static UserInfo GetTradeAccount(string userName)
- {
- var filePath = Path.Combine(UsersSettingsFolder, "LoginAccounts" + LocalDataExtensionName);
- if (!File.Exists(filePath)) return null;
- try
- {
- var result = BsonHelper.LoadData<UserInfo>(filePath);
- if (result == null || !result.Any()) return null;
- var dicResult = result.ToDictionary((item) => item.UserName);
- if (dicResult.ContainsKey(userName))
- {
- return dicResult[userName];
- }
- }
- catch (Exception)
- {
- File.Delete(filePath);
- return null;
- }
- return null;
- }
- /// <summary>
- /// 获取当前用户名
- /// </summary>
- /// <returns></returns>
- public static UserInfo GetCurrentUserInfo()
- {
- if (CurrentInfo != null)
- return CurrentInfo;
- else
- {
- return GetTradeAccount(UserManager.CurrentTradeAccount.LoginID.ToString());
- }
- }
-
- /// <summary>
- /// 保存交易账号
- /// </summary>
- /// <returns>List{LoginAccount}.</returns>
- public static void SaveTradeAccount(UserInfo tradeAccount)
- {
- if (tradeAccount == null) return;
- var filePath = Path.Combine(UsersSettingsFolder, "LoginAccounts" + LocalDataExtensionName);
- var accountHash = new Dictionary<string,UserInfo>();
- try
- {
- if (File.Exists(filePath))
- {
- var listHash = BsonHelper.LoadData<UserInfo>(filePath) as List<UserInfo>;
- if (listHash != null && listHash.Any())
- {
- accountHash = listHash.ToDictionary((item)=>item.UserName);
- }
- }
- accountHash[tradeAccount.UserName] = tradeAccount;
- BsonHelper.SaveData<UserInfo>(filePath, accountHash.Values.ToList(), null, true);
- CurrentInfo = tradeAccount;
- }
- catch (Exception ex)
- {
- LogHelper.WriteError(typeof(UserManager), "Save LoginAccount Error:" + ex.ToString());
- }
- }
- /// <summary>
- /// 登录成功保存内容
- /// </summary>
- public static void LoginedSave(UserInfo tradeAccount, bool IsSaveAccount)
- {
- UserInfo info = tradeAccount;
-
- info.LoginTime = DateTime.Now;
- info.RememberAccount = IsSaveAccount;
- info.LoginType = 0;
- info.MemberId = UserManager.CurrentTradeAccount.MemberAreaId;
- SaveTradeAccount(info);
- //记住本次登录用户
- var lastLoginUser = new LastLoginUser();
- lastLoginUser.MemberId = info.MemberId + string.Empty;
- lastLoginUser.MemberPath = Path.Combine(UserManager.SysConfigFolderPath, info.MemberId + string.Empty);
- SaveLastLoginUser(lastLoginUser);
- //CurrentUser = info;
-
-
- }
- /// <summary>
- /// 读取最一个用户登录的账号所属会员
- /// </summary>
- /// <returns></returns>
- public static LastLoginUser GetLastLoginUser()
- {
- string filePath = Path.Combine(UsersSettingsFolder, "AppDefault" + LocalDataExtensionName);
- var result = BsonHelper.LoadData<LastLoginUser>(filePath);
- if (result == null || !result.Any()) return null;
- return result.FirstOrDefault();
- }
- /// <summary>
- /// /保存最后一个用户的所属会员
- /// </summary>
- /// <param name="lastLoginUser">会员代码</param>
- public static void SaveLastLoginUser(LastLoginUser lastLoginUser)
- {
- string filePath = Path.Combine(UsersSettingsFolder, "AppDefault" + LocalDataExtensionName);
- BsonHelper.SaveData<LastLoginUser>(filePath, lastLoginUser, null, true);
- }
- #endregion
- #region 用户单据
- private static List<TradeCacheData> _tradeCacheDataList = new List<TradeCacheData>();
- /// <summary>
- /// 初始化缓存数据标志
- /// </summary>
- /// <param name="tradeAccount">The trade account.</param>
- public static void InitializeTradeCacheData(TradeAccount tradeAccount)
- {
- if (tradeAccount == null) return;
- var tradeCacheData = GetTradeCacheData(tradeAccount.AccountId);
- if (tradeCacheData == null) return;
- tradeCacheData.HoldOrderFlag = false;
- tradeCacheData.CloseOrderFlag = false;
- tradeCacheData.OpenCompleteFlag = false;
- tradeCacheData.CompleteOrderFlag = false;
- tradeCacheData.LimitOrderFlag = false;
- tradeCacheData.HoldingSummaryFlag = false;
- tradeCacheData.DeliveryOrderFlag = false;
- tradeCacheData.OpenPrePostOrderFlag = false;
- }
- public static Dictionary<eTradeMode, List<HoldingOrder>> GetMarketHoldOrders(TradeAccount account)
- {
- if (CurrentTradeAccount == null)
- {
- return new Dictionary<eTradeMode, List<HoldingOrder>>();
- }
- var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.AccountId);
- return tradeCacheData.MarketHoldDetails;
- }
- public static Dictionary<eTradeMode, List<HoldingSummary>> GetMarketHoldSummaries(TradeAccount account)
- {
- if (CurrentTradeAccount == null)
- {
- return new Dictionary<eTradeMode, List<HoldingSummary>>();
- }
- var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.AccountId);
- return tradeCacheData.MarketHoldSummaries;
- }
- /// <summary>
- /// Determines whether [has goods hold summary] [the specified account identifier].
- /// </summary>
- /// <param name="accountId">The account identifier.</param>
- /// <param name="buyOrSell">The buy or sell.</param>
- /// <param name="goodsId">The goods identifier.</param>
- /// <returns><c>true</c> if [has goods hold summary] [the specified account identifier]; otherwise, <c>false</c>.</returns>
- public static bool HasGoodsHoldSummary(ulong accountId, Direction buyOrSell, uint goodsId )
- {
- if (CurrentTradeAccount == null)
- {
- return false;
- }
- var tradeCacheData = GetTradeCacheData(accountId);
- if (tradeCacheData == null || tradeCacheData.HoldingSummaries == null ||
- !tradeCacheData.HoldingSummaries.Any()) return false;
- return tradeCacheData.HoldingSummaries.Any(z => z.GoodsId == goodsId && z.Direction == buyOrSell);
- }
- public static List<HoldingSummary> GetHoldSummaries(ulong accountId)
- {
- var tradeCacheData = GetTradeCacheData(accountId);
- return tradeCacheData.HoldingSummaries == null ? null : tradeCacheData.HoldingSummaries.ToList();
- }
- /// <summary>
- /// 按市场区分单据数据
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="orders"></param>
- public static void UpdateMarketHoldOrders<T>(Dictionary<eTradeMode, List<T>> orders)
- {
- if (CurrentTradeAccount == null) return;
- var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.AccountId);
- if (typeof(T).Equals(typeof(HoldingOrder)))
- {
- tradeCacheData.MarketHoldDetails = orders as Dictionary<eTradeMode, List<HoldingOrder>>;
- tradeCacheData.HoldOrderFlag = true;
- }
- else if (typeof(T).Equals(typeof(HoldingSummary)))
- {
- tradeCacheData.MarketHoldSummaries = orders as Dictionary<eTradeMode, List<HoldingSummary>>;
- tradeCacheData.HoldingSummaryFlag = true;
- }
- }
- /// <summary>
- /// 按市场类型更新单据信息
- /// </summary>
- /// <typeparam name="T">单据类型</typeparam>
- /// <param name="marketType">市场类型</param>
- /// <param name="orders">单据列表</param>
- public static void UpdateMarketOrders<T>(eTradeMode marketType, List<T> orders)
- {
- if (CurrentTradeAccount == null) return;
- var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.AccountId);
- if (typeof(T).Equals(typeof(CloseOrder)))
- {
- if (tradeCacheData.MarketCloseOrders == null)
- {
- tradeCacheData.MarketCloseOrders = new Dictionary<eTradeMode, List<CloseOrder>>();
- }
- tradeCacheData.MarketCloseOrders[marketType] = orders as List<CloseOrder>;
- tradeCacheData.CloseOrderFlag = true;
- }
- else if (typeof(T).Equals(typeof(CompleteOrder)))
- {
- if (tradeCacheData.MarketOpenCompletes == null)
- {
- tradeCacheData.MarketOpenCompletes = new Dictionary<eTradeMode, List<CompleteOrder>>();
- }
- tradeCacheData.MarketOpenCompletes[marketType] = orders as List<CompleteOrder>;
- tradeCacheData.OpenCompleteFlag = true;
- }
- else if (typeof(T).Equals(typeof(PrePostDetail)))//预埋单
- {
- if (tradeCacheData.MarketOpenPrePostOrder == null)
- {
- tradeCacheData.MarketOpenPrePostOrder = new Dictionary<eTradeMode, List<PrePostDetail>>();
- }
- tradeCacheData.MarketOpenPrePostOrder[marketType] = orders as List<PrePostDetail>;
- tradeCacheData.OpenPrePostOrderFlag = true;
- }
- else if (typeof(T).Equals(typeof(OrderDetail)))
- {
- if (tradeCacheData.MarketOrderDetails == null)
- {
- tradeCacheData.MarketOrderDetails = new Dictionary<eTradeMode, List<OrderDetail>>();
- }
- tradeCacheData.MarketOrderDetails[marketType] = orders as List<OrderDetail>;
- tradeCacheData.LimitOrderFlag = true;
- }
- }
- /// <summary>
- /// 从缓存中取相应的单据
- /// </summary>
- /// <typeparam name="T">单据类型</typeparam>
- /// <param name="account"></param>
- /// <returns></returns>
- public static Dictionary<eTradeMode, List<T>> GetMarketCommonOrder<T>(TradeAccount account)
- {
- if (CurrentTradeAccount == null) return null;
- var tradeCacheData = GetTradeCacheData(account.AccountId);
- if (typeof(T).Equals(typeof(CloseOrder)))
- {
- return tradeCacheData.MarketCloseOrders as Dictionary<eTradeMode, List<T>>;
- }
- else if (typeof(T).Equals(typeof(OrderDetail)))
- {
- return tradeCacheData.MarketOrderDetails as Dictionary<eTradeMode, List<T>>;
- }
- else if (typeof(T).Equals(typeof(CompleteOrder)))
- {
- return tradeCacheData.MarketOpenCompletes as Dictionary<eTradeMode, List<T>>;
- }
- else if (typeof(T).Equals(typeof(PrePostDetail)))
- {
- return tradeCacheData.MarketOpenPrePostOrder as Dictionary<eTradeMode, List<T>>;
- }
- return null;
- }
- /// <summary>
- /// 更新缓存的单据
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="tradeAccount">账号</param>
- /// <param name="holdDetails"></param>
- public static void UpdateCacheOrder<T>(List<T> orders)
- {
- if (CurrentTradeAccount == null) return;
- var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.FundsAccountId);
- if (orders is List<HoldingOrder>)
- {
- tradeCacheData.OpenOrders = new ObservableCollection<HoldingOrder>(orders as List<HoldingOrder>);
- tradeCacheData.HoldOrderFlag = true;
- }
- else if (orders is List<OrderDetail>)
- {
- tradeCacheData.LimitOrders = new ObservableCollection<OrderDetail>(orders as List<OrderDetail>);
- tradeCacheData.LimitOrderFlag = true;
- }
- else if (orders is List<CloseOrder>)
- {
- tradeCacheData.CloseOrders = new ObservableCollection<CloseOrder>(orders as List<CloseOrder>);
- tradeCacheData.CloseOrderFlag = true;
- }
- else if (orders is List<HoldingSummary>)
- {
- tradeCacheData.HoldingSummaries = new ObservableCollection<HoldingSummary>(orders as List<HoldingSummary>);
- //配置参数赋值到头寸记录
- foreach (var holdingSummary in tradeCacheData.HoldingSummaries)
- {
- holdingSummary.ShowCloseOrder = ApplicationParameter.ShowCloseOrder;
- }
- tradeCacheData.HoldingSummaryFlag = true;
- }
- else if (orders is List<CompleteOrder>)
- {
- tradeCacheData.ComplateOrders = new ObservableCollection<CompleteOrder>(orders as List<CompleteOrder>);
- tradeCacheData.CompleteOrderFlag = true;
- }
- else if (orders is List<DeliveryEntrustOrder>)
- {
- tradeCacheData.DryEntrustOrders = new ObservableCollection<DeliveryEntrustOrder>(orders as List<DeliveryEntrustOrder>);
- tradeCacheData.DryEntrustOrdersFlag = true;
- }
- else if (orders is List<DryHoldDetail>)
- {
- tradeCacheData.DryHoldOrders = new ObservableCollection<DryHoldDetail>(orders as List<DryHoldDetail>);
- tradeCacheData.DryHoldFlag = true;
- }
- else if (orders is List<DeliveryWarehouse>)
- {
- tradeCacheData.DeliveryWarehouseOrders = new ObservableCollection<DeliveryWarehouse>(orders as List<DeliveryWarehouse>);
- tradeCacheData.DeliveryWarehouseFlag = true;
- }
- else if (orders is List<DeliveryMatchModel>)
- {
- tradeCacheData.DeliveryMatchOrders = new ObservableCollection<DeliveryMatchModel>(orders as List<DeliveryMatchModel>);
- tradeCacheData.DeliveryMatchFlag = true;
- }
- else if (orders is List<TakaDeliveryGoodsQueryModel>)
- {
- tradeCacheData.TakaDeliveryGoodsOrder = new ObservableCollection<TakaDeliveryGoodsQueryModel>(orders as List<TakaDeliveryGoodsQueryModel>);
- tradeCacheData.TakaDeliveryGoodsFlag = true;
- }
- else if (orders is List<TakaDeliveryGoodsApplyModel>)
- {
- tradeCacheData.TakaDeliveryGoodsApplyOrder = new ObservableCollection<TakaDeliveryGoodsApplyModel>(orders as List<TakaDeliveryGoodsApplyModel>);
- tradeCacheData.TakaDeliveryGoodsApplyFlag = true;
- }
- else if (orders is List<DeliveryConfig>)
- {
- tradeCacheData.DeliveryConfigInfo = new ObservableCollection<DeliveryConfig>(orders as List<DeliveryConfig>);
- tradeCacheData.DeliveryGoodsConfigFlag = true;
- }
- else if (orders is List<DeliveryRuleModel>)
- {
- tradeCacheData.DeliveryGoodsRuleValue = new ObservableCollection<DeliveryRuleModel>(orders as List<DeliveryRuleModel>);
- tradeCacheData.DeliveryGoodsRuleFlag = true;
- }
- else if (orders is List<DelistingOrder>)
- {
- tradeCacheData.DelistingOrders = new ObservableCollection<DelistingOrder>(orders as List<DelistingOrder>);
- tradeCacheData.DelistingOrderFlag = true;
- }
- else if (orders is List<RelationperformConfigModel>)
- {
- tradeCacheData.RelationperformConfig = new ObservableCollection<RelationperformConfigModel>(orders as List<RelationperformConfigModel>);
- tradeCacheData.RelationperformConfigFlag = true;
- }
- }
- /// <summary>
- /// 获取缓存的单据内容
- /// </summary>
- /// <typeparam name="T">单据类型</typeparam>
- /// <returns></returns>
- public static ObservableCollection<T> GetCacheOrders<T>()
- {
- var orders = new ObservableCollection<T>();
- if (CurrentTradeAccount == null) return orders;
- var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.AccountId);
- if (orders is ObservableCollection<HoldingOrder>)
- {
- return tradeCacheData.OpenOrders as ObservableCollection<T>;
- }
- else if (orders is ObservableCollection<OrderDetail>)
- {
- return tradeCacheData.LimitOrders as ObservableCollection<T>;
- }
- else if (orders is ObservableCollection<CloseOrder>)
- {
- return tradeCacheData.CloseOrders as ObservableCollection<T>;
- }
- else if (orders is ObservableCollection<HoldingSummary>)
- {
- return tradeCacheData.HoldingSummaries as ObservableCollection<T>;
- }
- else if (orders is ObservableCollection<CompleteOrder>)
- {
- return tradeCacheData.ComplateOrders as ObservableCollection<T>;
- }
- else if (orders is ObservableCollection<DeliveryEntrustOrder>)
- {
- return tradeCacheData.DryEntrustOrders as ObservableCollection<T>;
-
- }
- else if (orders is ObservableCollection<DryHoldDetail>)
- {
- return tradeCacheData.DryHoldOrders as ObservableCollection<T>;
- }
- else if (orders is ObservableCollection<DelistingOrder>)
- {
- return tradeCacheData.DelistingOrders as ObservableCollection<T>;;
-
- }
- return orders;
- }
- /// <summary>
- /// 加载预警信息
- /// </summary>
- /// <param name="tradeAccount"></param>
- public static void LoadWarningOrders(TradeAccount tradeAccount)
- {
- if (tradeAccount != null)
- {
- var tradeCacheData = GetTradeCacheData(tradeAccount.AccountId);
- if (tradeCacheData != null)
- {
- var warningService = SimpleIoc.Default.GetInstance<IWarningService>();
- try
- {
- tradeCacheData.WarningOrders = warningService.GetWarnings(tradeAccount);
- }
- catch (Exception ex)
- {
- LogHelper.WriteError(typeof(UserManager), ex.Message);
- }
- }
- }
- }
- /// <summary>
- /// 保存预警信息
- /// </summary>
- /// <param name="tradeAccount"></param>
- /// <param name="warningOrders"></param>
- public static void SaveWarningOrders(TradeAccount tradeAccount, ObservableCollection<Warning> warningOrders)
- {
- if (tradeAccount != null)
- {
- var tradeCacheData = GetTradeCacheData(tradeAccount.AccountId);
- if (tradeCacheData != null)
- {
- tradeCacheData.WarningOrders = warningOrders;
- }
- }
- }
- public static ObservableCollection<Warning> GetWarningOrders(TradeAccount tradeAccount)
- {
- if (tradeAccount != null)
- {
- var tradeCacheData = GetTradeCacheData(tradeAccount.AccountId);
- if (tradeCacheData != null)
- return tradeCacheData.WarningOrders;
- }
- return null;
- }
- /// <summary>
- /// Gets the trade data.
- /// </summary>
- /// <param name="tradeCode">The trade code.</param>
- /// <returns>TradeData.</returns>
- public static TradeCacheData GetTradeCacheData(ulong accountId)
- {
- var tradeCacheData = _tradeCacheDataList.FirstOrDefault(z => z.AccountId == accountId);
- if (tradeCacheData == null)
- {
- tradeCacheData = new TradeCacheData(accountId);
- _tradeCacheDataList.Add(tradeCacheData);
- }
- return tradeCacheData;
- }
- /// <summary>
- /// 从缓存中读取持仓单信息
- /// </summary>
- /// <param name="tradeAccount">信息</param>
- /// <returns></returns>
- public static ObservableCollection<HoldingOrder> CacheOpenOrders(TradeAccount tradeAccount)
- {
- if (tradeAccount == null) return null;
- var tradeCacheData = GetTradeCacheData(tradeAccount.AccountId);
- if (tradeCacheData == null) return null;
- return tradeCacheData.OpenOrders;
- }
- #endregion
- #region 用户配置(行情列表抬头\布局) - 采用JSON存储
- private static string _userConfigFile
- {
- get
- {
- return Path.Combine(UserDataFolder, "UserConfig"+ LocalDataExtensionName);
- }
- }
- /// <summary>
- /// 获取默认布局
- /// </summary>
- /// <returns>UserLayout.</returns>
- private static UserLayout GetDefaultLayout()
- {
- ////返回布局默认值
- return new UserLayout()
- {
- QuoteGridRowLengthUnitType = GridUnitType.Star,
- QuoteGridRowLengthValue = 3,
- QuoteGridRowMinHeight = 300,
- QueryGridRowLengthUnitType = GridUnitType.Pixel,
- QueryGridRowLengthValue = 280,
- // QueryGridRowMinHeight = 200
- };
- }
- /// <summary>
- /// 获取当前主题
- /// </summary>
- /// <returns>ColorStyle.</returns>
- public static ColorStyle GetCurrentColorStyle()
- {
- var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
- var userConfig = userConfigs.FirstOrDefault();
- ////本地配置文件为空或是当前主题为None,则返回服务端默认配置
- if (null == userConfig || userConfig.CurrentColorStyle == ColorStyle.None)
- {
- var defaultColor = 1;
- if (!int.TryParse(string.IsNullOrWhiteSpace(ApplicationParameter.ColorStyle) ? "4" : ApplicationParameter.ColorStyle, out defaultColor)) defaultColor = 4;
- ////目前实现1,4,5三种主题
- if (Enum.IsDefined(typeof(ColorStyle), defaultColor)) return (ColorStyle)defaultColor;
- return ColorStyle.CoolBlack;
- };
- return userConfig.CurrentColorStyle;
- }
- /// <summary>
- /// 保存当前主题
- /// </summary>
- /// <param name="style">The style.</param>
- public static void SaveCurrentColorStyle(ColorStyle style)
- {
- var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
- var userConfig = userConfigs.FirstOrDefault() ?? new UserConfig();
- userConfig.CurrentColorStyle = style;
- //只存一个集合,每次覆盖
- JsonHelper.SaveData<UserConfig>(_userConfigFile, new List<UserConfig>() { userConfig }, null, true);
- }
- /// <summary>
- /// 获取当前布局
- /// </summary>
- /// <returns>List{QuoteListHeader}.</returns>
- public static LayoutStyle GetCurrentLayoutStyle()
- {
- if (!UserManager.IsAccountLogin) ////游客登录
- {
- return LayoutStyle.Horizontal;
- }
- var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
- var userConfig = userConfigs.FirstOrDefault();
- ////本地配置文件为空或是当前布局为None,则返回服务端默认配置
- if (null == userConfig || userConfig.CurrentLayoutStyle == LayoutStyle.None)
- {
- var defaultLayout = 1;
- if (!int.TryParse(string.IsNullOrWhiteSpace(ApplicationParameter.LayoutStyle) ? "1" : ApplicationParameter.LayoutStyle, out defaultLayout)) defaultLayout = 1;
- ////目前实现1,2两种布局
- if (Enum.IsDefined(typeof(LayoutStyle), defaultLayout)) return (LayoutStyle)defaultLayout;
- return LayoutStyle.Vertical;
- };
- return userConfig.CurrentLayoutStyle;
- }
- /// <summary>
- /// 保存当前布局
- /// </summary>
- /// <param name="style">The style.</param>
- public static void SaveCurrentLayoutStyle(LayoutStyle style)
- {
- var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
- var userConfig = userConfigs.FirstOrDefault() ?? new UserConfig();
- userConfig.CurrentLayoutStyle = style;
- //只存一个集合,每次覆盖
- JsonHelper.SaveData<UserConfig>(_userConfigFile, new List<UserConfig>() { userConfig }, null, true);
- }
- /// <summary>
- /// 获取布局
- /// </summary>
- /// <param name="style">The style.</param>
- /// <returns>List{QuoteListHeader}.</returns>
- public static UserLayout GetLayoutStyle(LayoutStyle style)
- {
- if (style == LayoutStyle.None) return null;
- var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
- var userConfig = userConfigs.FirstOrDefault();
- if (null == userConfig || null == userConfig.UserLayouts || !userConfig.UserLayouts.ContainsKey(style) || null == userConfig.UserLayouts[style])
- {
- var defaultLayout = GetDefaultLayout();
- return defaultLayout;
- };
- return userConfig.UserLayouts[style];
- }
- /// <summary>
- /// 保存布局
- /// </summary>
- /// <param name="style">The style.</param>
- /// <param name="userLayout">The user layout.</param>
- public static void SaveLayoutStyle(LayoutStyle style, UserLayout userLayout)
- {
- var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
- var userConfig = userConfigs.FirstOrDefault() ?? new UserConfig();
- if (userConfig.UserLayouts == null) userConfig.UserLayouts = new Dictionary<LayoutStyle, UserLayout>();
- userConfig.UserLayouts[style] = userLayout;
- //只存一个集合,每次覆盖
- JsonHelper.SaveData<UserConfig>(_userConfigFile, new List<UserConfig>() { userConfig }, null, true);
- }
- /// <summary>
- /// 获取抬头
- /// </summary>
- /// <param name="type">The type.</param>
- /// <returns>List{QuoteListHeader}.</returns>
- public static List<QuoteListHeader> GetQuoteListHeaders(QuoteListHeaderType type)
- {
- var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
- var userConfig = userConfigs.FirstOrDefault();
- if (null == userConfig || null == userConfig.QuoteGridHeaders || !userConfig.QuoteGridHeaders.ContainsKey(type) || null == userConfig.QuoteGridHeaders[type]) return null;
- if (IsUpdateHeaderVer(type, userConfig.ConfigHeadersVer)) ////保存的版本是否要更新
- {
- return null; ////返回空则更新
- }
- return userConfig.QuoteGridHeaders[type];
- }
- /// <summary>
- /// 保存抬头
- /// </summary>
- /// <param name="type">The type.</param>
- /// <param name="headers">The headers.</param>
- public static void SaveQuoteListHeaders(QuoteListHeaderType type, List<QuoteListHeader> headers)
- {
- object lockObject = new object();
- bool lockToken = false;
- try
- {
- Monitor.Enter(lockObject, ref lockToken);
- var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
- var userConfig = userConfigs.FirstOrDefault() ?? new UserConfig();
- if (userConfig.QuoteGridHeaders == null)
- userConfig.QuoteGridHeaders = new Dictionary<QuoteListHeaderType, List<QuoteListHeader>>();
- userConfig.QuoteGridHeaders[type] = headers;
- userConfig.ConfigHeadersVer = SaveHeaderVer(type, userConfig.ConfigHeadersVer); ////保存版本号
- //只存一个集合,每次覆盖
- JsonHelper.SaveData<UserConfig>(_userConfigFile, new List<UserConfig>() {userConfig}, null, true);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- }
- finally
- {
- if (lockToken)
- {
- Monitor.Exit(lockObject);
- }
- }
- }
- /// <summary>
- /// 是否更新抬头
- /// </summary>
- /// <param name="type"></param>
- /// <param name="localVerString"></param>
- /// <returns></returns>
- private static bool IsUpdateHeaderVer(QuoteListHeaderType type, string localVerString)
- {
- if (string.IsNullOrWhiteSpace(ApplicationParameter.ConfigHeadersVer))
- {
- return false; ////没有版本更新
- }
- var updateVer = new Version(ApplicationParameter.ConfigHeadersVer);
- if (string.IsNullOrWhiteSpace(localVerString))
- {
- return true; ////版本号全是0则不更新
- }
- var locolVer = new Version(localVerString);
- switch (type)
- {
- case QuoteListHeaderType.OTC:
- case QuoteListHeaderType.Channel:
- return updateVer.Minor > locolVer.Minor;
- case QuoteListHeaderType.Bid:
- return updateVer.Build > locolVer.Build;
- case QuoteListHeaderType.BidMarket:
- return updateVer.Revision > locolVer.Revision; ////混合版
- case QuoteListHeaderType.MySelected:
- return updateVer.Major > locolVer.Major; ////自选版本
- }
- return false;
- }
- ///
- private static string SaveHeaderVer(QuoteListHeaderType type, string localVerString)
- {
- if (string.IsNullOrWhiteSpace(ApplicationParameter.ConfigHeadersVer))
- {
- return string.Empty; ////没有版本更新
- }
- var updateVer = new Version(ApplicationParameter.ConfigHeadersVer);
- if (string.IsNullOrWhiteSpace(localVerString))
- {
- localVerString = "0.0.0.0";
- }
- var locolVer = new Version(localVerString);
- switch (type)
- {
- case QuoteListHeaderType.OTC:
- case QuoteListHeaderType.Channel:
- return string.Format("{0}.{1}.{2}.{3}", locolVer.Major, updateVer.Minor, locolVer.Build,
- locolVer.Revision);
- break;
- case QuoteListHeaderType.Bid:
- return string.Format("{0}.{1}.{2}.{3}", locolVer.Major, locolVer.Minor, updateVer.Build,
- locolVer.Revision);
- break;
- case QuoteListHeaderType.BidMarket:
- return string.Format("{0}.{1}.{2}.{3}", locolVer.Major, locolVer.Minor, locolVer.Build,
- updateVer.Revision);
- break;
- case QuoteListHeaderType.MySelected:
- return string.Format("{0}.{1}.{2}.{3}", updateVer.Major, locolVer.Minor, locolVer.Build,
- locolVer.Revision);
- break;
- }
- return string.Empty;
- }
- #endregion
- #region 是否为监控会员
- /// <summary>
- /// 是否为监控会员//如果返回false 角色按投资者处理
- /// </summary>
- /// <returns></returns>
- public static bool IsMonitorMember()
- {
- if (UserManager.CurrentTradeAccount != null)// && UserManager.CurrentTradeAccount.FundsAccounts != null)
- {
- #region MTP2.0 注销
- //if (UserManager.CurrentTradeAccount.FundsAccounts.Any() && UserManager.CurrentTradeAccount.FundsAccounts[0].AreaRoles != 0)
- //{
- // return UserManager.CurrentTradeAccount.AccountType == eLoginUserType.LOGINUSERTYPE_TRADER &&
- // (UserManager.CurrentTradeAccount.FundsAccounts[0].AreaRoles &
- // ((int)eAreaRole.AREAROLE_MAKEMEMBER + (int)eAreaRole.AREAROLE_TOPMAKEMEMBER + (int)eAreaRole.AREAROLE_INDUSTRYMEMBER)) > 0; ////产业会员,二级做市会员,做市会员ww风控取信息
- //}
- #endregion
- return UserManager.CurrentTradeAccount.AccountType != eUserType.USERTYPE_INVESTOR;
- }
- else
- {
- return false;
- }
- }
- #endregion
- /// <summary>
- /// 当前账号的可用资金
- /// </summary>
- /// <returns></returns>
- public static decimal CurrentAccountUsableFunds()
- {
- var usableFunds = 0m;
- if (UserManager.CurrentTradeAccount.FundsAccounts != null &&
- UserManager.CurrentTradeAccount.FundsAccounts.Any())
- {
- usableFunds = UserManager.CurrentTradeAccount.FundsAccounts[0].AvailMargin; ////默认使用可用数量
- }
- if (UserManager.IsMonitorMember())
- {
- if (UserManager.CurrentTradeAccount.MemberAccountInfo != null) ////监控会员取风控的资金信息
- {
- usableFunds = UserManager.CurrentTradeAccount.MemberAccountInfo.AvailMargin;
- }
- }
-
- return usableFunds;
- }
- /// <summary>
- /// 获取可用资金BY AccountId
- /// </summary>
- /// <returns></returns>
- public static decimal GetCurrentUsableFundsByAccountId(ulong AccountId)
- {
- var usableFunds = 0m;
- if (CacheManager.CacheTAAcountList !=null && CacheManager.CacheTAAcountList.Count > 0)
- {
- usableFunds = CacheManager.CacheTAAcountList.Where(x => x.FundsAccounts.AccountId == AccountId).Select(x=>x.FundsAccounts.AvailMargin).FirstOrDefault(); ////默认使用可用数量
- }
- return usableFunds;
- }
- }
- }
|