//----------------------------------------------------------------
//Module Name: FocusGoodsSelectViewModel
//Purpose:
//CopyRight: Muchinfo
//History:
//----------------------------------------------------------------
//DateTime Author Description
//----------------------------------------------------------------
//2014-03-26 deng.yinping Create
//----------------------------------------------------------------
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using Muchinfo.MTPClient.Data;
using Muchinfo.MTPClient.Infrastructure.Utilities;
using Muchinfo.MTPClient.IService;
using Muchinfo.MTPClient.Resources;
using Muchinfo.MTPClient.UI.Views;
using Muchinfo.WPF.Controls.Windows;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
namespace Muchinfo.MTPClient.UI.ViewModels
{
///
/// EditQuoteListHeaderViewModel类
///
public class FocusGoodsSelectViewModel : ViewModelBase
{
#region Fields
private ObservableCollection _exchangeList;
private ObservableCollection _selectedItems;
private TreeViewEntity _currentListViewItem;
///
/// 最多关注数量
///
private int _maxCount = 3;
#endregion Fields
#region Constructors
///
/// Initializes a new instance of the class.
///
public FocusGoodsSelectViewModel()
{
InitializeData();
}
#endregion Constructors
#region Properties
#region Public Properties
public ObservableCollection ExchangeList
{
get
{
return _exchangeList;
}
set
{
Set(() => ExchangeList, ref _exchangeList, value);
}
}
public ObservableCollection SelectedItems
{
get
{
return _selectedItems;
}
set
{
Set(() => SelectedItems, ref _selectedItems, value);
}
}
public TreeViewEntity CurrentListViewEntity
{
get
{
return _currentListViewItem;
}
set
{
Set(() => CurrentListViewEntity, ref _currentListViewItem, value);
}
}
///
/// 获取和设置the OK command
///
public RelayCommand OKCommand
{
get
{
return new RelayCommand((view) =>
{
view.DialogResult = true;
UserManager.SaveFocusGoodsList(SelectedItems == null ? null : SelectedItems.ToList());
});
}
}
///
/// 获取和设置the cancel command
///
public RelayCommand CancelCommand
{
get
{
return new RelayCommand((view) =>
{
view.DialogResult = false;
});
}
}
///
/// 获取和设置the in command
///
public RelayCommand InCommand
{
get
{
return new RelayCommand((entity) =>
{
//有子集,返回
if (entity == null || (entity.Children != null && entity.Children.Any())) return;
if (SelectedItems.Count == _maxCount)
{
MessageBoxHelper.ShowInfo(string.Format(Muchinfo_Resource.UI2014_CanOnlyAddCentainAmountFocusGoods, _maxCount.ToString()), Muchinfo_Resource.UI2014_FoucusGoodsSettingTips);
}
else
{
var item = SelectedItems.FirstOrDefault(z => z.Key == entity.Key);
if (item == null)
{
SelectedItems.Add(entity);
CurrentListViewEntity = entity;
}
}
});
}
}
///
/// 获取和设置the out command
///
public RelayCommand OutCommand
{
get
{
return new RelayCommand((entity) =>
{
if (entity == null) return;
var item = SelectedItems.FirstOrDefault(z => z.Key == entity.Key);
if (item == null) return;
SelectedItems.Remove(item);
});
}
}
///
/// 获取和设置up command
///
public RelayCommand UpCommand
{
get
{
return new RelayCommand((listbox) =>
{
if (listbox == null) return;
var item = listbox.SelectedItem;
if (item == null) return;
var index = listbox.SelectedIndex;
if (index == 0) return;
_selectedItems.Move(index, index - 1);
listbox.ScrollIntoView(item);
});
}
}
///
/// 获取和设置down command
///
public RelayCommand DownCommand
{
get
{
return new RelayCommand((view) =>
{
if (view == null) return;
var item = view.SelectedItem;
if (item == null) return;
var index = view.SelectedIndex;
if (index == (_selectedItems.Count - 1)) return;
_selectedItems.Move(index, index + 1);
view.ScrollIntoView(item);
});
}
}
#endregion Public Properties
#endregion Properties
///
/// 数据初始化
///
private void InitializeData()
{
var service = SimpleIoc.Default.GetInstance();
ExchangeList = service.GetExchangeTree();
//todo:获取本地数据初始化关注商品
var goodsList = UserManager.GetFocusGoodsList();
var list = new ObservableCollection();
if (goodsList != null && goodsList.Any())
{
foreach (var goods in goodsList)
{
if (goods == null) continue;
list.Add(new TreeViewEntity() { Key = goods.GoodsCode, Value = string.IsNullOrWhiteSpace(goods.Name) ? goods.GoodsCode : goods.Name });
}
}
SelectedItems = list;
}
}
}