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 10:22:14
//Author
//Description Create
//----------------------------------------------------------------
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
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;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
namespace Muchinfo.MTPClient.Trade.ViewModels
{
///
/// Class OpenFrameViewModel.
///
public class OpenFrameViewModel : ViewModelBase
{
private IGoodsService _goodsService;
private OrderType _orderType; ////打开口时默认订单类型
private Window _openWindow; ////服务成功关闭窗口
///
/// 是否忙,显示等待控件
///
private bool _isBusy;
///
/// 是否忙,显示等待控件
///
public bool IsBusy
{
get
{
return _isBusy;
}
set
{
Set(() => IsBusy, ref _isBusy, value);
}
}
private string _busyTips;
///
/// 服务忙提示
///
public string BusyTips
{
get
{
return _busyTips;
}
set
{
Set(() => BusyTips, ref _busyTips, value);
}
}
///
/// 账号信息
///
public uint AccountId { get; set; }
private List _goodsList;
///
/// 商品列表
///
public List GoodsList
{
get
{
return _goodsList;
}
set
{
Set(() => GoodsList, ref _goodsList, value);
}
}
private QuoteGoods _currentGoods;
///
/// 当前商品
///
public QuoteGoods CurrentGoods
{
get { return _currentGoods; }
set
{
Set(() => CurrentGoods, ref _currentGoods, value);
SetQuoteGoods();
}
}
private TradeOrderBase _tradeOrderBase;
///
/// 做市竞价类型
///
public TradeOrderBase TradeOrderBase
{
get
{
return _tradeOrderBase;
}
set
{
Set(() => TradeOrderBase, ref _tradeOrderBase, value);
}
}
///
/// Initializes a new instance of the class.
///
/// The quote goods.
/// 建仓类型使用OTC市价或限价参数
public OpenFrameViewModel(QuoteGoods quoteGoods, OrderType orderType = OrderType.MarketOpenOrder)
{
_goodsService = SimpleIoc.Default.GetInstance();
GoodsList = CacheManager.CacheGoodsBaseInfos; //_goodsService.GetGoodsInfoListByExchangeId(UserManager.CurrentTradeAccount.ExchangeId).ToList();
_orderType = orderType;
//todo:取可交易的商品,不一定是一个交易所的
if (quoteGoods != null)
{
CurrentGoods = GoodsList.FirstOrDefault((item) => item.GoodsCode == quoteGoods.GoodsCode);
}
////找不到商品取默认的
if (CurrentGoods == null)
{
CurrentGoods = GoodsList.FirstOrDefault();
}
AccountId = UserManager.CurrentTradeAccount.AccountId;
}
///
/// Sets the quote goods.
///
private void SetQuoteGoods()
{
if (_currentGoods != null)
{
if (_currentGoods.GoodsParameters != null && _currentGoods.GoodsParameters.TradeMode == eTradeMode.TRADEMODE_BIDDING)
{
OrderType orderType = _orderType == OrderType.MarketOpenOrder
? OrderType.BidMarketOpen
: OrderType.BidLimitOpen;
TradeOrderBase = new BidOpenOrderViewModel(_currentGoods, orderType);
}
else
{
TradeOrderBase = new MakeOrderViewModel(_currentGoods, _orderType);
}
}
}
///
/// 快速下单窗口取消
///
public RelayCommand CancelCommand
{
get
{
return new RelayCommand((dialog) =>
{
dialog.DialogResult = false;
});
}
}
private bool _oKButtonEnabled = true;
///
///设置按键不可用
///
public bool OKButtonEnabled
{
get
{
return _oKButtonEnabled;
}
set
{
Set(() => OKButtonEnabled, ref _oKButtonEnabled, value);
}
}
///
/// 下单确定
///
public RelayCommand OKCommand
{
get
{
return new RelayCommand((dialog) =>
{
OKButtonEnabled = false;
string errorMsg = string.Empty;
bool validateBool = _tradeOrderBase.Validated(); ////内容验证
if (validateBool)
{
_openWindow = dialog;
IsBusy = true;
BusyTips = Muchinfo_Resource.Window_Wait;
_tradeOrderBase.PostOrder(EntrurstSuccessCallBack, EntrurstErrorCallBack); ////获取表单内容
}
else
{
MessageBoxHelper.ShowInfo(errorMsg, Muchinfo_Resource.MessageBox_Error_Title);
OKButtonEnabled = true;
}
});
}
}
///
/// 提交成功返回
///
///
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);
}
///
/// 委托失败返回
///
///
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;
}));
}
}
}