QuoteBoardViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using GalaSoft.MvvmLight;
  2. using GalaSoft.MvvmLight.Command;
  3. using GalaSoft.MvvmLight.Ioc;
  4. using GalaSoft.MvvmLight.Messaging;
  5. using Muchinfo.MTPClient.Data.DTO;
  6. using Muchinfo.MTPClient.Data.Enums;
  7. using Muchinfo.MTPClient.Data.Model;
  8. using Muchinfo.MTPClient.Infrastructure.Cache;
  9. using Muchinfo.MTPClient.Infrastructure.Helpers;
  10. using Muchinfo.MTPClient.Infrastructure.Utilities;
  11. using Muchinfo.MTPClient.IService;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.Linq;
  15. namespace Muchinfo.MTPClient.Quotation.ViewModels
  16. {
  17. public class QuoteBoardViewModel : ViewModelBase
  18. {
  19. #region Fields
  20. private IGoodsService _goodsService;
  21. private IFavoriteService _ifavoriteService;
  22. private QuoteGoodsDTO _goodsItem;
  23. /// <summary>
  24. /// 数据集合
  25. /// </summary>
  26. private ObservableCollection<QuoteGoodsDTO> _itemsSource;
  27. private RelayCommand<QuoteGoodsDTO> _sellCommand;
  28. private RelayCommand<QuoteGoodsDTO> _buyCommand;
  29. #endregion Fields
  30. private bool _isVisibilityGoodsOfPAndX = false;
  31. /// <summary>
  32. /// 是否显示P+X商品数据源LIST
  33. /// </summary>
  34. public bool IsVisibilityGoodsOfPAndX
  35. {
  36. get
  37. {
  38. return _isVisibilityGoodsOfPAndX && (UserManager.CurrentGroupHasGoods && IsDeliveryVisible);
  39. }
  40. set
  41. {
  42. Set(() => IsVisibilityGoodsOfPAndX, ref _isVisibilityGoodsOfPAndX, value);
  43. }
  44. }
  45. private string _PDeliveryGoodsName;
  46. /// <summary>
  47. /// P交割模式商品
  48. /// </summary>
  49. public string PDeliveryGoodsName
  50. {
  51. get
  52. {
  53. return _PDeliveryGoodsName;
  54. }
  55. set
  56. {
  57. Set(() => PDeliveryGoodsName, ref _PDeliveryGoodsName, value);
  58. }
  59. }
  60. private eDeliveryContractLink _relationMode;
  61. /// <summary>
  62. /// 关联模式
  63. /// </summary>
  64. public eDeliveryContractLink RelationMode
  65. {
  66. get
  67. {
  68. return _relationMode;
  69. }
  70. set
  71. {
  72. Set(() => RelationMode, ref _relationMode, value);
  73. }
  74. }
  75. public QuoteGoods SelectedQuoteGoods { get; set; }
  76. private ObservableCollection<QuoteGoodsDTO> _goodsOfPAndXInfoList = new ObservableCollection<QuoteGoodsDTO>();
  77. /// <summary>
  78. /// 选择商品的P+X商品数据源
  79. /// </summary>
  80. public ObservableCollection<QuoteGoodsDTO> GoodsOfPandXInfoList
  81. {
  82. get
  83. {
  84. return _goodsOfPAndXInfoList;
  85. }
  86. set
  87. {
  88. Set(() => GoodsOfPandXInfoList, ref _goodsOfPAndXInfoList, value);
  89. }
  90. }
  91. #region 是否可交易
  92. #region P+X 信息显示高度
  93. private decimal _pXInfoHeight = decimal.Zero;
  94. /// <summary>
  95. /// P+X 信息显示高度
  96. /// </summary>
  97. public decimal PXInfoHeight
  98. {
  99. get
  100. {
  101. return _pXInfoHeight;
  102. }
  103. set
  104. {
  105. Set(() => PXInfoHeight, ref _pXInfoHeight, value);
  106. }
  107. }
  108. #endregion
  109. public bool IsDeliveryVisible
  110. {
  111. get
  112. {
  113. return ApplicationParameter.IsDeliveryVisible == 1 && UserManager.IsAccountLogin;
  114. }
  115. }
  116. /// <summary>
  117. /// 是否买入
  118. /// </summary>
  119. public bool IsBidEnable
  120. {
  121. get
  122. {
  123. //var buyFunc = UserManager.TradeFuncMenus.FirstOrDefault((item) => item.FuncMenuCode == UserManager.GetMenuCommandValue(MenuCommandType.BuyIn));
  124. var buyFunc = UserManager.TradeFuncMenus.FirstOrDefault((item) => item.FuncMenuCode == MenuCommandType.BuyIn.ToString());
  125. //买入
  126. if (buyFunc != null)
  127. {
  128. return buyFunc.IsEnabled;
  129. }
  130. return false;
  131. }
  132. }
  133. /// <summary>
  134. /// 是否卖出
  135. /// </summary>
  136. public bool IsAskEnable
  137. {
  138. get
  139. {
  140. //var tradeFunc =UserManager.TradeFuncMenus.FirstOrDefault((item) => item.FuncMenuCode == UserManager.GetMenuCommandValue(MenuCommandType.Sellout));
  141. var tradeFunc = UserManager.TradeFuncMenus.FirstOrDefault((item) => item.FuncMenuCode == MenuCommandType.Sellout.ToString());
  142. //卖出
  143. if (tradeFunc != null)
  144. {
  145. return tradeFunc.IsEnabled;
  146. }
  147. return false;
  148. }
  149. }
  150. #endregion
  151. #region Constructors
  152. /// <summary>
  153. /// Initializes a new instance of the <see cref="QuoteDataGridViewModel" /> class.
  154. /// </summary>
  155. public QuoteBoardViewModel()
  156. : base()
  157. {
  158. _goodsService = SimpleIoc.Default.GetInstance<IGoodsService>();
  159. _ifavoriteService = SimpleIoc.Default.GetInstance<IFavoriteService>();
  160. _sellCommand = new RelayCommand<QuoteGoodsDTO>((dto) =>
  161. {
  162. MessengerHelper.DefaultSend<QuoteGoodsDTO>(dto, MessengerTokens.QuoteBoardSelectItemChange);
  163. MessengerHelper.DefaultSend<QuoteGoodsDTO>(dto, MessengerTokens.QuickSell);
  164. });
  165. _buyCommand = new RelayCommand<QuoteGoodsDTO>((dto) =>
  166. {
  167. MessengerHelper.DefaultSend<QuoteGoodsDTO>(dto, MessengerTokens.QuoteBoardSelectItemChange);
  168. MessengerHelper.DefaultSend<QuoteGoodsDTO>(dto, MessengerTokens.QuickBuy);
  169. });
  170. //注册消息
  171. RegisterMessages();
  172. }
  173. #endregion Constructors
  174. #region Properties
  175. #region Public Properties
  176. /// <summary>
  177. /// 获取和设置数据集合
  178. /// </summary>
  179. public ObservableCollection<QuoteGoodsDTO> ItemsSource
  180. {
  181. get
  182. {
  183. return _itemsSource;
  184. }
  185. private set
  186. {
  187. _itemsSource = value;
  188. RaisePropertyChanged(() => ItemsSource);
  189. }
  190. }
  191. #endregion Public Properties
  192. #endregion Properties
  193. #region Methods
  194. #region Public Methods
  195. public RelayCommand<QuoteGoodsDTO> SellCommand
  196. {
  197. get { return _sellCommand; }
  198. }
  199. public RelayCommand<QuoteGoodsDTO> BuyCommand
  200. {
  201. get { return _buyCommand; }
  202. }
  203. /// <summary>
  204. /// 命令类型改变时更新数据集合,并刷新盘面
  205. /// </summary>
  206. /// <param name="newType">The new type.</param>
  207. /// <param name="goodsGroup">The goods group.</param>
  208. public void RefreshList(MenuCommandType newType, MarketsInfoModel goodsGroup)
  209. {
  210. if (_itemsSource != null) ItemsSource.Clear();
  211. if (goodsGroup.TradeMode == eTradeMode.MySelected)
  212. {
  213. var goodsList = _ifavoriteService.GetMyFavoriteGoodses(goodsGroup.MarketID);
  214. ItemsSource = goodsList == null ? null : _goodsService.ConvertGoodsDtos(goodsList.ToList());
  215. }
  216. else
  217. {
  218. ItemsSource = _goodsService.GetQuoteGoodsDTOList(newType, goodsGroup);
  219. }
  220. ////重新订阅并查询盘面
  221. UserManager.CurrentGoodsIds = ItemsSource == null ? new List<int>() : ItemsSource.Select(z => (int)z.GoodsId).ToList();
  222. QuoteProxyHelper.QuoteSubscribe(GoodsFromScr.Trade);
  223. }
  224. #endregion Public Methods
  225. #region Private Methods
  226. /// <summary>
  227. /// Registers the resource change.
  228. /// </summary>
  229. private void RegisterMessages()
  230. {
  231. //Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  232. //{
  233. // MessengerHelper.DefaultUnregister<string>(this, MessengerTokens.RegisterResourceChange);
  234. // MessengerHelper.DefaultRegister<string>(this, MessengerTokens.RegisterResourceChange, (e) =>
  235. // {
  236. // if (_itemsSource == null || !_itemsSource.Any()) return;
  237. // foreach (var item in _itemsSource)
  238. // {
  239. // if (item == null) continue;
  240. // item.UpdateNameColor(item.IsSelected);
  241. // }
  242. // });
  243. //}));
  244. ////注册实时行情消息
  245. MessengerHelper.QuoteRegister<List<QuoteGoods>>(this,
  246. MessengerTokens.ReceiveRealTimeQuote, (quoteList) =>
  247. {
  248. if (quoteList == null || !quoteList.Any() || _itemsSource == null || !_itemsSource.Any())
  249. return;
  250. foreach (var goods in quoteList)
  251. {
  252. if (goods == null) continue;
  253. var item = _itemsSource.FirstOrDefault(z => z.Symbol == goods.Symbol);
  254. if (item == null) continue;
  255. item.UpdateFromQuoteGoods(goods);
  256. }
  257. //更新X商品行情
  258. if (this._goodsItem != null)
  259. {
  260. SelectGoodsOfPAndX_ItemSource(_goodsItem);
  261. }
  262. });
  263. //MessengerHelper.DefaultRegister<QuoteGoodsDTO>(this, MessengerTokens.SelectGoodsChange, SelectGoodsOfPAndX_ItemSource);
  264. }
  265. /// <summary>
  266. /// 获取选择商品的P+X商品数据源
  267. /// </summary>
  268. public void SelectGoodsOfPAndX_ItemSource(QuoteGoodsDTO goodsItem)
  269. {
  270. //PXInfoHeight = 23;
  271. this._goodsItem = goodsItem;
  272. var quoteGoods = CacheManager.CacheGoodsBaseInfos.FirstOrDefault((item) => item.Symbol == goodsItem.Symbol);
  273. SelectedQuoteGoods = quoteGoods;
  274. IsVisibilityGoodsOfPAndX = false;
  275. var tempPAndXList = CacheManager.AllDeliveryRelationList.Where((item) => item.GoodsId == goodsItem.GoodsId).ToList();
  276. var dicModel = new List<QuoteGoods>();
  277. if (tempPAndXList != null && tempPAndXList.Count() > 0)
  278. {
  279. if (IsDeliveryVisible)
  280. {
  281. IsVisibilityGoodsOfPAndX = true;
  282. }
  283. foreach (var relationModel in tempPAndXList)
  284. {
  285. RelationMode = relationModel.RelationMode;
  286. QuoteGoods delivery_Goods = null;
  287. if (relationModel.RelationMode == eDeliveryContractLink.Normal)
  288. {
  289. delivery_Goods = quoteGoods;
  290. PDeliveryGoodsName = relationModel.deliverygoodsname;
  291. //PXInfoHeight = 51;
  292. }
  293. else if (relationModel.RelationMode == eDeliveryContractLink.PAndX)
  294. {
  295. delivery_Goods =
  296. CacheManager.CacheGoodsBaseInfos.FirstOrDefault((item) => relationModel.RelationGoodsId == item.GoodsId);
  297. // PXInfoHeight = 108;
  298. }
  299. if (delivery_Goods != null)
  300. {
  301. delivery_Goods.DeliveryGoodsName = relationModel.deliverygoodsname;
  302. dicModel.Add(delivery_Goods);
  303. } //// DeliveryGoodsName
  304. }
  305. var goodsDtos = _goodsService.ConvertGoodsDtos(dicModel.ToList());
  306. GoodsOfPandXInfoList = goodsDtos;
  307. }
  308. }
  309. /// <summary>
  310. /// Cleanups this instance.
  311. /// </summary>
  312. public override void Cleanup()
  313. {
  314. base.Cleanup();
  315. _itemsSource = null;
  316. }
  317. #endregion Private Methods
  318. #endregion Methods
  319. }
  320. }