009_DI-Elec/newFront/c#前端/SWS.CAD/ViewModels/UserNotificationViewModel.cs

312 lines
9.1 KiB
C#
Raw Normal View History

2025-08-15 16:34:31 +08:00
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;
2025-09-04 18:28:02 +08:00
using SWS.Commons;
using SWS.Model;
using SWS.Service;
2025-08-15 16:34:31 +08:00
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<ec_notification> _Notifications;
public ObservableCollection<ec_notification> Notifications
{
get { return _Notifications; }
set { _Notifications = value; RaisePropertyChanged(nameof(Notifications)); }
}
private ObservableCollection<ec_notification> _ListAllData;
public ObservableCollection<ec_notification> ListAllData
{
get { return _ListAllData; }
set { _ListAllData = value; RaisePropertyChanged(nameof(ListAllData)); }
}
Visibility visibility = Visibility.Visible;
/// <summary>
/// 是否显示提交按钮
/// </summary>
public Visibility IsShowSubmit
{
get
{
return visibility;
}
set { visibility = value; RaisePropertyChanged(nameof(IsShowSubmit)); }
}
int _mode = 0;
/// <summary>
/// 消息模式 0接收 1发送
/// </summary>
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;
/// <summary>
/// 消息提交命令
/// </summary>
public ICommand SubmitCommad { get; }
/// <summary>
/// 消息接收列表
/// </summary>
public ICommand Commad_Receive { get; }
/// <summary>
/// 消息发送列表
/// </summary>
public ICommand Commad_Send { get; }
/// <summary>
/// 消息发送列表
/// </summary>
public ICommand Commad_All { get; }
/// <summary>
/// 消息发送列表
/// </summary>
public ICommand Commad_Readed { get; }
/// <summary>
/// 消息发送列表
/// </summary>
public ICommand Commad_UnReaded { get; }
public UserNotificationViewModel()
{
Notifications = new ObservableCollection<ec_notification>();
ListAllData = new ObservableCollection<ec_notification>();
_NotificationService = GlobalObject.container.Resolve<NotificationService>();
eventAggregator = GlobalObject.container.Resolve<IEventAggregator>();
eventAggregator.GetEvent<SelectProjectEvent>().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
/// <summary>
/// 选择项目后,弹出用户收到的消息
/// </summary>
/// <param name="obj"></param>
private async void onSelectProject(List<ec_notification> 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<SelectProjectEvent>().Unsubscribe(onSelectProject);
// 关闭窗口 (需要通过 CommandParameter 传入窗口实例)
if (parameter is System.Windows.Window window)
{
window.Close();
}
this.Dispose();
}
#endregion
#region
/// <summary>
/// 消息接收列表
/// </summary>
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
/// <summary>
/// 消息发送列表
/// </summary>
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
/// <summary>
/// 全部显示
/// </summary>
public void onAll()
{
try
{
Notifications = new ObservableCollection<ec_notification>(ListAllData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region
/// <summary>
/// 已读
/// </summary>
public void onReaded()
{
try
{
var list = ListAllData.Where(a => a.CheckFLG == 1).ToList();
Notifications = new ObservableCollection<ec_notification>(list);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
#region
/// <summary>
/// 未读
/// </summary>
public void onUnReaded()
{
try
{
var list = ListAllData.Where(a => a.CheckFLG == 0).ToList();
Notifications = new ObservableCollection<ec_notification>(list);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
}
}