using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Muchinfo.MTPClient.Data.Enums;
using Muchinfo.MTPClient.Infrastructure.Utilities;
using Muchinfo.MTPClient.IService;
using System.Windows;
using System.Linq;
using Muchinfo.MTPClient.Data.Model.Delivery;
using Muchinfo.MTPClient.Infrastructure.MessageBox;
using Muchinfo.MTPClient.Resources;
using System.Collections.Generic;
using System;
using Muchinfo.MTPClient.Infrastructure.Helpers;
using Muchinfo.MTPClient.Data;
using GalaSoft.MvvmLight.Ioc;
namespace Muchinfo.MTPClient.Delivery.ViewModels
{
///
/// 仓单提货人信息
///
///
///
public class RecieveInfoViewModel : ViewModelBase
{
#region "=========私有成员/Private Data Members"
protected IDeliveryService _deliveryService;
private Window _openWindow;
private DeliveryWarehouse _itemWarehouse;
#endregion "Private Data Members"
#region "=========构造函数/Constructor/Initialization"
public RecieveInfoViewModel(DeliveryWarehouse itemWarehouse)
{
this._itemWarehouse = itemWarehouse;
GoodsName = itemWarehouse.DeliveryGoodsCodeNameDisplay;
GoodsUnitIdDisplay = itemWarehouse.GoodsUnitIdDisplay;
AvailableQty = itemWarehouse.Qty - itemWarehouse.FreezeQty - itemWarehouse.PayQty;
//RecieveAddress = itemWarehouse.address;
//RecievePerson = itemWarehouse.personname;
//PhoneNum = itemWarehouse.telphone;
_deliveryService = SimpleIoc.Default.GetInstance();
CheckRecievePersonInfo();
}
#endregion "Constructor/Initialization"
#region "=========接口重写/Interface implementation Or override"
//To do interface implementation
#endregion "Interface implementation Or override"
#region "=========公共属性/Public Properties To Get/Set "
#region 交易商品名称
private string _goodsName = string.Empty;
///
/// 交易商品名称
///
public string GoodsName
{
get
{
return _goodsName;
}
set
{
Set(() => GoodsName, ref _goodsName, value);
}
}
#endregion
#region 可用数量
private decimal _availableQty = decimal.Zero;
///
/// Sets and gets the Qty property.
///
public decimal AvailableQty
{
get
{
return _availableQty;
}
set
{
Set(() => AvailableQty, ref _availableQty, value);
}
}
#endregion
#region 商品单位
private string _GoodsUnitIdDisplay = string.Empty;
///
/// 商品单位
///
public string GoodsUnitIdDisplay
{
get
{
return _GoodsUnitIdDisplay;
}
set
{
Set(() => GoodsUnitIdDisplay, ref _GoodsUnitIdDisplay, value);
}
}
#endregion
#region 当前勾选单据
private List _currentSelectedOutStore = null;
///
/// 当前勾选的提货单据LIST
///
public List CurrentSelectedOutStore
{
get
{
return _currentSelectedOutStore;
}
set
{
Set(() => CurrentSelectedOutStore, ref _currentSelectedOutStore, value);
}
}
#endregion
#region 按钮是否可用
private bool _oKButtonEnabled = true;
///
///按钮是否可用
///
public bool OKButtonEnabled
{
get
{
return _oKButtonEnabled;
}
set
{
Set(() => OKButtonEnabled, ref _oKButtonEnabled, value);
}
}
#endregion
#region 是否忙
private bool _isBusy;
///
/// 是否忙
///
public bool IsBusy
{
get { return _isBusy; }
set { Set(() => IsBusy, ref _isBusy, value); }
}
#endregion
#region 收货人
private string _RecievePerson = string.Empty;
///
/// 收货人
///
public string RecievePerson
{
get
{
return _RecievePerson;
}
set
{
Set(() => RecievePerson, ref _RecievePerson, value);
}
}
#endregion
#region 收货地址
private string _RecieveAddress = string.Empty;
///
/// 收货地址
///
public string RecieveAddress
{
get
{
return _RecieveAddress;
}
set
{
Set(() => RecieveAddress, ref _RecieveAddress, value);
}
}
#endregion
#region 联系方式
private string _PhoneNum = string.Empty;
///
/// 联系方式
///
public string PhoneNum
{
get
{
return _PhoneNum;
}
set
{
Set(() => PhoneNum, ref _PhoneNum, value);
}
}
#endregion
private string _IdCardNum;
///
/// 證件號
///
public string IdCardNum
{
get
{
return _IdCardNum;
}
set
{
Set(() => IdCardNum, ref _IdCardNum, value);
}
}
private TakeGoodsWay _TakeGoodsWay;
public TakeGoodsWay TakeGoodsWay
{
get
{
return _TakeGoodsWay;
}
set
{
Set(() => TakeGoodsWay, ref _TakeGoodsWay, value);
}
}
#region 是否可以修改
private bool _isEnableToWrite = false;
///
/// 是否可以修改
///
public bool IsEnableToWrite
{
get
{
return _isEnableToWrite;
}
set
{
Set(() => IsEnableToWrite, ref _isEnableToWrite, value);
}
}
#endregion
#region 当前账号提货人信息
private DepositPersonalInfoRspModel _currentDepositPersonalInfo;
///
/// 当前账号提货人信息
///
public DepositPersonalInfoRspModel CurrentDepositPersonalInfo
{
get
{
return _currentDepositPersonalInfo;
}
set
{
Set(() => CurrentDepositPersonalInfo, ref _currentDepositPersonalInfo, value);
}
}
#endregion
#endregion "Public Properties To Get/Set "
#region "=========公共命令/Public Commands"
#region 取消选中Command
///
/// 取消选中
///
private RelayCommand unSelectCommand;
public RelayCommand UnSelectCommand
{
get
{
return unSelectCommand ?? (unSelectCommand = new RelayCommand(
() =>
{
//SettlementSelectedOutStore();
}));
}
}
#endregion
#region 提交确定Command
///
/// 提交确定
///
public RelayCommand OKCommand
{
get
{
return new RelayCommand((dialog) =>
{
CurrentSelectedOutStore = new List();
CurrentSelectedOutStore.Add(new OutStoreReqDetailModel() { WRId = (_itemWarehouse.WRID), StoreOutQty = ((_itemWarehouse.Qty - _itemWarehouse.FreezeQty - _itemWarehouse.PayQty)) });
OKButtonEnabled = false;
string errorMsg = string.Empty;
bool validateBool = Validated(ref errorMsg); ////内容验证
if (validateBool)
{
IsBusy = true;
_openWindow = dialog;
var newOrder = new OutStoreReqModel()
{
//申请
OperateType = (int)TakaGoodsOperateType.Apply,
RecievePerson = RecievePerson,
RecieveAddress = string.IsNullOrWhiteSpace(RecieveAddress) ? "--" : RecieveAddress.Trim(),
PhoneNum = PhoneNum.Trim(),
IdCardNum = IdCardNum.Trim(),
TakeGoodsWay =TakeGoodsWay,
OutStoreDetail = CurrentSelectedOutStore
};
if (UserManager.CurrentTradeAccount.FundsAccounts.Any() &&
UserManager.CurrentTradeAccount.FundsAccounts[0] != null)
{
newOrder.AccountID = newOrder.OperatorID = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
}
else
{
newOrder.AccountID = newOrder.OperatorID = UserManager.CurrentTradeAccount.AccountId;
}
try
{
_deliveryService.SubmitOutStoreReq(newOrder, EntrurstSuccessCallBack, EntrurstErrorCallBack);
}
finally
{
}
}
else
{
MessageBoxHelper.ShowInfo(errorMsg, Client_Resource.MessageBox_Error_Title);
OKButtonEnabled = true;
}
});
}
}
#endregion
#region 窗口取消Command
///
/// 窗口取消
///
public RelayCommand CancelCommand
{
get
{
return new RelayCommand((dialog) =>
{
dialog.DialogResult = false;
});
}
}
#endregion
#endregion "Public Commands"
#region "=========私有方法/Private Methods"
///
/// 检查收货人信息
///
private void CheckRecievePersonInfo()
{
if (_itemWarehouse != null)
{
RecieveAddress = _itemWarehouse.address;
RecievePerson = _itemWarehouse.personname;
PhoneNum = _itemWarehouse.telphone;
IdCardNum = _itemWarehouse.IdCardNum;
TakeGoodsWay = _itemWarehouse.TakeGoodsWay == TakeGoodsWay.Express?TakeGoodsWay.Express:TakeGoodsWay.Self;
return;
}
IsEnableToWrite = false;
if (string.IsNullOrEmpty(RecievePerson))
{
//读取登录人基本信息
GetDepositPersonalInfo();
IsEnableToWrite = true;
}
}
///
/// 获取交割提货个人信息
///
private void GetDepositPersonalInfo()
{
var accountid = UserManager.CurrentTradeAccount.AccountId;
//if (UserManager.CurrentTradeAccount.FundsAccounts != null && UserManager.CurrentTradeAccount.FundsAccounts.Any())
//{
// accountid = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
//}
var ReqModel = new DepositPersonalInfoReqModel();
ReqModel.accountId = (long)accountid;
IsBusy = true;
//交割提货个人信息查询
_deliveryService.SearchDepositPersonalInfoReq(ReqModel, QueryDepositPersonalInfoSuccess, QueryDepositPersonalInfoCallBack);
}
///
/// 交割提货个人信息-成功返回
///
///
protected void QueryDepositPersonalInfoSuccess(DepositPersonalInfoRspModel itemModel)
{
TakeGoodsWay = TakeGoodsWay.Self;
if (itemModel != null)
{
CurrentDepositPersonalInfo = itemModel;
if (CurrentDepositPersonalInfo != null)
{
RecieveAddress = CurrentDepositPersonalInfo.address;
RecievePerson = CurrentDepositPersonalInfo.personName;
PhoneNum = CurrentDepositPersonalInfo.telphone;
}
}
IsBusy = false;
}
///
/// 获取交割提货个人信息失败返回
///
///
private void QueryDepositPersonalInfoCallBack(ErrorEntity errorEntity)
{
IsBusy = false;
}
///
/// 提交提货成功返回
///
///
private void EntrurstSuccessCallBack(OutStoreRspModel order)
{
IsBusy = false;
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
OKButtonEnabled = true;
//提货成功
MessageBoxHelper.ShowSuccess(Client_Resource.Order_TakaDeliveryGoods_Success_Result,
Client_Resource.UI2014_Tips);
if (_openWindow != null)
{
_openWindow.Close();
this.Cleanup();
}
}));
//刷新正常单
MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.OrderNoticeToken);
}
///
/// 提货委托失败返回
///
///
private void EntrurstErrorCallBack(ErrorEntity errorEntity)
{
IsBusy = false;
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
OKButtonEnabled = true;
ErrorManager.ShowReturnError(errorEntity, Client_Resource.UI2014_Tips, true);
if (_openWindow != null)
{
_openWindow.Close();
this.Cleanup();
}
}));
}
#endregion "Private Methods"
#region "=========其它方法/Other Methods"
///
/// 数据验证
///
///
///
public bool Validated(ref string msg)
{
if (string.IsNullOrWhiteSpace(RecievePerson))
{
msg = Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_RecievePerson + Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_NeedToWrite;
return false;
}
if (TakeGoodsWay == TakeGoodsWay.Express && string.IsNullOrWhiteSpace(RecieveAddress))
{
msg = Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_RecieveAddress + Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_NeedToWrite;
return false;
}
if (string.IsNullOrWhiteSpace(PhoneNum))
{
msg = Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_PhoneNum + Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_NeedToWrite;
return false;
}
if (!string.IsNullOrWhiteSpace(PhoneNum) && !PhoneNum.StartsWith("1") || PhoneNum.Length != 11 || !ValidationHelper.IsMobile(PhoneNum))
{
msg = Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_PhoneNumErrFormat;
return false;
}
if (String.IsNullOrWhiteSpace(IdCardNum) || !ValidationHelper.IsIDcard(IdCardNum.Trim()))
{
msg = Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_IDCardNumErrFormat;
return false;
}
return true;
}
#endregion "Other Methods"
}
}