From 8fcb105be2c017d96ce4d144dfc1c4bd000bdb39 Mon Sep 17 00:00:00 2001 From: colin Date: Tue, 24 Mar 2026 14:31:27 +0800 Subject: [PATCH] feat: Add user notification query conditions --- .../Dto/UserNotificationGetByPagedDto.cs | 5 +- .../Notifications/MyNotificationAppService.cs | 38 ++++++--- .../EfCoreUserNotificationRepository.cs | 81 +++++++++++++++++-- 3 files changed, 106 insertions(+), 18 deletions(-) diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application.Contracts/LINGYUN/Abp/Notifications/Dto/UserNotificationGetByPagedDto.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application.Contracts/LINGYUN/Abp/Notifications/Dto/UserNotificationGetByPagedDto.cs index 14b9ae89a..e3510ba12 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application.Contracts/LINGYUN/Abp/Notifications/Dto/UserNotificationGetByPagedDto.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application.Contracts/LINGYUN/Abp/Notifications/Dto/UserNotificationGetByPagedDto.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using System; +using System.ComponentModel; using Volo.Abp.Application.Dtos; namespace LINGYUN.Abp.Notifications; @@ -9,4 +10,6 @@ public class UserNotificationGetByPagedDto : PagedAndSortedResultRequestDto [DisplayName("Notifications:State")] public NotificationReadState? ReadState { get; set; } + public DateTime? BeginCreationTime { get; set; } + public DateTime? EndCreationTime { get; set; } } diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application/LINGYUN/Abp/Notifications/MyNotificationAppService.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application/LINGYUN/Abp/Notifications/MyNotificationAppService.cs index 4e5956587..12acdbd1a 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application/LINGYUN/Abp/Notifications/MyNotificationAppService.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.Application/LINGYUN/Abp/Notifications/MyNotificationAppService.cs @@ -1,5 +1,8 @@ using Microsoft.AspNetCore.Authorization; +using System; using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; using System.Threading.Tasks; using Volo.Abp.Application.Dtos; using Volo.Abp.Users; @@ -56,17 +59,34 @@ public class MyNotificationAppService : AbpNotificationsApplicationServiceBase, public async virtual Task> GetListAsync(UserNotificationGetByPagedDto input) { - var totalCount = await UserNotificationRepository - .GetCountAsync( - CurrentUser.GetId(), - input.Filter, - input.ReadState); + Expression> expression = _ => true; + + if (input.ReadState.HasValue) + { + expression = expression.And(x => x.State == input.ReadState); + } + if (input.BeginCreationTime.HasValue) + { + expression = expression.And(x => x.CreationTime >= input.BeginCreationTime); + } + if (input.EndCreationTime.HasValue) + { + expression = expression.And(x => x.CreationTime <= input.EndCreationTime); + } + if (!input.Filter.IsNullOrWhiteSpace()) + { + expression = expression.And(x => + x.Name.Contains(input.Filter) || + x.NotificationTypeName.Contains(input.Filter)); + } + + var specification = new Volo.Abp.Specifications.ExpressionSpecification(expression); + + var userId = CurrentUser.GetId(); + var totalCount = await UserNotificationRepository.GetCountAsync(userId, specification); var notifications = await UserNotificationRepository - .GetListAsync( - CurrentUser.GetId(), - input.Filter, input.Sorting, - input.ReadState, input.SkipCount, input.MaxResultCount); + .GetListAsync(userId, specification, input.Sorting, input.SkipCount, input.MaxResultCount); return new PagedResultDto(totalCount, ObjectMapper.Map, List>(notifications)); diff --git a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreUserNotificationRepository.cs b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreUserNotificationRepository.cs index 077b6956c..651de3ce7 100644 --- a/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreUserNotificationRepository.cs +++ b/aspnet-core/modules/realtime-notifications/LINGYUN.Abp.Notifications.EntityFrameworkCore/LINGYUN/Abp/Notifications/EntityFrameworkCore/EfCoreUserNotificationRepository.cs @@ -1,13 +1,16 @@ using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Linq.Dynamic.Core; using System.Threading; using System.Threading.Tasks; +using System.Xml; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Specifications; namespace LINGYUN.Abp.Notifications.EntityFrameworkCore; @@ -151,6 +154,35 @@ public class EfCoreUserNotificationRepository : EfCoreRepository GetCountAsync( + Guid userId, + ISpecification specification, + CancellationToken cancellationToken = default) + { + var dbContext = await GetDbContextAsync(); + var notifilerQuery = from un in dbContext.Set() + join n in dbContext.Set() + on un.NotificationId equals n.NotificationId + where un.UserId == userId + select new UserNotificationInfo + { + NotificationId = n.NotificationId, + TenantId = n.TenantId, + Name = n.NotificationName, + ExtraProperties = n.ExtraProperties, + CreationTime = n.CreationTime, + NotificationTypeName = n.NotificationTypeName, + Severity = n.Severity, + State = un.ReadStatus, + Type = n.Type, + ContentType = n.ContentType + }; + + return await notifilerQuery + .Where(specification.ToExpression()) + .CountAsync(GetCancellationToken(cancellationToken)); + } + public async virtual Task> GetListAsync( Guid userId, string filter = "", @@ -165,14 +197,6 @@ public class EfCoreUserNotificationRepository : EfCoreRepository() - // .Where(x => x.UserId == userId) - // .WhereIf(readState.HasValue, x => x.ReadStatus == readState.Value); - - //var notificationQuery = dbContext.Set() - // .WhereIf(!filter.IsNullOrWhiteSpace(), nf => - // nf.NotificationName.Contains(filter) || - // nf.NotificationTypeName.Contains(filter)); var notifilerQuery = from un in dbContext.Set() join n in dbContext.Set() @@ -202,4 +226,45 @@ public class EfCoreUserNotificationRepository : EfCoreRepository> GetListAsync( + Guid userId, + ISpecification specification, + string sorting = nameof(Notification.CreationTime), + int skipCount = 0, + int maxResultCount = 10, + CancellationToken cancellationToken = default) + { + if (sorting.IsNullOrWhiteSpace()) + { + sorting = $"{nameof(Notification.CreationTime)} DESC"; + } + var dbContext = await GetDbContextAsync(); + + var notifilerQuery = from un in dbContext.Set() + join n in dbContext.Set() + on un.NotificationId equals n.NotificationId + where un.UserId == userId + select new UserNotificationInfo + { + NotificationId = n.NotificationId, + TenantId = n.TenantId, + Name = n.NotificationName, + ExtraProperties = n.ExtraProperties, + CreationTime = n.CreationTime, + NotificationTypeName = n.NotificationTypeName, + Severity = n.Severity, + State = un.ReadStatus, + Type = n.Type, + ContentType = n.ContentType, + Id = un.Id, + }; + + return await notifilerQuery + .Where(specification.ToExpression()) + .OrderBy(sorting) + .PageBy(skipCount, maxResultCount) + .AsNoTracking() + .ToListAsync(GetCancellationToken(cancellationToken)); + } }