PrePostDetailViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using GalaSoft.MvvmLight.Command;
  2. using Muchinfo.MTPClient.Data;
  3. using Muchinfo.MTPClient.Data.Enums;
  4. using Muchinfo.MTPClient.Data.Model;
  5. using Muchinfo.MTPClient.Data.Model.Account;
  6. using Muchinfo.MTPClient.Infrastructure.Cache;
  7. using Muchinfo.MTPClient.Infrastructure.Helpers;
  8. using Muchinfo.MTPClient.Infrastructure.MessageBox;
  9. using Muchinfo.MTPClient.Infrastructure.Utilities;
  10. using Muchinfo.MTPClient.Resources;
  11. using Muchinfo.WPF.Controls.Windows;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Windows;
  16. namespace Muchinfo.MTPClient.Account.ViewModels
  17. {
  18. /// <summary>
  19. /// This class contains properties that a View can data bind to.
  20. /// <para>
  21. /// See http://www.galasoft.ch/mvvm
  22. /// </para>
  23. /// </summary>
  24. public class PrePostDetailViewModel : MarketOrdersBase<PrePostDetail>
  25. {
  26. #region "Private Data Members"
  27. //To declare private service data
  28. #endregion "Private Data Members"
  29. #region "Constructor/Initialization"
  30. /// <summary>
  31. /// Initializes a new instance of the PrePostDetailViewModel class.
  32. /// </summary>
  33. public PrePostDetailViewModel()
  34. {
  35. MessengerHelper.DefaultRegister<TradeTreeMenu>(this, MessengerTokens.CancalOrder, (e) =>
  36. {
  37. CancelHoldingCammand.Execute(null);
  38. });
  39. }
  40. #endregion "Constructor/Initialization"
  41. #region "Interface implementation Or override"
  42. public override string SearchOrderCountId
  43. {
  44. get { return QueryStatement.SearchAllPrePostOrderCount; }
  45. }
  46. /// <summary>
  47. /// 单据注册消息
  48. /// </summary>
  49. public override void RegisterMessage()
  50. {
  51. base.RegisterMessage();
  52. //当选择时刷新
  53. var cacheData = UserManager.GetTradeCacheData(UserManager.CurrentTradeAccount.AccountId);
  54. if (!cacheData.OpenPrePostOrderFlag)
  55. {
  56. RefreshCommand.Execute(null);
  57. }
  58. else
  59. {
  60. UpdateOrders();
  61. }
  62. MessengerHelper.DefaultUnregister<TradeAccount>(this, MessengerTokens.PreOrderNoticeToken);
  63. MessengerHelper.DefaultRegister<TradeAccount>(this, MessengerTokens.PreOrderNoticeToken, (e) =>
  64. {
  65. //下单成功后刷新预埋单界面
  66. RefreshCommand.Execute(null);
  67. });
  68. }
  69. public override void QueryMarketOrder(eTradeMode marketType)
  70. {
  71. if (marketType == eTradeMode.TRADEMODE_SALE)
  72. {
  73. return;
  74. }
  75. IsBusy = true;
  76. var queryCommonParams = new List<QueryCommonParam>();
  77. var accountid = UserManager.CurrentTradeAccount.AccountId;
  78. //if (UserManager.CurrentTradeAccount.FundsAccounts != null && UserManager.CurrentTradeAccount.FundsAccounts.Any())
  79. //{
  80. // accountid = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
  81. //}
  82. queryCommonParams.Add(new QueryCommonParam() { ParamKey = "accountId", ParamValue = accountid + string.Empty });
  83. _orderService.QueryPrePostOrders(marketType, queryCommonParams, MarketOrderSuccess, QueryErrorCallback);
  84. }
  85. protected override void MarketOrderSuccess(eTradeMode marketType, List<PrePostDetail> orders)
  86. {
  87. IsBusy = false;
  88. UpdateOrders();
  89. base.MarketOrderSuccess(marketType, orders);
  90. }
  91. public override void UnRegisterMessage()
  92. {
  93. MessengerHelper.DefaultUnregister<TradeAccount>(this, MessengerTokens.OrderNotify);
  94. base.UnRegisterMessage();
  95. }
  96. /// <summary>
  97. /// 创建预埋单对象
  98. /// </summary>
  99. /// <returns></returns>
  100. public virtual NewEntrustOrder BuildEntrustOrder()
  101. {
  102. var entrustOrder = new NewEntrustOrder()
  103. {
  104. OperaterId = UserManager.CurrentTradeAccount.AccountId,
  105. TriggerType = (uint)CurrentLimitOrder.TriggerType,
  106. TriggerPrice = CurrentLimitOrder.TriggerPrice,
  107. GoodsCode = CurrentLimitOrder.goodscode,
  108. GoodsId = (uint)CurrentLimitOrder.GoodsId,
  109. BuyOrSell = CurrentLimitOrder.BuyOrSell,
  110. OrderFlag = (uint)OrderMethods.PreOrder,
  111. EntrustPrice = CurrentLimitOrder.orderprice,
  112. EntrustQuantity = CurrentLimitOrder.orderqty,
  113. BuildType = OpenCloseMode.BUILDTYPE_OPEN,
  114. EntrurstTime = ApplicationParameter.ServerTimeNow,
  115. //RelationTicket = CurrentLimitOrder.PreOrderId,
  116. };
  117. //if (UserManager.CurrentTradeAccount.FundsAccounts.Any() &&
  118. // UserManager.CurrentTradeAccount.FundsAccounts[0] != null)
  119. //{
  120. // entrustOrder.AccountId = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
  121. //}
  122. //else
  123. //{
  124. // entrustOrder.AccountId = UserManager.CurrentTradeAccount.AccountId;
  125. //}
  126. return entrustOrder;
  127. }
  128. #endregion "Interface implementation Or override"
  129. #region "Public Properties To Get/Set "
  130. #region 当前选择的预埋单单
  131. private PrePostDetail _currentLimitOrder;
  132. /// <summary>
  133. /// 当前选择的预埋单单
  134. /// </summary>
  135. public PrePostDetail CurrentLimitOrder
  136. {
  137. get { return _currentLimitOrder; }
  138. set
  139. {
  140. Set(() => CurrentLimitOrder, ref _currentLimitOrder, value);
  141. }
  142. }
  143. #endregion
  144. #endregion "Public Properties To Get/Set "
  145. #region "Public Commands"
  146. private RelayCommand<PrePostDetail> _dataGridMouseEnter;
  147. /// <summary>
  148. /// 多个DataGird中改变当前选中的CurrentHoldingSummaryItem
  149. /// </summary>
  150. public RelayCommand<PrePostDetail> DataGridMouseEnter
  151. {
  152. get
  153. {
  154. return _dataGridMouseEnter
  155. ?? (_dataGridMouseEnter = new RelayCommand<PrePostDetail>(
  156. p =>
  157. {
  158. if (p != null)
  159. {
  160. CurrentLimitOrder = p;
  161. }
  162. }));
  163. }
  164. }
  165. /// <summary>
  166. /// 撤销预埋单
  167. /// </summary>
  168. public RelayCommand CancelHoldingCammand
  169. {
  170. get
  171. {
  172. return new RelayCommand(() =>
  173. {
  174. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  175. {
  176. if (CurrentLimitOrder == null) return;
  177. long orderNumber = CurrentLimitOrder.PreOrderId;
  178. var boxResult = MessageBoxHelper.ShowQuestion(Muchinfo.MTPClient.Resources.Client_Resource.Models_MakesureCancel + orderNumber + Muchinfo.MTPClient.Resources.Client_Resource.Models_Bill,
  179. Muchinfo.MTPClient.Resources.Client_Resource.Models_CancelBillTips);
  180. if (boxResult == MessageBoxResult.Yes)
  181. {
  182. IsBusy = true;
  183. PostCancelOrder();
  184. }
  185. }));
  186. }, () => CanCancelOrderDetial());
  187. }
  188. }
  189. #endregion "Public Commands"
  190. #region "Private Methods"
  191. /// <summary>
  192. /// 更新当前单据
  193. /// </summary>
  194. private void UpdateOrders()
  195. {
  196. OrderSource = UserManager.GetMarketCommonOrder<PrePostDetail>(UserManager.CurrentTradeAccount);
  197. if (OrderSource != null && OrderSource.Any())
  198. {
  199. DicOrderCount[CurrentTradeMode] = OrderSource[CurrentTradeMode].Count;
  200. }
  201. else
  202. {
  203. var dicCountZero = new Dictionary<eTradeMode, int>();
  204. foreach (var marketType in MarketTypes)
  205. {
  206. dicCountZero[marketType] = 0;
  207. }
  208. DicOrderCount = dicCountZero;
  209. }
  210. //if (OrderSource != null && OrderSource.Any())
  211. //{
  212. // var dicCount = new Dictionary<eTradeMode, int>();
  213. // foreach (var marketType in MarketTypes)
  214. // {
  215. // if (OrderSource.ContainsKey(marketType))
  216. // {
  217. // dicCount[marketType] = OrderSource[marketType].Count;
  218. // }
  219. // else
  220. // {
  221. // dicCount[marketType] = 0;
  222. // }
  223. // }
  224. // DicOrderCount = dicCount;
  225. //}
  226. //else
  227. //{
  228. // var dicCountZero = new Dictionary<eTradeMode, int>();
  229. // foreach (var marketType in MarketTypes)
  230. // {
  231. // dicCountZero[marketType] = 0;
  232. // }
  233. // DicOrderCount = dicCountZero;
  234. //}
  235. }
  236. /// <summary>
  237. /// 提交成功返回
  238. /// </summary>
  239. /// <param name="order"></param>
  240. private void EntrurstSuccessCallBack(OrderDetail order)
  241. {
  242. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  243. {
  244. MessageBoxHelper.ShowSuccess(Muchinfo.MTPClient.Resources.Client_Resource.Account_CancelSuccess,
  245. Client_Resource.Cancel_MessageBox_Title);
  246. IsBusy = false;
  247. }));
  248. //刷新预埋单
  249. MessengerHelper.DefaultSend(false, MessengerTokens.PreOrderUpdate);
  250. }
  251. /// <summary>
  252. /// 委托失败返回
  253. /// </summary>
  254. /// <param name="errorEntity"></param>
  255. private void EntrurstErrorCallBack(ErrorEntity errorEntity)
  256. {
  257. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  258. {
  259. ErrorManager.ShowReturnError(errorEntity, Client_Resource.UI2014_Tips, true);
  260. IsBusy = false;
  261. }));
  262. }
  263. #endregion "Private Methods"
  264. #region "Other Methods like Validated"
  265. //public void QueryComplateOrderSuccessCallback(List<PrePostDetail> prePostOrders)
  266. //{
  267. // IsBusy = false;
  268. // UpdateOrders();
  269. //}
  270. /// <summary>
  271. /// 是否可撤单
  272. /// </summary>
  273. /// <returns></returns>
  274. public bool CanCancelOrderDetial()
  275. {
  276. if (_currentLimitOrder == null)
  277. {
  278. return false;
  279. }
  280. return (_currentLimitOrder.OrderStatus == PrepostStatus.PREPOST_STATUS_MARKET );
  281. }
  282. /// <summary>
  283. /// 发送撤单命令
  284. /// </summary>
  285. /// <param name="limitOrder">撤消的单据</param>
  286. /// <returns>错误代码</returns>
  287. public void PostCancelOrder()
  288. {
  289. var order = BuildEntrustOrder();
  290. var currentGoods = CacheManager.CacheGoodsBaseInfos.FirstOrDefault(
  291. (goods) => goods.GoodsParameters.GoodsId == CurrentLimitOrder.GoodsId);
  292. if (currentGoods == null) { return; }
  293. switch (currentGoods.TradeMode)
  294. {
  295. //case eTradeMode.TRADEMODE_BIDDINGMARKETMAKE:
  296. //_orderService.MixMarketEntrustOrder(order, EntrurstSuccessCallBack, EntrurstErrorCallBack);
  297. //break;
  298. case eTradeMode.TRADEMODE_BIDDING:
  299. //_orderService.BidMarketEntrustOrder(order, EntrurstSuccessCallBack, EntrurstErrorCallBack);
  300. //break;
  301. case eTradeMode.TRADEMODE_MARKETMAKE:
  302. _orderService.MakeMarketEntrustOrder(order, EntrurstSuccessCallBack, EntrurstErrorCallBack);
  303. break;
  304. //case eTradeMode.TRADEMODE_SALE:
  305. // var isaleService = SimpleIoc.Default.GetInstance<ISaleService>();
  306. // isaleService.SaleEntrurstOrder(order, EntrurstSuccessCallBack, EntrurstErrorCallBack);
  307. // break;
  308. default:
  309. break;
  310. }
  311. }
  312. #endregion "Other Methods like Validated"
  313. }
  314. }