| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- 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<SubPenData> _listSubSource=new List<SubPenData>();
- ///分笔数据
- 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;
- /// <summary>
- /// 是否忙
- /// </summary>
- public bool IsBusy
- {
- get { return _isBusy; }
- set { Set(() => IsBusy, ref _isBusy, value); }
- }
- private decimal _lastClose;
- /// <summary>
- /// 昨收
- /// </summary>
- 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);
- }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="tradeDays"></param>
- protected override void QueryReckenSuccess(List<QuoteTradePlan> 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);
- }
- /// <summary>
- /// 查询历史Tik数据
- /// </summary>
- /// <param name="historyCycle">历史Tik数据</param>
- 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<SubPenData> _showDataSource;
- /// <summary>
- /// 显示的列表
- /// </summary>
- public IEnumerable<SubPenData> ShowDataSource
- {
- get { return _showDataSource; }
- set { Set(() => ShowDataSource, ref _showDataSource, value); }
- }
- /// <summary>
- /// 当前页显示的列表最后一个数据的索引
- /// </summary>
- public int CurrentIndex { get; set; }
- /// <summary>
- /// 当前页
- /// </summary>
- public int CurrentPage { get; set; }
- /// <summary>
- /// 当前总页数
- /// </summary>
- public int TotalPage { get; set; }
- /// <summary>
- /// 每页显示数量
- /// </summary>
- public int Count { get; set; }
- private bool _isBidGoods;
- /// <summary>
- /// 竞价商品显示内容
- /// </summary>
- public bool IsBidGoods
- {
- get { return _isBidGoods; }
- set
- {
- Set(() => IsBidGoods, ref _isBidGoods, value);
- }
- }
- private double _itemWidth;
- /// <summary>
- /// 设置列表项的大小
- /// </summary>
- public double ItemWidth
- {
- get { return _itemWidth; }
- set {Set(() => ItemWidth, ref _itemWidth, value); }
- }
-
- /// <summary>
- /// 返回列表多少页
- /// </summary>
- /// <param name="pageItem">每页显示多少数据</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 增量取数据时更新总页数据
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 显示总页数
- /// </summary>
- /// <param name="addPage">0:更新当前页,大于0时显示下一页,小于0时,显示前一页</param>
- 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;
- }
- }
-
- /// <summary>
- /// 订阅行情
- /// </summary>
- private void RegReadTimeQuote()
- {
- MessengerHelper.QuoteRegister<List<QuoteGoods>>(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<SubPenData>();
- _listSubSource.Add(penData);
- }
- }
- }
- if (CurrentPage == TotalPage)
- {
- SetPageDataSource(0);
- }
- });
- }
- }
- }
|