using System; using System.Collections.Generic; using System.Linq; using System.Text; //---------------------------------------------------------------- //Module Name: $safeprojectname$ //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime 2017/2/20 16:00:41 //Author //Description Create //---------------------------------------------------------------- using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Ioc; using Muchinfo.MTPClient.Data; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Data.Model.Analysis; using Muchinfo.MTPClient.Data.Quote; using Muchinfo.MTPClient.Infrastructure.Cache; using Muchinfo.MTPClient.Infrastructure.Helpers; using Muchinfo.MTPClient.Infrastructure.Utilities; using Muchinfo.MTPClient.IService; namespace Muchinfo.MTPClient.Analysis.ViewModels { public class QuoteTikDetailViewModel : ChartViewModelBase { private List _listSubSource=new List(); ///分笔数据 private bool _lockQuery;///是否正在查询数据 public QuoteTikDetailViewModel(QuoteGoods goods ):base(goods) { _currentGoods = goods; if (CacheManager.TradeDayPlan.ContainsKey(goods.GoodsParameters.GoodsId)) { var tradePlan = CacheManager.TradeDayPlan[goods.GoodsParameters.GoodsId]; QueryQuoteTik(tradePlan); } else { ChartInitTradePlan(); } IsBidGoods = goods.TradeMode != eTradeMode.TRADEMODE_MARKETMAKE; LastClose = goods.LastClose; } private bool _isBusy; /// /// 是否忙 /// public bool IsBusy { get { return _isBusy; } set { Set(() => IsBusy, ref _isBusy, value); } } private decimal _lastClose; /// /// 昨收 /// public decimal LastClose { get { return _lastClose; } set { Set(() => LastClose, ref _lastClose, value); } } private void QueryQuoteTik(QuoteTradePlan tradePlan) { if (!_lockQuery) { if (tradePlan != null && tradePlan.OpenCloseDates != null && tradePlan.OpenCloseDates.Any()) { _lockQuery = true; DateTime openDate = ApplicationParameter.ServerTimeNow.Date, closeDate = ApplicationParameter.ServerTimeNow.Date.AddHours(24); IsBusy = true; openDate = tradePlan.OpenCloseDates[0].OpenDate; closeDate = tradePlan.OpenCloseDates[tradePlan.OpenCloseDates.Count - 1].CloseDate; _quoteDataService.GetHistoryCycleData(_currentGoods, CycleType.Tik, openDate, closeDate, 0, QueryTikSuccess, QueryTikError); } } } /// /// /// /// protected override void QueryReckenSuccess(List tradeDays) { base.QueryReckenSuccess(tradeDays); var goodsPlan = tradeDays.FirstOrDefault((item) => item.GoodsId == _currentGoods.GoodsId); if (goodsPlan == null) { goodsPlan = tradeDays.FirstOrDefault((item) => item.GoodsId == 0); } QueryQuoteTik(goodsPlan); } /// /// 查询历史Tik数据 /// /// 历史Tik数据 public void QueryTikSuccess(GoodsHistoryCycle historyCycle) { _lockQuery = false; IsBusy = false; if (historyCycle.SubDataPoints != null && historyCycle.SubDataPoints.Any()) { if (_listSubSource != null) { _listSubSource.InsertRange(0, historyCycle.SubDataPoints.ToList()); } else { _listSubSource = historyCycle.SubDataPoints.ToList(); } } ////累计实时行情 RegReadTimeQuote(); if (Count > 0) { CalcPageCountWithAdd() ; } SetPageDataSource(0); } public void QueryTikError(ErrorEntity error) { _lockQuery = false; IsBusy = false; ////累计实时行情 RegReadTimeQuote(); LogInfoHelper.WriteInfo(string.Format("请求分笔数据:{0}({1})", error.ReturnCode, error.ReturnDesc)); } private IEnumerable _showDataSource; /// /// 显示的列表 /// public IEnumerable ShowDataSource { get { return _showDataSource; } set { Set(() => ShowDataSource, ref _showDataSource, value); } } /// /// 当前页显示的列表最后一个数据的索引 /// public int CurrentIndex { get; set; } /// /// 当前页 /// public int CurrentPage { get; set; } /// /// 当前总页数 /// public int TotalPage { get; set; } /// /// 每页显示数量 /// public int Count { get; set; } private bool _isBidGoods; /// /// 竞价商品显示内容 /// public bool IsBidGoods { get { return _isBidGoods; } set { Set(() => IsBidGoods, ref _isBidGoods, value); } } private double _itemWidth; /// /// 设置列表项的大小 /// public double ItemWidth { get { return _itemWidth; } set {Set(() => ItemWidth, ref _itemWidth, value); } } /// /// 返回列表多少页 /// /// 每页显示多少数据 /// public int CalcPageCount(int pageItem) { Count = pageItem; TotalPage = 1; //默认显示一页 if (_listSubSource != null) { var count= (int)Math.Ceiling( _listSubSource.Count/(double)pageItem); TotalPage= count <= 0 ? 1 : count; } CurrentPage = TotalPage; ///默认显示最后一页 return TotalPage; } /// /// 增量取数据时更新总页数据 /// /// public int CalcPageCountWithAdd() { if (CurrentPage == 1&&TotalPage!=CurrentPage) { CurrentPage = TotalPage-1; ///从后倒数显示的当前页 } if (_listSubSource != null) { var count = (int)Math.Ceiling(_listSubSource.Count / (double)Count); TotalPage = count <= 0 ? 1 : count; var page = TotalPage - CurrentPage; CurrentPage = page>0?page:1; } return TotalPage; } /// /// 显示总页数 /// /// 0:更新当前页,大于0时显示下一页,小于0时,显示前一页 public void SetPageDataSource(int addPage) { var page = CurrentPage + addPage; if (page > TotalPage) { CurrentPage = TotalPage; } else if(page<=0) { if (_listSubSource.Any()) { if (CacheManager.TradeDayPlan.ContainsKey(_currentGoods.GoodsParameters.MarketTypeID)) { var tradePlan = CacheManager.TradeDayPlan[_currentGoods.GoodsParameters.MarketTypeID]; var isSub = _listSubSource[0].Time == tradePlan.OpenCloseDates[0].OpenDate; if (!isSub && !_lockQuery) ////向前获取数据 { _lockQuery = true; IsBusy = true; _quoteDataService.GetHistoryCycleData(_currentGoods, CycleType.Tik, tradePlan.OpenCloseDates[0].OpenDate, _listSubSource[0].Time, 0, QueryTikSuccess, QueryTikError); return; } } } CurrentPage = 1; } else { CurrentPage = page; } if (CurrentPage == TotalPage) { var index = _listSubSource.Count - Count < 0 ? 0 : _listSubSource.Count - Count - 1; ShowDataSource = _listSubSource.GetRange(index, _listSubSource.Count - index).Where(z=>z.CompleteQte > 0); CurrentIndex = _listSubSource.Count - 1; } else { var index = (CurrentPage-1)*Count; if (index <= 0) { index = 0; } ShowDataSource = _listSubSource.GetRange(index, Count).Where(z => z.CompleteQte > 0); CurrentIndex = index+Count - 1; } } /// /// 订阅行情 /// private void RegReadTimeQuote() { MessengerHelper.QuoteRegister>(this, MessengerTokens.ReceiveRealTikQuote, (quoteList) => { if (quoteList == null || !quoteList.Any()) return; foreach (var item in quoteList) { if (item == null) continue; if (_currentGoods.Symbol.ToLower() == item.Symbol.ToLower()) { if ((item.CurrentPrice == 0 || item.Date == DateTime.MinValue)) { continue; ////竞价不是成交的行情不显示分笔 } var penData = new SubPenData(_currentGoods.GoodsParameters.HqExchFigures) { Time = item.Date, Price = (double)item.CurrentPrice, CompleteQte = (double)item.CurrentVolume, }; if (null != _listSubSource) { _listSubSource.Add(penData); } else { _listSubSource = new List(); _listSubSource.Add(penData); } } } if (CurrentPage == TotalPage) { SetPageDataSource(0); } }); } } }