DepositApplyViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. //----------------------------------------------------------------
  6. //Module Name: $safeprojectname$
  7. //Purpose:
  8. //CopyRight: Muchinfo
  9. //History:
  10. //----------------------------------------------------------------
  11. //DateTime 2017/1/12 16:24:30
  12. //Author
  13. //Description Create
  14. //----------------------------------------------------------------
  15. using System.Windows;
  16. using GalaSoft.MvvmLight;
  17. using GalaSoft.MvvmLight.Command;
  18. using GalaSoft.MvvmLight.Ioc;
  19. using Muchinfo.MTPClient.Data;
  20. using Muchinfo.MTPClient.Data.Model;
  21. using Muchinfo.MTPClient.Infrastructure.MessageBox;
  22. using Muchinfo.MTPClient.Infrastructure.Utilities;
  23. using Muchinfo.MTPClient.IService;
  24. using Muchinfo.MTPClient.Resources;
  25. using Muchinfo.MTPClient.Infrastructure.Helpers;
  26. using Muchinfo.MTPClient.Data.Enums;
  27. namespace Muchinfo.MTPClient.Sale.ViewModels
  28. {
  29. public class DepositApplyViewModel:ViewModelBase
  30. {
  31. private IDepositService _iDepositService;
  32. private DepositPlan _curDepositPlan;
  33. private Window _openWindow;
  34. /// <summary>
  35. /// 当前托管计划
  36. /// </summary>
  37. public DepositPlan CurDepositPlan
  38. {
  39. get { return _curDepositPlan; }
  40. set
  41. {
  42. if (value != null && _curDepositPlan != value)
  43. {
  44. _iDepositService.QueryDepositWareHouse(value.ExAreaId,DepositWareHouseSuccess, ErrorQueryWareHouse); ///查询仓库
  45. MaxLot = value.MaxDepositNumber;
  46. }
  47. Set(() => CurDepositPlan, ref _curDepositPlan, value);
  48. Lot = 0;
  49. }
  50. }
  51. private List<DepositPlan> _depositPlans;
  52. /// <summary>
  53. /// 托管计划列表
  54. /// </summary>
  55. public List<DepositPlan> DepositPlans
  56. {
  57. get { return _depositPlans; }
  58. set { Set(() => DepositPlans, ref _depositPlans, value); }
  59. }
  60. private bool _isBusy;
  61. /// <summary>
  62. /// 是否正在加载
  63. /// </summary>
  64. public bool IsBusy
  65. {
  66. get { return _isBusy; }
  67. set { Set(() => IsBusy, ref _isBusy, value); }
  68. }
  69. public DepositApplyViewModel(DepositPlan deposit,List<DepositPlan>list)
  70. {
  71. _iDepositService = SimpleIoc.Default.GetInstance<IDepositService>();
  72. CurDepositPlan = deposit;
  73. DepositPlans = list.Where((item)=>item.IsDepositApply).ToList();
  74. IsBusy = true;
  75. }
  76. private decimal _lot;
  77. /// <summary>
  78. /// 数量
  79. /// </summary>
  80. public decimal Lot
  81. {
  82. get { return _lot; }
  83. set
  84. {
  85. Set(() => Lot, ref _lot, value);
  86. RaisePropertyChanged(() => FeeTips);
  87. RaisePropertyChanged(() => ErrorTips);
  88. RaisePropertyChanged(() => FreezeFee);
  89. }
  90. }
  91. public string ErrorTips
  92. {
  93. get
  94. {
  95. var error = string.Empty;
  96. if (Lot == 0)
  97. {
  98. return Client_Resource.Content_DepositApplyErrorTips;
  99. }
  100. if (MaxLot < Lot)
  101. {
  102. return Muchinfo.MTPClient.Resources.Client_Resource.Resources_DepositOrder_MaxLotError;
  103. }
  104. var usefunds = UserManager.CurrentAccountUsableFunds();
  105. if (usefunds < FreezeFee) ////资金不足
  106. {
  107. return Client_Resource.Content_DepositApplyMonenyErrorTips;
  108. }
  109. return error;
  110. }
  111. }
  112. private List<DepositWareHouse> _storelist;
  113. /// <summary>
  114. /// 仓库列表
  115. /// </summary>
  116. public List<DepositWareHouse> Storelist
  117. {
  118. get { return _storelist; }
  119. set { Set(() => Storelist, ref _storelist, value); }
  120. }
  121. private DepositWareHouse _curWareHouse;
  122. /// <summary>
  123. /// 当前的选择仓库
  124. /// </summary>
  125. public DepositWareHouse CurWareHouse
  126. {
  127. get { return _curWareHouse; }
  128. set { Set(() => CurWareHouse, ref _curWareHouse, value); }
  129. }
  130. private decimal _maxLot=long.MaxValue;
  131. /// <summary>
  132. /// 最大托管数量
  133. /// </summary>
  134. public decimal MaxLot
  135. {
  136. get { return _maxLot; }
  137. set { Set(() => MaxLot, ref _maxLot, value); }
  138. }
  139. public string FeeTips
  140. {
  141. get
  142. {
  143. if (CurDepositPlan != null)
  144. {
  145. var tip = string.Format(Client_Resource.AResources_DepositApplyFee, FreezeFee,
  146. CurDepositPlan.DepositReferPrice, Lot, CurDepositPlan.ListingRate);
  147. return tip;
  148. }
  149. return string.Empty;
  150. }
  151. }
  152. public decimal FreezeFee
  153. {
  154. get
  155. {
  156. if (CurDepositPlan != null)
  157. {
  158. return Math.Round(CurDepositPlan.DepositReferPrice*Lot*CurDepositPlan.ListingRate, 2,
  159. MidpointRounding.AwayFromZero);
  160. }
  161. return 0m ;
  162. }
  163. }
  164. /// <summary>
  165. /// 快速下单窗口取消
  166. /// </summary>
  167. public RelayCommand<Window> CancelCommand
  168. {
  169. get
  170. {
  171. return new RelayCommand<Window>((dialog) =>
  172. {
  173. dialog.DialogResult = false;
  174. });
  175. }
  176. }
  177. /// <summary>
  178. /// 下单确定
  179. /// </summary>
  180. public RelayCommand<Window> OKCommand
  181. {
  182. get
  183. {
  184. return new RelayCommand<Window>((dialog) =>
  185. {
  186. _openWindow = dialog;
  187. bool validateBool = Vailed();
  188. if (validateBool)
  189. {
  190. IsBusy = true;
  191. var accountId = UserManager.CurrentTradeAccount.AccountId;
  192. //if (UserManager.CurrentTradeAccount.FundsAccounts.Any() &&
  193. // UserManager.CurrentTradeAccount.FundsAccounts[0] != null)
  194. //{
  195. // accountId = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
  196. //}
  197. var depositOrder = new DepositOrder()
  198. {
  199. DepositNum = Lot,
  200. Accountid = accountId,
  201. BuildType = eBuildType.BUILDTYPE_OPEN,
  202. DepositId = CurDepositPlan.DepositId,
  203. WarehouseId = CurWareHouse.WareHouseId,
  204. };
  205. _iDepositService.DepositApply(depositOrder, DepositApplySuccess, ErrorDeosit);
  206. }
  207. }, IsOKEnable);
  208. }
  209. }
  210. private void DepositWareHouseSuccess(List<DepositWareHouse> list)
  211. {
  212. IsBusy = false;
  213. Storelist = list;
  214. if (Storelist != null)
  215. {
  216. CurWareHouse = null;
  217. CurWareHouse = Storelist.FirstOrDefault();
  218. }
  219. }
  220. private void ErrorQueryWareHouse(ErrorEntity errorEntity)
  221. {
  222. IsBusy = false;
  223. //todo:写错误日志
  224. }
  225. /// <summary>
  226. ///
  227. /// </summary>
  228. /// <param name="depositOrder"></param>
  229. private void DepositApplySuccess(DepositOrder depositOrder)
  230. {
  231. IsBusy = false;
  232. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  233. {
  234. MessageBoxHelper.ShowSuccess(Client_Resource.Content_DepositApplySuccess,
  235. Client_Resource.Open_MessageBox_Title);
  236. if (_openWindow != null)
  237. {
  238. _openWindow.Close();
  239. this.Cleanup();
  240. }
  241. }));
  242. //刷新正常单
  243. MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount, MessengerTokens.DepositOrderToken);
  244. }
  245. private void ErrorDeosit(ErrorEntity errorEntity)
  246. {
  247. IsBusy = false;
  248. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  249. {
  250. ErrorManager.ShowReturnError(errorEntity, Client_Resource.UI2014_Tips, true);
  251. if (_openWindow != null)
  252. {
  253. _openWindow.Close();
  254. this.Cleanup();
  255. }
  256. // IsBusy = false;
  257. }));
  258. }
  259. private bool IsOKEnable(Window item)
  260. {
  261. if (CurDepositPlan==null)
  262. {
  263. return false;
  264. }
  265. else if(CurWareHouse==null)
  266. {
  267. return false;
  268. }
  269. var usefunds = UserManager.CurrentAccountUsableFunds();
  270. if (usefunds < FreezeFee) ////资金不足
  271. {
  272. return false;
  273. }
  274. if (CurDepositPlan.MaxDepositNumber < Lot)
  275. {
  276. return false;
  277. }
  278. return Lot > 0;
  279. }
  280. private bool Vailed()
  281. {
  282. if (CurDepositPlan == null)
  283. {
  284. MessageBoxHelper.ShowInfo(Muchinfo.MTPClient.Resources.Client_Resource.Content_DepositUnSelect, Muchinfo.MTPClient.Resources.Client_Resource.Models_Tips, true);
  285. return false;
  286. }
  287. if (CurDepositPlan.MaxDepositNumber < Lot)
  288. {
  289. MessageBoxHelper.ShowInfo(Muchinfo.MTPClient.Resources.Client_Resource.Resources_DepositOrder_MaxLotError, Muchinfo.MTPClient.Resources.Client_Resource.Models_Tips, true);
  290. return false;
  291. }
  292. DateTime deTime = DateTime.Now;
  293. if (!DateTime.TryParse(CurDepositPlan.DepositStartTime, out deTime)||deTime.Date>ApplicationParameter.ServerTimeNow.Date)
  294. {
  295. MessageBoxHelper.ShowInfo(Muchinfo.MTPClient.Resources.Client_Resource.Content_DepositUnStarting, Muchinfo.MTPClient.Resources.Client_Resource.Models_Tips, true);
  296. return false;
  297. }
  298. else if (!DateTime.TryParse(CurDepositPlan.DepositEndTime, out deTime)||deTime.Date<ApplicationParameter.ServerTimeNow.Date)
  299. {
  300. MessageBoxHelper.ShowInfo(Muchinfo.MTPClient.Resources.Client_Resource.Content_DepositEnd, Muchinfo.MTPClient.Resources.Client_Resource.Models_Tips, true);
  301. return false;
  302. }
  303. return true;
  304. }
  305. }
  306. }