using System.Windows;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Muchinfo.MTPClient.Data;
using Muchinfo.MTPClient.Data.Enums;
using Muchinfo.MTPClient.Data.Model.Account;
using Muchinfo.MTPClient.Infrastructure.Utilities;
using Muchinfo.WPF.Controls.Windows;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Muchinfo.MTPClient.Account.ViewModels
{
public class CompleteOrderViewModel : RegisterMessageBase
{
private const int historyMaxDays = 90;
private bool _radioNow = true;
///
/// 当前查询
///
public bool RadioNow
{
get { return _radioNow; }
set
{
Set(() => RadioNow, ref _radioNow, value);
if (_radioNow)
{
_radioCustom = false;
QueryOrders();
}
}
}
private bool _radioCustom;
///
/// 自定义是否选中
///
public bool RadioCustom
{
get { return _radioCustom; }
set
{
Set(() => RadioCustom, ref _radioCustom, value);
EndDateTime = DateTime.Now.AddDays(-1); //查询历史为当前的前一天
StartDateTime = EndDateTime.AddDays(1 - EndDateTime.Day); //结束当月的第一天
if (_radioCustom)
{
_radioNow = false;
QueryOrders();
}
}
}
/// 查询开始时间
private DateTime _startDateTime;
///
/// 查询开始时间
///
public DateTime StartDateTime
{
get { return _startDateTime; }
set
{
Set(() => StartDateTime, ref _startDateTime, new DateTime(value.Year, value.Month, value.Day));
}
}
/// 查询结束时间
private DateTime _endDateTime;
///
/// 查询结束时间
///
public DateTime EndDateTime
{
get { return _endDateTime; }
set
{
Set(() => EndDateTime, ref _endDateTime, new DateTime(value.Year, value.Month, value.Day));
}
}
private ObservableCollection _completeOrders = new ObservableCollection();
///
/// 成交单
///
public ObservableCollection CompleteOrders
{
get { return _completeOrders; }
set { Set(() => CompleteOrders, ref _completeOrders, value); }
}
///
/// 刷新
///
public RelayCommand RefreshCommand
{
get
{
return new RelayCommand(() =>
{
QueryOrders();
});
}
}
///
/// 刷新账户信息
///
public override void RefreshContent()
{
RefreshCommand.Execute(null);
}
public CompleteOrderViewModel()
{
}
///
/// 单据注册消息
///
public override void RegisterMessage()
{
//当选择时刷新
var cacheData = UserManager.GetTradeCacheData(UserManager.CurrentTradeAccount.AccountId);
if (!cacheData.CompleteOrderFlag)
{
RefreshCommand.Execute(null);
}
else
{
UpdateOrders();
}
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
Messenger.Default.Unregister(this, MessengerTokens.OrderNotify);
Messenger.Default.Register(this, MessengerTokens.OrderNotify, (e) =>
{
if (RadioNow)
{
UpdateOrders();
}
});
}));
}
///
/// 更新当前单据
///
private void UpdateOrders()
{
CompleteOrders = UserManager.GetCacheOrders();
}
private void QueryOrders()
{
if (validate())
{
IsBusy = true;
var queryCommons = new List();
if (RadioNow) ////查询当前
{
queryCommons.Add(new QueryCommonParam() { ParamKey = "accountId", ParamValue = UserManager.CurrentTradeAccount.AccountId + string.Empty });
}
else ////查询历史
{
//todo:查询历史参数
queryCommons.Add(new QueryCommonParam() { ParamKey = "accountId", ParamValue = UserManager.CurrentTradeAccount.AccountId + string.Empty });
}
_orderService.QueryComplateOrders(QueryStatement.QueryComplateOrder, queryCommons, QueryComplateOrderSuccessCallback, QueryErrorCallback);
}
}
public void QueryComplateOrderSuccessCallback(List completeOrders)
{
IsBusy = false;
UpdateOrders();
}
///
/// 验证输入有效性
///
///
private bool validate()
{
if (RadioCustom)
{
if (StartDateTime.AddHours(-StartDateTime.Hour) > EndDateTime)
{
MessageBoxHelper.ShowInfo(
Muchinfo.MTPClient.Resources.Muchinfo_Resource.Models_EndDateLessBegin,
Muchinfo.MTPClient.Resources.Muchinfo_Resource.APP_Tips);
return false;
}
else if ((EndDateTime - StartDateTime).TotalDays > historyMaxDays)
{
MessageBoxHelper.ShowInfo(
string.Format(Muchinfo.MTPClient.Resources.Muchinfo_Resource.QueryDaysMustblow, historyMaxDays),
Muchinfo.MTPClient.Resources.Muchinfo_Resource.APP_Tips);
return false;
}
}
return true;
}
public override void UnRegisterMessage()
{
Messenger.Default.Unregister(this, MessengerTokens.OrderNotify);
base.UnRegisterMessage();
}
}
}