using Abp.Application.Services.Dto; using Abp.Auditing; using Abp.Authorization; using Abp.Configuration; using Abp.Notifications; using Abp.Runtime.Session; using VberZero.AppService.Base; using VberZero.AppService.Base.Dto; using VberZero.AppService.Notifications.Dto; using VberZero.Auditing; using VberZero.DomainService.Notifications; using VberZero.Settings; using VberZero.Tools.Lambda; using VberZero.Tools.StringModel; namespace VberZero.AppService.Notifications; [AbpAuthorize, AuditLog("消息通知管理", "消息")] public class NotificationAppServiceBase : VzAppServiceBase, INotificationAppServiceBase { //private INotificationDefinitionManager NotificationDefinitionManager { get; } private IUserNotificationManager UserNotificationManager { get; } private INotificationSubscriptionManager NotificationSubscriptionManager { get; } private readonly IAppNotifier _appNotifier; public NotificationAppServiceBase( IUserNotificationManager userNotificationManager, INotificationSubscriptionManager notificationSubscriptionManager, IAppNotifier appNotifier, ISettingManager settingManager) { UserNotificationManager = userNotificationManager; NotificationSubscriptionManager = notificationSubscriptionManager; SettingManager = settingManager; _appNotifier = appNotifier; } [DisableAuditing] public async Task GetUserNotificationById(Guid id) { var notification = await UserNotificationManager.GetUserNotificationAsync(AbpSession.TenantId, id); return notification; } [DisableAuditing] public async Task GetUserNotifications(GetUserNotificationsInput input) { var totalCount = await UserNotificationManager.GetUserNotificationCountAsync( AbpSession.ToUserIdentifier(), input.State, input.StartDate, input.EndDate ); var unreadCount = await UserNotificationManager.GetUserNotificationCountAsync( AbpSession.ToUserIdentifier(), UserNotificationState.Unread, input.StartDate, input.EndDate ); var notifications = await UserNotificationManager.GetUserNotificationsAsync( AbpSession.ToUserIdentifier(), input.State, input.SkipCount, input.MaxResultCount, input.StartDate, input.EndDate ); return new GetNotificationsOutput(totalCount, unreadCount, notifications); } [DisableAuditing] public async Task> GetAll(VzPagedRequestDto input) { UserNotificationState? state = null; DateTime? startDate = null, endDate = null; if (input.SearchList.Any()) { foreach (var l in input.SearchList) { if (string.IsNullOrEmpty(l.KeyWords)) { continue; } if (l.KeyField == "state") { state = l.KeyWords == "1" ? UserNotificationState.Read : UserNotificationState.Unread; continue; } if (l.KeyField == "date") { if (l.ExpType == (int)LambdaExpType.GreaterOrEqual) { startDate = Convert.ToDateTime(l.KeyWords); continue; } if (l.ExpType == (int)LambdaExpType.LessOrEqual) { endDate = Convert.ToDateTime(l.KeyWords); } } } } var totalCount = await UserNotificationManager.GetUserNotificationCountAsync(AbpSession.ToUserIdentifier(), state, startDate, endDate); var notifications = await UserNotificationManager.GetUserNotificationsAsync(AbpSession.ToUserIdentifier(), state, input.SkipCount, input.MaxResultCount, startDate, endDate); return new PagedResultDto(totalCount, notifications); } [AuditLog("全部已读")] public async Task SetAllNotificationsAsRead() { await UserNotificationManager.UpdateAllUserNotificationStatesAsync(AbpSession.ToUserIdentifier(), UserNotificationState.Read); } [AuditLog("已读")] public async Task SetNotificationAsRead(EntityDto input) { var userNotification = await UserNotificationManager.GetUserNotificationAsync(AbpSession.TenantId, input.Id); if (userNotification.UserId != AbpSession.GetUserId()) { ThrowError("ThisNotificationDoesntBelongToYou"); } await UserNotificationManager.UpdateUserNotificationStateAsync(AbpSession.TenantId, input.Id, UserNotificationState.Read); } [AuditLog("发送通知")] public async Task SendSysMessage(MsgDto input) { await _appNotifier.SendMsgAsync(VzNotificationNames.SystemMessage, input.Message, input.Severity); } [AuditLog("发送通知")] public async Task SendSysNotification(MsgDto input) { await _appNotifier.SendMsgAsync(VzNotificationNames.SystemNotification, input.Message, input.Severity); } [DisableAuditing] public async Task GetNotificationSettings() { var output = new NotificationSettingsDto { ReceiveNotifications = (await SettingManager.GetSettingValueAsync(VzSettingNames.ReceiveNotifications)).ValB(), //var notificationDefinitions = (await NotificationDefinitionManager.GetAllAvailableAsync(AbpSession.ToUserIdentifier())).Where(nd => nd.EntityType == null); //排除带EntityTyped的实体通知 Notifications = new List() }; foreach (var name in VzNotificationNames.CanBeSubscriptionName) { output.Notifications.Add(new NotificationSubscriptionWithDisplayNameDto() { Name = name, IsSubscribed = false, DisplayName = L("Notification_" + name) }); } var subscribedNotifications = (await NotificationSubscriptionManager .GetSubscribedNotificationsAsync(AbpSession.ToUserIdentifier())) .Select(ns => ns.NotificationName) .ToList(); output.Notifications.ForEach(n => n.IsSubscribed = subscribedNotifications.Contains(n.Name)); return output; } [AuditLog("变更通知设置")] public async Task UpdateNotificationSettings(UpdateNotificationSettingsInput input) { await SettingManager.ChangeSettingForUserAsync(AbpSession.ToUserIdentifier(), VzSettingNames.ReceiveNotifications, input.ReceiveNotifications.ToString()); foreach (var notification in input.Notifications) { if (notification.IsSubscribed) { await NotificationSubscriptionManager.SubscribeAsync(AbpSession.ToUserIdentifier(), notification.Name); } else { await NotificationSubscriptionManager.UnsubscribeAsync(AbpSession.ToUserIdentifier(), notification.Name); } } } [AuditLog("变更通知设置")] public async Task UpdateNotificationSetting(NotificationSubscriptionDto input) { if (input.Name == "receiveNotifications") { await SettingManager.ChangeSettingForUserAsync(AbpSession.ToUserIdentifier(), VzSettingNames.ReceiveNotifications, input.IsSubscribed.ToString()); } else { if (input.IsSubscribed) { await NotificationSubscriptionManager.SubscribeAsync(AbpSession.ToUserIdentifier(), input.Name); } else { await NotificationSubscriptionManager.UnsubscribeAsync(AbpSession.ToUserIdentifier(), input.Name); } } } [AuditLog("删除通知")] public async Task DeleteNotification(EntityDto input) { var notification = await UserNotificationManager.GetUserNotificationAsync(AbpSession.TenantId, input.Id); if (notification.UserId != AbpSession.GetUserId()) { ThrowError("ThisNotificationDoesntBelongToYou"); } await UserNotificationManager.DeleteUserNotificationAsync(AbpSession.TenantId, input.Id); } [AuditLog("删除全部通知")] public async Task DeleteAllUserNotifications(DeleteAllUserNotificationsInput input) { await UserNotificationManager.DeleteAllUserNotificationsAsync(AbpSession.ToUserIdentifier(), input.State, input.StartDate, input.EndDate); } }