CompleteOrderViewModel.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System.Windows;
  2. using GalaSoft.MvvmLight.Command;
  3. using GalaSoft.MvvmLight.Messaging;
  4. using Muchinfo.MTPClient.Data;
  5. using Muchinfo.MTPClient.Data.Enums;
  6. using Muchinfo.MTPClient.Data.Model.Account;
  7. using Muchinfo.MTPClient.Infrastructure.Utilities;
  8. using Muchinfo.WPF.Controls.Windows;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. namespace Muchinfo.MTPClient.Account.ViewModels
  13. {
  14. public class CompleteOrderViewModel : RegisterMessageBase
  15. {
  16. private const int historyMaxDays = 90;
  17. private bool _radioNow = true;
  18. /// <summary>
  19. /// 当前查询
  20. /// </summary>
  21. public bool RadioNow
  22. {
  23. get { return _radioNow; }
  24. set
  25. {
  26. Set(() => RadioNow, ref _radioNow, value);
  27. if (_radioNow)
  28. {
  29. _radioCustom = false;
  30. QueryOrders();
  31. }
  32. }
  33. }
  34. private bool _radioCustom;
  35. /// <summary>
  36. /// 自定义是否选中
  37. /// </summary>
  38. public bool RadioCustom
  39. {
  40. get { return _radioCustom; }
  41. set
  42. {
  43. Set(() => RadioCustom, ref _radioCustom, value);
  44. EndDateTime = DateTime.Now.AddDays(-1); //查询历史为当前的前一天
  45. StartDateTime = EndDateTime.AddDays(1 - EndDateTime.Day); //结束当月的第一天
  46. if (_radioCustom)
  47. {
  48. _radioNow = false;
  49. QueryOrders();
  50. }
  51. }
  52. }
  53. /// 查询开始时间
  54. private DateTime _startDateTime;
  55. /// <summary>
  56. /// 查询开始时间
  57. /// </summary>
  58. public DateTime StartDateTime
  59. {
  60. get { return _startDateTime; }
  61. set
  62. {
  63. Set(() => StartDateTime, ref _startDateTime, new DateTime(value.Year, value.Month, value.Day));
  64. }
  65. }
  66. /// 查询结束时间
  67. private DateTime _endDateTime;
  68. /// <summary>
  69. /// 查询结束时间
  70. /// </summary>
  71. public DateTime EndDateTime
  72. {
  73. get { return _endDateTime; }
  74. set
  75. {
  76. Set(() => EndDateTime, ref _endDateTime, new DateTime(value.Year, value.Month, value.Day));
  77. }
  78. }
  79. private ObservableCollection<CompleteOrder> _completeOrders = new ObservableCollection<CompleteOrder>();
  80. /// <summary>
  81. /// 成交单
  82. /// </summary>
  83. public ObservableCollection<CompleteOrder> CompleteOrders
  84. {
  85. get { return _completeOrders; }
  86. set { Set(() => CompleteOrders, ref _completeOrders, value); }
  87. }
  88. /// <summary>
  89. /// 刷新
  90. /// </summary>
  91. public RelayCommand RefreshCommand
  92. {
  93. get
  94. {
  95. return new RelayCommand(() =>
  96. {
  97. QueryOrders();
  98. });
  99. }
  100. }
  101. /// <summary>
  102. /// 刷新账户信息
  103. /// </summary>
  104. public override void RefreshContent()
  105. {
  106. RefreshCommand.Execute(null);
  107. }
  108. public CompleteOrderViewModel()
  109. {
  110. }
  111. /// <summary>
  112. /// 单据注册消息
  113. /// </summary>
  114. public override void RegisterMessage()
  115. {
  116. //当选择时刷新
  117. var cacheData = UserManager.GetTradeCacheData(UserManager.CurrentTradeAccount.AccountId);
  118. if (!cacheData.CompleteOrderFlag)
  119. {
  120. RefreshCommand.Execute(null);
  121. }
  122. else
  123. {
  124. UpdateOrders();
  125. }
  126. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  127. {
  128. Messenger.Default.Unregister<TradeAccount>(this, MessengerTokens.OrderNotify);
  129. Messenger.Default.Register<TradeAccount>(this, MessengerTokens.OrderNotify, (e) =>
  130. {
  131. if (RadioNow)
  132. {
  133. UpdateOrders();
  134. }
  135. });
  136. }));
  137. }
  138. /// <summary>
  139. /// 更新当前单据
  140. /// </summary>
  141. private void UpdateOrders()
  142. {
  143. CompleteOrders = UserManager.GetCacheOrders<CompleteOrder>();
  144. }
  145. private void QueryOrders()
  146. {
  147. if (validate())
  148. {
  149. IsBusy = true;
  150. var queryCommons = new List<QueryCommonParam>();
  151. if (RadioNow) ////查询当前
  152. {
  153. queryCommons.Add(new QueryCommonParam() { ParamKey = "accountId", ParamValue = UserManager.CurrentTradeAccount.AccountId + string.Empty });
  154. }
  155. else ////查询历史
  156. {
  157. //todo:查询历史参数
  158. queryCommons.Add(new QueryCommonParam() { ParamKey = "accountId", ParamValue = UserManager.CurrentTradeAccount.AccountId + string.Empty });
  159. }
  160. _orderService.QueryComplateOrders(QueryStatement.QueryComplateOrder, queryCommons, QueryComplateOrderSuccessCallback, QueryErrorCallback);
  161. }
  162. }
  163. public void QueryComplateOrderSuccessCallback(List<CompleteOrder> completeOrders)
  164. {
  165. IsBusy = false;
  166. UpdateOrders();
  167. }
  168. /// <summary>
  169. /// 验证输入有效性
  170. /// </summary>
  171. /// <returns></returns>
  172. private bool validate()
  173. {
  174. if (RadioCustom)
  175. {
  176. if (StartDateTime.AddHours(-StartDateTime.Hour) > EndDateTime)
  177. {
  178. MessageBoxHelper.ShowInfo(
  179. Muchinfo.MTPClient.Resources.Muchinfo_Resource.Models_EndDateLessBegin,
  180. Muchinfo.MTPClient.Resources.Muchinfo_Resource.APP_Tips);
  181. return false;
  182. }
  183. else if ((EndDateTime - StartDateTime).TotalDays > historyMaxDays)
  184. {
  185. MessageBoxHelper.ShowInfo(
  186. string.Format(Muchinfo.MTPClient.Resources.Muchinfo_Resource.QueryDaysMustblow, historyMaxDays),
  187. Muchinfo.MTPClient.Resources.Muchinfo_Resource.APP_Tips);
  188. return false;
  189. }
  190. }
  191. return true;
  192. }
  193. public override void UnRegisterMessage()
  194. {
  195. Messenger.Default.Unregister<TradeAccount>(this, MessengerTokens.OrderNotify);
  196. base.UnRegisterMessage();
  197. }
  198. }
  199. }