using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Infrastructure.Utilities; using System.Collections.ObjectModel; using System.Linq; namespace Muchinfo.MTPClient.Account.ViewModels { public class MessageInfoViewModel : ViewModelBase { private bool _nextIsEnabled; public bool NextIsEnabled { get { return _nextIsEnabled; } set { Set(() => NextIsEnabled, ref _nextIsEnabled, value); } } private bool _returnIsEnabled; public bool ReturnIsEnabled { get { return _returnIsEnabled; } set { Set(() => ReturnIsEnabled, ref _returnIsEnabled, value); } } private int CurrentListIndex { get; set; } private Announcement _currentAnnouncement; public Announcement CurrentAnnouncement { get { return _currentAnnouncement; } set { Set(() => CurrentAnnouncement, ref _currentAnnouncement, value); SetCommand(); UpdatReadStatus(); } } private ObservableCollection _messageOrders; public ObservableCollection MessageOrders { get { return _messageOrders; } set { Set(() => MessageOrders, ref _messageOrders, value); } } private MsgTypeEnum _msgTypeEnum; /// /// 消息类型 /// public MsgTypeEnum MsgTypeEnum { get { return _msgTypeEnum; } set { _msgTypeEnum = value; switch (value) { case MsgTypeEnum.None: break; case MsgTypeEnum.Bulletin: Title = Muchinfo.MTPClient.Resources.Muchinfo_Resource.Models_Notice; break; //case MsgTypeEnum.MSG: // break; case MsgTypeEnum.NTF: Title = Muchinfo.MTPClient.Resources.Muchinfo_Resource.Models_Inform; break; //case MsgTypeEnum.ALL: // break; default: break; } } } private string _title; /// /// 标题 /// public string Title { get { return _title; } set { Set(() => Title, ref _title, value); } } public void UpdatReadStatus() { if (this.CurrentAnnouncement.ReadStatus == 0) { //修改阅读状态 var userInfoList = UserManager.GetTradeAccounts(); if (userInfoList != null) { var userInfo = userInfoList.FirstOrDefault(z => z.UserName == UserManager.CurrentTradeAccount.LoginCode); if (this.MsgTypeEnum == MsgTypeEnum.Bulletin) { if (userInfo != null && !userInfo.ReadAnnouncementIds.Contains(this.CurrentAnnouncement.MsgID)) { this.CurrentAnnouncement.ReadStatus = 1; userInfo.ReadAnnouncementIds.Add(this.CurrentAnnouncement.MsgID); UserManager.SaveTradeAccount(userInfo); } } if (this.MsgTypeEnum == MsgTypeEnum.NTF) { if (userInfo != null && !userInfo.ReadNoticeIds.Contains(this.CurrentAnnouncement.MsgID)) { this.CurrentAnnouncement.ReadStatus = 1; userInfo.ReadNoticeIds.Add(this.CurrentAnnouncement.MsgID); UserManager.SaveTradeAccount(userInfo); } } } } } public void SetCommand() { if (MessageOrders != null && MessageOrders.Count > 0) { //this.CurrentListIndex = 0; //Announcement announcement = MessageOrders.FirstOrDefault(m => m.MsgID == this.CurrentAnnouncement.MsgID); //if (announcement != null) //{ // if (this.MessageOrders[0].MsgID == this.CurrentAnnouncement.MsgID) // this.ReturnIsEnabled = false; // else // this.ReturnIsEnabled = true; // if (this.MessageOrders[MessageOrders.Count - 1].MsgID == this.CurrentAnnouncement.MsgID) // NextIsEnabled = false; // else // NextIsEnabled = true; //} this.CurrentListIndex = -1; var announcement = MessageOrders.FirstOrDefault(m => m.MsgID == this.CurrentAnnouncement.MsgID); if (announcement != null) { this.CurrentListIndex = MessageOrders.IndexOf(announcement); if (this.MessageOrders[0].MsgID == this.CurrentAnnouncement.MsgID) this.ReturnIsEnabled = false; else this.ReturnIsEnabled = true; if (this.MessageOrders[MessageOrders.Count - 1].MsgID == this.CurrentAnnouncement.MsgID) NextIsEnabled = false; else NextIsEnabled = true; } } } /// /// 上一条 /// public RelayCommand ReturnCommand { get { return new RelayCommand(() => { this.CurrentAnnouncement = MessageOrders[this.CurrentListIndex - 1]; }); } } /// /// 下一条 /// public RelayCommand NextCommand { get { return new RelayCommand(() => { this.CurrentAnnouncement = MessageOrders[this.CurrentListIndex + 1]; }); } } } }