using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using Muchinfo.MTPClient.Data;
using Muchinfo.MTPClient.Data.Enums;
using Muchinfo.MTPClient.Data.Model.Account;
using Muchinfo.MTPClient.Data.Model.Delivery;
using Muchinfo.MTPClient.Infrastructure.Cache;
using Muchinfo.MTPClient.Infrastructure.Helpers;
using Muchinfo.MTPClient.Infrastructure.MessageBox;
using Muchinfo.MTPClient.Infrastructure.Utilities;
using Muchinfo.MTPClient.Infrastructure.Windows;
using Muchinfo.MTPClient.IService;
using Muchinfo.MTPClient.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace Muchinfo.MTPClient.Delivery.ViewModels
{
///
/// 提货申请
///
public class TakaDeliveryGoodsApplyViewModel : ViewModelBase
{
#region "=========私有成员/Private Data Members"
protected IDeliveryService _deliveryService;
private Window _openWindow;
#endregion "Private Data Members"
#region "=========构造函数/Constructor/Initialization"
public TakaDeliveryGoodsApplyViewModel()
{
_deliveryService = SimpleIoc.Default.GetInstance();
InitData();
}
#endregion "Constructor/Initialization"
#region "=========接口重写/Interface implementation Or override"
#endregion "Interface implementation Or override"
#region "=========公共属性/Public Properties To Get/Set "
#region 当前账号提货人信息
private DepositPersonalInfoRspModel _currentDepositPersonalInfo;
///
/// 当前账号提货人信息
///
public DepositPersonalInfoRspModel CurrentDepositPersonalInfo
{
get
{
return _currentDepositPersonalInfo;
}
set
{
Set(() => CurrentDepositPersonalInfo, ref _currentDepositPersonalInfo, value);
}
}
#endregion
#region 标题
public string Title
{
get
{
return Resources.Client_Resource.Resources_Service_SubmitOutStoreReq;
}
}
#endregion
#region 是否忙
private bool _isBusy;
///
/// 是否忙
///
public bool IsBusy
{
get { return _isBusy; }
set { Set(() => IsBusy, ref _isBusy, value); }
}
#endregion
#region 按钮是否可用
private bool _oKButtonEnabled = true;
///
///按钮是否可用
///
public bool OKButtonEnabled
{
get
{
return _oKButtonEnabled;
}
set
{
Set(() => OKButtonEnabled, ref _oKButtonEnabled, value);
}
}
#endregion
#region 当前选择仓库
private TakaDeliveryGoodsApplyModel _currentWarehouse = null;
///
/// Sets and gets the CurrentWarehouse property.
/// Changes to that property's value raise the PropertyChanged event.
///
public TakaDeliveryGoodsApplyModel CurrentWarehouse
{
get
{
return _currentWarehouse;
}
set
{
Set(() => CurrentWarehouse, ref _currentWarehouse, value);
}
}
#endregion
#region 仓库列表
private List _warehouseList = new List();
///
/// 仓库列表
/// Changes to that property's value raise the PropertyChanged event.
///
public List WarehouseList
{
get
{
return _warehouseList;
}
set
{
Set(() => WarehouseList, ref _warehouseList, 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
#region 提货方式
private TakeGoodsWay _takeGoodsWay;
///
/// 提货方式
///
public TakeGoodsWay TakeGoodsWay
{
get { return _takeGoodsWay; }
set { Set(() => TakeGoodsWay, ref _takeGoodsWay, value); }
}
#endregion
#region 身份证
private string _idCardNum;
///
/// 身份证号
///
public string IdCardNum
{
get { return _idCardNum; }
set { Set(() => IdCardNum, ref _idCardNum, value); }
}
#endregion
#region 所有提货仓单ListSource
public List TempTakaDeliveryGoodsOrderList = new List();
private List _allTakaDeliveryGoodsOrderList= new List();
///
/// 所有提货仓单ListSource
///
public List AllTakaDeliveryGoodsOrderList
{
get { return _allTakaDeliveryGoodsOrderList; }
set { Set(() => AllTakaDeliveryGoodsOrderList, ref _allTakaDeliveryGoodsOrderList, value); }
}
#endregion
#region 当前勾选单据
private List _currentSelectedOutStore = null;
///
/// 当前勾选的提货单据LIST
///
public List CurrentSelectedOutStore
{
get
{
return _currentSelectedOutStore;
}
set
{
Set(() => CurrentSelectedOutStore, ref _currentSelectedOutStore, value);
}
}
#endregion
#endregion "Public Properties To Get/Set "
#region "=========公共命令/Public Commands"
#region 窗口取消Command
///
/// 窗口取消
///
public RelayCommand CancelCommand
{
get
{
return new RelayCommand((dialog) =>
{
dialog.DialogResult = false;
});
}
}
#endregion
#region 选中Command
///
/// 选中
///
private RelayCommand selectCommand;
public RelayCommand SelectCommand
{
get
{
return selectCommand ?? (selectCommand = new RelayCommand(
() =>
{
SettlementSelectedOutStore();
}));
}
}
#endregion
#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) =>
{
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,
PhoneNum = PhoneNum,
OutStoreDetail = CurrentSelectedOutStore,
TakeGoodsWay=TakeGoodsWay,
OperatorID = UserManager.CurrentTradeAccount.AccountId,
IdCardNum = IdCardNum,
};
if (UserManager.CurrentTradeAccount.FundsAccounts.Any() &&
UserManager.CurrentTradeAccount.FundsAccounts[0] != null)
{
newOrder.AccountID = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
}
else
{
newOrder.AccountID = UserManager.CurrentTradeAccount.AccountId;
}
_deliveryService.SubmitOutStoreReq(newOrder, EntrurstSuccessCallBack, EntrurstErrorCallBack);
}
else
{
MessageBoxHelper.ShowInfo(errorMsg, Client_Resource.MessageBox_Error_Title);
OKButtonEnabled = true;
}
});
}
}
#endregion
#region 出库选择-发生改变-发送命令
private RelayCommand _warehouseSelectionChangedCommand;
///
/// Gets the WarehouseSelectionChangedCommand.
///
public RelayCommand WarehouseSelectionChangedCommand
{
get
{
return _warehouseSelectionChangedCommand
?? (_warehouseSelectionChangedCommand = new RelayCommand(
() =>
{
#region 处理事件ChangedEvent
FilteOutStoreOrderListByCombox();
#endregion
}));
}
}
#endregion
#endregion "Public Commands"
#region "=========私有方法/Private Methods"
///
/// 处理选择提货
///
private void SettlementSelectedOutStore()
{
_currentSelectedOutStore = new List();
var sourceList = AllTakaDeliveryGoodsOrderList.ToList().FindAll(p => p.IsSelected == true);
if (sourceList != null && sourceList.Count == 1) ///选择一条数据时显示
{
RecieveAddress = sourceList[0].Address;
RecievePerson = sourceList[0].PersonName;
PhoneNum = sourceList[0].Telphone;
TakeGoodsWay = sourceList[0].TakeGoodsWay;
IdCardNum = sourceList[0].IdCardNum;
}
long tempDeliveryGoodsId=0;
for (int i = 0; i < sourceList.Count(); i++)
{
_currentSelectedOutStore.Add(new OutStoreReqDetailModel() { WRId = (sourceList[i].WRID), StoreOutQty = ((sourceList[i].Qty - sourceList[i].FreezeQty -sourceList[i].PayQty)) });
if (tempDeliveryGoodsId == 0)
{
tempDeliveryGoodsId = sourceList[i].DeliveryGoodsId;
}
}
#region 只能提取相同的交割商品[注销]
//if (sourceList.Count() > 0)
//{
// var sourceListSame = AllTakaDeliveryGoodsOrderList.ToList().FindAll(p => p.IsSelected != true && p.DeliveryGoodsId != tempDeliveryGoodsId);
// for (int i = 0; i < sourceListSame.Count(); i++)
// {
// sourceListSame[i].IsEnabled = false;
// }
//}
//else
//{
// var sourceListAll = AllTakaDeliveryGoodsOrderList.ToList();
// for (int i = 0; i < sourceListAll.Count(); i++)
// {
// sourceListAll[i].IsEnabled = true;
// }
//}
#endregion
IsEnableButtonEvent();
}
///
/// 初始化数据
///
private void InitData()
{
var accountid = UserManager.CurrentTradeAccount.AccountId;
//if (UserManager.CurrentTradeAccount.FundsAccounts != null && UserManager.CurrentTradeAccount.FundsAccounts.Any())
//{
// accountid = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
//}
IsBusy = true;
_deliveryService.QueryTakaDeliveryGoodsOrdersAppy(accountid, MarketOrderSuccess, QueryErrorCallback);
//赋默赋值给下拉列表
_warehouseList.Add(new TakaDeliveryGoodsApplyModel() { WareHouseName = Client_Resource.DeliveryMatchView_AllWareHouse });
CurrentWarehouse = _warehouseList.FirstOrDefault();//默认选择
IsEnableButtonEvent();
//读取登录人基本信息
GetDepositPersonalInfo();
#region 测试数据
//for (int i = 0; i < 5; i++)
//{
// _allTakaDeliveryGoodsOrderList.Add(new TakaDeliveryGoodsApplyModel() { WRID = 10003 + i, WRCode = DateTime.Now.ToShortTimeString(),WareHouseName="仓库"+i });
//}
//TempTakaDeliveryGoodsOrderList = _allTakaDeliveryGoodsOrderList;
//BindingWarehouseComBoxList(_allTakaDeliveryGoodsOrderList);
#endregion
}
///
/// 获取交割提货个人信息
///
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)
{
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;
}
///
/// 过滤出库数据BY下拉列表
///
private void FilteOutStoreOrderListByCombox()
{
#region 重置勾选单据
var sourceList = AllTakaDeliveryGoodsOrderList.ToList().FindAll(p => p.IsSelected == true);
for (int i = 0; i < sourceList.Count(); i++)
{
sourceList[i].IsSelected = false;
}
#endregion
AllTakaDeliveryGoodsOrderList = TempTakaDeliveryGoodsOrderList;//回归数据
if (_currentWarehouse.WareHouseName != Client_Resource.DeliveryMatchView_AllWareHouse)//全部仓库
{
AllTakaDeliveryGoodsOrderList = AllTakaDeliveryGoodsOrderList.Where(x => x.WareHouseName == _currentWarehouse.WareHouseName).ToList();
}
}
///
/// 触发提交按钮是否可用
///
private void IsEnableButtonEvent()
{
if (_currentSelectedOutStore != null && _currentSelectedOutStore.Any() && _currentSelectedOutStore.Count() > 0)
{
OKButtonEnabled = true;
}
else
{
OKButtonEnabled = false;
}
}
///
/// 绑定-仓库-下拉列表数据
///
///
private void BindingWarehouseComBoxList(List itemList)
{
if (itemList != null && itemList.Any())
{
foreach (var item in itemList)
{
//绑定-仓库-下拉列表数据
if (!WarehouseList.Exists(x => x.WareHouseName == item.WareHouseName))
{
WarehouseList.Add(item);
}
}
}
}
///
/// 查询成功返回
///
///
protected void MarketOrderSuccess(List orders)
{
UpdateItemSource(orders);
IsBusy = false;
}
///
/// 查询失败返回
///
///
public void QueryErrorCallback(ErrorEntity errorEntity)
{
IsBusy = false;
//var errorMsg = string.Format("{0}:{1}", errorEntity.RequestFunc, ErrorManager.FormatErrorMsg(errorEntity)); //错误信息显示到状态
//MessengerHelper.DefaultSend(errorMsg, MessengerTokens.ErrorMessage);
// base.QueryErrorCallback();
}
///
/// 更新数据源-绑定数据源
///
///
private void UpdateItemSource(List orders)
{
if (orders != null)
{
foreach (var item in orders)
{
#region 合约单位转换
var goodsUnit = CacheManager.TradeGoodsUnit.FirstOrDefault((x) => x.UNITID == item.GoodsUnitId);
if (goodsUnit != null)
{
item.GoodsUnitIdDisplay = goodsUnit.GOODSUNITNAME;
}
#endregion
#region 是否可以提货逻辑
//可用数量 = 注册数量-冻结数量-兑付数量
var canTakeGoodsNum = item.Qty - item.FreezeQty - item.PayQty;
//正常状态 且 可用数量大于零 可以提货
if (canTakeGoodsNum > 0 && item.Status == eWRStatus.Normal)
{
item.CanTakeGoods = Visibility.Visible;
}
#endregion
}
AllTakaDeliveryGoodsOrderList = TempTakaDeliveryGoodsOrderList = orders.Where(x => x.Status == eWRStatus.Normal && x.CanTakeGoods == Visibility.Visible).ToList();//所有单据
BindingWarehouseComBoxList(AllTakaDeliveryGoodsOrderList);
}
}
///
/// 提交提货成功返回
///
///
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 (RecievePerson.Trim() == "")
{
msg = Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_RecievePerson + Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_NeedToWrite;
return false;
}
if (TakeGoodsWay == TakeGoodsWay.Express && RecieveAddress.Trim() == "")
{
msg = Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_RecieveAddress + Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_NeedToWrite;
return false;
}
if (PhoneNum.Trim() == "")
{
msg = Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_PhoneNum + Muchinfo.MTPClient.Resources.Client_Resource.TakaDeliveryGoodsApplyViewModel_NeedToWrite;
return false;
}
if (PhoneNum.Trim() != "" && !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"
}
}