using System.Collections.ObjectModel; using System.Windows.Controls; using System.Windows.Input; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Ioc; using Muchinfo.MTPClient.Analysis.Views.PriceCard; 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; using Muchinfo.PC.Common.Extensions; using System; using System.Collections.Generic; using System.Linq; //---------------------------------------------------------------- //Module Name: $safeprojectname$ //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime 2015/12/12 14:10:10 //Author //Description Create //---------------------------------------------------------------- using System.Threading.Tasks; using System.Windows; namespace Muchinfo.MTPClient.Analysis.ViewModels { public class QuotePriceViewModel : ViewModelBase, IDisposable { public IQuoteDataService _iQuoteDataService; // private List _allSource = new List(); private QuoteGoods _currentGoods; //当前显示的商品 private short _tickCount = 100; private FrameworkElement _goodsCategory; private static object _lockObject = new object(); /// /// 商品分类显示盘面 /// public FrameworkElement GoodsCategory { get { return _goodsCategory; } set { Set(() => GoodsCategory, ref _goodsCategory, value); } } /// /// 报价牌中的商品 /// public QuoteGoods Goods { get { return _currentGoods; } set { bool isupdate = _currentGoods == null || !_currentGoods.Equals(value); Set(() => Goods, ref _currentGoods, value); if (value != null) { UiInitQuotePriceView( ); } if (isupdate) { UpdateCommissions(_currentGoods); InitData(); } RaisePropertyChanged(()=> IsQuoteQueueVbs); } } /// /// 是否显示大力士 /// public bool IsQuoteQueueVbs { get { if (_currentGoods == null) return false; switch (_currentGoods.TradeMode) { case eTradeMode.TRADEMODE_MARKETMAKE: return false; ////做市不显示 case eTradeMode.TRADEMODE_BIDDING: var showType = (QueueShowType) ApplicationParameter.QuoteQueueInfo; return showType == QueueShowType.Bid || showType == QueueShowType.All; case eTradeMode.TRADEMODE_BIDDINGMARKETMAKE: var showType1 = (QueueShowType) ApplicationParameter.QuoteQueueInfo; return showType1 == QueueShowType.BidMake || showType1 == QueueShowType.All; default: return false; } } } #region 是否显示买卖更多=》点选市场才显示 /// /// 是否显示买卖更多=》点选市场才显示 /// public Visibility IsVisibility_More { get { if (Goods.TradeMode == eTradeMode.TRADEMODE_LISTING_SELECT) { return Visibility.Visible; } else { return Visibility.Collapsed; } } } #endregion #region 是否显示间隔线-分割买卖五档 /// /// 是否显示间隔线-分割买卖五档 /// public Visibility IsVisibility_Line { get { if (Goods.TradeMode != eTradeMode.TRADEMODE_LISTING_SELECT) { return Visibility.Visible; } else { return Visibility.Collapsed; } } } #endregion private RelayCommand _getAskCommissionCommand; /// /// 卖五档 更多 /// public RelayCommand GetAskCommissionCommand { get { return _getAskCommissionCommand ?? (_getAskCommissionCommand = new RelayCommand( p => { var SearchParameters = new SearchCommissionsParameter(); SearchParameters.BuyOrSell = Direction.Ask; SearchParameters.CurrentContenModel = TabContetMode.HistoryCommissions; SearchParameters.GoodsId = Goods.GoodsId; // MessengerHelper.DefaultSend(TabContetMode.HistoryCommissions, MessengerTokens.HistoryCommissionsTab); //MessengerHelper.DefaultSend(false, MessengerTokens.IsShowSellCommondBtn); MessengerHelper.DefaultSend(SearchParameters, MessengerTokens.HistoryCommissionsTab); })); } } private RelayCommand _getBidCommissionCommand; /// /// 买五档 更多 /// public RelayCommand GetBidCommissionCommand { get { return _getBidCommissionCommand ?? (_getBidCommissionCommand = new RelayCommand( p => { var SearchParameters = new SearchCommissionsParameter(); SearchParameters.BuyOrSell = Direction.Bid; SearchParameters.CurrentContenModel = TabContetMode.HistoryCommissions; SearchParameters.GoodsId = Goods.GoodsId; // MessengerHelper.DefaultSend(TabContetMode.HistoryCommissions, MessengerTokens.HistoryCommissionsTab); // MessengerHelper.DefaultSend(false, MessengerTokens.IsShowBuyCommondBtn); MessengerHelper.DefaultSend(SearchParameters, MessengerTokens.HistoryCommissionsTab); })); } } private bool _isBidGoods; /// /// 竞价商品显示内容 /// public bool IsBidGoods { get { return _isBidGoods; } set { Set(() => IsBidGoods, ref _isBidGoods, value); } } private double _showCount; /// /// 显示数量 /// public double ShowCount { get { return _showCount; } set { _showCount = value; } } private bool _isBusy; /// /// 显示忙等待 /// public bool IsBusy { get { return _isBusy; } set { Set(() => IsBusy, ref _isBusy, value); } } private List _listSubSource=new List(); private ObservableCollection _dataSource=new ObservableCollection(); /// /// 分笔数据 /// public ObservableCollection DataSource { get { return _dataSource; } set { Set(() => DataSource, ref _dataSource, value); } } public QuotePriceViewModel() { _iQuoteDataService = SimpleIoc.Default.GetInstance(); if (ApplicationParameter.QuoteTickCount > 0) { _tickCount = ApplicationParameter.QuoteTickCount; } } public void TaskInitQuotePriceView(QuoteTradePlan tradePlan,QuoteGoods goods) { //#if DEBUG // DataSource = new ObservableCollection(); // int count = 60; // Random random=new Random(ApplicationParameter.ServerTimeNow.Millisecond); // for (int i = 0; i < count; i++) // { // var penData = new SubPenData(Goods.GoodsParameters.HqExchFigures); // penData.Time = ApplicationParameter.ServerTimeNow.AddMinutes(-count + i); // penData.Price = random.Next(4000, 4500); // penData.CompleteQte = random.Next(20, 100); // DataSource.Add(penData); // } // ////累计实时行情 // RegReadTimeQuote(); //#else Goods = goods; if (tradePlan != null &&tradePlan.OpenCloseDates!=null&& tradePlan.OpenCloseDates.Any()) { DateTime openDate = ApplicationParameter.ServerTimeNow.Date, closeDate = ApplicationParameter.ServerTimeNow.Date.AddHours(24); openDate = tradePlan.OpenCloseDates[0].OpenDate; closeDate = tradePlan.OpenCloseDates[tradePlan.OpenCloseDates.Count - 1].CloseDate; //IsBusy = true; //暂时将加载状态条去掉,后续开启 _iQuoteDataService.GetHistoryCycleData(Goods, CycleType.Tik, openDate, closeDate, _tickCount, QueryTikSuccess, QueryTikError); } //#endif } /// /// 查询历史Tik数据 /// /// 历史Tik数据 public void QueryTikSuccess(GoodsHistoryCycle historyCycle) { IsBusy = false; if (historyCycle != null && historyCycle.Symbol.ToUpper() == _currentGoods.Symbol.ToUpper()) ///当前商品是否一致 { if (historyCycle.SubDataPoints != null && historyCycle.SubDataPoints.Any()) { // 任务 #90594 _listSubSource = historyCycle.SubDataPoints.ToList().Where(z=>z.CompleteQte > 0).ToList(); //todo:滚动到最后一条数据 MessengerHelper.DefaultSend(this, MessengerTokens.UpdateTik); } } ////累计实时行情 RegReadTimeQuote(); } #region 大力士 private Commission _currentCommission; /// /// 当前选择的五档 /// public Commission CurrentCommission { get { return _currentCommission; } set { Set(() => CurrentCommission, ref _currentCommission, value); AnalysisPackage(_currentCommission); } } private List _entructInfos; /// /// 显示委托信息列表 /// public List EntructInfos { get { return _entructInfos; } set { Set(() => EntructInfos, ref _entructInfos, value); } } /// /// 委托信息显示解析(大利市) /// private void AnalysisPackage(Commission commission) { var entructInfos = new List(); if (_currentGoods != null && commission != null) { var askBidQueue = commission.Direction == Direction.Ask ? _currentGoods.AskQueueInfo : _currentGoods.BidQueueInfo; if (!string.IsNullOrWhiteSpace(askBidQueue)) { string[] queueStrings = askBidQueue.Split(';'); if (queueStrings.Length >= (commission.Index - 1)) ////分解5档 { var accountsOrder = queueStrings[commission.Index - 1]; if (!string.IsNullOrWhiteSpace(accountsOrder)) { string[] dataVals = accountsOrder.Split(','); for (int i = 0; i < dataVals.Length; i += 2) { var entruct = new EntructInfo(); entruct.AccountCode = dataVals[i]; if (i + 1 < dataVals.Length) { entruct.Qty = dataVals[i + 1]; } entructInfos.Add(entruct); } } } } } EntructInfos = entructInfos; } #endregion public void QueryTikError(ErrorEntity error) { IsBusy = false; ////累计实时行情 RegReadTimeQuote(); LogInfoHelper.WriteInfo(string.Format("请求分笔数据:{0}({1})", error.ReturnCode, error.ReturnDesc)); } /// /// 更新商品 /// /// 商品信息 public void UpdateGoods(QuoteGoods goods) { Goods = goods; _listSubSource.Clear(); DataSource.Clear(); } public void UiInitQuotePriceView() { Application.Current.Dispatcher.BeginInvoke(new Action(() => { switch (_currentGoods.TradeMode) { case eTradeMode.TRADEMODE_MARKETMAKE: GoodsCategory = new PriceCardDiskAskBid(this); IsBidGoods = false; break; case eTradeMode.TRADEMODE_BIDDINGMARKETMAKE: case eTradeMode.TRADEMODE_BIDDING: case eTradeMode.TRADEMODE_LISTING_SELECT: case eTradeMode.TRADEMODE_ENTRUST_HEDGE: GoodsCategory = new PriceCardDiskMakeMatch2(this); IsBidGoods = true; break; default: GoodsCategory = new PriceCardDiskAskBid(this); break; } })); } private void RegReadTimeQuote() { MessengerHelper.QuoteUnregister>(this, MessengerTokens.ReceiveRealTikQuote); MessengerHelper.QuoteRegister>(this, MessengerTokens.ReceiveRealTikQuote, (quoteList) => { if (quoteList == null || !quoteList.Any()) return; lock (_lockObject) { foreach (var item in quoteList) { if (item == null) continue; if (Goods.Symbol.ToUpper() == item.Symbol.ToUpper()) { UpdateCommissions(item); ////更新五档 if ((item.CurrentPrice==0||item.Date==DateTime.MinValue)) { continue; ////竞价不是成交的行情不显示分笔 } var penData = new SubPenData(Goods.GoodsParameters.HqExchFigures) { Time = item.Date, Price =(double) item.CurrentPrice, CompleteQte =(double) item.CurrentVolume, IsChangedByTotalVolume = item.IsChangedByTotalVolume, }; // 任务 #90594 if (penData.CompleteQte <= 0 || !penData.IsChangedByTotalVolume) continue; if (null != _listSubSource) { if (_listSubSource.Count >= _tickCount) { var removeNum = _listSubSource.Count - (_tickCount - 1); //移除数量 for (int i = 0; i < removeNum; i++) { _listSubSource.RemoveAt(0); } } _listSubSource.Add(penData); MessengerHelper.DefaultSend(this, MessengerTokens.UpdateTik); } else { _listSubSource = new List(); _listSubSource.Add(penData); MessengerHelper.DefaultSend(this, MessengerTokens.UpdateTik); } break; } } } }); } public void Dispose() { this.Cleanup(); //if (this._allSource != null) //{ // foreach (var subPenData in _allSource) // { // subPenData.Dispose(); // } // this._allSource.Clear(); //} //this._allSource = null; } /// /// 更新数据 /// public void UpdateDataSource(int showCount) { if (_listSubSource.Any()) { var index = _listSubSource.Count - showCount < 0 ? 0 : _listSubSource.Count - showCount; DataSource =new ObservableCollection(_listSubSource.GetRange(index, _listSubSource.Count - index)); } } public void SendQuoteTick() { if (_currentGoods!=null) { MessengerHelper.DefaultSend(_currentGoods, MessengerTokens.ShowTikDetail); } } #region 切换商品 /// /// 下一商品 /// public RelayCommand NextCommand { get { return new RelayCommand(() => { MessengerHelper.DefaultSend(true, MessengerTokens.ChartGoodsChange); }, IsCanNext); } } /// /// 前一个商品 /// public RelayCommand ForwordCommand { get { return new RelayCommand(() => { MessengerHelper.DefaultSend(false, MessengerTokens.ChartGoodsChange); }, IsCanForword); } } /// /// 是否可前一个 /// /// private bool IsCanForword() { if (_currentGoods == null) { return false; } var goodsList = CacheManager.CacheGoodsBaseInfos.Where((item) => item.TradeMode != eTradeMode.TRADEMODE_SALE).ToList(); if (!goodsList.Any()) { return false; } var index = goodsList.FindIndex((item) => _currentGoods.Symbol == item.Symbol); return index != -1 && index != 0; ///当前是否为第一个 } /// /// 是否可下一个商品 /// /// private bool IsCanNext() { if (_currentGoods == null) { return false; } var goodsList = CacheManager.CacheGoodsBaseInfos.Where((item) => item.TradeMode != eTradeMode.TRADEMODE_SALE).ToList(); if (!goodsList.Any()) { return false; } var index = goodsList.FindIndex((item) => _currentGoods.Symbol == item.Symbol); return index != -1 && index != goodsList.Count - 1; ///当前是否为第一个 } #endregion #region 五档 private List _commissions; /// /// 卖五档 /// public List Commissions { get { return _commissions; } set { Set(() => Commissions, ref _commissions, value); } } private List _bidcommissions; /// /// 买五档 /// public List BidCommissions { get { return _bidcommissions; } set { Set(() => BidCommissions, ref _bidcommissions, value); } } /// /// 设置买卖档 /// /// if set to true [bid]. /// The commissions. /// List{Commission}. private List SortCommissions(bool bid, Commission[] commissions, string formatStr) { var commissionLsit = new List(); int index = 1; foreach (var commission in commissions) { commission.Index = index; commission.FormatString = formatStr; commissionLsit.Add(commission); index++; } if (!bid) { commissionLsit = commissionLsit.OrderByDescending((item) => item.Index).ToList(); } return commissionLsit; } /// /// 更新五档行情 /// /// private void UpdateCommissions(QuoteGoods goods) { if (goods != null) { //var askCommissions = SortCommissions(false, goods.AskList, Goods.FormatPrice); //var bidCommissions = SortCommissions(true, goods.BidList, Goods.FormatPrice); //askCommissions.AddRange(bidCommissions); //Commissions = askCommissions; Commissions = SortCommissions(false, goods.AskList, Goods.FormatPrice); BidCommissions = SortCommissions(true, goods.BidList, Goods.FormatPrice); } } #endregion #region 是否显示换手率 private void InitData() { HiddenDataByMarket(); } /// /// 隐藏数据逻辑 /// private void HiddenDataByMarket() { if (_currentGoods != null && _currentGoods.GoodsParameters != null) switch (_currentGoods.GoodsParameters.TradeMode) { case eTradeMode.TRADEMODE_MARKETMAKE: case eTradeMode.TRADEMODE_BIDDING: IsVisibilityTurnoverRate = Visibility.Visible; break; case eTradeMode.TRADEMODE_BIDDINGMARKETMAKE: //换手率混合市场应隐藏 IsVisibilityTurnoverRate = Visibility.Collapsed; break; default: IsVisibilityTurnoverRate = Visibility.Visible; break; } } private Visibility _isVisibilityTurnoverRate = Visibility.Visible; /// /// 是否显示换手率- /// public Visibility IsVisibilityTurnoverRate { get { return _isVisibilityTurnoverRate; } set { Set(() => IsVisibilityTurnoverRate, ref _isVisibilityTurnoverRate, value); } } #endregion } }