| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Abp.Application.Services.Dto;
- using Abp.Auditing;
- using Abp.Authorization;
- using Abp.Configuration;
- using Abp.Notifications;
- using Abp.Runtime.Session;
- using WeApp.BaseSystem.Notifications.Dto;
- using WeApp.Configuration;
- using IwbZero.AppServiceBase;
- using IwbZero.Auditing;
- using IwbZero.ToolCommon.Lambda;
- namespace WeApp.BaseSystem.Notifications
- {
- [AbpAuthorize, AuditLog("通知")]
- public class NotificationAppService : IwbAppServiceBase, INotificationAppService
- {
- private INotificationDefinitionManager NotificationDefinitionManager { get; }
- private IUserNotificationManager UserNotificationManager { get; }
- private INotificationSubscriptionManager NotificationSubscriptionManager { get; }
- public NotificationAppService(
- INotificationDefinitionManager notificationDefinitionManager,
- IUserNotificationManager userNotificationManager,
- INotificationSubscriptionManager notificationSubscriptionManager)
- {
- NotificationDefinitionManager = notificationDefinitionManager;
- UserNotificationManager = userNotificationManager;
- NotificationSubscriptionManager = notificationSubscriptionManager;
- }
- [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);
- }
- public async Task<PagedResultDto<UserNotification>> GetAll(IwbPagedRequestDto 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(IwbLanguageMessage.NotificationNotBelong);
- }
- await UserNotificationManager.UpdateUserNotificationStateAsync(AbpSession.TenantId, input.Id, UserNotificationState.Read);
- }
- [DisableAuditing]
- public async Task<GetNotificationSettingsOutput> GetNotificationSettings()
- {
- var output = new GetNotificationSettingsOutput
- {
- ReceiveNotifications =
- await SettingManager.GetSettingValueAsync<bool>(IwbSettingNames.ReceiveNotifications)
- };
- //排除带EntityTyped的实体通知
- var notificationDefinitions = (await NotificationDefinitionManager.GetAllAvailableAsync(AbpSession.ToUserIdentifier())).Where(nd => nd.EntityType == null);
- output.Notifications = ObjectMapper.Map<List<NotificationSubscriptionWithDisplayNameDto>>(notificationDefinitions);
- 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(), IwbSettingNames.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 DeleteNotification(EntityDto<Guid> input)
- {
- var notification = await UserNotificationManager.GetUserNotificationAsync(AbpSession.TenantId, input.Id);
- if (notification.UserId != AbpSession.GetUserId())
- {
- ThrowError(IwbLanguageMessage.NotificationNotBelong);
- }
- 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);
- }
- }
- }
|