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.Helpers;
using Muchinfo.MTPClient.Infrastructure.MessageBox;
using Muchinfo.MTPClient.Infrastructure.Utilities;
using Muchinfo.MTPClient.Resources;
using Muchinfo.WPF.Controls.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace Muchinfo.MTPClient.Account.ViewModels
{
///
/// This class contains properties that a View can data bind to.
///
/// See http://www.galasoft.ch/mvvm
///
///
public class PrePostDetailViewModel : MarketOrdersBase
{
#region "Private Data Members"
//To declare private service data
#endregion "Private Data Members"
#region "Constructor/Initialization"
///
/// Initializes a new instance of the PrePostDetailViewModel class.
///
public PrePostDetailViewModel()
{
MessengerHelper.DefaultRegister(this, MessengerTokens.CancalOrder, (e) =>
{
CancelHoldingCammand.Execute(null);
});
}
#endregion "Constructor/Initialization"
#region "Interface implementation Or override"
public override string SearchOrderCountId
{
get { return QueryStatement.SearchAllPrePostOrderCount; }
}
///
/// 单据注册消息
///
public override void RegisterMessage()
{
base.RegisterMessage();
//当选择时刷新
var cacheData = UserManager.GetTradeCacheData(UserManager.CurrentTradeAccount.AccountId);
if (!cacheData.OpenPrePostOrderFlag)
{
RefreshCommand.Execute(null);
}
else
{
UpdateOrders();
}
MessengerHelper.DefaultUnregister(this, MessengerTokens.PreOrderNoticeToken);
MessengerHelper.DefaultRegister(this, MessengerTokens.PreOrderNoticeToken, (e) =>
{
//下单成功后刷新预埋单界面
RefreshCommand.Execute(null);
});
}
public override void QueryMarketOrder(eTradeMode marketType)
{
if (marketType == eTradeMode.TRADEMODE_SALE)
{
return;
}
IsBusy = true;
var queryCommonParams = new List();
var accountid = UserManager.CurrentTradeAccount.AccountId;
//if (UserManager.CurrentTradeAccount.FundsAccounts != null && UserManager.CurrentTradeAccount.FundsAccounts.Any())
//{
// accountid = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
//}
queryCommonParams.Add(new QueryCommonParam() { ParamKey = "accountId", ParamValue = accountid + string.Empty });
_orderService.QueryPrePostOrders(marketType, queryCommonParams, MarketOrderSuccess, QueryErrorCallback);
}
protected override void MarketOrderSuccess(eTradeMode marketType, List orders)
{
IsBusy = false;
UpdateOrders();
base.MarketOrderSuccess(marketType, orders);
}
public override void UnRegisterMessage()
{
MessengerHelper.DefaultUnregister(this, MessengerTokens.OrderNotify);
base.UnRegisterMessage();
}
///
/// 创建预埋单对象
///
///
public virtual NewEntrustOrder BuildEntrustOrder()
{
var entrustOrder = new NewEntrustOrder()
{
OperaterId = UserManager.CurrentTradeAccount.AccountId,
TriggerType = (uint)CurrentLimitOrder.TriggerType,
TriggerPrice = CurrentLimitOrder.TriggerPrice,
GoodsCode = CurrentLimitOrder.goodscode,
GoodsId = (uint)CurrentLimitOrder.GoodsId,
BuyOrSell = CurrentLimitOrder.BuyOrSell,
OrderFlag = (uint)OrderMethods.PreOrder,
EntrustPrice = CurrentLimitOrder.orderprice,
EntrustQuantity = CurrentLimitOrder.orderqty,
BuildType = OpenCloseMode.BUILDTYPE_OPEN,
EntrurstTime = ApplicationParameter.ServerTimeNow,
//RelationTicket = CurrentLimitOrder.PreOrderId,
};
//if (UserManager.CurrentTradeAccount.FundsAccounts.Any() &&
// UserManager.CurrentTradeAccount.FundsAccounts[0] != null)
//{
// entrustOrder.AccountId = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
//}
//else
//{
// entrustOrder.AccountId = UserManager.CurrentTradeAccount.AccountId;
//}
return entrustOrder;
}
#endregion "Interface implementation Or override"
#region "Public Properties To Get/Set "
#region 当前选择的预埋单单
private PrePostDetail _currentLimitOrder;
///
/// 当前选择的预埋单单
///
public PrePostDetail CurrentLimitOrder
{
get { return _currentLimitOrder; }
set
{
Set(() => CurrentLimitOrder, ref _currentLimitOrder, value);
}
}
#endregion
#endregion "Public Properties To Get/Set "
#region "Public Commands"
private RelayCommand _dataGridMouseEnter;
///
/// 多个DataGird中改变当前选中的CurrentHoldingSummaryItem
///
public RelayCommand DataGridMouseEnter
{
get
{
return _dataGridMouseEnter
?? (_dataGridMouseEnter = new RelayCommand(
p =>
{
if (p != null)
{
CurrentLimitOrder = p;
}
}));
}
}
///
/// 撤销预埋单
///
public RelayCommand CancelHoldingCammand
{
get
{
return new RelayCommand(() =>
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
if (CurrentLimitOrder == null) return;
long orderNumber = CurrentLimitOrder.PreOrderId;
var boxResult = MessageBoxHelper.ShowQuestion(Muchinfo.MTPClient.Resources.Client_Resource.Models_MakesureCancel + orderNumber + Muchinfo.MTPClient.Resources.Client_Resource.Models_Bill,
Muchinfo.MTPClient.Resources.Client_Resource.Models_CancelBillTips);
if (boxResult == MessageBoxResult.Yes)
{
IsBusy = true;
PostCancelOrder();
}
}));
}, () => CanCancelOrderDetial());
}
}
#endregion "Public Commands"
#region "Private Methods"
///
/// 更新当前单据
///
private void UpdateOrders()
{
OrderSource = UserManager.GetMarketCommonOrder(UserManager.CurrentTradeAccount);
if (OrderSource != null && OrderSource.Any())
{
DicOrderCount[CurrentTradeMode] = OrderSource[CurrentTradeMode].Count;
}
else
{
var dicCountZero = new Dictionary();
foreach (var marketType in MarketTypes)
{
dicCountZero[marketType] = 0;
}
DicOrderCount = dicCountZero;
}
//if (OrderSource != null && OrderSource.Any())
//{
// var dicCount = new Dictionary();
// foreach (var marketType in MarketTypes)
// {
// if (OrderSource.ContainsKey(marketType))
// {
// dicCount[marketType] = OrderSource[marketType].Count;
// }
// else
// {
// dicCount[marketType] = 0;
// }
// }
// DicOrderCount = dicCount;
//}
//else
//{
// var dicCountZero = new Dictionary();
// foreach (var marketType in MarketTypes)
// {
// dicCountZero[marketType] = 0;
// }
// DicOrderCount = dicCountZero;
//}
}
///
/// 提交成功返回
///
///
private void EntrurstSuccessCallBack(OrderDetail order)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
MessageBoxHelper.ShowSuccess(Muchinfo.MTPClient.Resources.Client_Resource.Account_CancelSuccess,
Client_Resource.Cancel_MessageBox_Title);
IsBusy = false;
}));
//刷新预埋单
MessengerHelper.DefaultSend(false, MessengerTokens.PreOrderUpdate);
}
///
/// 委托失败返回
///
///
private void EntrurstErrorCallBack(ErrorEntity errorEntity)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
ErrorManager.ShowReturnError(errorEntity, Client_Resource.UI2014_Tips, true);
IsBusy = false;
}));
}
#endregion "Private Methods"
#region "Other Methods like Validated"
//public void QueryComplateOrderSuccessCallback(List prePostOrders)
//{
// IsBusy = false;
// UpdateOrders();
//}
///
/// 是否可撤单
///
///
public bool CanCancelOrderDetial()
{
if (_currentLimitOrder == null)
{
return false;
}
return (_currentLimitOrder.OrderStatus == PrepostStatus.PREPOST_STATUS_MARKET );
}
///
/// 发送撤单命令
///
/// 撤消的单据
/// 错误代码
public void PostCancelOrder()
{
var order = BuildEntrustOrder();
var currentGoods = CacheManager.CacheGoodsBaseInfos.FirstOrDefault(
(goods) => goods.GoodsParameters.GoodsId == CurrentLimitOrder.GoodsId);
if (currentGoods == null) { return; }
switch (currentGoods.TradeMode)
{
//case eTradeMode.TRADEMODE_BIDDINGMARKETMAKE:
//_orderService.MixMarketEntrustOrder(order, EntrurstSuccessCallBack, EntrurstErrorCallBack);
//break;
case eTradeMode.TRADEMODE_BIDDING:
//_orderService.BidMarketEntrustOrder(order, EntrurstSuccessCallBack, EntrurstErrorCallBack);
//break;
case eTradeMode.TRADEMODE_MARKETMAKE:
_orderService.MakeMarketEntrustOrder(order, EntrurstSuccessCallBack, EntrurstErrorCallBack);
break;
//case eTradeMode.TRADEMODE_SALE:
// var isaleService = SimpleIoc.Default.GetInstance();
// isaleService.SaleEntrurstOrder(order, EntrurstSuccessCallBack, EntrurstErrorCallBack);
// break;
default:
break;
}
}
#endregion "Other Methods like Validated"
}
}