using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Ioc; using Muchinfo.MTPClient.Data; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Helper; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Data.Model.Account; using Muchinfo.MTPClient.Infrastructure.Utilities; using Muchinfo.MTPClient.IService; using Muchinfo.MTPClient.Resources; using System; using System.Collections.Generic; using System.IO; //---------------------------------------------------------------- //Module Name: $safeprojectname$ //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime 2016/1/16 10:06:39 //Author //Description Create //---------------------------------------------------------------- using System.Windows; using System.Windows.Media.Imaging; namespace Muchinfo.MTPClient.Trade.ViewModels { public class TradeOrderBase : ViewModelBase { private QuoteGoods _currentGoods; ////当前商品 private bool _isBidDirection = true; private ExpirationType _currentExpirationType = ExpirationType.Today; private OrderType _currentOrderType; ////订单类型 private decimal _pips = 5; //// 成交点差 protected IOrderService _orderService; /// LimitOrderVisibility /// private Visibility _limitOrderGridVisibility; private decimal _lot; /// /// MarketOrderVisibility /// private Visibility _marketOrderGridVisibility; public TradeOrderBase(QuoteGoods currentGoods, OrderType orderType) { _currentGoods = currentGoods; CurrentOrderType = orderType; _orderService = SimpleIoc.Default.GetInstance(); IsBidDirection = true; } /// /// 价格格式化 /// public string PriceFormat { get { if (this._currentGoods != null) { return _currentGoods.FormatPrice; } else { return "F0"; } } } /// /// 表单买卖方向 /// public bool IsBidDirection { get { return _isBidDirection; } set { Set(() => IsBidDirection, ref _isBidDirection, value); SetQuotePrice(); } } /// /// 当前商品 /// public QuoteGoods CurrentGoods { get { return _currentGoods; } } /// /// Sets and gets the CurrentOrderType property. /// Changes to that property's value raise the PropertyChanged event. /// public OrderType CurrentOrderType { get { return _currentOrderType; } set { Set(() => CurrentOrderType, ref _currentOrderType, value); if (value == OrderType.MarketOpenOrder || value == OrderType.BidMarketOpen) { MarketOrderGridVisibility = Visibility.Visible; LimitOrderGridVisibility = Visibility.Collapsed; } else if (value == OrderType.LimitOpenOrder || value == OrderType.BidLimitOpen) { MarketOrderGridVisibility = Visibility.Collapsed; LimitOrderGridVisibility = Visibility.Visible; } } } /// /// Sets and gets the LimitOrderVisibility property. /// Changes to that property's value raise the PropertyChanged event. /// public Visibility LimitOrderGridVisibility { get { return _limitOrderGridVisibility; } set { Set(() => LimitOrderGridVisibility, ref _limitOrderGridVisibility, value); } } /// /// Sets and gets the Lot property. /// Changes to that property's value raise the PropertyChanged event. /// public decimal Lot { get { return _lot; } set { Set(() => Lot, ref _lot, value); // RaisePropertyChanged(() => LotVailedString); } } /// /// Sets and gets the MarketOrderVisibility property. /// Changes to that property's value raise the PropertyChanged event. /// public Visibility MarketOrderGridVisibility { get { return _marketOrderGridVisibility; } set { //if (_marketOrderGridVisibility == Visibility.Visible) //{ // WindowHeight = 580; //} //else //{ // WindowHeight = 580; //} Set(() => MarketOrderGridVisibility, ref _marketOrderGridVisibility, value); } } /// /// 商品图片资源 /// public BitmapImage ImageSource { get { if (File.Exists(UserManager.SysConfigFolderPath + "\\GoodsImages\\" + _currentGoods.GoodsCode + "." + ApplicationParameter.ImageExtension)) { return new BitmapImage(new Uri(UserManager.SysConfigFolderPath + "\\GoodsImages\\" + _currentGoods.GoodsCode + "." + ApplicationParameter.ImageExtension)); } else { return null; } } } /// /// 图片可见性 /// public Visibility ImageVisibility { get { if (File.Exists(UserManager.SysConfigFolderPath + "\\GoodsImages\\" + _currentGoods.GoodsCode + "." + ApplicationParameter.ImageExtension)) { return Visibility.Visible; } else { return Visibility.Collapsed; } } } /// /// Sets and gets the Pips property. /// Changes to that property's value raise the PropertyChanged event. /// public decimal Pips { get { return _pips; } set { Set(() => Pips, ref _pips, value); } } /// /// 是否允许点差 /// private bool _isAllowPips; /// /// 是否允许点差 /// public bool IsAllowPips { get { return _isAllowPips; } set { Set(() => IsAllowPips, ref _isAllowPips, value); } } private bool _isBidMarket; /// /// 是否是竞价商品 /// public bool IsBidMarket { get { return _isBidMarket; } set { Set(() => IsBidMarket, ref _isBidMarket, value); } } /// /// 获取和设置the expiration types /// public Dictionary ExpirationTypes { get { var types = new Dictionary(); ExpirationType expirationType; try { string[] expirationData = ApplicationParameter.ExpirationType.Split(','); for (int i = 0; i < expirationData.Length; i++) { expirationType = (ExpirationType)System.Enum.Parse(typeof(ExpirationType), expirationData[i], true); types.Add(expirationType, expirationType.Discription()); } } catch (Exception e) { } //types.Add(ExpirationType.SomeTime, "指定时间有效"); //types.Add(ExpirationType.Weekday, ExpirationType.Weekday.Discription()); return types; } } /// /// Sets and gets the CurrentExpirationType property. /// Changes to that property's value raise the PropertyChanged event. /// public ExpirationType CurrentExpirationType { get { return _currentExpirationType; } set { Set(() => CurrentExpirationType, ref _currentExpirationType, value); } } private decimal _executePrice; /// /// 限价建仓单-执行价格 /// public decimal ExecutePrice { get { return _executePrice; } set { Set(() => ExecutePrice, ref _executePrice, value); } } private decimal _quotePrice; //当前行情价 /// /// 当前行情价 /// public decimal QuotePrice { get { return _quotePrice; } set { Set(() => QuotePrice, ref _quotePrice, value); } } #region 虚函数属性 /// /// 验证信息 /// /// public virtual bool Validated() { return true; } /// /// 获取和设置the order types /// public virtual Dictionary OrderTypes { get { var types = new Dictionary { {OrderType.MarketOpenOrder, Muchinfo_Resource.OrderType_Enum_MarketOpen}, {OrderType.LimitOpenOrder, Muchinfo_Resource.OrderType_Enum_LimitOpen} }; return types; } } /// /// 创建下单委托对象 /// /// 下单委托对象 public virtual NewEntrustOrder BuildEntrustOrder() { var entrustOrder = new NewEntrustOrder() { GoodsCode = this.CurrentGoods.GoodsCode, MarketId = (uint)this.CurrentGoods.GoodsParameters.MarketId, GoodId = (uint)this.CurrentGoods.GoodsParameters.GoodsId, AccountType = UserManager.CurrentTradeAccount.AccountType, BuyOrSell = IsBidDirection ? Direction.Bid : Direction.Ask, EntrustQuantity = Lot, AccountId = UserManager.CurrentTradeAccount.AccountId, OpenType = OpenClose.Open, EntrurstTime = ApplicationParameter.ServerTimeNow, MemberId = (uint)UserManager.CurrentTradeAccount.MemberAreaId, }; entrustOrder.OrderMode = OrderMode.InValid; // entrustOrder.TradeCloseMode = TradeCloseMode.IsVailed; entrustOrder.ValidType = CurrentExpirationType; return entrustOrder; } /// /// 提交表单 /// /// 成功回调 /// 错误返回 public virtual void PostOrder(Action successAction, Action errorAction) { } /// /// 设置当前下单价格 /// protected virtual void SetQuotePrice() { //todo:暂时设置默认值 if (CurrentGoods != null) { ExecutePrice = IsBidDirection ? CurrentGoods.BidPrice : CurrentGoods.AskPrice; QuotePrice = ExecutePrice; } } #endregion /// /// Cleanups this instance. /// public override void Cleanup() { ////取消注册消息 base.Cleanup(); ApplicationParameter.QuotationMessenger.Unregister(this); } } }