QuoteTikDetailViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. //----------------------------------------------------------------
  6. //Module Name: $safeprojectname$
  7. //Purpose:
  8. //CopyRight: Muchinfo
  9. //History:
  10. //----------------------------------------------------------------
  11. //DateTime 2017/2/20 16:00:41
  12. //Author
  13. //Description Create
  14. //----------------------------------------------------------------
  15. using GalaSoft.MvvmLight;
  16. using GalaSoft.MvvmLight.Ioc;
  17. using Muchinfo.MTPClient.Data;
  18. using Muchinfo.MTPClient.Data.Enums;
  19. using Muchinfo.MTPClient.Data.Model;
  20. using Muchinfo.MTPClient.Data.Model.Analysis;
  21. using Muchinfo.MTPClient.Data.Quote;
  22. using Muchinfo.MTPClient.Infrastructure.Cache;
  23. using Muchinfo.MTPClient.Infrastructure.Helpers;
  24. using Muchinfo.MTPClient.Infrastructure.Utilities;
  25. using Muchinfo.MTPClient.IService;
  26. namespace Muchinfo.MTPClient.Analysis.ViewModels
  27. {
  28. public class QuoteTikDetailViewModel : ChartViewModelBase
  29. {
  30. private List<SubPenData> _listSubSource=new List<SubPenData>();
  31. ///分笔数据
  32. private bool _lockQuery;///是否正在查询数据
  33. public QuoteTikDetailViewModel(QuoteGoods goods ):base(goods)
  34. {
  35. _currentGoods = goods;
  36. if (CacheManager.TradeDayPlan.ContainsKey(goods.GoodsParameters.GoodsId))
  37. {
  38. var tradePlan = CacheManager.TradeDayPlan[goods.GoodsParameters.GoodsId];
  39. QueryQuoteTik(tradePlan);
  40. }
  41. else
  42. {
  43. ChartInitTradePlan();
  44. }
  45. IsBidGoods = goods.TradeMode != eTradeMode.TRADEMODE_MARKETMAKE;
  46. LastClose = goods.LastClose;
  47. }
  48. private bool _isBusy;
  49. /// <summary>
  50. /// 是否忙
  51. /// </summary>
  52. public bool IsBusy
  53. {
  54. get { return _isBusy; }
  55. set { Set(() => IsBusy, ref _isBusy, value); }
  56. }
  57. private decimal _lastClose;
  58. /// <summary>
  59. /// 昨收
  60. /// </summary>
  61. public decimal LastClose
  62. {
  63. get { return _lastClose; }
  64. set { Set(() => LastClose, ref _lastClose, value); }
  65. }
  66. private void QueryQuoteTik(QuoteTradePlan tradePlan)
  67. {
  68. if (!_lockQuery)
  69. {
  70. if (tradePlan != null && tradePlan.OpenCloseDates != null && tradePlan.OpenCloseDates.Any())
  71. {
  72. _lockQuery = true;
  73. DateTime openDate = ApplicationParameter.ServerTimeNow.Date,
  74. closeDate = ApplicationParameter.ServerTimeNow.Date.AddHours(24);
  75. IsBusy = true;
  76. openDate = tradePlan.OpenCloseDates[0].OpenDate;
  77. closeDate = tradePlan.OpenCloseDates[tradePlan.OpenCloseDates.Count - 1].CloseDate;
  78. _quoteDataService.GetHistoryCycleData(_currentGoods, CycleType.Tik, openDate, closeDate, 0,
  79. QueryTikSuccess, QueryTikError);
  80. }
  81. }
  82. }
  83. /// <summary>
  84. ///
  85. /// </summary>
  86. /// <param name="tradeDays"></param>
  87. protected override void QueryReckenSuccess(List<QuoteTradePlan> tradeDays)
  88. {
  89. base.QueryReckenSuccess(tradeDays);
  90. var goodsPlan = tradeDays.FirstOrDefault((item) => item.GoodsId == _currentGoods.GoodsId);
  91. if (goodsPlan == null)
  92. {
  93. goodsPlan = tradeDays.FirstOrDefault((item) => item.GoodsId == 0);
  94. }
  95. QueryQuoteTik(goodsPlan);
  96. }
  97. /// <summary>
  98. /// 查询历史Tik数据
  99. /// </summary>
  100. /// <param name="historyCycle">历史Tik数据</param>
  101. public void QueryTikSuccess(GoodsHistoryCycle historyCycle)
  102. {
  103. _lockQuery = false;
  104. IsBusy = false;
  105. if (historyCycle.SubDataPoints != null && historyCycle.SubDataPoints.Any())
  106. {
  107. if (_listSubSource != null)
  108. {
  109. _listSubSource.InsertRange(0, historyCycle.SubDataPoints.ToList());
  110. }
  111. else
  112. {
  113. _listSubSource = historyCycle.SubDataPoints.ToList();
  114. }
  115. }
  116. ////累计实时行情
  117. RegReadTimeQuote();
  118. if (Count > 0)
  119. {
  120. CalcPageCountWithAdd() ;
  121. }
  122. SetPageDataSource(0);
  123. }
  124. public void QueryTikError(ErrorEntity error)
  125. {
  126. _lockQuery = false;
  127. IsBusy = false;
  128. ////累计实时行情
  129. RegReadTimeQuote();
  130. LogInfoHelper.WriteInfo(string.Format("请求分笔数据:{0}({1})", error.ReturnCode, error.ReturnDesc));
  131. }
  132. private IEnumerable<SubPenData> _showDataSource;
  133. /// <summary>
  134. /// 显示的列表
  135. /// </summary>
  136. public IEnumerable<SubPenData> ShowDataSource
  137. {
  138. get { return _showDataSource; }
  139. set { Set(() => ShowDataSource, ref _showDataSource, value); }
  140. }
  141. /// <summary>
  142. /// 当前页显示的列表最后一个数据的索引
  143. /// </summary>
  144. public int CurrentIndex { get; set; }
  145. /// <summary>
  146. /// 当前页
  147. /// </summary>
  148. public int CurrentPage { get; set; }
  149. /// <summary>
  150. /// 当前总页数
  151. /// </summary>
  152. public int TotalPage { get; set; }
  153. /// <summary>
  154. /// 每页显示数量
  155. /// </summary>
  156. public int Count { get; set; }
  157. private bool _isBidGoods;
  158. /// <summary>
  159. /// 竞价商品显示内容
  160. /// </summary>
  161. public bool IsBidGoods
  162. {
  163. get { return _isBidGoods; }
  164. set
  165. {
  166. Set(() => IsBidGoods, ref _isBidGoods, value);
  167. }
  168. }
  169. private double _itemWidth;
  170. /// <summary>
  171. /// 设置列表项的大小
  172. /// </summary>
  173. public double ItemWidth
  174. {
  175. get { return _itemWidth; }
  176. set {Set(() => ItemWidth, ref _itemWidth, value); }
  177. }
  178. /// <summary>
  179. /// 返回列表多少页
  180. /// </summary>
  181. /// <param name="pageItem">每页显示多少数据</param>
  182. /// <returns></returns>
  183. public int CalcPageCount(int pageItem)
  184. {
  185. Count = pageItem;
  186. TotalPage = 1; //默认显示一页
  187. if (_listSubSource != null)
  188. {
  189. var count= (int)Math.Ceiling( _listSubSource.Count/(double)pageItem);
  190. TotalPage= count <= 0 ? 1 : count;
  191. }
  192. CurrentPage = TotalPage; ///默认显示最后一页
  193. return TotalPage;
  194. }
  195. /// <summary>
  196. /// 增量取数据时更新总页数据
  197. /// </summary>
  198. /// <returns></returns>
  199. public int CalcPageCountWithAdd()
  200. {
  201. if (CurrentPage == 1&&TotalPage!=CurrentPage)
  202. {
  203. CurrentPage = TotalPage-1; ///从后倒数显示的当前页
  204. }
  205. if (_listSubSource != null)
  206. {
  207. var count = (int)Math.Ceiling(_listSubSource.Count / (double)Count);
  208. TotalPage = count <= 0 ? 1 : count;
  209. var page = TotalPage - CurrentPage;
  210. CurrentPage = page>0?page:1;
  211. }
  212. return TotalPage;
  213. }
  214. /// <summary>
  215. /// 显示总页数
  216. /// </summary>
  217. /// <param name="addPage">0:更新当前页,大于0时显示下一页,小于0时,显示前一页</param>
  218. public void SetPageDataSource(int addPage)
  219. {
  220. var page = CurrentPage + addPage;
  221. if (page > TotalPage)
  222. {
  223. CurrentPage = TotalPage;
  224. }
  225. else if(page<=0)
  226. {
  227. if (_listSubSource.Any())
  228. {
  229. if (CacheManager.TradeDayPlan.ContainsKey(_currentGoods.GoodsParameters.MarketTypeID))
  230. {
  231. var tradePlan = CacheManager.TradeDayPlan[_currentGoods.GoodsParameters.MarketTypeID];
  232. var isSub = _listSubSource[0].Time == tradePlan.OpenCloseDates[0].OpenDate;
  233. if (!isSub && !_lockQuery) ////向前获取数据
  234. {
  235. _lockQuery = true;
  236. IsBusy = true;
  237. _quoteDataService.GetHistoryCycleData(_currentGoods, CycleType.Tik,
  238. tradePlan.OpenCloseDates[0].OpenDate, _listSubSource[0].Time, 0, QueryTikSuccess,
  239. QueryTikError);
  240. return;
  241. }
  242. }
  243. }
  244. CurrentPage = 1;
  245. }
  246. else
  247. {
  248. CurrentPage = page;
  249. }
  250. if (CurrentPage == TotalPage)
  251. {
  252. var index = _listSubSource.Count - Count < 0 ? 0 : _listSubSource.Count - Count - 1;
  253. ShowDataSource = _listSubSource.GetRange(index, _listSubSource.Count - index).Where(z=>z.CompleteQte > 0);
  254. CurrentIndex = _listSubSource.Count - 1;
  255. }
  256. else
  257. {
  258. var index = (CurrentPage-1)*Count;
  259. if (index <= 0)
  260. {
  261. index = 0;
  262. }
  263. ShowDataSource = _listSubSource.GetRange(index, Count).Where(z => z.CompleteQte > 0);
  264. CurrentIndex = index+Count - 1;
  265. }
  266. }
  267. /// <summary>
  268. /// 订阅行情
  269. /// </summary>
  270. private void RegReadTimeQuote()
  271. {
  272. MessengerHelper.QuoteRegister<List<QuoteGoods>>(this, MessengerTokens.ReceiveRealTikQuote, (quoteList) =>
  273. {
  274. if (quoteList == null || !quoteList.Any()) return;
  275. foreach (var item in quoteList)
  276. {
  277. if (item == null) continue;
  278. if (_currentGoods.Symbol.ToLower() == item.Symbol.ToLower())
  279. {
  280. if ((item.CurrentPrice == 0 || item.Date == DateTime.MinValue))
  281. {
  282. continue; ////竞价不是成交的行情不显示分笔
  283. }
  284. var penData = new SubPenData(_currentGoods.GoodsParameters.HqExchFigures)
  285. {
  286. Time = item.Date,
  287. Price = (double)item.CurrentPrice,
  288. CompleteQte = (double)item.CurrentVolume,
  289. };
  290. if (null != _listSubSource)
  291. {
  292. _listSubSource.Add(penData);
  293. }
  294. else
  295. {
  296. _listSubSource = new List<SubPenData>();
  297. _listSubSource.Add(penData);
  298. }
  299. }
  300. }
  301. if (CurrentPage == TotalPage)
  302. {
  303. SetPageDataSource(0);
  304. }
  305. });
  306. }
  307. }
  308. }