QuerySaleGoodsViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using GalaSoft.MvvmLight;
  2. using GalaSoft.MvvmLight.Command;
  3. using GalaSoft.MvvmLight.Ioc;
  4. using Microsoft.Practices.ServiceLocation;
  5. using Muchinfo.MTPClient.Data;
  6. using Muchinfo.MTPClient.Data.DTO;
  7. using Muchinfo.MTPClient.Data.Enums;
  8. using Muchinfo.MTPClient.Data.Helper;
  9. using Muchinfo.MTPClient.Data.Model;
  10. using Muchinfo.MTPClient.Data.Model.Sale;
  11. using Muchinfo.MTPClient.Infrastructure.Cache;
  12. using Muchinfo.MTPClient.Infrastructure.Helpers;
  13. using Muchinfo.MTPClient.Infrastructure.Utilities;
  14. using Muchinfo.MTPClient.IService;
  15. using Muchinfo.MTPClient.Resources;
  16. using Muchinfo.MTPClient.Sale.Views;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Collections.ObjectModel;
  20. using System.Linq;
  21. using System.Windows;
  22. namespace Muchinfo.MTPClient.Sale.ViewModels
  23. {
  24. public class QuerySaleGoodsViewModel : ViewModelBase
  25. {
  26. private ISaleService _saleService;
  27. private long _goodsGroupId;
  28. //申购中的商品
  29. private ObservableCollection<SaleGoods> purchaseGoodsList;
  30. //战略投资者商品
  31. private List<StrategicInvestor> strategicInvestors;
  32. #region 构造函数
  33. public QuerySaleGoodsViewModel()
  34. {
  35. MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.RefrashSaleGoodsCommand, RegisterMethod);
  36. }
  37. #endregion
  38. #region 成员变量
  39. private SaleGoods _currentDataGridItem;
  40. /// <summary>
  41. /// 当前选中DataGridItem
  42. /// </summary>
  43. public SaleGoods CurrentDataGridItem
  44. {
  45. get { return _currentDataGridItem; }
  46. set
  47. {
  48. Set(() => CurrentDataGridItem, ref _currentDataGridItem, value);
  49. SendCurrentGoods(_currentDataGridItem);
  50. }
  51. }
  52. /// <summary>
  53. /// 发行商品
  54. /// </summary>
  55. private List<SaleGoods> _saleGoodsList;
  56. public List<SaleGoods> SaleGoodsList
  57. {
  58. get { return _saleGoodsList; }
  59. set { Set(() => SaleGoodsList, ref _saleGoodsList, value); }
  60. }
  61. private List<QuoteGoods> _quoteGoodsList;
  62. public List<QuoteGoods> QuoteGoodsList
  63. {
  64. get { return _quoteGoodsList; }
  65. set { Set(() => QuoteGoodsList, ref _quoteGoodsList, value); }
  66. }
  67. private string _goodCode = string.Empty;
  68. /// <summary>
  69. /// 商品代码
  70. /// </summary>
  71. public string GoodCode
  72. {
  73. get
  74. {
  75. return _goodCode.Trim();
  76. }
  77. set
  78. {
  79. Set(() => GoodCode, ref _goodCode, value);
  80. }
  81. }
  82. #endregion
  83. #region 私有方法
  84. /// <summary>
  85. /// 查询
  86. /// </summary>
  87. public RelayCommand SelectCommand
  88. {
  89. get
  90. {
  91. return new RelayCommand(() =>
  92. {
  93. QuerySaleGoods();
  94. });
  95. }
  96. }
  97. /// <summary>
  98. /// 重置
  99. /// </summary>
  100. public RelayCommand ResetCommand
  101. {
  102. get
  103. {
  104. return new RelayCommand(() =>
  105. {
  106. GoodCode = string.Empty;
  107. });
  108. }
  109. }
  110. /// <summary>
  111. /// 申购
  112. /// </summary>
  113. public RelayCommand DetailCommand
  114. {
  115. get
  116. {
  117. return new RelayCommand(() =>
  118. {
  119. if (CurrentDataGridItem == null)
  120. {
  121. //todo:提示无选择商品
  122. return;
  123. }
  124. var win = new SaleOrderFrame(CurrentDataGridItem)
  125. {
  126. Owner = Application.Current.MainWindow,
  127. WindowStartupLocation = WindowStartupLocation.CenterOwner
  128. };
  129. win.ShowDialog();
  130. });
  131. }
  132. }
  133. public void QuerySaleGoods()
  134. {
  135. QuoteGoodsList =
  136. CacheManager.CacheGoodsBaseInfos.Where((item) => item.GoodsParameters.SortId == _goodsGroupId)
  137. .ToList();
  138. if (UserManager.IsAccountLogin)
  139. {
  140. if (QuoteGoodsList == null || !QuoteGoodsList.Any())
  141. {
  142. SaleGoodsList = null;
  143. return;
  144. }
  145. var queryParamers = new List<QueryCommonParam>() {};
  146. var goodsIdString = string.Empty;
  147. foreach (var quoteGoods in QuoteGoodsList)
  148. {
  149. if ((quoteGoods.ContainsGoodsSrc & (int) GoodsFromScr.Trade) > 0) ///只查询交易的商品
  150. {
  151. goodsIdString += quoteGoods.GoodsParameters.GoodsId + ",";
  152. }
  153. }
  154. goodsIdString = goodsIdString.TrimEnd(',');
  155. if (!string.IsNullOrEmpty(goodsIdString))
  156. {
  157. queryParamers.Add(new QueryCommonParam() {ParamKey = "goodsIds", ParamValue = goodsIdString});
  158. _saleService.QueryAccountSaleGoodsRpt(QueryStatement.SearchSaleGoods, queryParamers, QuerySuccess,
  159. QueryErrorFunc);
  160. }
  161. }
  162. else ////游客登录
  163. {
  164. if (UserManager.BrownSaleGoodses == null)
  165. {
  166. var quoteDataServie = SimpleIoc.Default.GetInstance<IQuoteDataService>();
  167. quoteDataServie.QuerySaleGoodsParam(QuoteQuerySaleParamSuccess, QueryErrorFunc);
  168. }
  169. else
  170. {
  171. SetCurrentSaleGoods();
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// 设置发售商品显示内容
  177. /// </summary>
  178. private void SetCurrentSaleGoods()
  179. {
  180. if (QuoteGoodsList == null || !QuoteGoodsList.Any()||UserManager.BrownSaleGoodses==null || !UserManager.BrownSaleGoodses.Any())
  181. {
  182. SaleGoodsList = null;
  183. return;
  184. }
  185. int index = 1;
  186. var goodsList = new List<SaleGoods>();
  187. foreach (var quoteGoodse in QuoteGoodsList)
  188. {
  189. var goods= UserManager.BrownSaleGoodses.FirstOrDefault((item) => item.GoodsId == quoteGoodse.GoodsId);
  190. if (goods != null)
  191. {
  192. goods.OrderNumber = index;
  193. goods.GoodsName = quoteGoodse.Name;
  194. goods.GoodsCode = quoteGoodse.GoodsCode;
  195. goods.AgreeUnit = quoteGoodse.GoodsParameters.AgreeUnit;
  196. goods.GoodsId = quoteGoodse.GoodsId;
  197. goods.RunStepsDisplay = goods.RunSteps.Discription();
  198. goods.GoodsCodeColor = quoteGoodse.GoodsCodeColor;
  199. goods.GoodsNameColor = quoteGoodse.NameColor;
  200. goods.DefaultColor = quoteGoodse.DefaultColor;
  201. goods.IsBrownMode = true;
  202. goodsList.Add(goods);
  203. index++;
  204. }
  205. }
  206. SaleGoodsList = goodsList;
  207. }
  208. /// <summary>
  209. /// 查询发售商品参数回应
  210. /// </summary>
  211. /// <param name="goodses"></param>
  212. private void QuoteQuerySaleParamSuccess(List<SaleGoods> goodses )
  213. {
  214. if (goodses != null && goodses.Any())
  215. {
  216. UserManager.BrownSaleGoodses = goodses;
  217. SetCurrentSaleGoods();
  218. }
  219. }
  220. //private void QuoteQuerySaleError(ErrorEntity error)
  221. //{
  222. // ErrorManager.ShowReturnError(error, Client_Resource.UI2014_Tips, true);
  223. //}
  224. /// <summary>
  225. /// 查询申购 的商品
  226. /// </summary>
  227. /// <param name="saleGoodses"></param>
  228. public void QuerySuccess(List<SaleGoods> saleGoodses)
  229. {
  230. if (saleGoodses == null || !saleGoodses.Any()) return;
  231. var saleGoodsList = new List<SaleGoods>();
  232. var dicSaleGoods = saleGoodses.ToDictionary((item) => item.GoodsId);
  233. int index = 1;
  234. foreach (var quoteGoods in QuoteGoodsList)
  235. {
  236. if (quoteGoods == null || quoteGoods.ContainsGoodsSrc == (int)GoodsFromScr.Brown) continue;
  237. var saleGoods = new SaleGoods(quoteGoods)
  238. {
  239. OrderNumber = index,
  240. AgreeUnitString = CacheManager.TradeGoodsUnit.Where(x => x.UNITID == quoteGoods.GoodsParameters.AgreeUnitType).Select(x => x.GOODSUNITNAME).FirstOrDefault()
  241. };
  242. if (CacheManager.TradeGoodsFeeRules != null)
  243. {
  244. var rule = CacheManager.TradeGoodsFeeRules.FirstOrDefault(z => z.Key == quoteGoods.GoodsId).Value;
  245. if (rule != null && rule.GoodsRules != null)
  246. {
  247. var minKey = (int)eTradeRule.TRADERULE_SPPT;
  248. var maxKey = (int)eTradeRule.TRADERULE_SPPT;
  249. var maxHolderKey = (int)eTradeRule.TRADERULE_SPPT;
  250. ////单笔最小申购数量
  251. if (rule.GoodsRules.ContainsKey(minKey))
  252. {
  253. long minLot = 1;
  254. long.TryParse(rule.GoodsRules[minKey].FeeValue + string.Empty, out minLot);
  255. saleGoods.PerMinSubQty = minLot;
  256. }
  257. ////单笔最大申购数量
  258. if (rule.GoodsRules.ContainsKey(maxKey))
  259. {
  260. long maxLot = 10;
  261. long.TryParse(rule.GoodsRules[maxKey].FeeValue + string.Empty, out maxLot);
  262. saleGoods.PerMaxSubQty = maxLot;
  263. }
  264. ////最大可申购数量
  265. if (rule.GoodsRules.ContainsKey(maxHolderKey))
  266. {
  267. long Qty = long.MaxValue;
  268. long.TryParse(rule.GoodsRules[maxHolderKey].FeeValue + string.Empty, out Qty);
  269. //saleGoods.Qty = Qty;
  270. }
  271. }
  272. }
  273. ////todo:测试用
  274. if (saleGoods.PerMinSubQty == 0) saleGoods.PerMinSubQty = 10;
  275. if (saleGoods.PerMaxSubQty == 0) saleGoods.PerMaxSubQty = 1000;
  276. if (dicSaleGoods.ContainsKey(quoteGoods.GoodsId))
  277. {
  278. saleGoods.Planissueqty = dicSaleGoods[quoteGoods.GoodsId].Planissueqty;
  279. saleGoods.ApplyStartTime = dicSaleGoods[quoteGoods.GoodsId].ApplyStartTime;
  280. saleGoods.ApplyEndTime = dicSaleGoods[quoteGoods.GoodsId].ApplyEndTime;
  281. }
  282. //只显示申购中和待申购状态的商品
  283. if (saleGoods.RunSteps == eRunStep.RUNSTEP_BIDDINGPRICE || saleGoods.RunSteps == eRunStep.RUNSTEP_BIDDINGCONTINUOUS)
  284. {
  285. saleGoodsList.Add(saleGoods);
  286. index++;
  287. }
  288. }
  289. SaleGoodsList = saleGoodsList;
  290. CacheManager.SaleGoodses[_goodsGroupId] = SaleGoodsList;
  291. //默认选择第一个
  292. CurrentDataGridItem = SaleGoodsList.FirstOrDefault();
  293. }
  294. /// <summary>
  295. /// 错误处理
  296. /// </summary>
  297. /// <param name="error">错误内容</param>
  298. private void QueryErrorFunc(ErrorEntity error)
  299. {
  300. SaleGoodsList = null;
  301. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  302. {
  303. ErrorManager.ShowReturnError(error, Client_Resource.UI2014_Tips, true);
  304. }));
  305. }
  306. private void RegisterMethod(bool obj)
  307. {
  308. this.SelectCommand.Execute(null);
  309. }
  310. /// <summary>
  311. ///
  312. /// </summary>
  313. /// <param name="goodsGroupId"></param>
  314. public void InitQuery(long goodsGroupId)
  315. {
  316. _goodsGroupId = goodsGroupId;
  317. _saleService = ServiceLocator.Current.GetInstance<ISaleService>();
  318. QuerySaleGoods();
  319. }
  320. /// <summary>
  321. /// 发当前商品
  322. /// </summary>
  323. /// <param name="saleGoods">发售商品</param>
  324. private void SendCurrentGoods(SaleGoods saleGoods)
  325. {
  326. if (saleGoods == null) return;
  327. var quoteGoods= CacheManager.CacheGoodsBaseInfos.FirstOrDefault((item) => item.GoodsId == saleGoods.GoodsId);
  328. if (quoteGoods == null) return;
  329. var goodsDTO = new QuoteGoodsDTO(quoteGoods);
  330. MessengerHelper.DefaultSend<QuoteGoodsDTO>(goodsDTO, MessengerTokens.SelectGoodsChange);
  331. }
  332. #endregion
  333. }
  334. }