using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using Muchinfo.MTPClient.Data.DTO;
using Muchinfo.MTPClient.Data.Enums;
using Muchinfo.MTPClient.Data.Model;
using Muchinfo.MTPClient.Infrastructure.Cache;
using Muchinfo.MTPClient.Infrastructure.Helpers;
using Muchinfo.MTPClient.Infrastructure.Utilities;
using Muchinfo.MTPClient.IService;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace Muchinfo.MTPClient.Quotation.ViewModels
{
public class QuoteBoardViewModel : ViewModelBase
{
#region Fields
private IGoodsService _goodsService;
private IFavoriteService _ifavoriteService;
private QuoteGoodsDTO _goodsItem;
///
/// 数据集合
///
private ObservableCollection _itemsSource;
private RelayCommand _sellCommand;
private RelayCommand _buyCommand;
#endregion Fields
private bool _isVisibilityGoodsOfPAndX = false;
///
/// 是否显示P+X商品数据源LIST
///
public bool IsVisibilityGoodsOfPAndX
{
get
{
return _isVisibilityGoodsOfPAndX && (UserManager.CurrentGroupHasGoods && IsDeliveryVisible);
}
set
{
Set(() => IsVisibilityGoodsOfPAndX, ref _isVisibilityGoodsOfPAndX, value);
}
}
private string _PDeliveryGoodsName;
///
/// P交割模式商品
///
public string PDeliveryGoodsName
{
get
{
return _PDeliveryGoodsName;
}
set
{
Set(() => PDeliveryGoodsName, ref _PDeliveryGoodsName, value);
}
}
private eDeliveryContractLink _relationMode;
///
/// 关联模式
///
public eDeliveryContractLink RelationMode
{
get
{
return _relationMode;
}
set
{
Set(() => RelationMode, ref _relationMode, value);
}
}
public QuoteGoods SelectedQuoteGoods { get; set; }
private ObservableCollection _goodsOfPAndXInfoList = new ObservableCollection();
///
/// 选择商品的P+X商品数据源
///
public ObservableCollection GoodsOfPandXInfoList
{
get
{
return _goodsOfPAndXInfoList;
}
set
{
Set(() => GoodsOfPandXInfoList, ref _goodsOfPAndXInfoList, value);
}
}
#region 是否可交易
#region P+X 信息显示高度
private decimal _pXInfoHeight = decimal.Zero;
///
/// P+X 信息显示高度
///
public decimal PXInfoHeight
{
get
{
return _pXInfoHeight;
}
set
{
Set(() => PXInfoHeight, ref _pXInfoHeight, value);
}
}
#endregion
public bool IsDeliveryVisible
{
get
{
return ApplicationParameter.IsDeliveryVisible == 1 && UserManager.IsAccountLogin;
}
}
///
/// 是否买入
///
public bool IsBidEnable
{
get
{
//var buyFunc = UserManager.TradeFuncMenus.FirstOrDefault((item) => item.FuncMenuCode == UserManager.GetMenuCommandValue(MenuCommandType.BuyIn));
var buyFunc = UserManager.TradeFuncMenus.FirstOrDefault((item) => item.FuncMenuCode == MenuCommandType.BuyIn.ToString());
//买入
if (buyFunc != null)
{
return buyFunc.IsEnabled;
}
return false;
}
}
///
/// 是否卖出
///
public bool IsAskEnable
{
get
{
//var tradeFunc =UserManager.TradeFuncMenus.FirstOrDefault((item) => item.FuncMenuCode == UserManager.GetMenuCommandValue(MenuCommandType.Sellout));
var tradeFunc = UserManager.TradeFuncMenus.FirstOrDefault((item) => item.FuncMenuCode == MenuCommandType.Sellout.ToString());
//卖出
if (tradeFunc != null)
{
return tradeFunc.IsEnabled;
}
return false;
}
}
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
public QuoteBoardViewModel()
: base()
{
_goodsService = SimpleIoc.Default.GetInstance();
_ifavoriteService = SimpleIoc.Default.GetInstance();
_sellCommand = new RelayCommand((dto) =>
{
MessengerHelper.DefaultSend(dto, MessengerTokens.QuoteBoardSelectItemChange);
MessengerHelper.DefaultSend(dto, MessengerTokens.QuickSell);
});
_buyCommand = new RelayCommand((dto) =>
{
MessengerHelper.DefaultSend(dto, MessengerTokens.QuoteBoardSelectItemChange);
MessengerHelper.DefaultSend(dto, MessengerTokens.QuickBuy);
});
//注册消息
RegisterMessages();
}
#endregion Constructors
#region Properties
#region Public Properties
///
/// 获取和设置数据集合
///
public ObservableCollection ItemsSource
{
get
{
return _itemsSource;
}
private set
{
_itemsSource = value;
RaisePropertyChanged(() => ItemsSource);
}
}
#endregion Public Properties
#endregion Properties
#region Methods
#region Public Methods
public RelayCommand SellCommand
{
get { return _sellCommand; }
}
public RelayCommand BuyCommand
{
get { return _buyCommand; }
}
///
/// 命令类型改变时更新数据集合,并刷新盘面
///
/// The new type.
/// The goods group.
public void RefreshList(MenuCommandType newType, MarketsInfoModel goodsGroup)
{
if (_itemsSource != null) ItemsSource.Clear();
if (goodsGroup.TradeMode == eTradeMode.MySelected)
{
var goodsList = _ifavoriteService.GetMyFavoriteGoodses(goodsGroup.MarketID);
ItemsSource = goodsList == null ? null : _goodsService.ConvertGoodsDtos(goodsList.ToList());
}
else
{
ItemsSource = _goodsService.GetQuoteGoodsDTOList(newType, goodsGroup);
}
////重新订阅并查询盘面
UserManager.CurrentGoodsIds = ItemsSource == null ? new List() : ItemsSource.Select(z => (int)z.GoodsId).ToList();
QuoteProxyHelper.QuoteSubscribe(GoodsFromScr.Trade);
}
#endregion Public Methods
#region Private Methods
///
/// Registers the resource change.
///
private void RegisterMessages()
{
//Application.Current.Dispatcher.BeginInvoke(new Action(() =>
//{
// MessengerHelper.DefaultUnregister(this, MessengerTokens.RegisterResourceChange);
// MessengerHelper.DefaultRegister(this, MessengerTokens.RegisterResourceChange, (e) =>
// {
// if (_itemsSource == null || !_itemsSource.Any()) return;
// foreach (var item in _itemsSource)
// {
// if (item == null) continue;
// item.UpdateNameColor(item.IsSelected);
// }
// });
//}));
////注册实时行情消息
MessengerHelper.QuoteRegister>(this,
MessengerTokens.ReceiveRealTimeQuote, (quoteList) =>
{
if (quoteList == null || !quoteList.Any() || _itemsSource == null || !_itemsSource.Any())
return;
foreach (var goods in quoteList)
{
if (goods == null) continue;
var item = _itemsSource.FirstOrDefault(z => z.Symbol == goods.Symbol);
if (item == null) continue;
item.UpdateFromQuoteGoods(goods);
}
//更新X商品行情
if (this._goodsItem != null)
{
SelectGoodsOfPAndX_ItemSource(_goodsItem);
}
});
//MessengerHelper.DefaultRegister(this, MessengerTokens.SelectGoodsChange, SelectGoodsOfPAndX_ItemSource);
}
///
/// 获取选择商品的P+X商品数据源
///
public void SelectGoodsOfPAndX_ItemSource(QuoteGoodsDTO goodsItem)
{
//PXInfoHeight = 23;
this._goodsItem = goodsItem;
var quoteGoods = CacheManager.CacheGoodsBaseInfos.FirstOrDefault((item) => item.Symbol == goodsItem.Symbol);
SelectedQuoteGoods = quoteGoods;
IsVisibilityGoodsOfPAndX = false;
var tempPAndXList = CacheManager.AllDeliveryRelationList.Where((item) => item.GoodsId == goodsItem.GoodsId).ToList();
var dicModel = new List();
if (tempPAndXList != null && tempPAndXList.Count() > 0)
{
if (IsDeliveryVisible)
{
IsVisibilityGoodsOfPAndX = true;
}
foreach (var relationModel in tempPAndXList)
{
RelationMode = relationModel.RelationMode;
QuoteGoods delivery_Goods = null;
if (relationModel.RelationMode == eDeliveryContractLink.Normal)
{
delivery_Goods = quoteGoods;
PDeliveryGoodsName = relationModel.deliverygoodsname;
//PXInfoHeight = 51;
}
else if (relationModel.RelationMode == eDeliveryContractLink.PAndX)
{
delivery_Goods =
CacheManager.CacheGoodsBaseInfos.FirstOrDefault((item) => relationModel.RelationGoodsId == item.GoodsId);
// PXInfoHeight = 108;
}
if (delivery_Goods != null)
{
delivery_Goods.DeliveryGoodsName = relationModel.deliverygoodsname;
dicModel.Add(delivery_Goods);
} //// DeliveryGoodsName
}
var goodsDtos = _goodsService.ConvertGoodsDtos(dicModel.ToList());
GoodsOfPandXInfoList = goodsDtos;
}
}
///
/// Cleanups this instance.
///
public override void Cleanup()
{
base.Cleanup();
_itemsSource = null;
}
#endregion Private Methods
#endregion Methods
}
}