using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Ioc; using Muchinfo.MTPClient.Data; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Data.Model.Account; using Muchinfo.MTPClient.Data.Model.GoodRules; using Muchinfo.MTPClient.Data.Model.Sale; using Muchinfo.MTPClient.Infrastructure.Cache; using Muchinfo.MTPClient.Infrastructure.Helpers; using Muchinfo.MTPClient.Infrastructure.MessageBox; using Muchinfo.MTPClient.Infrastructure.Utilities; using Muchinfo.MTPClient.IService; using Muchinfo.MTPClient.Resources; using Muchinfo.WPF.Controls.Windows; using System; using System.Collections.Generic; using System.Linq; //---------------------------------------------------------------- //Module Name: $safeprojectname$ //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime 2016/4/5 9:51:28 //Author //Description Create //---------------------------------------------------------------- using System.Windows; namespace Muchinfo.MTPClient.Sale.ViewModels { public class SaleOrderViewModel : ViewModelBase { private Direction _direction; ////买卖方向 private Window _openWindow; ///下单窗口 protected IGoodsService _goodsService; private ePriceMode _priceMode; /// /// 当前账号 /// public TradeAccount CurrentAccount { get { return UserManager.CurrentTradeAccount; } } /// /// 取价方式 /// public ePriceMode PriceMode { get { return _priceMode; } set { Set(() => PriceMode, ref _priceMode, value); } } /// /// 买卖方向 /// public Direction Direction { get { return _direction; } set { if (value == Direction.unSet) { return; } Set(() => Direction, ref _direction, value); } } private string _winTitle; /// /// 标题 /// public string WinTitle { get { return _winTitle; } set { Set(() => WinTitle, ref _winTitle, value); } } private List _saleGoodses; /// /// 发售商品 /// public List SaleGoodses { get { return _saleGoodses; } set { Set(() => SaleGoodses, ref _saleGoodses, value); } } private SaleGoods _currentSaleGoods; /// /// 当前发售商品 /// public SaleGoods CurrentSaleGoods { get { return _currentSaleGoods; } set { Set(() => CurrentSaleGoods, ref _currentSaleGoods, value); SetGoodsInit(); } } private decimal _executePrice; /// /// 委托价格 /// public decimal ExecutePrice { get { return _executePrice; } set { Set(() => ExecutePrice, ref _executePrice, value); } } private decimal _lot = decimal.Zero; /// /// 数量 /// public decimal Lot { get { return _lot; } set { Set(() => Lot, ref _lot, value); _agreeUnitLot = (_currentSaleGoods == null) ? "0" : _currentSaleGoods.AgreeUnit * _lot + string.Empty; RaisePropertyChanged(() => AgreeUnitLot); } } private bool _oKButtonEnabled = true; /// ///设置按键不可用 /// public bool OKButtonEnabled { get { return _oKButtonEnabled; } set { Set(() => OKButtonEnabled, ref _oKButtonEnabled, value); } } private bool _isBusy; /// /// 是否忙 /// public bool IsBusy { get { return _isBusy; } set { Set(() => IsBusy, ref _isBusy, value); } } private decimal _quotePrice; //当前行情价 /// /// 当前行情价 /// public decimal QuotePrice { get { return _quotePrice; } set { Set(() => QuotePrice, ref _quotePrice, value); } } #region 申购单位 private decimal _perOrderQty = decimal.Zero; /// /// 申购单位 /// public decimal PerOrderQty { get { return _perOrderQty; } set { Set(() => PerOrderQty, ref _perOrderQty, value); } } #endregion #region 最小数量 private decimal _minLot; /// /// 最小数量 /// public decimal MinLot { get { return _minLot; } set { Set(() => MinLot, ref _minLot, value); } } #endregion private decimal _maxLot; /// /// 最大数量 /// public decimal MaxLot { get { return _maxLot; } set { Set(() => MaxLot, ref _maxLot, value); } } private string _agreeUnitLot; /// /// 合约数量 /// public string AgreeUnitLot { get { return _agreeUnitLot; } set { Set(() => AgreeUnitLot, ref _agreeUnitLot, value); } } /// /// 下单确定 /// public RelayCommand OKCommand { get { return new RelayCommand((dialog) => { OKButtonEnabled = false; string errorMsg = string.Empty; //bool validateBool = true;////内容验证 bool validateBool = Validated(ref errorMsg); ////内容验证 if (validateBool) { _openWindow = dialog; var newOrder = new NewEntrustOrder() { BuyOrSell = Direction, OperaterId = UserManager.CurrentTradeAccount.AccountId, // GoodsCode = this.CurrentGoods.GoodsCode, // MarketId = (uint)this.CurrentSaleGoods.MarketId, GoodsId = (uint)CurrentSaleGoods.GoodsId, AccountType = UserManager.CurrentTradeAccount.AccountType, BuildType = OpenCloseMode.BUILDTYPE_OPEN, EntrurstTime = ApplicationParameter.ServerTimeNow, MemberAreaId = UserManager.CurrentTradeAccount.MemberAreaId, EntrustQuantity = Lot, PriceMode = this.PriceMode, }; // if (UserManager.CurrentTradeAccount.FundsAccounts.Any() && //UserManager.CurrentTradeAccount.FundsAccounts[0] != null) // { // newOrder.AccountId = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId; // } // else // { // newOrder.AccountId = UserManager.CurrentTradeAccount.AccountId; // } newOrder.EntrustPrice = PriceMode == ePriceMode.PRICEMODE_LIMIT ? ExecutePrice : QuotePrice; // newOrder.CurtQuotePrice = QuotePrice; if (ComfirmCheck(newOrder)) { IsBusy = true; var saleService = SimpleIoc.Default.GetInstance(); saleService.SaleEntrurstOrder(newOrder, EntrurstSuccessCallBack, EntrurstErrorCallBack); } else { OKButtonEnabled = true; } } else { MessageBoxHelper.ShowInfo(errorMsg, Client_Resource.MessageBox_Error_Title); OKButtonEnabled = true; } }); } } private bool ComfirmCheck(NewEntrustOrder order) { var userInfo = UserManager.GetCurrentUserInfo(); ////交易菜单设置 if (userInfo.TradeParams == null) { userInfo.TradeParams = new TradeParams(); } var result = true; if (userInfo.TradeParams.IsOrderComfrim) { var comfim = new OrderComfirmModel() { GoodsName = CurrentSaleGoods.GoodsName, Lot = order.EntrustQuantity, Direction = order.BuyOrSell , OrderPrice = order.EntrustPrice, }; var comfirm = new OrderComfirm() {DataContext = comfim, Owner = Application.Current.MainWindow}; if (comfirm.ShowDialog() != true) { result = false; } } return result; } /// /// 快速下单窗口取消 /// public RelayCommand CancelCommand { get { return new RelayCommand((dialog) => { dialog.DialogResult = false; }); } } /// /// 提交成功返回 /// /// private void EntrurstSuccessCallBack(OrderDetail order) { IsBusy = false; Application.Current.Dispatcher.BeginInvoke(new Action(() => { OKButtonEnabled = true; // IsBusy = false; CloseOrderWin(); })); MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken); } /// /// 下单关窗口 /// private void CloseOrderWin() { var userInfo = UserManager.GetCurrentUserInfo(); ////交易菜单设置 if (userInfo.TradeParams == null) { userInfo.TradeParams = new TradeParams(); } if (userInfo.TradeParams.IsSuccessComfrim) ////下单成功是否关窗口 { MessageBoxHelper.ShowSuccess(Client_Resource.Order_Success_Result, Client_Resource.Open_MessageBox_Title); } if (_openWindow != null) { _openWindow.Close(); this.Cleanup(); } } /// /// 委托失败返回 /// /// private void EntrurstErrorCallBack(ErrorEntity errorEntity) { IsBusy = false; Application.Current.Dispatcher.BeginInvoke(new Action(() => { OKButtonEnabled = true; ErrorManager.ShowReturnError(errorEntity, Client_Resource.UI2014_Tips, true); if (_openWindow != null) { _openWindow.Close(); this.Cleanup(); } // IsBusy = false; })); } public SaleOrderViewModel(SaleGoods saleGoods) { _goodsService = SimpleIoc.Default.GetInstance(); if (CacheManager.SaleGoodses != null && CacheManager.SaleGoodses.Any()) { var goodsList = new List(); foreach (var key in CacheManager.SaleGoodses.Keys) { goodsList.AddRange(CacheManager.SaleGoodses[key]); } SaleGoodses = goodsList.Where((item) => item.RunSteps == eRunStep.RUNSTEP_BIDDINGPRICE).ToList(); } CurrentSaleGoods = saleGoods; //NEW 申购单位 【错误 #29137】 var tempPerOrderQty = _goodsService.GetGoodsParamerRule((int)CurrentSaleGoods.GoodsId, CurrentSaleGoods.TradeMode, GoodsTradeConts.MinSalePerOrderQty); PerOrderQty = tempPerOrderQty.FeeValue; Lot = PerOrderQty; } private void SetGoodsInit() { if (CurrentSaleGoods != null) { PerOrderQty = 1; var tempPerOrderQty = _goodsService.GetGoodsParamerRule((int)CurrentSaleGoods.GoodsId, CurrentSaleGoods.TradeMode, GoodsTradeConts.MinSalePerOrderQty); if (tempPerOrderQty != null) { PerOrderQty = tempPerOrderQty.FeeValue; } QuotePrice = ExecutePrice = CurrentSaleGoods.SalePrice; Lot = PerOrderQty; MaxLot = Math.Round(_currentSaleGoods.PerMaxSubQty, 2, MidpointRounding.AwayFromZero); MinLot = Math.Round(_currentSaleGoods.PerMinSubQty, 2, MidpointRounding.AwayFromZero); var enableOpenNum = GetEnableNum(ExecutePrice); //根据资金算出可建仓数 if (enableOpenNum > 0) { //enableOpenNum = _currentSaleGoods.PerMinSubQty == 0 ? enableOpenNum : Math.Floor(enableOpenNum / _currentSaleGoods.PerMinSubQty) * _currentSaleGoods.PerMinSubQty; ////下单数量应该为最小的整数倍 enableOpenNum = PerOrderQty == 0 ? enableOpenNum : Math.Floor(enableOpenNum / PerOrderQty) * PerOrderQty; ////下单数量应该为申购单位的整数倍 MaxLot = Math.Min(enableOpenNum, MaxLot); } else { MaxLot = decimal.Zero; } MaxLot = Math.Round(MaxLot, 2, MidpointRounding.AwayFromZero); MinLot = Math.Round(MinLot, 2, MidpointRounding.AwayFromZero); } } public bool Validated(ref string msg) { if (Lot.ToString().Length <= 0 || Lot <= 0 || Lot == decimal.Zero || Lot > MaxLot) { //数量不在正确范围内 msg = Muchinfo.MTPClient.Resources.Client_Resource.ErrorNumRange; return false; } if (Lot > CurrentSaleGoods.PerMaxSubQty) { //msg = "申购数量超过单笔最大申购数量"; msg = Muchinfo.MTPClient.Resources.Client_Resource.ErrorSaleNum; return false; } if (Lot < CurrentSaleGoods.PerMinSubQty) { //msg = "申购数量应大于单笔最小申购数量"; msg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.Error_SaleNum, CurrentSaleGoods.PerMinSubQty); return false; } if (Lot != 0 && (Lot % PerOrderQty) != 0) { //数量输入有误!应为{设定申购单位}的倍数. msg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.SaleOrderViewModel_InputNumShouldBeRight, PerOrderQty); return false; } return true; } /// /// 根据可用资金算出可下单的数量 /// /// protected decimal GetEnableNum(decimal price) { decimal enablenum = 0; decimal usableFunds = 0; decimal dividernum = 0; //可用资金 usableFunds = UserManager.CurrentAccountUsableFunds(); dividernum = GetDivider(price); if (dividernum != 0) { var calcLot = usableFunds / dividernum; if (calcLot <= 0) { enablenum = 0; } else if (calcLot >= int.MaxValue) { enablenum = int.MaxValue; } else { enablenum = calcLot; //数量 } } return enablenum > 0 ? enablenum : 0; } /// /// 每手下单所需资金 /// /// 价格 /// private decimal GetDivider(decimal price) { decimal dividernum = 0; decimal dividernumChange = 0; if (Direction == Direction.Bid) { //【买】建仓手续费 dividernumChange = GetTradeFeeBy(eTradeFeeType.TRADEFEETYPE_TRADECHARGE, price); } else { //【卖】建仓手续费 dividernumChange = GetTradeFeeBy(eTradeFeeType.TRADEFEETYPE_TRADECHARGE, price); } //竞价全额 if (CurrentSaleGoods != null && CurrentSaleGoods.MoneyMode == eMoneyMode.MONEYMODE_PAY) { //dividernum = _currentGoods.CurrentPrice == 0 ? _currentGoods.LastClose : _currentGoods.CurrentPrice; dividernum = price * CurrentSaleGoods.AgreeUnit + dividernumChange; return dividernum; } return GetTradeFeeBy(eTradeFeeType.TRADEFEETYPE_TRADECHARGE, price) + dividernumChange; } /// /// 获取指定【交易费用类型】的费用 /// /// /// /// private decimal GetTradeFeeBy(eTradeFeeType tradeFeeType, decimal price) { decimal dividerTradeFee = 0; #region 类型:建仓保证金 //获取交易费用类型=建仓保证金 var eTradeFee = _goodsService.GetTradeFee(CurrentSaleGoods.TradeMode, tradeFeeType); GoodsFeeType goodsfee = null; GoodsFeeType accountfee = null; #endregion if (CurrentSaleGoods != null && CacheManager.TradeGoodsFeeRules != null && CacheManager.TradeGoodsFeeRules.ContainsKey((int)CurrentSaleGoods.GoodsId)) { #region 类型:建仓保证金 if (CacheManager.TradeGoodsFeeRules[(int)CurrentSaleGoods.GoodsId].GoodsFees.ContainsKey((int)eTradeFee)) { goodsfee = CacheManager.TradeGoodsFeeRules[(int)CurrentSaleGoods.GoodsId].GoodsFees[(int)eTradeFee]; } else if (CacheManager.TradeGoodsFeeRules[(int)CurrentSaleGoods.GoodsId].AccountGoodsFees.ContainsKey((int)eTradeFee)) { accountfee = CacheManager.TradeGoodsFeeRules[(int)CurrentSaleGoods.GoodsId].AccountGoodsFees[(int)eTradeFee]; } #endregion } #region 类型:建仓保证金 //商品是总额百分比模式 if (goodsfee != null && (int)eCalMode.CALMODE_FIXEDRATIO == goodsfee.FeeMode) { //可用资金/合约单位*价格*占用百分比 或者可用资金/合约单位固定值 if (accountfee != null) { switch ((int)accountfee.FeeMode) { case (int)eCalMode.CALMODE_FIXEDRATIO: dividerTradeFee = (CurrentSaleGoods.AgreeUnit) * price * (accountfee.FeeValue + goodsfee.FeeValue); break; case (int)eCalMode.CALMODE_FIXEDNUMBER: dividerTradeFee = (CurrentSaleGoods.AgreeUnit) * price * goodsfee.FeeValue + accountfee.FeeValue; break; } } else { dividerTradeFee = (CurrentSaleGoods.AgreeUnit) * price * goodsfee.FeeValue; } } //商品是合约单位固定值的模式 else if (goodsfee != null && (int)eCalMode.CALMODE_FIXEDNUMBER == goodsfee.FeeMode) { //可用资金/合约单位*价格*占用百分比 或者可用资金/合约单位固定值 if (accountfee != null) { switch ((int)accountfee.FeeMode) { case (int)eCalMode.CALMODE_FIXEDRATIO: dividerTradeFee = (CurrentSaleGoods.AgreeUnit) * price * accountfee.FeeValue + goodsfee.FeeValue; break; case (int)eCalMode.CALMODE_FIXEDNUMBER: dividerTradeFee = accountfee.FeeValue + goodsfee.FeeValue; break; } } else { dividerTradeFee = goodsfee.FeeValue; } } #endregion return dividerTradeFee; } } }