OpenFrameViewModel.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //----------------------------------------------------------------
  7. //Module Name: $safeprojectname$
  8. //Purpose:
  9. //CopyRight: Muchinfo
  10. //History:
  11. //----------------------------------------------------------------
  12. //DateTime 2016/1/16 10:22:14
  13. //Author
  14. //Description Create
  15. //----------------------------------------------------------------
  16. using System.Windows;
  17. using GalaSoft.MvvmLight;
  18. using GalaSoft.MvvmLight.Command;
  19. using Muchinfo.MTPClient.Data;
  20. using Muchinfo.MTPClient.Data.Enums;
  21. using Muchinfo.MTPClient.Data.Model;
  22. using Muchinfo.MTPClient.Data.Model.Account;
  23. using Muchinfo.MTPClient.Infrastructure.Cache;
  24. using Muchinfo.MTPClient.Infrastructure.Utilities;
  25. using Muchinfo.MTPClient.IService;
  26. using Muchinfo.MTPClient.Resources;
  27. using Muchinfo.MTPClient.Service;
  28. using Muchinfo.WPF.Controls.Windows;
  29. using GalaSoft.MvvmLight.Ioc;
  30. using GalaSoft.MvvmLight.Messaging;
  31. namespace Muchinfo.MTPClient.Trade.ViewModels
  32. {
  33. /// <summary>
  34. /// Class OpenFrameViewModel.
  35. /// </summary>
  36. public class OpenFrameViewModel : ViewModelBase
  37. {
  38. private IGoodsService _goodsService;
  39. private OrderType _orderType; ////打开口时默认订单类型
  40. private Window _openWindow; ////服务成功关闭窗口
  41. /// <summary>
  42. /// 是否忙,显示等待控件
  43. /// </summary>
  44. private bool _isBusy;
  45. /// <summary>
  46. /// 是否忙,显示等待控件
  47. /// </summary>
  48. public bool IsBusy
  49. {
  50. get
  51. {
  52. return _isBusy;
  53. }
  54. set
  55. {
  56. Set(() => IsBusy, ref _isBusy, value);
  57. }
  58. }
  59. private string _busyTips;
  60. /// <summary>
  61. /// 服务忙提示
  62. /// </summary>
  63. public string BusyTips
  64. {
  65. get
  66. {
  67. return _busyTips;
  68. }
  69. set
  70. {
  71. Set(() => BusyTips, ref _busyTips, value);
  72. }
  73. }
  74. /// <summary>
  75. /// 账号信息
  76. /// </summary>
  77. public uint AccountId { get; set; }
  78. private List<QuoteGoods> _goodsList;
  79. /// <summary>
  80. /// 商品列表
  81. /// </summary>
  82. public List<QuoteGoods> GoodsList
  83. {
  84. get
  85. {
  86. return _goodsList;
  87. }
  88. set
  89. {
  90. Set(() => GoodsList, ref _goodsList, value);
  91. }
  92. }
  93. private QuoteGoods _currentGoods;
  94. /// <summary>
  95. /// 当前商品
  96. /// </summary>
  97. public QuoteGoods CurrentGoods
  98. {
  99. get { return _currentGoods; }
  100. set
  101. {
  102. Set(() => CurrentGoods, ref _currentGoods, value);
  103. SetQuoteGoods();
  104. }
  105. }
  106. private TradeOrderBase _tradeOrderBase;
  107. /// <summary>
  108. /// 做市竞价类型
  109. /// </summary>
  110. public TradeOrderBase TradeOrderBase
  111. {
  112. get
  113. {
  114. return _tradeOrderBase;
  115. }
  116. set
  117. {
  118. Set(() => TradeOrderBase, ref _tradeOrderBase, value);
  119. }
  120. }
  121. /// <summary>
  122. /// Initializes a new instance of the <see cref="OpenFrameViewModel" /> class.
  123. /// </summary>
  124. /// <param name="quoteGoods">The quote goods.</param>
  125. /// <param name="orderType">建仓类型使用OTC市价或限价参数</param>
  126. public OpenFrameViewModel(QuoteGoods quoteGoods, OrderType orderType = OrderType.MarketOpenOrder)
  127. {
  128. _goodsService = SimpleIoc.Default.GetInstance<IGoodsService>();
  129. GoodsList = CacheManager.CacheGoodsBaseInfos; //_goodsService.GetGoodsInfoListByExchangeId(UserManager.CurrentTradeAccount.ExchangeId).ToList();
  130. _orderType = orderType;
  131. //todo:取可交易的商品,不一定是一个交易所的
  132. if (quoteGoods != null)
  133. {
  134. CurrentGoods = GoodsList.FirstOrDefault((item) => item.GoodsCode == quoteGoods.GoodsCode);
  135. }
  136. ////找不到商品取默认的
  137. if (CurrentGoods == null)
  138. {
  139. CurrentGoods = GoodsList.FirstOrDefault();
  140. }
  141. AccountId = UserManager.CurrentTradeAccount.AccountId;
  142. }
  143. /// <summary>
  144. /// Sets the quote goods.
  145. /// </summary>
  146. private void SetQuoteGoods()
  147. {
  148. if (_currentGoods != null)
  149. {
  150. if (_currentGoods.GoodsParameters != null && _currentGoods.GoodsParameters.TradeMode == eTradeMode.TRADEMODE_BIDDING)
  151. {
  152. OrderType orderType = _orderType == OrderType.MarketOpenOrder
  153. ? OrderType.BidMarketOpen
  154. : OrderType.BidLimitOpen;
  155. TradeOrderBase = new BidOpenOrderViewModel(_currentGoods, orderType);
  156. }
  157. else
  158. {
  159. TradeOrderBase = new MakeOrderViewModel(_currentGoods, _orderType);
  160. }
  161. }
  162. }
  163. /// <summary>
  164. /// 快速下单窗口取消
  165. /// </summary>
  166. public RelayCommand<Window> CancelCommand
  167. {
  168. get
  169. {
  170. return new RelayCommand<Window>((dialog) =>
  171. {
  172. dialog.DialogResult = false;
  173. });
  174. }
  175. }
  176. private bool _oKButtonEnabled = true;
  177. /// <summary>
  178. ///设置按键不可用
  179. /// </summary>
  180. public bool OKButtonEnabled
  181. {
  182. get
  183. {
  184. return _oKButtonEnabled;
  185. }
  186. set
  187. {
  188. Set(() => OKButtonEnabled, ref _oKButtonEnabled, value);
  189. }
  190. }
  191. /// <summary>
  192. /// 下单确定
  193. /// </summary>
  194. public RelayCommand<Window> OKCommand
  195. {
  196. get
  197. {
  198. return new RelayCommand<Window>((dialog) =>
  199. {
  200. OKButtonEnabled = false;
  201. string errorMsg = string.Empty;
  202. bool validateBool = _tradeOrderBase.Validated(); ////内容验证
  203. if (validateBool)
  204. {
  205. _openWindow = dialog;
  206. IsBusy = true;
  207. BusyTips = Muchinfo_Resource.Window_Wait;
  208. _tradeOrderBase.PostOrder(EntrurstSuccessCallBack, EntrurstErrorCallBack); ////获取表单内容
  209. }
  210. else
  211. {
  212. MessageBoxHelper.ShowInfo(errorMsg, Muchinfo_Resource.MessageBox_Error_Title);
  213. OKButtonEnabled = true;
  214. }
  215. });
  216. }
  217. }
  218. /// <summary>
  219. /// 提交成功返回
  220. /// </summary>
  221. /// <param name="order"></param>
  222. private void EntrurstSuccessCallBack(OrderDetail order)
  223. {
  224. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  225. {
  226. MessageBoxHelper.ShowSuccess(Muchinfo_Resource.Order_Success_Result,
  227. Muchinfo_Resource.Open_MessageBox_Title);
  228. IsBusy = false;
  229. if (_openWindow != null)
  230. {
  231. _openWindow.Close();
  232. this.Cleanup();
  233. }
  234. }));
  235. Messenger.Default.Send(UserManager.CurrentTradeAccount, MessengerTokens.OrderNotify);
  236. }
  237. /// <summary>
  238. /// 委托失败返回
  239. /// </summary>
  240. /// <param name="errorEntity"></param>
  241. private void EntrurstErrorCallBack(ErrorEntity errorEntity)
  242. {
  243. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  244. {
  245. ErrorManager.ShowReturnError(errorEntity, Muchinfo_Resource.UI2014_Tips, true);
  246. if (_openWindow != null)
  247. {
  248. _openWindow.Close();
  249. this.Cleanup();
  250. }
  251. IsBusy = false;
  252. }));
  253. }
  254. }
  255. }