using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//----------------------------------------------------------------
//Module Name: $safeprojectname$
//Purpose:
//CopyRight: Muchinfo
//History:
//----------------------------------------------------------------
//DateTime 2017/1/12 16:24:30
//Author
//Description Create
//----------------------------------------------------------------
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using Muchinfo.MTPClient.Data;
using Muchinfo.MTPClient.Data.Model;
using Muchinfo.MTPClient.Infrastructure.MessageBox;
using Muchinfo.MTPClient.Infrastructure.Utilities;
using Muchinfo.MTPClient.IService;
using Muchinfo.MTPClient.Resources;
using Muchinfo.MTPClient.Infrastructure.Helpers;
using Muchinfo.MTPClient.Data.Enums;
namespace Muchinfo.MTPClient.Sale.ViewModels
{
public class DepositApplyViewModel:ViewModelBase
{
private IDepositService _iDepositService;
private DepositPlan _curDepositPlan;
private Window _openWindow;
///
/// 当前托管计划
///
public DepositPlan CurDepositPlan
{
get { return _curDepositPlan; }
set
{
if (value != null && _curDepositPlan != value)
{
_iDepositService.QueryDepositWareHouse(value.ExAreaId,DepositWareHouseSuccess, ErrorQueryWareHouse); ///查询仓库
MaxLot = value.MaxDepositNumber;
}
Set(() => CurDepositPlan, ref _curDepositPlan, value);
Lot = 0;
}
}
private List _depositPlans;
///
/// 托管计划列表
///
public List DepositPlans
{
get { return _depositPlans; }
set { Set(() => DepositPlans, ref _depositPlans, value); }
}
private bool _isBusy;
///
/// 是否正在加载
///
public bool IsBusy
{
get { return _isBusy; }
set { Set(() => IsBusy, ref _isBusy, value); }
}
public DepositApplyViewModel(DepositPlan deposit,Listlist)
{
_iDepositService = SimpleIoc.Default.GetInstance();
CurDepositPlan = deposit;
DepositPlans = list.Where((item)=>item.IsDepositApply).ToList();
IsBusy = true;
}
private decimal _lot;
///
/// 数量
///
public decimal Lot
{
get { return _lot; }
set
{
Set(() => Lot, ref _lot, value);
RaisePropertyChanged(() => FeeTips);
RaisePropertyChanged(() => ErrorTips);
RaisePropertyChanged(() => FreezeFee);
}
}
public string ErrorTips
{
get
{
var error = string.Empty;
if (Lot == 0)
{
return Client_Resource.Content_DepositApplyErrorTips;
}
if (MaxLot < Lot)
{
return Muchinfo.MTPClient.Resources.Client_Resource.Resources_DepositOrder_MaxLotError;
}
var usefunds = UserManager.CurrentAccountUsableFunds();
if (usefunds < FreezeFee) ////资金不足
{
return Client_Resource.Content_DepositApplyMonenyErrorTips;
}
return error;
}
}
private List _storelist;
///
/// 仓库列表
///
public List Storelist
{
get { return _storelist; }
set { Set(() => Storelist, ref _storelist, value); }
}
private DepositWareHouse _curWareHouse;
///
/// 当前的选择仓库
///
public DepositWareHouse CurWareHouse
{
get { return _curWareHouse; }
set { Set(() => CurWareHouse, ref _curWareHouse, value); }
}
private decimal _maxLot=long.MaxValue;
///
/// 最大托管数量
///
public decimal MaxLot
{
get { return _maxLot; }
set { Set(() => MaxLot, ref _maxLot, value); }
}
public string FeeTips
{
get
{
if (CurDepositPlan != null)
{
var tip = string.Format(Client_Resource.AResources_DepositApplyFee, FreezeFee,
CurDepositPlan.DepositReferPrice, Lot, CurDepositPlan.ListingRate);
return tip;
}
return string.Empty;
}
}
public decimal FreezeFee
{
get
{
if (CurDepositPlan != null)
{
return Math.Round(CurDepositPlan.DepositReferPrice*Lot*CurDepositPlan.ListingRate, 2,
MidpointRounding.AwayFromZero);
}
return 0m ;
}
}
///
/// 快速下单窗口取消
///
public RelayCommand CancelCommand
{
get
{
return new RelayCommand((dialog) =>
{
dialog.DialogResult = false;
});
}
}
///
/// 下单确定
///
public RelayCommand OKCommand
{
get
{
return new RelayCommand((dialog) =>
{
_openWindow = dialog;
bool validateBool = Vailed();
if (validateBool)
{
IsBusy = true;
var accountId = UserManager.CurrentTradeAccount.AccountId;
//if (UserManager.CurrentTradeAccount.FundsAccounts.Any() &&
// UserManager.CurrentTradeAccount.FundsAccounts[0] != null)
//{
// accountId = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
//}
var depositOrder = new DepositOrder()
{
DepositNum = Lot,
Accountid = accountId,
BuildType = eBuildType.BUILDTYPE_OPEN,
DepositId = CurDepositPlan.DepositId,
WarehouseId = CurWareHouse.WareHouseId,
};
_iDepositService.DepositApply(depositOrder, DepositApplySuccess, ErrorDeosit);
}
}, IsOKEnable);
}
}
private void DepositWareHouseSuccess(List list)
{
IsBusy = false;
Storelist = list;
if (Storelist != null)
{
CurWareHouse = null;
CurWareHouse = Storelist.FirstOrDefault();
}
}
private void ErrorQueryWareHouse(ErrorEntity errorEntity)
{
IsBusy = false;
//todo:写错误日志
}
///
///
///
///
private void DepositApplySuccess(DepositOrder depositOrder)
{
IsBusy = false;
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
MessageBoxHelper.ShowSuccess(Client_Resource.Content_DepositApplySuccess,
Client_Resource.Open_MessageBox_Title);
if (_openWindow != null)
{
_openWindow.Close();
this.Cleanup();
}
}));
//刷新正常单
MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.DepositOrderToken);
}
private void ErrorDeosit(ErrorEntity errorEntity)
{
IsBusy = false;
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
ErrorManager.ShowReturnError(errorEntity, Client_Resource.UI2014_Tips, true);
if (_openWindow != null)
{
_openWindow.Close();
this.Cleanup();
}
// IsBusy = false;
}));
}
private bool IsOKEnable(Window item)
{
if (CurDepositPlan==null)
{
return false;
}
else if(CurWareHouse==null)
{
return false;
}
var usefunds = UserManager.CurrentAccountUsableFunds();
if (usefunds < FreezeFee) ////资金不足
{
return false;
}
if (CurDepositPlan.MaxDepositNumber < Lot)
{
return false;
}
return Lot > 0;
}
private bool Vailed()
{
if (CurDepositPlan == null)
{
MessageBoxHelper.ShowInfo(Muchinfo.MTPClient.Resources.Client_Resource.Content_DepositUnSelect, Muchinfo.MTPClient.Resources.Client_Resource.Models_Tips, true);
return false;
}
if (CurDepositPlan.MaxDepositNumber < Lot)
{
MessageBoxHelper.ShowInfo(Muchinfo.MTPClient.Resources.Client_Resource.Resources_DepositOrder_MaxLotError, Muchinfo.MTPClient.Resources.Client_Resource.Models_Tips, true);
return false;
}
DateTime deTime = DateTime.Now;
if (!DateTime.TryParse(CurDepositPlan.DepositStartTime, out deTime)||deTime.Date>ApplicationParameter.ServerTimeNow.Date)
{
MessageBoxHelper.ShowInfo(Muchinfo.MTPClient.Resources.Client_Resource.Content_DepositUnStarting, Muchinfo.MTPClient.Resources.Client_Resource.Models_Tips, true);
return false;
}
else if (!DateTime.TryParse(CurDepositPlan.DepositEndTime, out deTime)||deTime.Date