UserManager.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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)
  881. userConfig.QuoteGridHeaders = new Dictionary<QuoteListHeaderType, List<QuoteListHeader>>();
  882. userConfig.QuoteGridHeaders[type] = headers;
  883. userConfig.ConfigHeadersVer = SaveHeaderVer(type, userConfig.ConfigHeadersVer); ////保存版本号
  884. //只存一个集合,每次覆盖
  885. JsonHelper.SaveData<UserConfig>(_userConfigFile, new List<UserConfig>() {userConfig}, null, true);
  886. }
  887. catch (Exception e)
  888. {
  889. Console.WriteLine(e.ToString());
  890. }
  891. finally
  892. {
  893. if (lockToken)
  894. {
  895. Monitor.Exit(lockObject);
  896. }
  897. }
  898. }
  899. /// <summary>
  900. /// 是否更新抬头
  901. /// </summary>
  902. /// <param name="type"></param>
  903. /// <param name="localVerString"></param>
  904. /// <returns></returns>
  905. private static bool IsUpdateHeaderVer(QuoteListHeaderType type, string localVerString)
  906. {
  907. if (string.IsNullOrWhiteSpace(ApplicationParameter.ConfigHeadersVer))
  908. {
  909. return false; ////没有版本更新
  910. }
  911. var updateVer = new Version(ApplicationParameter.ConfigHeadersVer);
  912. if (string.IsNullOrWhiteSpace(localVerString))
  913. {
  914. return true; ////版本号全是0则不更新
  915. }
  916. var locolVer = new Version(localVerString);
  917. switch (type)
  918. {
  919. case QuoteListHeaderType.OTC:
  920. case QuoteListHeaderType.Channel:
  921. return updateVer.Minor > locolVer.Minor;
  922. case QuoteListHeaderType.Bid:
  923. return updateVer.Build > locolVer.Build;
  924. case QuoteListHeaderType.BidMarket:
  925. return updateVer.Revision > locolVer.Revision; ////混合版
  926. case QuoteListHeaderType.MySelected:
  927. return updateVer.Major > locolVer.Major; ////自选版本
  928. }
  929. return false;
  930. }
  931. ///
  932. private static string SaveHeaderVer(QuoteListHeaderType type, string localVerString)
  933. {
  934. if (string.IsNullOrWhiteSpace(ApplicationParameter.ConfigHeadersVer))
  935. {
  936. return string.Empty; ////没有版本更新
  937. }
  938. var updateVer = new Version(ApplicationParameter.ConfigHeadersVer);
  939. if (string.IsNullOrWhiteSpace(localVerString))
  940. {
  941. localVerString = "0.0.0.0";
  942. }
  943. var locolVer = new Version(localVerString);
  944. switch (type)
  945. {
  946. case QuoteListHeaderType.OTC:
  947. case QuoteListHeaderType.Channel:
  948. return string.Format("{0}.{1}.{2}.{3}", locolVer.Major, updateVer.Minor, locolVer.Build,
  949. locolVer.Revision);
  950. break;
  951. case QuoteListHeaderType.Bid:
  952. return string.Format("{0}.{1}.{2}.{3}", locolVer.Major, locolVer.Minor, updateVer.Build,
  953. locolVer.Revision);
  954. break;
  955. case QuoteListHeaderType.BidMarket:
  956. return string.Format("{0}.{1}.{2}.{3}", locolVer.Major, locolVer.Minor, locolVer.Build,
  957. updateVer.Revision);
  958. break;
  959. case QuoteListHeaderType.MySelected:
  960. return string.Format("{0}.{1}.{2}.{3}", updateVer.Major, locolVer.Minor, locolVer.Build,
  961. locolVer.Revision);
  962. break;
  963. }
  964. return string.Empty;
  965. }
  966. #endregion
  967. #region 是否为监控会员
  968. /// <summary>
  969. /// 是否为监控会员//如果返回false 角色按投资者处理
  970. /// </summary>
  971. /// <returns></returns>
  972. public static bool IsMonitorMember()
  973. {
  974. if (UserManager.CurrentTradeAccount != null)// && UserManager.CurrentTradeAccount.FundsAccounts != null)
  975. {
  976. #region MTP2.0 注销
  977. //if (UserManager.CurrentTradeAccount.FundsAccounts.Any() && UserManager.CurrentTradeAccount.FundsAccounts[0].AreaRoles != 0)
  978. //{
  979. // return UserManager.CurrentTradeAccount.AccountType == eLoginUserType.LOGINUSERTYPE_TRADER &&
  980. // (UserManager.CurrentTradeAccount.FundsAccounts[0].AreaRoles &
  981. // ((int)eAreaRole.AREAROLE_MAKEMEMBER + (int)eAreaRole.AREAROLE_TOPMAKEMEMBER + (int)eAreaRole.AREAROLE_INDUSTRYMEMBER)) > 0; ////产业会员,二级做市会员,做市会员ww风控取信息
  982. //}
  983. #endregion
  984. return UserManager.CurrentTradeAccount.AccountType != eUserType.USERTYPE_INVESTOR;
  985. }
  986. else
  987. {
  988. return false;
  989. }
  990. }
  991. #endregion
  992. /// <summary>
  993. /// 当前账号的可用资金
  994. /// </summary>
  995. /// <returns></returns>
  996. public static decimal CurrentAccountUsableFunds()
  997. {
  998. var usableFunds = 0m;
  999. if (UserManager.CurrentTradeAccount.FundsAccounts != null &&
  1000. UserManager.CurrentTradeAccount.FundsAccounts.Any())
  1001. {
  1002. usableFunds = UserManager.CurrentTradeAccount.FundsAccounts[0].AvailMargin; ////默认使用可用数量
  1003. }
  1004. if (UserManager.IsMonitorMember())
  1005. {
  1006. if (UserManager.CurrentTradeAccount.MemberAccountInfo != null) ////监控会员取风控的资金信息
  1007. {
  1008. usableFunds = UserManager.CurrentTradeAccount.MemberAccountInfo.AvailMargin;
  1009. }
  1010. }
  1011. return usableFunds;
  1012. }
  1013. /// <summary>
  1014. /// 获取可用资金BY AccountId
  1015. /// </summary>
  1016. /// <returns></returns>
  1017. public static decimal GetCurrentUsableFundsByAccountId(ulong AccountId)
  1018. {
  1019. var usableFunds = 0m;
  1020. if (CacheManager.CacheTAAcountList !=null && CacheManager.CacheTAAcountList.Count > 0)
  1021. {
  1022. usableFunds = CacheManager.CacheTAAcountList.Where(x => x.FundsAccounts.AccountId == AccountId).Select(x=>x.FundsAccounts.AvailMargin).FirstOrDefault(); ////默认使用可用数量
  1023. }
  1024. return usableFunds;
  1025. }
  1026. }
  1027. }