完善系统通知,增加系统通知日志记录已读和未读通知

This commit is contained in:
2023-09-28 14:49:16 +08:00
parent b932f6f6a0
commit 1c08c837e6
11 changed files with 237 additions and 36 deletions
+134 -2
View File
@@ -3,8 +3,14 @@ using Infrastructure.Model;
using IPTools.Core;
using Microsoft.AspNetCore.SignalR;
using System.Web;
using Mapster;
using UAParser;
using ZR.Infrastructure.Constant;
using ZR.Model.System;
using ZR.Service.System.IService;
using ZR.ServiceCore.Model;
using ZR.ServiceCore.Model.Dto;
using ZR.ServiceCore.Services.IService;
namespace ZR.ServiceCore.Signalr
{
@@ -18,10 +24,12 @@ namespace ZR.ServiceCore.Signalr
public static List<OnlineUsers> users = new();
//private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly ISysNoticeService SysNoticeService;
private readonly ISysNoticeLogService _sysNoticeLogService;
public MessageHub(ISysNoticeService noticeService)
public MessageHub(ISysNoticeService noticeService, ISysNoticeLogService sysNoticeLogService)
{
SysNoticeService = noticeService;
_sysNoticeLogService = sysNoticeLogService;
}
private ApiResult SendNotice()
@@ -68,7 +76,55 @@ namespace ZR.ServiceCore.Signalr
onlineClients.Add(onlineUser);
Log.WriteLine(msg: $"{name},{Context.ConnectionId}连接服务端success,当前已连接{onlineClients.Count}个");
//Clients.All.SendAsync("welcome", $"欢迎您:{name},当前时间:{DateTime.Now}");
Clients.Caller.SendAsync(HubsConstant.MoreNotice, SendNotice());
var noticeRes = (List<SysNotice>) SendNotice()[ApiResult.DATA_TAG];
var notifications = _sysNoticeLogService.Queryable()
.Where(it => it.UserId == userid)
.ToList();
var unreadNotificationIds = notifications
.Where(it => it.Status == SysNoticeLogStatus.Unread)
.Select(it => it.NoticeId)
.ToList();
var readNotificationIds = notifications
.Where(it => it.Status == SysNoticeLogStatus.Read)
.Select(it => it.NoticeId)
.ToList();
var unreadAndReadNoticeIds = unreadNotificationIds
.Union(readNotificationIds).ToList();
foreach (var notice in noticeRes.Select(it => it.NoticeId).ToList().Except(unreadAndReadNoticeIds))
{
_sysNoticeLogService.Insertable(new SysNoticeLog
{
NoticeId = notice,
UserId = userid,
Status = SysNoticeLogStatus.Unread
}).ExecuteCommand();
}
foreach (var notice in unreadAndReadNoticeIds.Except(noticeRes.Select(it => it.NoticeId).ToList()))
{
_sysNoticeLogService.Deleteable()
.Where(it => it.UserId == userid && it.NoticeId == notice)
.ExecuteCommand();
}
var newUnReadNotificationIds = _sysNoticeLogService
.Queryable().Where(it => it.Status == SysNoticeLogStatus.Unread && it.UserId == userid)
.Select(it => it.NoticeId)
.ToList();
var newReadNotificationIds = _sysNoticeLogService
.Queryable().Where(it => it.Status == SysNoticeLogStatus.Read && it.UserId == userid)
.Select(it => it.NoticeId)
.ToList();
var config = new TypeAdapterConfig();
config.ForType<SysNotice, SysNoticeDto>()
.Map(dest => dest.NoticeId, src => src.NoticeId.ToString());
var newUnReadNotifications = noticeRes.Where(it =>
newUnReadNotificationIds.Contains(it.NoticeId)).ToList().Adapt<List<SysNoticeDto>>(config);
var newReadNotifications = noticeRes.Where(it =>
newReadNotificationIds.Contains(it.NoticeId)).ToList().Adapt<List<SysNoticeDto>>(config);
Clients.Caller.SendAsync(HubsConstant.MoreNotice, newUnReadNotifications, newReadNotifications);
// Clients.Caller.SendAsync(HubsConstant.MoreNotice, SendNotice());
// Clients.Caller.SendAsync(HubsConstant.ConnId, onlineUser.ConnnectionId);
}
OnlineUsers userInfo = GetUserById(userid);
@@ -174,6 +230,82 @@ namespace ZR.ServiceCore.Signalr
Console.WriteLine($"用户{userName}对{toConnectId}-{toUserId}说:{message}");
}
/// <summary>
/// 全部标为已读
/// </summary>
[HubMethodName("AllReadNotice")]
public async Task AllReadNotice()
{
var userId = HttpContextExtension.GetUId(App.HttpContext);
var unreadNotificationIds = await _sysNoticeLogService.Queryable()
.Where(it => it.Status == SysNoticeLogStatus.Unread && it.UserId == userId)
.Select(it => it.NoticeId)
.ToListAsync();
foreach (var notice in unreadNotificationIds)
{
await _sysNoticeLogService.Updateable(new SysNoticeLog
{
Status = SysNoticeLogStatus.Read
})
.IgnoreColumns(it => new { it.NoticeId, it.UserId })
.Where(it => it.UserId == userId && it.NoticeId == notice
&& it.Status == SysNoticeLogStatus.Unread)
.ExecuteCommandAsync();
}
var newNotifications = await _sysNoticeLogService.Queryable()
.Where(it => it.UserId == userId)
.ToListAsync();
var newUnReadNotificationIds = newNotifications
.Where(it => it.Status == SysNoticeLogStatus.Unread)
.Select(it => it.NoticeId)
.ToList();
var newReadNotificationIds = newNotifications
.Where(it => it.Status == SysNoticeLogStatus.Read)
.Select(it => it.NoticeId)
.ToList();
var noticeRes = (List<SysNotice>) SendNotice()[ApiResult.DATA_TAG];
var newUnReadNotifications = noticeRes.Where(it =>
newUnReadNotificationIds.Contains(it.NoticeId)).ToList();
var newReadNotifications = noticeRes.Where(it =>
newReadNotificationIds.Contains(it.NoticeId)).ToList();
await Clients.Caller.SendAsync(HubsConstant.MoreNotice, newUnReadNotifications, newReadNotifications);
}
/// <summary>
/// 标记已读
/// </summary>
/// <param name="noticeId"></param>
[HubMethodName("ReadNotice")]
public async Task ReadNotice(string noticeId)
{
var userid = HttpContextExtension.GetUId(App.HttpContext);
await _sysNoticeLogService.Updateable(new SysNoticeLog
{
Status = SysNoticeLogStatus.Read
})
.IgnoreColumns(it => new { it.NoticeId, it.UserId })
.Where(it => it.NoticeId == noticeId.ParseToLong() && it.UserId == userid
&& it.Status == SysNoticeLogStatus.Unread)
.ExecuteCommandAsync();
var newNotifications = await _sysNoticeLogService.Queryable()
.Where(it => it.UserId == userid)
.ToListAsync();
var newUnReadNotificationIds = newNotifications
.Where(it => it.Status == SysNoticeLogStatus.Unread)
.Select(it => it.NoticeId)
.ToList();
var newReadNotificationIds = newNotifications
.Where(it => it.Status == SysNoticeLogStatus.Read)
.Select(it => it.NoticeId)
.ToList();
var noticeRes = (List<SysNotice>) SendNotice()[ApiResult.DATA_TAG];
var newUnReadNotifications = noticeRes.Where(it =>
newUnReadNotificationIds.Contains(it.NoticeId)).ToList();
var newReadNotifications = noticeRes.Where(it =>
newReadNotificationIds.Contains(it.NoticeId)).ToList();
await Clients.Caller.SendAsync(HubsConstant.MoreNotice, newUnReadNotifications, newReadNotifications);
}
private OnlineUsers GetUserByConnId(string connId)
{
return onlineClients.Where(p => p.ConnnectionId == connId).FirstOrDefault();