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

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
+2 -1
View File
@@ -8,13 +8,14 @@ namespace ZR.ServiceCore.Model.Dto
/// </summary>
public class SysNoticeDto
{
public int NoticeId { get; set; }
public string NoticeId { get; set; }
[Required]
public string NoticeTitle { get; set; }
public int NoticeType { get; set; }
public string NoticeContent { get; set; }
public int Status { get; set; }
public string Remark { get; set; }
public DateTime Create_time { get; set; }
}
/// <summary>
+3 -2
View File
@@ -15,8 +15,9 @@ namespace ZR.ServiceCore.Model
/// <summary>
/// 公告ID
/// </summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "notice_id")]
public int NoticeId { get; set; }
[SugarColumn(IsPrimaryKey = true, ColumnName = "notice_id")]
[JsonConverter(typeof(ValueToStringConverter))]
public long NoticeId { get; set; }
/// <summary>
/// 公告标题
/// </summary>
+13
View File
@@ -0,0 +1,13 @@
namespace ZR.Model.System;
[SugarTable("sys_notice_log")]
public class SysNoticeLog
{
[JsonConverter(typeof(ValueToStringConverter))]
public long NoticeId { get; set; }
[JsonConverter(typeof(ValueToStringConverter))]
public long UserId { get; set; }
public string Status { get; set; }
}
@@ -0,0 +1,9 @@
using ZR.Model.System;
using ZR.Service;
namespace ZR.ServiceCore.Services.IService;
public interface ISysNoticeLogService : IBaseService<SysNoticeLog>
{
}
@@ -0,0 +1,6 @@
namespace ZR.ServiceCore.Services.IService;
public class ISysRoleFieldService
{
}
@@ -0,0 +1,12 @@
using Infrastructure.Attribute;
using ZR.Model.System;
using ZR.Service;
using ZR.ServiceCore.Services.IService;
namespace ZR.ServiceCore.Services;
[AppService(ServiceType = typeof(ISysNoticeLogService), ServiceLifetime = LifeTime.Transient)]
public class SysNoticeLogService : BaseService<SysNoticeLog>, ISysNoticeLogService
{
}
+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();
+13 -12
View File
@@ -1,4 +1,5 @@
using ZR.Common;
using ZR.Common.Cache;
namespace ZR.ServiceCore.SqlSugar
{
@@ -6,32 +7,32 @@ namespace ZR.ServiceCore.SqlSugar
{
public void Add<V>(string key, V value)
{
//RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30));
CacheHelper.SetCache(key, value);
RedisServer.Cache.Set(key, value, 3600 + RedisHelper.RandomExpired(5, 30));
// CacheHelper.SetCache(key, value);
}
public void Add<V>(string key, V value, int cacheDurationInSeconds)
{
//RedisServer.Cache.Set(key, value, cacheDurationInSeconds);
CacheHelper.SetCaches(key, value, cacheDurationInSeconds);
RedisServer.Cache.Set(key, value, cacheDurationInSeconds);
// CacheHelper.SetCaches(key, value, cacheDurationInSeconds);
}
public bool ContainsKey<V>(string key)
{
//return RedisServer.Cache.Exists(key);
return CacheHelper.Exists(key);
return RedisServer.Cache.Exists(key);
// return CacheHelper.Exists(key);
}
public V Get<V>(string key)
{
//return RedisServer.Cache.Get<V>(key);
return (V)CacheHelper.Get(key);
return RedisServer.Cache.Get<V>(key);
// return (V)CacheHelper.Get(key);
}
public IEnumerable<string> GetAllKey<V>()
{
//return RedisServer.Cache.Keys("*");
return CacheHelper.GetCacheKeys();
return RedisServer.Cache.Keys("*");
// return CacheHelper.GetCacheKeys();
}
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
@@ -59,8 +60,8 @@ namespace ZR.ServiceCore.SqlSugar
public void Remove<V>(string key)
{
//RedisServer.Cache.Del(key);
CacheHelper.Remove(key);
RedisServer.Cache.Del(key);
// CacheHelper.Remove(key);
}
}
}