PirceCardViewModel.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //----------------------------------------------------------------
  2. //Module Name: $safeprojectname$
  3. //Purpose:
  4. //CopyRight: Muchinfo
  5. //History:
  6. //----------------------------------------------------------------
  7. //DateTime 2016/6/18 17:43:11
  8. //Author
  9. //Description Create
  10. //----------------------------------------------------------------
  11. using GalaSoft.MvvmLight;
  12. using GalaSoft.MvvmLight.Command;
  13. using Muchinfo.MTPClient.Data.Enums;
  14. using Muchinfo.MTPClient.Data.Model;
  15. using Muchinfo.MTPClient.Infrastructure.Cache;
  16. using Muchinfo.MTPClient.Infrastructure.Helpers;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Windows;
  21. namespace Muchinfo.MTPClient.Analysis.ViewModels
  22. {
  23. public class PirceCardViewModel : ViewModelBase, IDisposable
  24. {
  25. private QuoteGoods _currentGoods;
  26. public PirceCardViewModel(QuoteGoods goods)
  27. {
  28. Goods = goods;
  29. //RegisterMessage();
  30. //InitData();
  31. }
  32. /// <summary>
  33. /// 当前商品
  34. /// </summary>
  35. public QuoteGoods Goods
  36. {
  37. get
  38. {
  39. return _currentGoods;
  40. }
  41. set
  42. {
  43. bool isupdate = _currentGoods == null || _currentGoods.Equals(value);
  44. Set(() => Goods, ref _currentGoods, value);
  45. if (isupdate)
  46. {
  47. RegisterMessage();
  48. InitData();
  49. //MessengerHelper.DefaultSend<QuoteGoods>(value, MessengerTokens.GoodsChartChange);
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// 初始化数据
  55. /// </summary>
  56. #region 是否显示换手率
  57. private void InitData()
  58. {
  59. HiddenDataByMarket();
  60. }
  61. /// <summary>
  62. /// 隐藏数据逻辑
  63. /// </summary>
  64. private void HiddenDataByMarket()
  65. {
  66. if (_currentGoods != null )
  67. switch (_currentGoods.TradeMode)
  68. {
  69. case eTradeMode.TRADEMODE_MARKETMAKE:
  70. case eTradeMode.TRADEMODE_BIDDING:
  71. case eTradeMode.TRADEMODE_LISTING_SELECT:
  72. IsVisibilityTurnoverRate = Visibility.Visible;
  73. break;
  74. case eTradeMode.TRADEMODE_BIDDINGMARKETMAKE:
  75. //换手率混合市场应隐藏
  76. IsVisibilityTurnoverRate = Visibility.Collapsed;
  77. break;
  78. default:
  79. IsVisibilityTurnoverRate = Visibility.Visible;
  80. break;
  81. }
  82. }
  83. private Visibility _isVisibilityTurnoverRate = Visibility.Visible;
  84. /// <summary>
  85. /// 是否显示换手率-
  86. /// </summary>
  87. public Visibility IsVisibilityTurnoverRate
  88. {
  89. get
  90. {
  91. return _isVisibilityTurnoverRate;
  92. }
  93. set
  94. {
  95. Set(() => IsVisibilityTurnoverRate, ref _isVisibilityTurnoverRate, value);
  96. }
  97. }
  98. #endregion
  99. private List<Commission> _bidCommissions;
  100. /// <summary>
  101. /// 显示买入档
  102. /// </summary>
  103. public List<Commission> BidCommissions
  104. {
  105. get { return _bidCommissions; }
  106. set { Set(() => BidCommissions, ref _bidCommissions, value); }
  107. }
  108. private List<Commission> _askCommissions;
  109. /// <summary>
  110. /// 显示卖出档
  111. /// </summary>
  112. public List<Commission> AskCommissions
  113. {
  114. get { return _askCommissions; }
  115. set { Set(() => AskCommissions, ref _askCommissions, value); }
  116. }
  117. /// <summary>
  118. /// 设置买卖档
  119. /// </summary>
  120. /// <param name="bid">if set to <c>true</c> [bid].</param>
  121. /// <param name="commissions">The commissions.</param>
  122. /// <returns>List{Commission}.</returns>
  123. private List<Commission> SortCommissions(bool bid, Commission[] commissions, string formatStr)
  124. {
  125. var commissionLsit = new List<Commission>();
  126. int index = 1;
  127. foreach (var commission in commissions)
  128. {
  129. commission.Index = index;
  130. commission.FormatString = formatStr;
  131. commissionLsit.Add(commission);
  132. index++;
  133. }
  134. if (!bid)
  135. {
  136. commissionLsit = commissionLsit.OrderByDescending((item) => item.Index).ToList();
  137. }
  138. return commissionLsit;
  139. }
  140. private void RegisterMessage()
  141. {
  142. if (Goods.GoodsParameters.TradeMode == eTradeMode.TRADEMODE_BIDDINGMARKETMAKE ||
  143. Goods.GoodsParameters.TradeMode != eTradeMode.TRADEMODE_BIDDINGMARKETMAKE)
  144. {
  145. AskCommissions = SortCommissions(false, Goods.AskList, Goods.FormatPrice);
  146. BidCommissions = SortCommissions(true, Goods.BidList, Goods.FormatPrice);
  147. MessengerHelper.DefaultUnregister<List<QuoteGoods>>(this, MessengerTokens.ReceiveRealTimeQuote);
  148. MessengerHelper.QuoteRegister<List<QuoteGoods>>(this,
  149. MessengerTokens.ReceiveRealTimeQuote, (quoteList) =>
  150. {
  151. if (quoteList == null || !quoteList.Any()) return;
  152. foreach (var item in quoteList)
  153. {
  154. if (item == null) continue;
  155. if (Goods.Symbol.ToLower() == item.Symbol.ToLower())
  156. {
  157. AskCommissions = SortCommissions(false, item.AskList, Goods.FormatPrice);
  158. BidCommissions = SortCommissions(true, item.BidList, Goods.FormatPrice);
  159. }
  160. }
  161. });
  162. }
  163. }
  164. public void Dispose()
  165. {
  166. this.Cleanup();
  167. }
  168. /// <summary>
  169. /// 下一商品
  170. /// </summary>
  171. public RelayCommand NextCommand
  172. {
  173. get
  174. {
  175. return new RelayCommand(() =>
  176. {
  177. MessengerHelper.DefaultSend<bool>(true, MessengerTokens.ChartGoodsChange);
  178. }, IsCanNext);
  179. }
  180. }
  181. /// <summary>
  182. /// 前一个商品
  183. /// </summary>
  184. public RelayCommand ForwordCommand
  185. {
  186. get
  187. {
  188. return new RelayCommand(() =>
  189. {
  190. MessengerHelper.DefaultSend<bool>(false, MessengerTokens.ChartGoodsChange);
  191. }, IsCanForword);
  192. }
  193. }
  194. /// <summary>
  195. /// 是否可前一个
  196. /// </summary>
  197. /// <returns></returns>
  198. private bool IsCanForword()
  199. {
  200. if (_currentGoods == null)
  201. {
  202. return false;
  203. }
  204. var goodsList = CacheManager.CacheGoodsBaseInfos.Where((item) => item.TradeMode != eTradeMode.TRADEMODE_SALE).ToList();
  205. if (!goodsList.Any())
  206. {
  207. return false;
  208. }
  209. var index= goodsList.FindIndex((item) => _currentGoods.Symbol == item.Symbol);
  210. return index!=-1&&index != 0; ///当前是否为第一个
  211. }
  212. /// <summary>
  213. /// 是否可下一个商品
  214. /// </summary>
  215. /// <returns></returns>
  216. private bool IsCanNext()
  217. {
  218. if (_currentGoods == null)
  219. {
  220. return false;
  221. }
  222. var goodsList = CacheManager.CacheGoodsBaseInfos.Where((item) => item.TradeMode != eTradeMode.TRADEMODE_SALE).ToList();
  223. if (!goodsList.Any())
  224. {
  225. return false;
  226. }
  227. var index = goodsList.FindIndex((item) => _currentGoods.Symbol == item.Symbol);
  228. return index != -1 && index != goodsList.Count-1; ///当前是否为第一个
  229. }
  230. }
  231. }