| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- 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<StrategicInvestor> _strategicInvestors = new List<StrategicInvestor>();//战略投资者商品
- #region 构造函数
- public string TradeCode { get; set; }
- public ApplyBuyViewModel(SaleGoods purchaseGood, ObservableCollection<SaleGoods> saleGoods, List<StrategicInvestor> strategicInvestors)
- {
- _orderService = SimpleIoc.Default.GetInstance<IOrderService>();
- _saleService = ServiceLocator.Current.GetInstance<ISaleService>();
- 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> _saleGoods;
- /// <summary>
- /// 可申购商品
- /// </summary>
- public ObservableCollection<SaleGoods> SaleGoods
- {
- get { return _saleGoods; }
- set { Set(() => SaleGoods, ref _saleGoods, value); }
- }
- private string _goodsCode;
- /// <summary>
- /// 当前选中的商品
- /// </summary>
- 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;
- /// <summary>
- /// 当前选中商品价格
- /// </summary>
- public decimal SalePrice
- {
- get { return _salePrice; }
- set { Set(() => SalePrice, ref _salePrice, value); }
- }
- private long _purchaseNumber;
- /// <summary>
- /// 申购数量
- /// </summary>
- 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;
- /// <summary>
- /// 最大申购数量
- /// 申购投资者的可申购金额计算出的对应可申购数量和商品管理中设置的每个申购投资者申购数量上限进行比较,取二者的较小值。
- /// </summary>
- 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<Commission> _bidCommissions = SortCommissions(true);
- /// <summary>
- /// 显示买入档
- /// </summary>
- public List<Commission> BidCommissions
- {
- get { return _bidCommissions; }
- set { Set(() => BidCommissions, ref _bidCommissions, value); }
- }
- private List<Commission> _askCommissions = SortCommissions(false);
- /// <summary>
- /// 显示卖出档
- /// </summary>
- public List<Commission> AskCommissions
- {
- get { return _askCommissions; }
- set { Set(() => AskCommissions, ref _askCommissions, value); }
- }
- private string _busyTips;
- /// <summary>
- /// 服务忙提示
- /// </summary>
- public string BusyTips
- {
- get
- {
- return _busyTips;
- }
- set
- {
- Set(() => BusyTips, ref _busyTips, value);
- }
- }
- private bool _isBusy;
- /// <summary>
- /// 是否忙,显示等待控件
- /// </summary>
- public bool IsBusy
- {
- get
- {
- return _isBusy;
- }
- set
- {
- Set(() => IsBusy, ref _isBusy, value);
- }
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 设置买卖档
- /// </summary>
- /// <param name="bid">if set to <c>true</c> [bid].</param>
- /// <param name="commissions">The commissions.</param>
- /// <returns>List{Commission}.</returns>
- private static List<Commission> SortCommissions(bool bid)
- {
- var commissionLsit = new List<Commission>();
- 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<Window> CancelCommand
- {
- get
- {
- return new RelayCommand<Window>((dialog) =>
- {
- dialog.DialogResult = false;
- });
- }
- }
- public RelayCommand<Window> OKCommand
- {
- get
- {
- return new RelayCommand<Window>((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;
- }
- });
- }
- }
- /// <summary>
- /// 发送下单消息
- /// </summary>
- /// <param name="order">The order.</param>
- /// <returns>返回下单错误码</returns>
- 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);
- }
- /// <summary>
- /// 验证表单数据是否正确
- /// </summary>
- /// <param name="errorMsg"></param>
- /// <returns></returns>
- private bool VailedFormData(out string errorMsg)
- {
- if (string.IsNullOrEmpty(GoodsCode))
- {
- errorMsg = Muchinfo_Resource.GoodsSelect_Vailed;
- return false;
- }
- errorMsg = string.Empty;
- return true;
- }
- #endregion
- }
- }
|