using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Ioc; using Muchinfo.PC.Common.Extensions; using Muchinfo.MTPClient.Account.Views; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Infrastructure.Utilities; using Muchinfo.MTPClient.IService; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Windows.Controls; namespace Muchinfo.MTPClient.Account.ViewModels { public class RiskControlNoticeViewModel : ViewModelBase { private const int PageSize = 8; public RiskControlNoticeViewModel() { } public RiskControlNoticeViewModel(System.Collections.ObjectModel.ObservableCollection riskList) { RiskMessages = riskList; } /// /// 风控通知集合 /// private ObservableCollection _riskMessages = new ObservableCollection(); /// /// 风控通知集合 /// public ObservableCollection RiskMessages { get { return _riskMessages; } set { Set(() => RiskMessages, ref _riskMessages, value); RaisePropertyChanged(() => RiskMessages); if (RiskMessages != null && RiskMessages.Count > 0) { if (RiskMessages.Count % PageSize == 0) { TotalPageCount = RiskMessages.Count / PageSize; } else { TotalPageCount = RiskMessages.Count / PageSize + 1; } RaisePropertyChanged(() => CurrentPageMessages); } else { TotalPageCount = 0; CurrentPageIndex = 0; } } } public void InsertNewRisk(Announcement newAnnouncement) { if (RiskMessages != null) { RiskMessages.Insert(0, newAnnouncement); if (RiskMessages.Count % PageSize == 0) { TotalPageCount = RiskMessages.Count / PageSize; } else { TotalPageCount = RiskMessages.Count / PageSize + 1; } RaisePropertyChanged(() => CurrentPageMessages); } else { TotalPageCount = 0; CurrentPageIndex = 0; } } public ObservableCollection CurrentPageMessages { get { var query = RiskMessages.Skip((_currentPageIndex - 1) * PageSize).Take(PageSize); ObservableCollection temp = new ObservableCollection(); foreach (Announcement an in query) { temp.Add(an); } return temp; } set { RaisePropertyChanged(() => CurrentPageMessages); } } /// /// 当前选中风控通知 /// private Announcement _currentRiskMessage; public Announcement CurrentRiskMessage { get { return _currentRiskMessage; } set { Set(() => CurrentRiskMessage, ref _currentRiskMessage, value); RaisePropertyChanged(() => CurrentRiskTitle); RaisePropertyChanged(() => CurrentRiskContent); RaisePropertyChanged(() => CurrentRiskLevel); if (CurrentRiskMessage != null) { if (CurrentRiskMessage.ReadStatus == 0) { Task.Factory.TryStartNew(() => { var announcementService = SimpleIoc.Default.GetInstance(); announcementService.ModifyBulletinNotifyStatus(UserManager.CurrentTradeAccount, CurrentRiskMessage); }); CurrentRiskMessage.ReadStatus = 1; RaisePropertyChanged(() => CurrentRiskMessage); } } } } private int _currentPageIndex = 1; public int CurrentPageIndex { get { if (_totalPageCount == 0) { _currentPageIndex = 0; } if (_totalPageCount != 0 && _currentPageIndex == 0) { _currentPageIndex = 1; } return _currentPageIndex; } set { Set(() => CurrentPageIndex, ref _currentPageIndex, value); RaisePropertyChanged(() => CurrentPageIndex); RaisePropertyChanged(() => CurrentPageMessages); } } private int _totalPageCount = 0; public int TotalPageCount { get { return _totalPageCount; } set { Set(() => TotalPageCount, ref _totalPageCount, value); RaisePropertyChanged(() => TotalPageCount); RaisePropertyChanged(() => CurrentPageIndex); } } public string CurrentRiskTitle { get { if (CurrentRiskMessage != null) { return CurrentRiskMessage.Title; } else { return ""; } } } public string CurrentRiskContent { get { if (CurrentRiskMessage != null) { return CurrentRiskMessage.Content; } else { return ""; } } } public int CurrentRiskLevel { get { if (CurrentRiskMessage != null) { return CurrentRiskMessage.RiskLevel; } else { return 0; } } } private bool _isBusy; /// /// 是否忙等待 /// public bool IsBusy { get { return _isBusy; } set { Set(() => IsBusy, ref _isBusy, value); } } public RelayCommand ClosedCommand { get { return new RelayCommand((v) => v.Close()); } } //下一页 public RelayCommand NextCommand { get { return new RelayCommand(() => { if (CurrentPageIndex <= TotalPageCount - 1) { CurrentPageIndex += 1; } }); } } public RelayCommand PrevCommand { get { return new RelayCommand(() => { if (CurrentPageIndex > 1) { CurrentPageIndex -= 1; } }); } } public RelayCommand QueryCommand { get { return new RelayCommand((v) => { int index; if (int.TryParse(v.Text, out index)) { if (index >= 1 && index <= TotalPageCount) { CurrentPageIndex = index; } } }); } } /// /// 加载数据 /// public void LoadRiskMessages() { var tradeCacheData = UserManager.GetTradeCacheData((UserManager.CurrentTradeAccount.AccountId)); IsBusy = true; Task.Factory.TryStartNew(() => { //RiskMessages = UserManager.GetMessageNoticeOrders(UserManager.CurrentTradeAccount, true); RiskMessages = tradeCacheData.MessageNoticeOrders; }, () => IsBusy = false); } } }