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 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> 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(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(IwbLanguageMessage.NotificationNotBelong); } await UserNotificationManager.UpdateUserNotificationStateAsync(AbpSession.TenantId, input.Id, UserNotificationState.Read); } [DisableAuditing] public async Task GetNotificationSettings() { var output = new GetNotificationSettingsOutput { ReceiveNotifications = await SettingManager.GetSettingValueAsync(IwbSettingNames.ReceiveNotifications) }; //排除带EntityTyped的实体通知 var notificationDefinitions = (await NotificationDefinitionManager.GetAllAvailableAsync(AbpSession.ToUserIdentifier())).Where(nd => nd.EntityType == null); output.Notifications = ObjectMapper.Map>(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 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); } } }