using Newtonsoft.Json.Linq; using Newtonsoft.Json; using Prism.Commands; using Prism.Events; using SWS.CAD.Event; using SWS.CAD.ViewModels.myViewModelBase; using SWS.CAD.Views; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using Unity; using Teigha.GraphicsInterface; using SWS.Commons; using SWS.Model; using SWS.Service; namespace SWS.CAD.ViewModels { public class UserNotificationViewModel : DialogBase { #region BINDING private ec_notification _select; public ec_notification select { get { return _select; } set { _select = value; RaisePropertyChanged(nameof(select)); } } private ObservableCollection _Notifications; public ObservableCollection Notifications { get { return _Notifications; } set { _Notifications = value; RaisePropertyChanged(nameof(Notifications)); } } private ObservableCollection _ListAllData; public ObservableCollection ListAllData { get { return _ListAllData; } set { _ListAllData = value; RaisePropertyChanged(nameof(ListAllData)); } } Visibility visibility = Visibility.Visible; /// /// 是否显示提交按钮 /// public Visibility IsShowSubmit { get { return visibility; } set { visibility = value; RaisePropertyChanged(nameof(IsShowSubmit)); } } int _mode = 0; /// /// 消息模式 0:接收 1:发送 /// public int mode { get { return _mode; } set { _mode = value; if (value == 1) { IsShowSubmit = Visibility.Hidden; title = GlobalObject.curProject.ProjectName + " 项目消息 - 我发送的"; } else { IsShowSubmit = Visibility.Visible; title = GlobalObject.curProject.ProjectName + " 项目消息 - 我收到的"; } } } #endregion IEventAggregator eventAggregator; NotificationService _NotificationService; /// /// 消息提交命令 /// public ICommand SubmitCommad { get; } /// /// 消息接收列表 /// public ICommand Commad_Receive { get; } /// /// 消息发送列表 /// public ICommand Commad_Send { get; } /// /// 消息发送列表 /// public ICommand Commad_All { get; } /// /// 消息发送列表 /// public ICommand Commad_Readed { get; } /// /// 消息发送列表 /// public ICommand Commad_UnReaded { get; } public UserNotificationViewModel() { Notifications = new ObservableCollection(); ListAllData = new ObservableCollection(); _NotificationService = GlobalObject.container.Resolve(); eventAggregator = GlobalObject.container.Resolve(); eventAggregator.GetEvent().Subscribe(onSelectProject, ThreadOption.UIThread, false); SubmitCommad = new DelegateCommand(SubmitCmd); Commad_Receive = new DelegateCommand(onReceive); Commad_Send = new DelegateCommand(onSend); Commad_All = new DelegateCommand(onAll); Commad_Readed = new DelegateCommand(onReaded); Commad_UnReaded = new DelegateCommand(onUnReaded); title = "项目消息"; } #region 选择项目后,弹出用户收到的消息 /// /// 选择项目后,弹出用户收到的消息 /// /// private async void onSelectProject(List list) { mode = 0; Notifications.Clear(); ListAllData.Clear(); if (list == null|| !list.Any()) { mode = 0; var infos = await _NotificationService.GetUserAllNotification(mode); ListAllData.AddRange(infos); onAll(); } else { mode = 0; ListAllData.AddRange(list); onAll(); } } #endregion public override void onWindow_loaded(object para) { base.onWindow_loaded(para); } //按钮的作用:点击后,将当前选中的行(可能是多行),提交给数据库的某个接口。将每个数据的“未读” 设置为 已读 public async void SubmitCmd() { if (select == null) { MessageBox.Show("请选择一条消息!"); return; } else if (select.CheckFLG==1) { return; } string ids = select.ID; var flag = await _NotificationService.SubmitUnread(ids); if (flag) { MessageBox.Show("提交数据成功!"); } else { MessageBox.Show("提交数据失败!"); } } #region 确定取消按钮重写 public override void ExecuteCloseCommand(object parameter) { close(parameter); } public override void ExecuteOKCommandAsync(object parameter) { close(parameter); } private void close(object parameter) { eventAggregator.GetEvent().Unsubscribe(onSelectProject); // 关闭窗口 (需要通过 CommandParameter 传入窗口实例) if (parameter is System.Windows.Window window) { window.Close(); } this.Dispose(); } #endregion #region 消息接收列表 /// /// 消息接收列表 /// public async void onReceive() { try { IsBusy = true; Notifications.Clear(); ListAllData.Clear(); mode = 0; var infos = await _NotificationService.GetUserAllNotification(mode); ListAllData.AddRange(infos); onAll(); IsBusy = false; } catch (Exception ex) { IsBusy = false; MessageBox.Show(ex.Message); } } #endregion #region 消息发送列表 /// /// 消息发送列表 /// public async void onSend() { try { IsBusy = true; BusyContent = "数据加载中..."; Notifications.Clear(); ListAllData.Clear(); mode = 1; var infos = await _NotificationService.GetUserAllNotification(mode); ListAllData.AddRange(infos); onAll(); IsBusy = false; } catch (Exception ex) { IsBusy = false; MessageBox.Show(ex.Message); } } #endregion #region 全部显示 /// /// 全部显示 /// public void onAll() { try { Notifications = new ObservableCollection(ListAllData); } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region 已读 /// /// 已读 /// public void onReaded() { try { var list = ListAllData.Where(a => a.CheckFLG == 1).ToList(); Notifications = new ObservableCollection(list); } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region 未读 /// /// 未读 /// public void onUnReaded() { try { var list = ListAllData.Where(a => a.CheckFLG == 0).ToList(); Notifications = new ObservableCollection(list); } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion } }