UserManager.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. using System.Collections;
  2. using GalaSoft.MvvmLight.Ioc;
  3. using Muchinfo.MTPClient.Data;
  4. using Muchinfo.MTPClient.Data.Enums;
  5. using Muchinfo.MTPClient.Data.Model;
  6. using Muchinfo.MTPClient.Data.Model.Account;
  7. using Muchinfo.MTPClient.Data.Model.Config;
  8. using Muchinfo.MTPClient.Data.Model.Delivery;
  9. using Muchinfo.MTPClient.Data.Model.Listing;
  10. using Muchinfo.MTPClient.Data.Model.Sale;
  11. using Muchinfo.MTPClient.Infrastructure.Enums;
  12. using Muchinfo.MTPClient.Infrastructure.Helpers;
  13. using Muchinfo.MTPClient.IService;
  14. using Muchinfo.PC.Common.Bosn;
  15. using Muchinfo.PC.Common.Helpers;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Collections.ObjectModel;
  19. using System.IO;
  20. using System.Linq;
  21. using System.Threading;
  22. using System.Windows;
  23. using Muchinfo.MTPClient.Data.DTO;
  24. using Muchinfo.MTPClient.Infrastructure.Cache;
  25. namespace Muchinfo.MTPClient.Infrastructure.Utilities
  26. {
  27. /// <summary>
  28. /// UserManager类
  29. /// </summary>
  30. public class UserManager
  31. {
  32. #region 字段
  33. private static object _lockObject = new object();
  34. /// <summary>
  35. /// 本地数据文件后缀名
  36. /// </summary>
  37. public const string LocalDataExtensionName = ".dll";
  38. #endregion
  39. #region 公有属性
  40. /// <summary>
  41. /// 当前列表显示的商品
  42. /// </summary>
  43. /// <value>The current goods i ds.</value>
  44. public static List<int> CurrentGoodsIds { get; set; }
  45. /// <summary>
  46. /// 交易面板选中的当前Id
  47. /// </summary>
  48. public static int CurrentGoodsId { get; set; }
  49. #region 当前商品是否有商品
  50. private static bool _currentGroupHasGoods=false;
  51. /// <summary>
  52. /// 当前商品是否有商品
  53. /// </summary>
  54. public static bool CurrentGroupHasGoods
  55. {
  56. get { return _currentGroupHasGoods; }
  57. set { _currentGroupHasGoods = value; }
  58. }
  59. #endregion
  60. /// <summary>
  61. /// 主界面是否被解锁 //todo:什么用?
  62. /// </summary>
  63. public static bool MainPanelIsUnLock
  64. {
  65. get;
  66. set;
  67. }
  68. /// <summary>
  69. /// 行情窗口类型-List/Board
  70. /// </summary>
  71. /// <value>The type of the quotation.</value>
  72. public static TabContetMode QuotationWindowType { get; set; }
  73. /// <summary>
  74. /// 当前用户
  75. /// </summary>
  76. public static UserInfo CurrentInfo;
  77. #endregion
  78. #region 用户缓存信息
  79. private static List<TradeAccount> _tradeAccounts = new List<TradeAccount>();
  80. /// <summary>
  81. /// 登录的交易账号集合
  82. /// </summary>
  83. public static List<TradeAccount> TradeAccounts
  84. {
  85. get
  86. {
  87. return _tradeAccounts;
  88. }
  89. }
  90. private static TradeAccount _currentTradeAccount;
  91. /// <summary>
  92. /// 当前选中的交易账号
  93. /// </summary>
  94. public static TradeAccount CurrentTradeAccount
  95. {
  96. get { return _currentTradeAccount; }
  97. set
  98. {
  99. _currentTradeAccount = value;
  100. //当时账号更新的交易Header需更新
  101. MessengerHelper.DefaultSend(_currentTradeAccount, MessengerTokens.UpdateTradeAccount);
  102. }
  103. }
  104. /// <summary>
  105. /// 是否为游客登录
  106. /// </summary>
  107. public static bool IsAccountLogin
  108. {
  109. get;
  110. set;
  111. }
  112. /// <summary>
  113. /// 关注商品列表
  114. /// </summary>
  115. private static List<QuoteGoods> _foucusGoodsList;
  116. /// <summary>
  117. /// 游客登录查询的商品
  118. /// </summary>
  119. public static List<SaleGoods> BrownSaleGoodses { get; set; }
  120. /// <summary>
  121. /// 用户名可操作的菜单
  122. /// </summary>
  123. public static List<SysMenuItem> AccessFuncMenus { get; set; }
  124. /// <summary>
  125. /// 本地固定菜单
  126. /// </summary>
  127. public static List<SysMenuItem> FixedFuncMenus { get; set; }
  128. /// <summary>
  129. /// 缓冲本地菜单
  130. /// </summary>
  131. public static Dictionary<MenuCommandType, string> LocalFuncMenus { get; set; }
  132. /// <summary>
  133. /// 获取菜单类型
  134. /// </summary>
  135. /// <param name="FuncMenuCode"></param>
  136. /// <returns></returns>
  137. public static MenuCommandType GetMenuCommandType(string FuncMenuCode)
  138. {
  139. return UserManager.LocalFuncMenus.Where(x => x.Value == FuncMenuCode).Select(x => x.Key).FirstOrDefault();
  140. }
  141. /// <summary>
  142. /// 获取菜单对应ResourceCode
  143. /// </summary>
  144. /// <returns></returns>
  145. public static string GetMenuCommandValue(MenuCommandType typeKey)
  146. {
  147. return UserManager.LocalFuncMenus.Where(x => x.Key == typeKey).Select(x => x.Value).FirstOrDefault();
  148. }
  149. private static List<SysMenuItem> _tradeMenuItems=new List<SysMenuItem>();
  150. /// <summary>
  151. /// 交易菜单
  152. /// </summary>
  153. public static List<SysMenuItem> TradeFuncMenus
  154. {
  155. get
  156. {
  157. return _tradeMenuItems;
  158. }
  159. set
  160. {
  161. _tradeMenuItems=value;
  162. }
  163. }
  164. /// <summary>
  165. /// Gets or sets the trade rate temporary configuration.
  166. /// </summary>
  167. /// <value>The trade rate temporary configuration.</value>
  168. public static List<TradeRateTmpConfig> TradeRateTmpConfigs { get; set; }
  169. /// <summary>
  170. /// 外部汇率模版配置表
  171. /// </summary>
  172. /// <value>The trade rate temporary configuration.</value>
  173. public static List<TradeRateTmpConfig> ExtenalTradeRateTmpConfigs { get; set; }
  174. /// <summary>
  175. /// 资金账户汇率模版配置
  176. /// </summary>
  177. /// <value>The trade rate temporary configuration.</value>
  178. public static List<TradeRateTmpConfig> TaAccountExchangeRateConfigs { get; set; }
  179. #endregion
  180. #region 用户数据路径
  181. /// <summary>
  182. /// 系统配置路径
  183. /// </summary>
  184. public static string SysConfigFolderPath
  185. {
  186. get
  187. {
  188. return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config");
  189. }
  190. }
  191. /// <summary>
  192. /// 获取登录用户或游客配置的文件夹
  193. /// </summary>
  194. /// <value>The user settings folder.</value>
  195. public static string UserSettingsFolder
  196. {
  197. get
  198. {
  199. var usersFolder = UsersSettingsFolder;
  200. var folder = CurrentTradeAccount == null || CurrentTradeAccount.LoginID==uint.MaxValue ?
  201. Path.Combine(usersFolder, "Guest") : Path.Combine(usersFolder, CurrentTradeAccount.LoginID.ToString());
  202. if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
  203. return folder;
  204. }
  205. }
  206. /// <summary>
  207. /// 获取程序目录的"Users"文件夹
  208. /// </summary>
  209. public static string UsersSettingsFolder
  210. {
  211. get
  212. {
  213. var userConfigRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Users");
  214. if (!Directory.Exists(userConfigRoot)) Directory.CreateDirectory(userConfigRoot);
  215. return userConfigRoot;
  216. }
  217. }
  218. /// <summary>
  219. /// 用户基本数据文件夹
  220. /// </summary>
  221. public static string UserDataFolder
  222. {
  223. get
  224. {
  225. var usersFolder = UserSettingsFolder;
  226. var folder = Path.Combine(usersFolder, "Data");
  227. if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
  228. return folder;
  229. }
  230. }
  231. #endregion
  232. #region 登录账号读写 - BSON存储
  233. /// <summary>
  234. /// 获取登录的交易账号集合
  235. /// </summary>
  236. /// <returns>List{LoginAccount}.</returns>
  237. public static List<UserInfo> GetTradeAccounts()
  238. {
  239. var filePath = Path.Combine(UsersSettingsFolder, "LoginAccounts" + LocalDataExtensionName);
  240. if (!File.Exists(filePath)) return null;
  241. try
  242. {
  243. var result = BsonHelper.LoadData<UserInfo>(filePath);
  244. if (result == null||!result.Any()) return null;
  245. return result.OrderByDescending(z => z.LoginTime).ToList();
  246. }
  247. catch (Exception ex)
  248. {
  249. LogHelper.WriteError(typeof(UserManager), "get LoginAccount Error:" + ex.ToString());
  250. File.Delete(filePath);
  251. return null;
  252. }
  253. }
  254. /// <summary>
  255. /// 获取用户保存的配置
  256. /// </summary>
  257. /// <param name="userName"></param>
  258. /// <returns></returns>
  259. public static UserInfo GetTradeAccount(string userName)
  260. {
  261. var filePath = Path.Combine(UsersSettingsFolder, "LoginAccounts" + LocalDataExtensionName);
  262. if (!File.Exists(filePath)) return null;
  263. try
  264. {
  265. var result = BsonHelper.LoadData<UserInfo>(filePath);
  266. if (result == null || !result.Any()) return null;
  267. var dicResult = result.ToDictionary((item) => item.UserName);
  268. if (dicResult.ContainsKey(userName))
  269. {
  270. return dicResult[userName];
  271. }
  272. }
  273. catch (Exception)
  274. {
  275. File.Delete(filePath);
  276. return null;
  277. }
  278. return null;
  279. }
  280. /// <summary>
  281. /// 获取当前用户名
  282. /// </summary>
  283. /// <returns></returns>
  284. public static UserInfo GetCurrentUserInfo()
  285. {
  286. if (CurrentInfo != null)
  287. return CurrentInfo;
  288. else
  289. {
  290. return GetTradeAccount(UserManager.CurrentTradeAccount.LoginID.ToString());
  291. }
  292. }
  293. /// <summary>
  294. /// 保存交易账号
  295. /// </summary>
  296. /// <returns>List{LoginAccount}.</returns>
  297. public static void SaveTradeAccount(UserInfo tradeAccount)
  298. {
  299. if (tradeAccount == null) return;
  300. var filePath = Path.Combine(UsersSettingsFolder, "LoginAccounts" + LocalDataExtensionName);
  301. var accountHash = new Dictionary<string,UserInfo>();
  302. try
  303. {
  304. if (File.Exists(filePath))
  305. {
  306. var listHash = BsonHelper.LoadData<UserInfo>(filePath) as List<UserInfo>;
  307. if (listHash != null && listHash.Any())
  308. {
  309. accountHash = listHash.ToDictionary((item)=>item.UserName);
  310. }
  311. }
  312. accountHash[tradeAccount.UserName] = tradeAccount;
  313. BsonHelper.SaveData<UserInfo>(filePath, accountHash.Values.ToList(), null, true);
  314. CurrentInfo = tradeAccount;
  315. }
  316. catch (Exception ex)
  317. {
  318. LogHelper.WriteError(typeof(UserManager), "Save LoginAccount Error:" + ex.ToString());
  319. }
  320. }
  321. /// <summary>
  322. /// 登录成功保存内容
  323. /// </summary>
  324. public static void LoginedSave(UserInfo tradeAccount, bool IsSaveAccount)
  325. {
  326. UserInfo info = tradeAccount;
  327. info.LoginTime = DateTime.Now;
  328. info.RememberAccount = IsSaveAccount;
  329. info.LoginType = 0;
  330. info.MemberId = UserManager.CurrentTradeAccount.MemberAreaId;
  331. SaveTradeAccount(info);
  332. //记住本次登录用户
  333. var lastLoginUser = new LastLoginUser();
  334. lastLoginUser.MemberId = info.MemberId + string.Empty;
  335. lastLoginUser.MemberPath = Path.Combine(UserManager.SysConfigFolderPath, info.MemberId + string.Empty);
  336. SaveLastLoginUser(lastLoginUser);
  337. //CurrentUser = info;
  338. }
  339. /// <summary>
  340. /// 读取最一个用户登录的账号所属会员
  341. /// </summary>
  342. /// <returns></returns>
  343. public static LastLoginUser GetLastLoginUser()
  344. {
  345. string filePath = Path.Combine(UsersSettingsFolder, "AppDefault" + LocalDataExtensionName);
  346. var result = BsonHelper.LoadData<LastLoginUser>(filePath);
  347. if (result == null || !result.Any()) return null;
  348. return result.FirstOrDefault();
  349. }
  350. /// <summary>
  351. /// /保存最后一个用户的所属会员
  352. /// </summary>
  353. /// <param name="lastLoginUser">会员代码</param>
  354. public static void SaveLastLoginUser(LastLoginUser lastLoginUser)
  355. {
  356. string filePath = Path.Combine(UsersSettingsFolder, "AppDefault" + LocalDataExtensionName);
  357. BsonHelper.SaveData<LastLoginUser>(filePath, lastLoginUser, null, true);
  358. }
  359. #endregion
  360. #region 用户单据
  361. private static List<TradeCacheData> _tradeCacheDataList = new List<TradeCacheData>();
  362. /// <summary>
  363. /// 初始化缓存数据标志
  364. /// </summary>
  365. /// <param name="tradeAccount">The trade account.</param>
  366. public static void InitializeTradeCacheData(TradeAccount tradeAccount)
  367. {
  368. if (tradeAccount == null) return;
  369. var tradeCacheData = GetTradeCacheData(tradeAccount.AccountId);
  370. if (tradeCacheData == null) return;
  371. tradeCacheData.HoldOrderFlag = false;
  372. tradeCacheData.CloseOrderFlag = false;
  373. tradeCacheData.OpenCompleteFlag = false;
  374. tradeCacheData.CompleteOrderFlag = false;
  375. tradeCacheData.LimitOrderFlag = false;
  376. tradeCacheData.HoldingSummaryFlag = false;
  377. tradeCacheData.DeliveryOrderFlag = false;
  378. tradeCacheData.OpenPrePostOrderFlag = false;
  379. }
  380. public static Dictionary<eTradeMode, List<HoldingOrder>> GetMarketHoldOrders(TradeAccount account)
  381. {
  382. if (CurrentTradeAccount == null)
  383. {
  384. return new Dictionary<eTradeMode, List<HoldingOrder>>();
  385. }
  386. var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.AccountId);
  387. return tradeCacheData.MarketHoldDetails;
  388. }
  389. public static Dictionary<eTradeMode, List<HoldingSummary>> GetMarketHoldSummaries(TradeAccount account)
  390. {
  391. if (CurrentTradeAccount == null)
  392. {
  393. return new Dictionary<eTradeMode, List<HoldingSummary>>();
  394. }
  395. var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.AccountId);
  396. return tradeCacheData.MarketHoldSummaries;
  397. }
  398. /// <summary>
  399. /// Determines whether [has goods hold summary] [the specified account identifier].
  400. /// </summary>
  401. /// <param name="accountId">The account identifier.</param>
  402. /// <param name="buyOrSell">The buy or sell.</param>
  403. /// <param name="goodsId">The goods identifier.</param>
  404. /// <returns><c>true</c> if [has goods hold summary] [the specified account identifier]; otherwise, <c>false</c>.</returns>
  405. public static bool HasGoodsHoldSummary(ulong accountId, Direction buyOrSell, uint goodsId )
  406. {
  407. if (CurrentTradeAccount == null)
  408. {
  409. return false;
  410. }
  411. var tradeCacheData = GetTradeCacheData(accountId);
  412. if (tradeCacheData == null || tradeCacheData.HoldingSummaries == null ||
  413. !tradeCacheData.HoldingSummaries.Any()) return false;
  414. return tradeCacheData.HoldingSummaries.Any(z => z.GoodsId == goodsId && z.Direction == buyOrSell);
  415. }
  416. public static List<HoldingSummary> GetHoldSummaries(ulong accountId)
  417. {
  418. var tradeCacheData = GetTradeCacheData(accountId);
  419. return tradeCacheData.HoldingSummaries == null ? null : tradeCacheData.HoldingSummaries.ToList();
  420. }
  421. /// <summary>
  422. /// 按市场区分单据数据
  423. /// </summary>
  424. /// <typeparam name="T"></typeparam>
  425. /// <param name="orders"></param>
  426. public static void UpdateMarketHoldOrders<T>(Dictionary<eTradeMode, List<T>> orders)
  427. {
  428. if (CurrentTradeAccount == null) return;
  429. var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.AccountId);
  430. if (typeof(T).Equals(typeof(HoldingOrder)))
  431. {
  432. tradeCacheData.MarketHoldDetails = orders as Dictionary<eTradeMode, List<HoldingOrder>>;
  433. tradeCacheData.HoldOrderFlag = true;
  434. }
  435. else if (typeof(T).Equals(typeof(HoldingSummary)))
  436. {
  437. tradeCacheData.MarketHoldSummaries = orders as Dictionary<eTradeMode, List<HoldingSummary>>;
  438. tradeCacheData.HoldingSummaryFlag = true;
  439. }
  440. }
  441. /// <summary>
  442. /// 按市场类型更新单据信息
  443. /// </summary>
  444. /// <typeparam name="T">单据类型</typeparam>
  445. /// <param name="marketType">市场类型</param>
  446. /// <param name="orders">单据列表</param>
  447. public static void UpdateMarketOrders<T>(eTradeMode marketType, List<T> orders)
  448. {
  449. if (CurrentTradeAccount == null) return;
  450. var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.AccountId);
  451. if (typeof(T).Equals(typeof(CloseOrder)))
  452. {
  453. if (tradeCacheData.MarketCloseOrders == null)
  454. {
  455. tradeCacheData.MarketCloseOrders = new Dictionary<eTradeMode, List<CloseOrder>>();
  456. }
  457. tradeCacheData.MarketCloseOrders[marketType] = orders as List<CloseOrder>;
  458. tradeCacheData.CloseOrderFlag = true;
  459. }
  460. else if (typeof(T).Equals(typeof(CompleteOrder)))
  461. {
  462. if (tradeCacheData.MarketOpenCompletes == null)
  463. {
  464. tradeCacheData.MarketOpenCompletes = new Dictionary<eTradeMode, List<CompleteOrder>>();
  465. }
  466. tradeCacheData.MarketOpenCompletes[marketType] = orders as List<CompleteOrder>;
  467. tradeCacheData.OpenCompleteFlag = true;
  468. }
  469. else if (typeof(T).Equals(typeof(PrePostDetail)))//预埋单
  470. {
  471. if (tradeCacheData.MarketOpenPrePostOrder == null)
  472. {
  473. tradeCacheData.MarketOpenPrePostOrder = new Dictionary<eTradeMode, List<PrePostDetail>>();
  474. }
  475. tradeCacheData.MarketOpenPrePostOrder[marketType] = orders as List<PrePostDetail>;
  476. tradeCacheData.OpenPrePostOrderFlag = true;
  477. }
  478. else if (typeof(T).Equals(typeof(OrderDetail)))
  479. {
  480. if (tradeCacheData.MarketOrderDetails == null)
  481. {
  482. tradeCacheData.MarketOrderDetails = new Dictionary<eTradeMode, List<OrderDetail>>();
  483. }
  484. tradeCacheData.MarketOrderDetails[marketType] = orders as List<OrderDetail>;
  485. tradeCacheData.LimitOrderFlag = true;
  486. }
  487. }
  488. /// <summary>
  489. /// 从缓存中取相应的单据
  490. /// </summary>
  491. /// <typeparam name="T">单据类型</typeparam>
  492. /// <param name="account"></param>
  493. /// <returns></returns>
  494. public static Dictionary<eTradeMode, List<T>> GetMarketCommonOrder<T>(TradeAccount account)
  495. {
  496. if (CurrentTradeAccount == null) return null;
  497. var tradeCacheData = GetTradeCacheData(account.AccountId);
  498. if (typeof(T).Equals(typeof(CloseOrder)))
  499. {
  500. return tradeCacheData.MarketCloseOrders as Dictionary<eTradeMode, List<T>>;
  501. }
  502. else if (typeof(T).Equals(typeof(OrderDetail)))
  503. {
  504. return tradeCacheData.MarketOrderDetails as Dictionary<eTradeMode, List<T>>;
  505. }
  506. else if (typeof(T).Equals(typeof(CompleteOrder)))
  507. {
  508. return tradeCacheData.MarketOpenCompletes as Dictionary<eTradeMode, List<T>>;
  509. }
  510. else if (typeof(T).Equals(typeof(PrePostDetail)))
  511. {
  512. return tradeCacheData.MarketOpenPrePostOrder as Dictionary<eTradeMode, List<T>>;
  513. }
  514. return null;
  515. }
  516. /// <summary>
  517. /// 更新缓存的单据
  518. /// </summary>
  519. /// <typeparam name="T">类型</typeparam>
  520. /// <param name="tradeAccount">账号</param>
  521. /// <param name="holdDetails"></param>
  522. public static void UpdateCacheOrder<T>(List<T> orders)
  523. {
  524. if (CurrentTradeAccount == null) return;
  525. var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.FundsAccountId);
  526. if (orders is List<HoldingOrder>)
  527. {
  528. tradeCacheData.OpenOrders = new ObservableCollection<HoldingOrder>(orders as List<HoldingOrder>);
  529. tradeCacheData.HoldOrderFlag = true;
  530. }
  531. else if (orders is List<OrderDetail>)
  532. {
  533. tradeCacheData.LimitOrders = new ObservableCollection<OrderDetail>(orders as List<OrderDetail>);
  534. tradeCacheData.LimitOrderFlag = true;
  535. }
  536. else if (orders is List<CloseOrder>)
  537. {
  538. tradeCacheData.CloseOrders = new ObservableCollection<CloseOrder>(orders as List<CloseOrder>);
  539. tradeCacheData.CloseOrderFlag = true;
  540. }
  541. else if (orders is List<HoldingSummary>)
  542. {
  543. tradeCacheData.HoldingSummaries = new ObservableCollection<HoldingSummary>(orders as List<HoldingSummary>);
  544. //配置参数赋值到头寸记录
  545. foreach (var holdingSummary in tradeCacheData.HoldingSummaries)
  546. {
  547. holdingSummary.ShowCloseOrder = ApplicationParameter.ShowCloseOrder;
  548. }
  549. tradeCacheData.HoldingSummaryFlag = true;
  550. }
  551. else if (orders is List<CompleteOrder>)
  552. {
  553. tradeCacheData.ComplateOrders = new ObservableCollection<CompleteOrder>(orders as List<CompleteOrder>);
  554. tradeCacheData.CompleteOrderFlag = true;
  555. }
  556. else if (orders is List<DeliveryEntrustOrder>)
  557. {
  558. tradeCacheData.DryEntrustOrders = new ObservableCollection<DeliveryEntrustOrder>(orders as List<DeliveryEntrustOrder>);
  559. tradeCacheData.DryEntrustOrdersFlag = true;
  560. }
  561. else if (orders is List<DryHoldDetail>)
  562. {
  563. tradeCacheData.DryHoldOrders = new ObservableCollection<DryHoldDetail>(orders as List<DryHoldDetail>);
  564. tradeCacheData.DryHoldFlag = true;
  565. }
  566. else if (orders is List<DeliveryWarehouse>)
  567. {
  568. tradeCacheData.DeliveryWarehouseOrders = new ObservableCollection<DeliveryWarehouse>(orders as List<DeliveryWarehouse>);
  569. tradeCacheData.DeliveryWarehouseFlag = true;
  570. }
  571. else if (orders is List<DeliveryMatchModel>)
  572. {
  573. tradeCacheData.DeliveryMatchOrders = new ObservableCollection<DeliveryMatchModel>(orders as List<DeliveryMatchModel>);
  574. tradeCacheData.DeliveryMatchFlag = true;
  575. }
  576. else if (orders is List<TakaDeliveryGoodsQueryModel>)
  577. {
  578. tradeCacheData.TakaDeliveryGoodsOrder = new ObservableCollection<TakaDeliveryGoodsQueryModel>(orders as List<TakaDeliveryGoodsQueryModel>);
  579. tradeCacheData.TakaDeliveryGoodsFlag = true;
  580. }
  581. else if (orders is List<TakaDeliveryGoodsApplyModel>)
  582. {
  583. tradeCacheData.TakaDeliveryGoodsApplyOrder = new ObservableCollection<TakaDeliveryGoodsApplyModel>(orders as List<TakaDeliveryGoodsApplyModel>);
  584. tradeCacheData.TakaDeliveryGoodsApplyFlag = true;
  585. }
  586. else if (orders is List<DeliveryConfig>)
  587. {
  588. tradeCacheData.DeliveryConfigInfo = new ObservableCollection<DeliveryConfig>(orders as List<DeliveryConfig>);
  589. tradeCacheData.DeliveryGoodsConfigFlag = true;
  590. }
  591. else if (orders is List<DeliveryRuleModel>)
  592. {
  593. tradeCacheData.DeliveryGoodsRuleValue = new ObservableCollection<DeliveryRuleModel>(orders as List<DeliveryRuleModel>);
  594. tradeCacheData.DeliveryGoodsRuleFlag = true;
  595. }
  596. else if (orders is List<DelistingOrder>)
  597. {
  598. tradeCacheData.DelistingOrders = new ObservableCollection<DelistingOrder>(orders as List<DelistingOrder>);
  599. tradeCacheData.DelistingOrderFlag = true;
  600. }
  601. else if (orders is List<RelationperformConfigModel>)
  602. {
  603. tradeCacheData.RelationperformConfig = new ObservableCollection<RelationperformConfigModel>(orders as List<RelationperformConfigModel>);
  604. tradeCacheData.RelationperformConfigFlag = true;
  605. }
  606. }
  607. /// <summary>
  608. /// 获取缓存的单据内容
  609. /// </summary>
  610. /// <typeparam name="T">单据类型</typeparam>
  611. /// <returns></returns>
  612. public static ObservableCollection<T> GetCacheOrders<T>()
  613. {
  614. var orders = new ObservableCollection<T>();
  615. if (CurrentTradeAccount == null) return orders;
  616. var tradeCacheData = GetTradeCacheData(CurrentTradeAccount.AccountId);
  617. if (orders is ObservableCollection<HoldingOrder>)
  618. {
  619. return tradeCacheData.OpenOrders as ObservableCollection<T>;
  620. }
  621. else if (orders is ObservableCollection<OrderDetail>)
  622. {
  623. return tradeCacheData.LimitOrders as ObservableCollection<T>;
  624. }
  625. else if (orders is ObservableCollection<CloseOrder>)
  626. {
  627. return tradeCacheData.CloseOrders as ObservableCollection<T>;
  628. }
  629. else if (orders is ObservableCollection<HoldingSummary>)
  630. {
  631. return tradeCacheData.HoldingSummaries as ObservableCollection<T>;
  632. }
  633. else if (orders is ObservableCollection<CompleteOrder>)
  634. {
  635. return tradeCacheData.ComplateOrders as ObservableCollection<T>;
  636. }
  637. else if (orders is ObservableCollection<DeliveryEntrustOrder>)
  638. {
  639. return tradeCacheData.DryEntrustOrders as ObservableCollection<T>;
  640. }
  641. else if (orders is ObservableCollection<DryHoldDetail>)
  642. {
  643. return tradeCacheData.DryHoldOrders as ObservableCollection<T>;
  644. }
  645. else if (orders is ObservableCollection<DelistingOrder>)
  646. {
  647. return tradeCacheData.DelistingOrders as ObservableCollection<T>;;
  648. }
  649. return orders;
  650. }
  651. /// <summary>
  652. /// 加载预警信息
  653. /// </summary>
  654. /// <param name="tradeAccount"></param>
  655. public static void LoadWarningOrders(TradeAccount tradeAccount)
  656. {
  657. if (tradeAccount != null)
  658. {
  659. var tradeCacheData = GetTradeCacheData(tradeAccount.AccountId);
  660. if (tradeCacheData != null)
  661. {
  662. var warningService = SimpleIoc.Default.GetInstance<IWarningService>();
  663. try
  664. {
  665. tradeCacheData.WarningOrders = warningService.GetWarnings(tradeAccount);
  666. }
  667. catch (Exception ex)
  668. {
  669. LogHelper.WriteError(typeof(UserManager), ex.Message);
  670. }
  671. }
  672. }
  673. }
  674. /// <summary>
  675. /// 保存预警信息
  676. /// </summary>
  677. /// <param name="tradeAccount"></param>
  678. /// <param name="warningOrders"></param>
  679. public static void SaveWarningOrders(TradeAccount tradeAccount, ObservableCollection<Warning> warningOrders)
  680. {
  681. if (tradeAccount != null)
  682. {
  683. var tradeCacheData = GetTradeCacheData(tradeAccount.AccountId);
  684. if (tradeCacheData != null)
  685. {
  686. tradeCacheData.WarningOrders = warningOrders;
  687. }
  688. }
  689. }
  690. public static ObservableCollection<Warning> GetWarningOrders(TradeAccount tradeAccount)
  691. {
  692. if (tradeAccount != null)
  693. {
  694. var tradeCacheData = GetTradeCacheData(tradeAccount.AccountId);
  695. if (tradeCacheData != null)
  696. return tradeCacheData.WarningOrders;
  697. }
  698. return null;
  699. }
  700. /// <summary>
  701. /// Gets the trade data.
  702. /// </summary>
  703. /// <param name="tradeCode">The trade code.</param>
  704. /// <returns>TradeData.</returns>
  705. public static TradeCacheData GetTradeCacheData(ulong accountId)
  706. {
  707. var tradeCacheData = _tradeCacheDataList.FirstOrDefault(z => z.AccountId == accountId);
  708. if (tradeCacheData == null)
  709. {
  710. tradeCacheData = new TradeCacheData(accountId);
  711. _tradeCacheDataList.Add(tradeCacheData);
  712. }
  713. return tradeCacheData;
  714. }
  715. /// <summary>
  716. /// 从缓存中读取持仓单信息
  717. /// </summary>
  718. /// <param name="tradeAccount">信息</param>
  719. /// <returns></returns>
  720. public static ObservableCollection<HoldingOrder> CacheOpenOrders(TradeAccount tradeAccount)
  721. {
  722. if (tradeAccount == null) return null;
  723. var tradeCacheData = GetTradeCacheData(tradeAccount.AccountId);
  724. if (tradeCacheData == null) return null;
  725. return tradeCacheData.OpenOrders;
  726. }
  727. #endregion
  728. #region 用户配置(行情列表抬头\布局) - 采用JSON存储
  729. private static string _userConfigFile
  730. {
  731. get
  732. {
  733. return Path.Combine(UserDataFolder, "UserConfig"+ LocalDataExtensionName);
  734. }
  735. }
  736. /// <summary>
  737. /// 获取默认布局
  738. /// </summary>
  739. /// <returns>UserLayout.</returns>
  740. private static UserLayout GetDefaultLayout()
  741. {
  742. ////返回布局默认值
  743. return new UserLayout()
  744. {
  745. QuoteGridRowLengthUnitType = GridUnitType.Star,
  746. QuoteGridRowLengthValue = 3,
  747. QuoteGridRowMinHeight = 300,
  748. QueryGridRowLengthUnitType = GridUnitType.Pixel,
  749. QueryGridRowLengthValue = 280,
  750. // QueryGridRowMinHeight = 200
  751. };
  752. }
  753. /// <summary>
  754. /// 获取当前主题
  755. /// </summary>
  756. /// <returns>ColorStyle.</returns>
  757. public static ColorStyle GetCurrentColorStyle()
  758. {
  759. var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
  760. var userConfig = userConfigs.FirstOrDefault();
  761. ////本地配置文件为空或是当前主题为None,则返回服务端默认配置
  762. if (null == userConfig || userConfig.CurrentColorStyle == ColorStyle.None)
  763. {
  764. var defaultColor = 1;
  765. if (!int.TryParse(string.IsNullOrWhiteSpace(ApplicationParameter.ColorStyle) ? "4" : ApplicationParameter.ColorStyle, out defaultColor)) defaultColor = 4;
  766. ////目前实现1,4,5三种主题
  767. if (Enum.IsDefined(typeof(ColorStyle), defaultColor)) return (ColorStyle)defaultColor;
  768. return ColorStyle.CoolBlack;
  769. };
  770. return userConfig.CurrentColorStyle;
  771. }
  772. /// <summary>
  773. /// 保存当前主题
  774. /// </summary>
  775. /// <param name="style">The style.</param>
  776. public static void SaveCurrentColorStyle(ColorStyle style)
  777. {
  778. var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
  779. var userConfig = userConfigs.FirstOrDefault() ?? new UserConfig();
  780. userConfig.CurrentColorStyle = style;
  781. //只存一个集合,每次覆盖
  782. JsonHelper.SaveData<UserConfig>(_userConfigFile, new List<UserConfig>() { userConfig }, null, true);
  783. }
  784. /// <summary>
  785. /// 获取当前布局
  786. /// </summary>
  787. /// <returns>List{QuoteListHeader}.</returns>
  788. public static LayoutStyle GetCurrentLayoutStyle()
  789. {
  790. if (!UserManager.IsAccountLogin) ////游客登录
  791. {
  792. return LayoutStyle.Horizontal;
  793. }
  794. var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
  795. var userConfig = userConfigs.FirstOrDefault();
  796. ////本地配置文件为空或是当前布局为None,则返回服务端默认配置
  797. if (null == userConfig || userConfig.CurrentLayoutStyle == LayoutStyle.None)
  798. {
  799. var defaultLayout = 1;
  800. if (!int.TryParse(string.IsNullOrWhiteSpace(ApplicationParameter.LayoutStyle) ? "1" : ApplicationParameter.LayoutStyle, out defaultLayout)) defaultLayout = 1;
  801. ////目前实现1,2两种布局
  802. if (Enum.IsDefined(typeof(LayoutStyle), defaultLayout)) return (LayoutStyle)defaultLayout;
  803. return LayoutStyle.Vertical;
  804. };
  805. return userConfig.CurrentLayoutStyle;
  806. }
  807. /// <summary>
  808. /// 保存当前布局
  809. /// </summary>
  810. /// <param name="style">The style.</param>
  811. public static void SaveCurrentLayoutStyle(LayoutStyle style)
  812. {
  813. var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
  814. var userConfig = userConfigs.FirstOrDefault() ?? new UserConfig();
  815. userConfig.CurrentLayoutStyle = style;
  816. //只存一个集合,每次覆盖
  817. JsonHelper.SaveData<UserConfig>(_userConfigFile, new List<UserConfig>() { userConfig }, null, true);
  818. }
  819. /// <summary>
  820. /// 获取布局
  821. /// </summary>
  822. /// <param name="style">The style.</param>
  823. /// <returns>List{QuoteListHeader}.</returns>
  824. public static UserLayout GetLayoutStyle(LayoutStyle style)
  825. {
  826. if (style == LayoutStyle.None) return null;
  827. var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
  828. var userConfig = userConfigs.FirstOrDefault();
  829. if (null == userConfig || null == userConfig.UserLayouts || !userConfig.UserLayouts.ContainsKey(style) || null == userConfig.UserLayouts[style])
  830. {
  831. var defaultLayout = GetDefaultLayout();
  832. return defaultLayout;
  833. };
  834. return userConfig.UserLayouts[style];
  835. }
  836. /// <summary>
  837. /// 保存布局
  838. /// </summary>
  839. /// <param name="style">The style.</param>
  840. /// <param name="userLayout">The user layout.</param>
  841. public static void SaveLayoutStyle(LayoutStyle style, UserLayout userLayout)
  842. {
  843. var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
  844. var userConfig = userConfigs.FirstOrDefault() ?? new UserConfig();
  845. if (userConfig.UserLayouts == null) userConfig.UserLayouts = new Dictionary<LayoutStyle, UserLayout>();
  846. userConfig.UserLayouts[style] = userLayout;
  847. //只存一个集合,每次覆盖
  848. JsonHelper.SaveData<UserConfig>(_userConfigFile, new List<UserConfig>() { userConfig }, null, true);
  849. }
  850. /// <summary>
  851. /// 获取抬头
  852. /// </summary>
  853. /// <param name="type">The type.</param>
  854. /// <returns>List{QuoteListHeader}.</returns>
  855. public static List<QuoteListHeader> GetQuoteListHeaders(QuoteListHeaderType type)
  856. {
  857. var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
  858. var userConfig = userConfigs.FirstOrDefault();
  859. if (null == userConfig || null == userConfig.QuoteGridHeaders || !userConfig.QuoteGridHeaders.ContainsKey(type) || null == userConfig.QuoteGridHeaders[type]) return null;
  860. if (IsUpdateHeaderVer(type, userConfig.ConfigHeadersVer)) ////保存的版本是否要更新
  861. {
  862. return null; ////返回空则更新
  863. }
  864. return userConfig.QuoteGridHeaders[type];
  865. }
  866. /// <summary>
  867. /// 保存抬头
  868. /// </summary>
  869. /// <param name="type">The type.</param>
  870. /// <param name="headers">The headers.</param>
  871. public static void SaveQuoteListHeaders(QuoteListHeaderType type, List<QuoteListHeader> headers)
  872. {
  873. object lockObject = new object();
  874. bool lockToken = false;
  875. try
  876. {
  877. Monitor.Enter(lockObject, ref lockToken);
  878. var userConfigs = JsonHelper.LoadData<UserConfig>(_userConfigFile);
  879. var userConfig = userConfigs.FirstOrDefault() ?? new UserConfig();
  880. if (userConfig.QuoteGridHeaders == null) userConfig.QuoteGridHeaders = new Dictionary<QuoteListHeaderType, List<QuoteListHeader>>();
  881. userConfig.QuoteGridHeaders[type] = headers;
  882. userConfig.ConfigHeadersVer = SaveHeaderVer(type, userConfig.ConfigHeadersVer); ////保存版本号
  883. //只存一个集合,每次覆盖
  884. JsonHelper.SaveData<UserConfig>(_userConfigFile, new List<UserConfig>() { userConfig }, null, true);
  885. }
  886. finally
  887. {
  888. if (lockToken)
  889. {
  890. Monitor.Exit(lockObject);
  891. }
  892. }
  893. }
  894. /// <summary>
  895. /// 是否更新抬头
  896. /// </summary>
  897. /// <param name="type"></param>
  898. /// <param name="localVerString"></param>
  899. /// <returns></returns>
  900. private static bool IsUpdateHeaderVer(QuoteListHeaderType type, string localVerString)
  901. {
  902. if (string.IsNullOrWhiteSpace(ApplicationParameter.ConfigHeadersVer))
  903. {
  904. return false; ////没有版本更新
  905. }
  906. var updateVer = new Version(ApplicationParameter.ConfigHeadersVer);
  907. if (string.IsNullOrWhiteSpace(localVerString))
  908. {
  909. return true; ////版本号全是0则不更新
  910. }
  911. var locolVer = new Version(localVerString);
  912. switch (type)
  913. {
  914. case QuoteListHeaderType.OTC:
  915. case QuoteListHeaderType.Channel:
  916. return updateVer.Minor > locolVer.Minor;
  917. case QuoteListHeaderType.Bid:
  918. return updateVer.Build > locolVer.Build;
  919. case QuoteListHeaderType.BidMarket:
  920. return updateVer.Revision > locolVer.Revision; ////混合版
  921. case QuoteListHeaderType.MySelected:
  922. return updateVer.Major > locolVer.Major; ////自选版本
  923. }
  924. return false;
  925. }
  926. ///
  927. private static string SaveHeaderVer(QuoteListHeaderType type, string localVerString)
  928. {
  929. if (string.IsNullOrWhiteSpace(ApplicationParameter.ConfigHeadersVer))
  930. {
  931. return string.Empty; ////没有版本更新
  932. }
  933. var updateVer = new Version(ApplicationParameter.ConfigHeadersVer);
  934. if (string.IsNullOrWhiteSpace(localVerString))
  935. {
  936. localVerString = "0.0.0.0";
  937. }
  938. var locolVer = new Version(localVerString);
  939. switch (type)
  940. {
  941. case QuoteListHeaderType.OTC:
  942. case QuoteListHeaderType.Channel:
  943. return string.Format("{0}.{1}.{2}.{3}", locolVer.Major, updateVer.Minor, locolVer.Build,
  944. locolVer.Revision);
  945. break;
  946. case QuoteListHeaderType.Bid:
  947. return string.Format("{0}.{1}.{2}.{3}", locolVer.Major, locolVer.Minor, updateVer.Build,
  948. locolVer.Revision);
  949. break;
  950. case QuoteListHeaderType.BidMarket:
  951. return string.Format("{0}.{1}.{2}.{3}", locolVer.Major, locolVer.Minor, locolVer.Build,
  952. updateVer.Revision);
  953. break;
  954. case QuoteListHeaderType.MySelected:
  955. return string.Format("{0}.{1}.{2}.{3}", updateVer.Major, locolVer.Minor, locolVer.Build,
  956. locolVer.Revision);
  957. break;
  958. }
  959. return string.Empty;
  960. }
  961. #endregion
  962. #region 是否为监控会员
  963. /// <summary>
  964. /// 是否为监控会员//如果返回false 角色按投资者处理
  965. /// </summary>
  966. /// <returns></returns>
  967. public static bool IsMonitorMember()
  968. {
  969. if (UserManager.CurrentTradeAccount != null)// && UserManager.CurrentTradeAccount.FundsAccounts != null)
  970. {
  971. #region MTP2.0 注销
  972. //if (UserManager.CurrentTradeAccount.FundsAccounts.Any() && UserManager.CurrentTradeAccount.FundsAccounts[0].AreaRoles != 0)
  973. //{
  974. // return UserManager.CurrentTradeAccount.AccountType == eLoginUserType.LOGINUSERTYPE_TRADER &&
  975. // (UserManager.CurrentTradeAccount.FundsAccounts[0].AreaRoles &
  976. // ((int)eAreaRole.AREAROLE_MAKEMEMBER + (int)eAreaRole.AREAROLE_TOPMAKEMEMBER + (int)eAreaRole.AREAROLE_INDUSTRYMEMBER)) > 0; ////产业会员,二级做市会员,做市会员ww风控取信息
  977. //}
  978. #endregion
  979. return UserManager.CurrentTradeAccount.AccountType != eUserType.USERTYPE_INVESTOR;
  980. }
  981. else
  982. {
  983. return false;
  984. }
  985. }
  986. #endregion
  987. /// <summary>
  988. /// 当前账号的可用资金
  989. /// </summary>
  990. /// <returns></returns>
  991. public static decimal CurrentAccountUsableFunds()
  992. {
  993. var usableFunds = 0m;
  994. if (UserManager.CurrentTradeAccount.FundsAccounts != null &&
  995. UserManager.CurrentTradeAccount.FundsAccounts.Any())
  996. {
  997. usableFunds = UserManager.CurrentTradeAccount.FundsAccounts[0].AvailMargin; ////默认使用可用数量
  998. }
  999. if (UserManager.IsMonitorMember())
  1000. {
  1001. if (UserManager.CurrentTradeAccount.MemberAccountInfo != null) ////监控会员取风控的资金信息
  1002. {
  1003. usableFunds = UserManager.CurrentTradeAccount.MemberAccountInfo.AvailMargin;
  1004. }
  1005. }
  1006. return usableFunds;
  1007. }
  1008. /// <summary>
  1009. /// 获取可用资金BY AccountId
  1010. /// </summary>
  1011. /// <returns></returns>
  1012. public static decimal GetCurrentUsableFundsByAccountId(ulong AccountId)
  1013. {
  1014. var usableFunds = 0m;
  1015. if (CacheManager.CacheTAAcountList !=null && CacheManager.CacheTAAcountList.Count > 0)
  1016. {
  1017. usableFunds = CacheManager.CacheTAAcountList.Where(x => x.FundsAccounts.AccountId == AccountId).Select(x=>x.FundsAccounts.AvailMargin).FirstOrDefault(); ////默认使用可用数量
  1018. }
  1019. return usableFunds;
  1020. }
  1021. }
  1022. }