| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- 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<UserNotification> GetUserNotificationById(Guid id)
- {
- var notification = await UserNotificationManager.GetUserNotificationAsync(AbpSession.TenantId, id);
- return notification;
- }
- [DisableAuditing]
- public async Task<GetNotificationsOutput> 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<PagedResultDto<UserNotification>> 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<UserNotification>(totalCount, notifications);
- }
- [AuditLog("全部已读")]
- public async Task SetAllNotificationsAsRead()
- {
- await UserNotificationManager.UpdateAllUserNotificationStatesAsync(AbpSession.ToUserIdentifier(), UserNotificationState.Read);
- }
- [AuditLog("已读")]
- public async Task SetNotificationAsRead(EntityDto<Guid> 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<NotificationSettingsDto> 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<NotificationSubscriptionWithDisplayNameDto>()
- };
- 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<Guid> 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);
- }
- }
|