FocusGoodsSelectViewModel.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //----------------------------------------------------------------
  2. //Module Name: FocusGoodsSelectViewModel
  3. //Purpose:
  4. //CopyRight: Muchinfo
  5. //History:
  6. //----------------------------------------------------------------
  7. //DateTime Author Description
  8. //----------------------------------------------------------------
  9. //2014-03-26 deng.yinping Create
  10. //----------------------------------------------------------------
  11. using GalaSoft.MvvmLight;
  12. using GalaSoft.MvvmLight.Command;
  13. using GalaSoft.MvvmLight.Ioc;
  14. using Muchinfo.MTPClient.Data;
  15. using Muchinfo.MTPClient.Infrastructure.Utilities;
  16. using Muchinfo.MTPClient.IService;
  17. using Muchinfo.MTPClient.Resources;
  18. using Muchinfo.MTPClient.UI.Views;
  19. using Muchinfo.WPF.Controls.Windows;
  20. using System.Collections.ObjectModel;
  21. using System.Linq;
  22. using System.Windows.Controls;
  23. namespace Muchinfo.MTPClient.UI.ViewModels
  24. {
  25. /// <summary>
  26. /// EditQuoteListHeaderViewModel类
  27. /// </summary>
  28. public class FocusGoodsSelectViewModel : ViewModelBase
  29. {
  30. #region Fields
  31. private ObservableCollection<TreeViewEntity> _exchangeList;
  32. private ObservableCollection<TreeViewEntity> _selectedItems;
  33. private TreeViewEntity _currentListViewItem;
  34. /// <summary>
  35. /// 最多关注数量
  36. /// </summary>
  37. private int _maxCount = 3;
  38. #endregion Fields
  39. #region Constructors
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="EditQuoteListHeaderViewModel" /> class.
  42. /// </summary>
  43. public FocusGoodsSelectViewModel()
  44. {
  45. InitializeData();
  46. }
  47. #endregion Constructors
  48. #region Properties
  49. #region Public Properties
  50. public ObservableCollection<TreeViewEntity> ExchangeList
  51. {
  52. get
  53. {
  54. return _exchangeList;
  55. }
  56. set
  57. {
  58. Set(() => ExchangeList, ref _exchangeList, value);
  59. }
  60. }
  61. public ObservableCollection<TreeViewEntity> SelectedItems
  62. {
  63. get
  64. {
  65. return _selectedItems;
  66. }
  67. set
  68. {
  69. Set(() => SelectedItems, ref _selectedItems, value);
  70. }
  71. }
  72. public TreeViewEntity CurrentListViewEntity
  73. {
  74. get
  75. {
  76. return _currentListViewItem;
  77. }
  78. set
  79. {
  80. Set(() => CurrentListViewEntity, ref _currentListViewItem, value);
  81. }
  82. }
  83. /// <summary>
  84. /// 获取和设置the OK command
  85. /// </summary>
  86. public RelayCommand<FocusGoodsSelectView> OKCommand
  87. {
  88. get
  89. {
  90. return new RelayCommand<FocusGoodsSelectView>((view) =>
  91. {
  92. view.DialogResult = true;
  93. UserManager.SaveFocusGoodsList(SelectedItems == null ? null : SelectedItems.ToList());
  94. });
  95. }
  96. }
  97. /// <summary>
  98. /// 获取和设置the cancel command
  99. /// </summary>
  100. public RelayCommand<FocusGoodsSelectView> CancelCommand
  101. {
  102. get
  103. {
  104. return new RelayCommand<FocusGoodsSelectView>((view) =>
  105. {
  106. view.DialogResult = false;
  107. });
  108. }
  109. }
  110. /// <summary>
  111. /// 获取和设置the in command
  112. /// </summary>
  113. public RelayCommand<TreeViewEntity> InCommand
  114. {
  115. get
  116. {
  117. return new RelayCommand<TreeViewEntity>((entity) =>
  118. {
  119. //有子集,返回
  120. if (entity == null || (entity.Children != null && entity.Children.Any())) return;
  121. if (SelectedItems.Count == _maxCount)
  122. {
  123. MessageBoxHelper.ShowInfo(string.Format(Muchinfo_Resource.UI2014_CanOnlyAddCentainAmountFocusGoods, _maxCount.ToString()), Muchinfo_Resource.UI2014_FoucusGoodsSettingTips);
  124. }
  125. else
  126. {
  127. var item = SelectedItems.FirstOrDefault(z => z.Key == entity.Key);
  128. if (item == null)
  129. {
  130. SelectedItems.Add(entity);
  131. CurrentListViewEntity = entity;
  132. }
  133. }
  134. });
  135. }
  136. }
  137. /// <summary>
  138. /// 获取和设置the out command
  139. /// </summary>
  140. public RelayCommand<TreeViewEntity> OutCommand
  141. {
  142. get
  143. {
  144. return new RelayCommand<TreeViewEntity>((entity) =>
  145. {
  146. if (entity == null) return;
  147. var item = SelectedItems.FirstOrDefault(z => z.Key == entity.Key);
  148. if (item == null) return;
  149. SelectedItems.Remove(item);
  150. });
  151. }
  152. }
  153. /// <summary>
  154. /// 获取和设置up command
  155. /// </summary>
  156. public RelayCommand<ListBox> UpCommand
  157. {
  158. get
  159. {
  160. return new RelayCommand<ListBox>((listbox) =>
  161. {
  162. if (listbox == null) return;
  163. var item = listbox.SelectedItem;
  164. if (item == null) return;
  165. var index = listbox.SelectedIndex;
  166. if (index == 0) return;
  167. _selectedItems.Move(index, index - 1);
  168. listbox.ScrollIntoView(item);
  169. });
  170. }
  171. }
  172. /// <summary>
  173. /// 获取和设置down command
  174. /// </summary>
  175. public RelayCommand<ListBox> DownCommand
  176. {
  177. get
  178. {
  179. return new RelayCommand<ListBox>((view) =>
  180. {
  181. if (view == null) return;
  182. var item = view.SelectedItem;
  183. if (item == null) return;
  184. var index = view.SelectedIndex;
  185. if (index == (_selectedItems.Count - 1)) return;
  186. _selectedItems.Move(index, index + 1);
  187. view.ScrollIntoView(item);
  188. });
  189. }
  190. }
  191. #endregion Public Properties
  192. #endregion Properties
  193. /// <summary>
  194. /// 数据初始化
  195. /// </summary>
  196. private void InitializeData()
  197. {
  198. var service = SimpleIoc.Default.GetInstance<IGoodsService>();
  199. ExchangeList = service.GetExchangeTree();
  200. //todo:获取本地数据初始化关注商品
  201. var goodsList = UserManager.GetFocusGoodsList();
  202. var list = new ObservableCollection<TreeViewEntity>();
  203. if (goodsList != null && goodsList.Any())
  204. {
  205. foreach (var goods in goodsList)
  206. {
  207. if (goods == null) continue;
  208. list.Add(new TreeViewEntity() { Key = goods.GoodsCode, Value = string.IsNullOrWhiteSpace(goods.Name) ? goods.GoodsCode : goods.Name });
  209. }
  210. }
  211. SelectedItems = list;
  212. }
  213. }
  214. }