using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Ioc; using Microsoft.Practices.ServiceLocation; using Muchinfo.MTPClient.Data; using Muchinfo.MTPClient.Data.DTO; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Helper; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Data.Model.Sale; using Muchinfo.MTPClient.Infrastructure.Cache; using Muchinfo.MTPClient.Infrastructure.Helpers; using Muchinfo.MTPClient.Infrastructure.Utilities; using Muchinfo.MTPClient.IService; using Muchinfo.MTPClient.Resources; using Muchinfo.MTPClient.Sale.Views; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; namespace Muchinfo.MTPClient.Sale.ViewModels { public class QuerySaleGoodsViewModel : ViewModelBase { private ISaleService _saleService; private long _goodsGroupId; //申购中的商品 private ObservableCollection purchaseGoodsList; //战略投资者商品 private List strategicInvestors; #region 构造函数 public QuerySaleGoodsViewModel() { MessengerHelper.DefaultRegister(this, MessengerTokens.RefrashSaleGoodsCommand, RegisterMethod); } #endregion #region 成员变量 private SaleGoods _currentDataGridItem; /// /// 当前选中DataGridItem /// public SaleGoods CurrentDataGridItem { get { return _currentDataGridItem; } set { Set(() => CurrentDataGridItem, ref _currentDataGridItem, value); SendCurrentGoods(_currentDataGridItem); } } /// /// 发行商品 /// private List _saleGoodsList; public List SaleGoodsList { get { return _saleGoodsList; } set { Set(() => SaleGoodsList, ref _saleGoodsList, value); } } private List _quoteGoodsList; public List QuoteGoodsList { get { return _quoteGoodsList; } set { Set(() => QuoteGoodsList, ref _quoteGoodsList, value); } } private string _goodCode = string.Empty; /// /// 商品代码 /// public string GoodCode { get { return _goodCode.Trim(); } set { Set(() => GoodCode, ref _goodCode, value); } } #endregion #region 私有方法 /// /// 查询 /// public RelayCommand SelectCommand { get { return new RelayCommand(() => { QuerySaleGoods(); }); } } /// /// 重置 /// public RelayCommand ResetCommand { get { return new RelayCommand(() => { GoodCode = string.Empty; }); } } /// /// 申购 /// public RelayCommand DetailCommand { get { return new RelayCommand(() => { if (CurrentDataGridItem == null) { //todo:提示无选择商品 return; } var win = new SaleOrderFrame(CurrentDataGridItem) { Owner = Application.Current.MainWindow, WindowStartupLocation = WindowStartupLocation.CenterOwner }; win.ShowDialog(); }); } } public void QuerySaleGoods() { QuoteGoodsList = CacheManager.CacheGoodsBaseInfos.Where((item) => item.GoodsParameters.SortId == _goodsGroupId) .ToList(); if (UserManager.IsAccountLogin) { if (QuoteGoodsList == null || !QuoteGoodsList.Any()) { SaleGoodsList = null; return; } var queryParamers = new List() {}; var goodsIdString = string.Empty; foreach (var quoteGoods in QuoteGoodsList) { if ((quoteGoods.ContainsGoodsSrc & (int) GoodsFromScr.Trade) > 0) ///只查询交易的商品 { goodsIdString += quoteGoods.GoodsParameters.GoodsId + ","; } } goodsIdString = goodsIdString.TrimEnd(','); if (!string.IsNullOrEmpty(goodsIdString)) { queryParamers.Add(new QueryCommonParam() {ParamKey = "goodsIds", ParamValue = goodsIdString}); _saleService.QueryAccountSaleGoodsRpt(QueryStatement.SearchSaleGoods, queryParamers, QuerySuccess, QueryErrorFunc); } } else ////游客登录 { if (UserManager.BrownSaleGoodses == null) { var quoteDataServie = SimpleIoc.Default.GetInstance(); quoteDataServie.QuerySaleGoodsParam(QuoteQuerySaleParamSuccess, QueryErrorFunc); } else { SetCurrentSaleGoods(); } } } /// /// 设置发售商品显示内容 /// private void SetCurrentSaleGoods() { if (QuoteGoodsList == null || !QuoteGoodsList.Any()||UserManager.BrownSaleGoodses==null || !UserManager.BrownSaleGoodses.Any()) { SaleGoodsList = null; return; } int index = 1; var goodsList = new List(); foreach (var quoteGoodse in QuoteGoodsList) { var goods= UserManager.BrownSaleGoodses.FirstOrDefault((item) => item.GoodsId == quoteGoodse.GoodsId); if (goods != null) { goods.OrderNumber = index; goods.GoodsName = quoteGoodse.Name; goods.GoodsCode = quoteGoodse.GoodsCode; goods.AgreeUnit = quoteGoodse.GoodsParameters.AgreeUnit; goods.GoodsId = quoteGoodse.GoodsId; goods.RunStepsDisplay = goods.RunSteps.Discription(); goods.GoodsCodeColor = quoteGoodse.GoodsCodeColor; goods.GoodsNameColor = quoteGoodse.NameColor; goods.DefaultColor = quoteGoodse.DefaultColor; goods.IsBrownMode = true; goodsList.Add(goods); index++; } } SaleGoodsList = goodsList; } /// /// 查询发售商品参数回应 /// /// private void QuoteQuerySaleParamSuccess(List goodses ) { if (goodses != null && goodses.Any()) { UserManager.BrownSaleGoodses = goodses; SetCurrentSaleGoods(); } } //private void QuoteQuerySaleError(ErrorEntity error) //{ // ErrorManager.ShowReturnError(error, Client_Resource.UI2014_Tips, true); //} /// /// 查询申购 的商品 /// /// public void QuerySuccess(List saleGoodses) { if (saleGoodses == null || !saleGoodses.Any()) return; var saleGoodsList = new List(); var dicSaleGoods = saleGoodses.ToDictionary((item) => item.GoodsId); int index = 1; foreach (var quoteGoods in QuoteGoodsList) { if (quoteGoods == null || quoteGoods.ContainsGoodsSrc == (int)GoodsFromScr.Brown) continue; var saleGoods = new SaleGoods(quoteGoods) { OrderNumber = index, AgreeUnitString = CacheManager.TradeGoodsUnit.Where(x => x.UNITID == quoteGoods.GoodsParameters.AgreeUnitType).Select(x => x.GOODSUNITNAME).FirstOrDefault() }; if (CacheManager.TradeGoodsFeeRules != null) { var rule = CacheManager.TradeGoodsFeeRules.FirstOrDefault(z => z.Key == quoteGoods.GoodsId).Value; if (rule != null && rule.GoodsRules != null) { var minKey = (int)eTradeRule.TRADERULE_SPPT; var maxKey = (int)eTradeRule.TRADERULE_SPPT; var maxHolderKey = (int)eTradeRule.TRADERULE_SPPT; ////单笔最小申购数量 if (rule.GoodsRules.ContainsKey(minKey)) { long minLot = 1; long.TryParse(rule.GoodsRules[minKey].FeeValue + string.Empty, out minLot); saleGoods.PerMinSubQty = minLot; } ////单笔最大申购数量 if (rule.GoodsRules.ContainsKey(maxKey)) { long maxLot = 10; long.TryParse(rule.GoodsRules[maxKey].FeeValue + string.Empty, out maxLot); saleGoods.PerMaxSubQty = maxLot; } ////最大可申购数量 if (rule.GoodsRules.ContainsKey(maxHolderKey)) { long Qty = long.MaxValue; long.TryParse(rule.GoodsRules[maxHolderKey].FeeValue + string.Empty, out Qty); //saleGoods.Qty = Qty; } } } ////todo:测试用 if (saleGoods.PerMinSubQty == 0) saleGoods.PerMinSubQty = 10; if (saleGoods.PerMaxSubQty == 0) saleGoods.PerMaxSubQty = 1000; if (dicSaleGoods.ContainsKey(quoteGoods.GoodsId)) { saleGoods.Planissueqty = dicSaleGoods[quoteGoods.GoodsId].Planissueqty; saleGoods.ApplyStartTime = dicSaleGoods[quoteGoods.GoodsId].ApplyStartTime; saleGoods.ApplyEndTime = dicSaleGoods[quoteGoods.GoodsId].ApplyEndTime; } //只显示申购中和待申购状态的商品 if (saleGoods.RunSteps == eRunStep.RUNSTEP_BIDDINGPRICE || saleGoods.RunSteps == eRunStep.RUNSTEP_BIDDINGCONTINUOUS) { saleGoodsList.Add(saleGoods); index++; } } SaleGoodsList = saleGoodsList; CacheManager.SaleGoodses[_goodsGroupId] = SaleGoodsList; //默认选择第一个 CurrentDataGridItem = SaleGoodsList.FirstOrDefault(); } /// /// 错误处理 /// /// 错误内容 private void QueryErrorFunc(ErrorEntity error) { SaleGoodsList = null; Application.Current.Dispatcher.BeginInvoke(new Action(() => { ErrorManager.ShowReturnError(error, Client_Resource.UI2014_Tips, true); })); } private void RegisterMethod(bool obj) { this.SelectCommand.Execute(null); } /// /// /// /// public void InitQuery(long goodsGroupId) { _goodsGroupId = goodsGroupId; _saleService = ServiceLocator.Current.GetInstance(); QuerySaleGoods(); } /// /// 发当前商品 /// /// 发售商品 private void SendCurrentGoods(SaleGoods saleGoods) { if (saleGoods == null) return; var quoteGoods= CacheManager.CacheGoodsBaseInfos.FirstOrDefault((item) => item.GoodsId == saleGoods.GoodsId); if (quoteGoods == null) return; var goodsDTO = new QuoteGoodsDTO(quoteGoods); MessengerHelper.DefaultSend(goodsDTO, MessengerTokens.SelectGoodsChange); } #endregion } }