| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //----------------------------------------------------------------
- //Module Name: $safeprojectname$
- //Purpose:
- //CopyRight: Muchinfo
- //History:
- //----------------------------------------------------------------
- //DateTime 2016/1/16 18:44:34
- //Author
- //Description Create
- //----------------------------------------------------------------
- using System.Windows;
- using GalaSoft.MvvmLight;
- using GalaSoft.MvvmLight.Command;
- using GalaSoft.MvvmLight.Ioc;
- using GalaSoft.MvvmLight.Messaging;
- using Muchinfo.MTPClient.Data;
- using Muchinfo.MTPClient.Data.Enums;
- using Muchinfo.MTPClient.Data.Model;
- using Muchinfo.MTPClient.Data.Model.Account;
- using Muchinfo.MTPClient.Infrastructure.Cache;
- using Muchinfo.MTPClient.Infrastructure.Utilities;
- using Muchinfo.MTPClient.IService;
- using Muchinfo.MTPClient.Resources;
- using Muchinfo.MTPClient.Service;
- using Muchinfo.WPF.Controls.Windows;
- namespace Muchinfo.MTPClient.Trade.ViewModels
- {
- public class CloseFrameViewModel : ViewModelBase
- {
-
- /// <summary>
- /// 是否忙,显示等待控件
- /// </summary>
- private bool _isBusy;
- /// <summary>
- /// 是否忙,显示等待控件
- /// </summary>
- public bool IsBusy
- {
- get
- {
- return _isBusy;
- }
- set
- {
- Set(() => IsBusy, ref _isBusy, value);
- }
- }
- private string _busyTips;
- /// <summary>
- /// 服务忙提示
- /// </summary>
- public string BusyTips
- {
- get
- {
- return _busyTips;
- }
- set
- {
- Set(() => BusyTips, ref _busyTips, value);
- }
- }
- /// <summary>
- /// 是否可选择商品
- /// </summary>
- public bool IsGoodsSelect { get; set; }
- private List<QuoteGoods> _closeGoodses = new List<QuoteGoods>();
- /// <summary>
- /// 平仓商品
- /// </summary>
- public List<QuoteGoods> CloseGoodses
- {
- get { return _closeGoodses; }
- set { Set(() => CloseGoodses, ref _closeGoodses, value); }
- }
- private QuoteGoods _currentGoods;
- /// <summary>
- /// 当前商品
- /// </summary>
- public QuoteGoods CurrentGoods
- {
- get { return _currentGoods; }
- set
- {
- Set(() => CurrentGoods, ref _currentGoods, value);
- if (IsGoodsSelect)
- {
- SetCurrentGoods();
- }
- }
- }
- private CloseOrderBase _closeOrderBase;
- public CloseOrderBase CloseOrderBase
- {
- get { return _closeOrderBase; }
- set
- {
- Set(() => CloseOrderBase, ref _closeOrderBase, value);
- }
- }
- private string _title = Muchinfo_Resource.ClosePage_Title;
- /// <summary>
- /// 平仓界面标题
- /// </summary>
- public string Title
- {
- get { return _title; }
- set { Set(() => Title, ref _title, value); }
- }
- public uint AccoutId
- {
- get
- {
- return UserManager.CurrentTradeAccount.AccountId;
- }
- }
- private IGoodsService _goodsService;
- /// <summary>
- /// Initializes a new instance of the <see cref="OpenFrameViewModel" /> class.
- /// </summary>
- /// <param name="quoteGoods">The quote goods.</param>
- /// <param name="orderType">Type of the order.</param>
- public CloseFrameViewModel(HoldingOrder holdOrder, OrderType orderType = OrderType.MarketCloseOrder)
- {
- var tempGoods=CacheManager.CacheGoodsBaseInfos.FirstOrDefault(
- (goods) => holdOrder.GoodsId==goods.GoodsId);
- CloseGoodses.Add(tempGoods);
-
- CurrentGoods = CloseGoodses.FirstOrDefault();
-
- //#if DEBUG
- // CloseOrderBase = new BidCloseViewModel(holdOrder, CurrentGoods, OrderType.BidMarketClose);
- //#else
- if (CurrentGoods != null && CurrentGoods.GoodsParameters.TradeMode == eTradeMode.TRADEMODE_BIDDING)
- {
- CloseOrderBase = new BidCloseViewModel(holdOrder, CurrentGoods, orderType);
- }
- else
- {
- CloseOrderBase = new MakeCloseViewModel(holdOrder, CurrentGoods, orderType);
- }
- Title = string.Format(Muchinfo_Resource.ClosePage_Tilte_OrderNum, holdOrder.OrderID);
- //#endif
- }
- public CloseFrameViewModel(HoldingSummary holdingSummary, OrderType orderType = OrderType.MarketCloseOrder)
- {
- CloseGoodses.Add(CacheManager.CacheGoodsBaseInfos.FirstOrDefault((goods) => holdingSummary.GoodsId==goods.GoodsId));
- CurrentGoods = CloseGoodses.FirstOrDefault();
-
- //#if DEBUG
- // CloseOrderBase = new BidCloseViewModel(holdingSummary, CurrentGoods, OrderType.BidMarketClose);
- //#else
- if (CurrentGoods.GoodsParameters.TradeMode == eTradeMode.TRADEMODE_BIDDING)
- {
- CloseOrderBase = new BidCloseViewModel(holdingSummary, CurrentGoods, orderType);
- }
- else
- {
- CloseOrderBase = new MakeCloseViewModel(holdingSummary, CurrentGoods);
- }
- //#endif
- }
- public CloseFrameViewModel(QuoteGoods selectQuoteGoods)
- {
- IsGoodsSelect = true;
- _goodsService = SimpleIoc.Default.GetInstance<IGoodsService>();
- var holdingSummaries = UserManager.GetCacheOrders<HoldingSummary>();
- var quoteGoodses = holdingSummaries.Select((holding) => holding.GoodsId).Distinct();
- CloseGoodses = new List<QuoteGoods>();
- var goodsDic = CacheManager.CacheGoodsBaseInfos.ToDictionary((goods) => goods.GoodsId);
- foreach (var code in quoteGoodses)
- {
- if (goodsDic.ContainsKey(code))
- {
- CloseGoodses.Add(goodsDic[code]);
- }
-
- }
- if (selectQuoteGoods != null)
- {
- var fristGoods = CloseGoodses.FirstOrDefault((goods) => goods != null &&
- goods.GoodsId == selectQuoteGoods.GoodsId);
- CurrentGoods = fristGoods ?? CloseGoodses.FirstOrDefault();
- }
- else
- {
- CurrentGoods = CloseGoodses.FirstOrDefault();
- }
-
- }
-
- private void SetCurrentGoods()
- {
- //todo:提示用户无持仓
- if (!IsGoodsSelect || CurrentGoods==null)
- {
- return;
- }
-
- var holdingSummaries = UserManager.GetCacheOrders<HoldingSummary>().Where((item) => item.GoodsId == CurrentGoods.GoodsId).ToList();
- var direction= holdingSummaries.Count >= 2; ////是否可选择方向
- if (CurrentGoods!=null&&CurrentGoods.GoodsParameters.TradeMode == eTradeMode.TRADEMODE_BIDDING)
- {
- CloseOrderBase = new BidCloseViewModel(CurrentGoods, holdingSummaries, direction);
- }
- else
- {
- CloseOrderBase = new MakeCloseViewModel(CurrentGoods, holdingSummaries, direction);
- }
- }
- /// <summary>
- /// 快速下单窗口取消
- /// </summary>
- public RelayCommand<Window> CancelCommand
- {
- get
- {
- return new RelayCommand<Window>((dialog) =>
- {
- dialog.DialogResult = false;
- });
- }
- }
- private bool _oKButtonEnabled = true;
- /// <summary>
- ///设置按键不可用
- /// </summary>
- public bool OKButtonEnabled
- {
- get
- {
- return _oKButtonEnabled;
- }
- set
- {
- Set(() => OKButtonEnabled, ref _oKButtonEnabled, value);
- }
- }
- private Window _openWindow;
- /// <summary>
- /// 下单确定
- /// </summary>
- public RelayCommand<Window> OKCommand
- {
- get
- {
- return new RelayCommand<Window>((dialog) =>
- {
- OKButtonEnabled = false;
- string errorMsg = string.Empty;
- _openWindow = dialog;
- bool validateBool = CloseOrderBase.Validated(); ////内容验证
- if (validateBool)
- {
- IsBusy = true;
- BusyTips = Muchinfo_Resource.Busy_Summit_Wait;
- CloseOrderBase.PostOrder(EntrurstSuccessCallBack, EntrurstErrorCallBack); ////获取表单内容
- }
- else
- {
- MessageBoxHelper.ShowInfo(errorMsg, Muchinfo_Resource.MessageBox_Error_Title);
- OKButtonEnabled = true;
- }
- });
- }
- }
- /// <summary>
- /// 提交成功返回
- /// </summary>
- /// <param name="order"></param>
- private void EntrurstSuccessCallBack(OrderDetail order)
- {
-
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- MessageBoxHelper.ShowSuccess(Muchinfo_Resource.Order_Success_Result,
- Muchinfo_Resource.Open_MessageBox_Title);
- IsBusy = false;
- if (_openWindow != null)
- {
- _openWindow.Close();
- this.Cleanup();
- }
- }));
- Messenger.Default.Send(UserManager.CurrentTradeAccount, MessengerTokens.OrderNotify);
- }
- /// <summary>
- /// 委托失败返回
- /// </summary>
- /// <param name="errorEntity"></param>
- private void EntrurstErrorCallBack(ErrorEntity errorEntity)
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- ErrorManager.ShowReturnError(errorEntity, Muchinfo_Resource.UI2014_Tips, true);
- if (_openWindow != null)
- {
- _openWindow.Close();
- this.Cleanup();
- }
- IsBusy = false;
- }));
- }
- }
- }
|