CloseFrameViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 18:44:34
  13. //Author
  14. //Description Create
  15. //----------------------------------------------------------------
  16. using System.Windows;
  17. using GalaSoft.MvvmLight;
  18. using GalaSoft.MvvmLight.Command;
  19. using GalaSoft.MvvmLight.Ioc;
  20. using GalaSoft.MvvmLight.Messaging;
  21. using Muchinfo.MTPClient.Data;
  22. using Muchinfo.MTPClient.Data.Enums;
  23. using Muchinfo.MTPClient.Data.Model;
  24. using Muchinfo.MTPClient.Data.Model.Account;
  25. using Muchinfo.MTPClient.Infrastructure.Cache;
  26. using Muchinfo.MTPClient.Infrastructure.Utilities;
  27. using Muchinfo.MTPClient.IService;
  28. using Muchinfo.MTPClient.Resources;
  29. using Muchinfo.MTPClient.Service;
  30. using Muchinfo.WPF.Controls.Windows;
  31. namespace Muchinfo.MTPClient.Trade.ViewModels
  32. {
  33. public class CloseFrameViewModel : ViewModelBase
  34. {
  35. /// <summary>
  36. /// 是否忙,显示等待控件
  37. /// </summary>
  38. private bool _isBusy;
  39. /// <summary>
  40. /// 是否忙,显示等待控件
  41. /// </summary>
  42. public bool IsBusy
  43. {
  44. get
  45. {
  46. return _isBusy;
  47. }
  48. set
  49. {
  50. Set(() => IsBusy, ref _isBusy, value);
  51. }
  52. }
  53. private string _busyTips;
  54. /// <summary>
  55. /// 服务忙提示
  56. /// </summary>
  57. public string BusyTips
  58. {
  59. get
  60. {
  61. return _busyTips;
  62. }
  63. set
  64. {
  65. Set(() => BusyTips, ref _busyTips, value);
  66. }
  67. }
  68. /// <summary>
  69. /// 是否可选择商品
  70. /// </summary>
  71. public bool IsGoodsSelect { get; set; }
  72. private List<QuoteGoods> _closeGoodses = new List<QuoteGoods>();
  73. /// <summary>
  74. /// 平仓商品
  75. /// </summary>
  76. public List<QuoteGoods> CloseGoodses
  77. {
  78. get { return _closeGoodses; }
  79. set { Set(() => CloseGoodses, ref _closeGoodses, value); }
  80. }
  81. private QuoteGoods _currentGoods;
  82. /// <summary>
  83. /// 当前商品
  84. /// </summary>
  85. public QuoteGoods CurrentGoods
  86. {
  87. get { return _currentGoods; }
  88. set
  89. {
  90. Set(() => CurrentGoods, ref _currentGoods, value);
  91. if (IsGoodsSelect)
  92. {
  93. SetCurrentGoods();
  94. }
  95. }
  96. }
  97. private CloseOrderBase _closeOrderBase;
  98. public CloseOrderBase CloseOrderBase
  99. {
  100. get { return _closeOrderBase; }
  101. set
  102. {
  103. Set(() => CloseOrderBase, ref _closeOrderBase, value);
  104. }
  105. }
  106. private string _title = Muchinfo_Resource.ClosePage_Title;
  107. /// <summary>
  108. /// 平仓界面标题
  109. /// </summary>
  110. public string Title
  111. {
  112. get { return _title; }
  113. set { Set(() => Title, ref _title, value); }
  114. }
  115. public uint AccoutId
  116. {
  117. get
  118. {
  119. return UserManager.CurrentTradeAccount.AccountId;
  120. }
  121. }
  122. private IGoodsService _goodsService;
  123. /// <summary>
  124. /// Initializes a new instance of the <see cref="OpenFrameViewModel" /> class.
  125. /// </summary>
  126. /// <param name="quoteGoods">The quote goods.</param>
  127. /// <param name="orderType">Type of the order.</param>
  128. public CloseFrameViewModel(HoldingOrder holdOrder, OrderType orderType = OrderType.MarketCloseOrder)
  129. {
  130. var tempGoods=CacheManager.CacheGoodsBaseInfos.FirstOrDefault(
  131. (goods) => holdOrder.GoodsId==goods.GoodsId);
  132. CloseGoodses.Add(tempGoods);
  133. CurrentGoods = CloseGoodses.FirstOrDefault();
  134. //#if DEBUG
  135. // CloseOrderBase = new BidCloseViewModel(holdOrder, CurrentGoods, OrderType.BidMarketClose);
  136. //#else
  137. if (CurrentGoods != null && CurrentGoods.GoodsParameters.TradeMode == eTradeMode.TRADEMODE_BIDDING)
  138. {
  139. CloseOrderBase = new BidCloseViewModel(holdOrder, CurrentGoods, orderType);
  140. }
  141. else
  142. {
  143. CloseOrderBase = new MakeCloseViewModel(holdOrder, CurrentGoods, orderType);
  144. }
  145. Title = string.Format(Muchinfo_Resource.ClosePage_Tilte_OrderNum, holdOrder.OrderID);
  146. //#endif
  147. }
  148. public CloseFrameViewModel(HoldingSummary holdingSummary, OrderType orderType = OrderType.MarketCloseOrder)
  149. {
  150. CloseGoodses.Add(CacheManager.CacheGoodsBaseInfos.FirstOrDefault((goods) => holdingSummary.GoodsId==goods.GoodsId));
  151. CurrentGoods = CloseGoodses.FirstOrDefault();
  152. //#if DEBUG
  153. // CloseOrderBase = new BidCloseViewModel(holdingSummary, CurrentGoods, OrderType.BidMarketClose);
  154. //#else
  155. if (CurrentGoods.GoodsParameters.TradeMode == eTradeMode.TRADEMODE_BIDDING)
  156. {
  157. CloseOrderBase = new BidCloseViewModel(holdingSummary, CurrentGoods, orderType);
  158. }
  159. else
  160. {
  161. CloseOrderBase = new MakeCloseViewModel(holdingSummary, CurrentGoods);
  162. }
  163. //#endif
  164. }
  165. public CloseFrameViewModel(QuoteGoods selectQuoteGoods)
  166. {
  167. IsGoodsSelect = true;
  168. _goodsService = SimpleIoc.Default.GetInstance<IGoodsService>();
  169. var holdingSummaries = UserManager.GetCacheOrders<HoldingSummary>();
  170. var quoteGoodses = holdingSummaries.Select((holding) => holding.GoodsId).Distinct();
  171. CloseGoodses = new List<QuoteGoods>();
  172. var goodsDic = CacheManager.CacheGoodsBaseInfos.ToDictionary((goods) => goods.GoodsId);
  173. foreach (var code in quoteGoodses)
  174. {
  175. if (goodsDic.ContainsKey(code))
  176. {
  177. CloseGoodses.Add(goodsDic[code]);
  178. }
  179. }
  180. if (selectQuoteGoods != null)
  181. {
  182. var fristGoods = CloseGoodses.FirstOrDefault((goods) => goods != null &&
  183. goods.GoodsId == selectQuoteGoods.GoodsId);
  184. CurrentGoods = fristGoods ?? CloseGoodses.FirstOrDefault();
  185. }
  186. else
  187. {
  188. CurrentGoods = CloseGoodses.FirstOrDefault();
  189. }
  190. }
  191. private void SetCurrentGoods()
  192. {
  193. //todo:提示用户无持仓
  194. if (!IsGoodsSelect || CurrentGoods==null)
  195. {
  196. return;
  197. }
  198. var holdingSummaries = UserManager.GetCacheOrders<HoldingSummary>().Where((item) => item.GoodsId == CurrentGoods.GoodsId).ToList();
  199. var direction= holdingSummaries.Count >= 2; ////是否可选择方向
  200. if (CurrentGoods!=null&&CurrentGoods.GoodsParameters.TradeMode == eTradeMode.TRADEMODE_BIDDING)
  201. {
  202. CloseOrderBase = new BidCloseViewModel(CurrentGoods, holdingSummaries, direction);
  203. }
  204. else
  205. {
  206. CloseOrderBase = new MakeCloseViewModel(CurrentGoods, holdingSummaries, direction);
  207. }
  208. }
  209. /// <summary>
  210. /// 快速下单窗口取消
  211. /// </summary>
  212. public RelayCommand<Window> CancelCommand
  213. {
  214. get
  215. {
  216. return new RelayCommand<Window>((dialog) =>
  217. {
  218. dialog.DialogResult = false;
  219. });
  220. }
  221. }
  222. private bool _oKButtonEnabled = true;
  223. /// <summary>
  224. ///设置按键不可用
  225. /// </summary>
  226. public bool OKButtonEnabled
  227. {
  228. get
  229. {
  230. return _oKButtonEnabled;
  231. }
  232. set
  233. {
  234. Set(() => OKButtonEnabled, ref _oKButtonEnabled, value);
  235. }
  236. }
  237. private Window _openWindow;
  238. /// <summary>
  239. /// 下单确定
  240. /// </summary>
  241. public RelayCommand<Window> OKCommand
  242. {
  243. get
  244. {
  245. return new RelayCommand<Window>((dialog) =>
  246. {
  247. OKButtonEnabled = false;
  248. string errorMsg = string.Empty;
  249. _openWindow = dialog;
  250. bool validateBool = CloseOrderBase.Validated(); ////内容验证
  251. if (validateBool)
  252. {
  253. IsBusy = true;
  254. BusyTips = Muchinfo_Resource.Busy_Summit_Wait;
  255. CloseOrderBase.PostOrder(EntrurstSuccessCallBack, EntrurstErrorCallBack); ////获取表单内容
  256. }
  257. else
  258. {
  259. MessageBoxHelper.ShowInfo(errorMsg, Muchinfo_Resource.MessageBox_Error_Title);
  260. OKButtonEnabled = true;
  261. }
  262. });
  263. }
  264. }
  265. /// <summary>
  266. /// 提交成功返回
  267. /// </summary>
  268. /// <param name="order"></param>
  269. private void EntrurstSuccessCallBack(OrderDetail order)
  270. {
  271. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  272. {
  273. MessageBoxHelper.ShowSuccess(Muchinfo_Resource.Order_Success_Result,
  274. Muchinfo_Resource.Open_MessageBox_Title);
  275. IsBusy = false;
  276. if (_openWindow != null)
  277. {
  278. _openWindow.Close();
  279. this.Cleanup();
  280. }
  281. }));
  282. Messenger.Default.Send(UserManager.CurrentTradeAccount, MessengerTokens.OrderNotify);
  283. }
  284. /// <summary>
  285. /// 委托失败返回
  286. /// </summary>
  287. /// <param name="errorEntity"></param>
  288. private void EntrurstErrorCallBack(ErrorEntity errorEntity)
  289. {
  290. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  291. {
  292. ErrorManager.ShowReturnError(errorEntity, Muchinfo_Resource.UI2014_Tips, true);
  293. if (_openWindow != null)
  294. {
  295. _openWindow.Close();
  296. this.Cleanup();
  297. }
  298. IsBusy = false;
  299. }));
  300. }
  301. }
  302. }