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
{
///
/// 是否忙,显示等待控件
///
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 bool IsGoodsSelect { get; set; }
private List _closeGoodses = new List();
///
/// 平仓商品
///
public List CloseGoodses
{
get { return _closeGoodses; }
set { Set(() => CloseGoodses, ref _closeGoodses, value); }
}
private QuoteGoods _currentGoods;
///
/// 当前商品
///
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;
///
/// 平仓界面标题
///
public string Title
{
get { return _title; }
set { Set(() => Title, ref _title, value); }
}
public uint AccoutId
{
get
{
return UserManager.CurrentTradeAccount.AccountId;
}
}
private IGoodsService _goodsService;
///
/// Initializes a new instance of the class.
///
/// The quote goods.
/// Type of the order.
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();
var holdingSummaries = UserManager.GetCacheOrders();
var quoteGoodses = holdingSummaries.Select((holding) => holding.GoodsId).Distinct();
CloseGoodses = new List();
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().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);
}
}
///
/// 快速下单窗口取消
///
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);
}
}
private Window _openWindow;
///
/// 下单确定
///
public RelayCommand OKCommand
{
get
{
return new RelayCommand((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;
}
});
}
}
///
/// 提交成功返回
///
///
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;
}));
}
}
}