using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Ioc; using GalaSoft.MvvmLight.Messaging; using Microsoft.Practices.ServiceLocation; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Data.Model.Sale; using Muchinfo.MTPClient.Infrastructure.Utilities; using Muchinfo.MTPClient.IService; using Muchinfo.MTPClient.Resources; using Muchinfo.WPF.Controls.Windows; namespace Muchinfo.MTPClient.Sale.ViewModels { public class ApplyBuyViewModel : ViewModelBase { private IOrderService _orderService; //单据服务 private ISaleService _saleService;//发售服务 private List _strategicInvestors = new List();//战略投资者商品 #region 构造函数 public string TradeCode { get; set; } public ApplyBuyViewModel(SaleGoods purchaseGood, ObservableCollection saleGoods, List strategicInvestors) { _orderService = SimpleIoc.Default.GetInstance(); _saleService = ServiceLocator.Current.GetInstance(); TradeCode = UserManager.CurrentTradeAccount.TradeCode; RaisePropertyChanged(() => TradeCode); SaleGoods = saleGoods; if (strategicInvestors != null) { _strategicInvestors = strategicInvestors; } GoodsCode = purchaseGood.GoodsCode; } #endregion #region 成员变量 //最小申购数量 private int applyMinQty; //可购买数量(可用保证金/单价) private Int64 number; //申购数量上限 private Int64 maxNumber; //增量手续费和基本手续费之和 private decimal Charge; private ObservableCollection _saleGoods; /// /// 可申购商品 /// public ObservableCollection SaleGoods { get { return _saleGoods; } set { Set(() => SaleGoods, ref _saleGoods, value); } } private string _goodsCode; /// /// 当前选中的商品 /// public string GoodsCode { get { return _goodsCode; } set { Set(() => GoodsCode, ref _goodsCode, value); if (SaleGoods != null) { var firstOrDefault = SaleGoods.FirstOrDefault(k => k.GoodsCode == _goodsCode); if (firstOrDefault == null) { return; } var strategicInvestor = _strategicInvestors.FirstOrDefault(k => k.GoodsCode == _goodsCode); if (strategicInvestor == null) { SalePrice = firstOrDefault.SalePrice; maxNumber = int.Parse(firstOrDefault.SingerMaxRatio.ToString()) - firstOrDefault.SumQty; } else { SalePrice = strategicInvestor.SalePrice; maxNumber = strategicInvestor.PlacingQty - firstOrDefault.SumQty; } applyMinQty = firstOrDefault.ApplyMinQty; //发售手续费 decimal saleCharge = 0; //增量手续费 decimal ChargeNum = 0; if (firstOrDefault.ChargeType == ChargeModeType.FixedNumber) { saleCharge = firstOrDefault.ChargeNum / firstOrDefault.AgreeUnit; } else { saleCharge = firstOrDefault.SalePrice * firstOrDefault.ChargeNum; } if (!string.IsNullOrEmpty(UserManager.CurrentTradeAccount.ContentRightType)) { var rulesIds = UserManager.CurrentTradeAccount.ContentRightType.Split(',').ToList(); var index = -1; for (int i = 0; i < rulesIds.Count(); i++) { if (i % 2 == 0 && rulesIds[i].Equals(UserManager.SaleMarkId + string.Empty)) { index = i; break; } } if (index + 1 < rulesIds.Count && index != -1) { int pRule = -1; if (int.TryParse(rulesIds[index + 1], out pRule)) { var rules = _saleService.QueryParameterRule(pRule, firstOrDefault.GoodsId, UserManager.CurrentTradeAccount.LoginCode); if (rules != null && rules.Any()) { if (rules[0].MarketMarginType == ChargeModeType.FixedNumber) { ChargeNum = rules[0].MarketMargin / firstOrDefault.AgreeUnit; } else { ChargeNum = firstOrDefault.SalePrice * rules[0].MarketMargin; } } } } } else { Remark = "交易参数获取失败。"; OKButtonEnabled = false; return; } Charge = saleCharge + ChargeNum; if (Charge > 0) { PurchaseNumber = 0; return; } if (UserManager.CurrentTradeAccount.AccountType == eAccountType.ACCOUNTTYPE_TRADER) { number = Convert.ToInt64(Math.Floor(UserManager.CurrentTradeAccount.FundsAccounts[0].AvailMargin / (firstOrDefault.SalePrice - Charge) / firstOrDefault.AgreeUnit).ToString()); } else { number = Convert.ToInt64(Math.Floor(UserManager.CurrentTradeAccount.MemberAccountInfo.NetWorth / (firstOrDefault.SalePrice - Charge) / firstOrDefault.AgreeUnit).ToString()); } MaxPurchaseNumber = maxNumber < number ? maxNumber : number; PurchaseNumber = 0; } } } private decimal _salePrice; /// /// 当前选中商品价格 /// public decimal SalePrice { get { return _salePrice; } set { Set(() => SalePrice, ref _salePrice, value); } } private long _purchaseNumber; /// /// 申购数量 /// public long PurchaseNumber { get { return _purchaseNumber; } set { Set(() => PurchaseNumber, ref _purchaseNumber, value); if (Charge > 0) { Remark = "手续费参数设置错误,不能申购。"; OKButtonEnabled = false; } else if (number <= 0) { Remark = "资金不足。"; OKButtonEnabled = false; } else if (maxNumber <= 0) { Remark = "已达申购总量限制,不能继续申购。"; OKButtonEnabled = false; } else if (_purchaseNumber > MaxPurchaseNumber) { Remark = "已超出最大申购数量,不能继续申购。"; OKButtonEnabled = false; } else if (_purchaseNumber < applyMinQty) { Remark = string.Format(Muchinfo_Resource.ApplyBuy_MinNum, applyMinQty); OKButtonEnabled = false; } else if (_purchaseNumber % applyMinQty != 0) { Remark = string.Format(Muchinfo_Resource.ApplyBuy_IntNum, applyMinQty); OKButtonEnabled = false; } else { Remark = ""; OKButtonEnabled = true; } } } private Int64 _maxPurchaseNumber; /// /// 最大申购数量 /// 申购投资者的可申购金额计算出的对应可申购数量和商品管理中设置的每个申购投资者申购数量上限进行比较,取二者的较小值。 /// public Int64 MaxPurchaseNumber { get { return _maxPurchaseNumber; } set { Set(() => MaxPurchaseNumber, ref _maxPurchaseNumber, value); } } private bool _okButtonEnabled = true; public bool OKButtonEnabled { get { return _okButtonEnabled; } set { Set(() => OKButtonEnabled, ref _okButtonEnabled, value); } } private string _remark; public string Remark { get { return _remark; } set { Set(() => Remark, ref _remark, value); } } private List _bidCommissions = SortCommissions(true); /// /// 显示买入档 /// public List BidCommissions { get { return _bidCommissions; } set { Set(() => BidCommissions, ref _bidCommissions, value); } } private List _askCommissions = SortCommissions(false); /// /// 显示卖出档 /// public List AskCommissions { get { return _askCommissions; } set { Set(() => AskCommissions, ref _askCommissions, value); } } private string _busyTips; /// /// 服务忙提示 /// public string BusyTips { get { return _busyTips; } set { Set(() => BusyTips, ref _busyTips, value); } } private bool _isBusy; /// /// 是否忙,显示等待控件 /// public bool IsBusy { get { return _isBusy; } set { Set(() => IsBusy, ref _isBusy, value); } } #endregion #region 私有方法 /// /// 设置买卖档 /// /// if set to true [bid]. /// The commissions. /// List{Commission}. private static List SortCommissions(bool bid) { var commissionLsit = new List(); for (int i = 1; i < 6; i++) { Commission cs = new Commission() { Index = i, Volume = 0 }; commissionLsit.Add(cs); } if (!bid) { commissionLsit = commissionLsit.OrderByDescending((item) => item.Index).ToList(); } return commissionLsit; } public RelayCommand CancelCommand { get { return new RelayCommand((dialog) => { dialog.DialogResult = false; }); } } public RelayCommand OKCommand { get { return new RelayCommand((dialog) => { OKButtonEnabled = false; string errorMsg = string.Empty; bool validateBool = VailedFormData(out errorMsg); if (validateBool) { IsBusy = true; BusyTips = "正在提交数据,请稍候..."; Task.Factory.StartNew(() => { EntrustOrder order; try { int errorCode = PostOrder(out order); Application.Current.Dispatcher.BeginInvoke(new Action(() => { if (errorCode == 0) { MessageBoxHelper.ShowSuccess(Muchinfo_Resource.Content_ApplyOK, Muchinfo_Resource.Content_ApplyRemarkTitle); if (dialog.IsLoaded) { dialog.DialogResult = true; } // LogManager.WriteEntrustLog(order, GoodsCode); } else { ErrorManager.ShowReturnError(errorCode, Muchinfo_Resource.MessageBox_Error_Title); OKButtonEnabled = true; } Messenger.Default.Send(true, MessengerTokens.RefrashSaleGoodsCommand); IsBusy = false; })); } catch (Exception ex) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { IsBusy = false; OKButtonEnabled = true; throw new Exception(ex.Message); })); } }); } else { MessageBoxHelper.ShowInfo(errorMsg, Muchinfo_Resource.MessageBox_Error_Title); OKButtonEnabled = true; } }); } } /// /// 发送下单消息 /// /// The order. /// 返回下单错误码 private int PostOrder(out EntrustOrder order) { var entrustOrder = new EntrustOrder() { TradeCode = UserManager.CurrentTradeAccount.TradeCode, GoodsCode = GoodsCode, AccountType = UserManager.CurrentTradeAccount.AccountType, OrderType = OrderType.OrderTypeSale, BuyOrSell = Direction.Bid, EntrustQuantity = PurchaseNumber, EntrustPrice = SalePrice }; order = entrustOrder; return _orderService.EntrustOrder(entrustOrder, UserManager.CurrentTradeAccount.LoginCode); } /// /// 验证表单数据是否正确 /// /// /// private bool VailedFormData(out string errorMsg) { if (string.IsNullOrEmpty(GoodsCode)) { errorMsg = Muchinfo_Resource.GoodsSelect_Vailed; return false; } errorMsg = string.Empty; return true; } #endregion } }