| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651 |
- 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;
- /// <summary>
- /// 当前账号
- /// </summary>
- public TradeAccount CurrentAccount
- {
- get
- {
- return UserManager.CurrentTradeAccount;
- }
- }
- /// <summary>
- /// 取价方式
- /// </summary>
- public ePriceMode PriceMode
- {
- get { return _priceMode; }
- set
- {
- Set(() => PriceMode, ref _priceMode, value);
- }
- }
- /// <summary>
- /// 买卖方向
- /// </summary>
- public Direction Direction
- {
- get
- {
- return _direction;
- }
- set
- {
- if (value == Direction.unSet)
- {
- return;
- }
- Set(() => Direction, ref _direction, value);
- }
- }
- private string _winTitle;
- /// <summary>
- /// 标题
- /// </summary>
- public string WinTitle
- {
- get { return _winTitle; }
- set { Set(() => WinTitle, ref _winTitle, value); }
- }
- private List<SaleGoods> _saleGoodses;
- /// <summary>
- /// 发售商品
- /// </summary>
- public List<SaleGoods> SaleGoodses
- {
- get { return _saleGoodses; }
- set { Set(() => SaleGoodses, ref _saleGoodses, value); }
- }
- private SaleGoods _currentSaleGoods;
- /// <summary>
- /// 当前发售商品
- /// </summary>
- public SaleGoods CurrentSaleGoods
- {
- get { return _currentSaleGoods; }
- set
- {
- Set(() => CurrentSaleGoods, ref _currentSaleGoods, value);
- SetGoodsInit();
- }
- }
- private decimal _executePrice;
- /// <summary>
- /// 委托价格
- /// </summary>
- public decimal ExecutePrice
- {
- get { return _executePrice; }
- set
- {
- Set(() => ExecutePrice, ref _executePrice, value);
- }
- }
- private decimal _lot = decimal.Zero;
- /// <summary>
- /// 数量
- /// </summary>
- 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;
- /// <summary>
- ///设置按键不可用
- /// </summary>
- public bool OKButtonEnabled
- {
- get
- {
- return _oKButtonEnabled;
- }
- set
- {
- Set(() => OKButtonEnabled, ref _oKButtonEnabled, value);
- }
- }
- private bool _isBusy;
- /// <summary>
- /// 是否忙
- /// </summary>
- public bool IsBusy
- {
- get { return _isBusy; }
- set { Set(() => IsBusy, ref _isBusy, value); }
- }
- private decimal _quotePrice; //当前行情价
- /// <summary>
- /// 当前行情价
- /// </summary>
- public decimal QuotePrice
- {
- get
- {
- return _quotePrice;
- }
- set
- {
- Set(() => QuotePrice, ref _quotePrice, value);
- }
- }
- #region 申购单位
- private decimal _perOrderQty = decimal.Zero;
- /// <summary>
- /// 申购单位
- /// </summary>
- public decimal PerOrderQty
- {
- get
- {
- return _perOrderQty;
- }
- set
- {
- Set(() => PerOrderQty, ref _perOrderQty, value);
- }
- }
- #endregion
- #region 最小数量
- private decimal _minLot;
- /// <summary>
- /// 最小数量
- /// </summary>
- public decimal MinLot
- {
- get { return _minLot; }
- set { Set(() => MinLot, ref _minLot, value); }
- }
- #endregion
- private decimal _maxLot;
- /// <summary>
- /// 最大数量
- /// </summary>
- public decimal MaxLot
- {
- get { return _maxLot; }
- set { Set(() => MaxLot, ref _maxLot, value); }
- }
- private string _agreeUnitLot;
- /// <summary>
- /// 合约数量
- /// </summary>
- public string AgreeUnitLot
- {
- get { return _agreeUnitLot; }
- set { Set(() => AgreeUnitLot, ref _agreeUnitLot, value); }
- }
- /// <summary>
- /// 下单确定
- /// </summary>
- public RelayCommand<Window> OKCommand
- {
- get
- {
- return new RelayCommand<Window>((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<ISaleService>();
- 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;
- }
- /// <summary>
- /// 快速下单窗口取消
- /// </summary>
- public RelayCommand<Window> CancelCommand
- {
- get
- {
- return new RelayCommand<Window>((dialog) =>
- {
- dialog.DialogResult = false;
- });
- }
- }
- /// <summary>
- /// 提交成功返回
- /// </summary>
- /// <param name="order"></param>
- private void EntrurstSuccessCallBack(OrderDetail order)
- {
- IsBusy = false;
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- OKButtonEnabled = true;
-
- // IsBusy = false;
- CloseOrderWin();
- }));
- MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken);
- }
- /// <summary>
- /// 下单关窗口
- /// </summary>
- 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();
-
- }
- }
- /// <summary>
- /// 委托失败返回
- /// </summary>
- /// <param name="errorEntity"></param>
- 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<IGoodsService>();
- if (CacheManager.SaleGoodses != null && CacheManager.SaleGoodses.Any())
- {
- var goodsList = new List<SaleGoods>();
- 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;
- }
- /// <summary>
- /// 根据可用资金算出可下单的数量
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 每手下单所需资金
- /// </summary>
- /// <param name="price">价格</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取指定【交易费用类型】的费用
- /// </summary>
- /// <param name="tradeFeeType"></param>
- /// <param name="price"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|