NotificationService.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Abp.Application.Services.Dto;
  6. using Abp.Auditing;
  7. using Abp.Authorization;
  8. using Abp.Configuration;
  9. using Abp.Notifications;
  10. using Abp.Runtime.Session;
  11. using WeApp.BaseSystem.Notifications.Dto;
  12. using WeApp.Configuration;
  13. using IwbZero.AppServiceBase;
  14. using IwbZero.Auditing;
  15. using IwbZero.ToolCommon.Lambda;
  16. namespace WeApp.BaseSystem.Notifications
  17. {
  18. [AbpAuthorize, AuditLog("通知")]
  19. public class NotificationAppService : IwbAppServiceBase, INotificationAppService
  20. {
  21. private INotificationDefinitionManager NotificationDefinitionManager { get; }
  22. private IUserNotificationManager UserNotificationManager { get; }
  23. private INotificationSubscriptionManager NotificationSubscriptionManager { get; }
  24. public NotificationAppService(
  25. INotificationDefinitionManager notificationDefinitionManager,
  26. IUserNotificationManager userNotificationManager,
  27. INotificationSubscriptionManager notificationSubscriptionManager)
  28. {
  29. NotificationDefinitionManager = notificationDefinitionManager;
  30. UserNotificationManager = userNotificationManager;
  31. NotificationSubscriptionManager = notificationSubscriptionManager;
  32. }
  33. [DisableAuditing]
  34. public async Task<GetNotificationsOutput> GetUserNotifications(GetUserNotificationsInput input)
  35. {
  36. var totalCount = await UserNotificationManager.GetUserNotificationCountAsync(
  37. AbpSession.ToUserIdentifier(), input.State, input.StartDate, input.EndDate
  38. );
  39. var unreadCount = await UserNotificationManager.GetUserNotificationCountAsync(
  40. AbpSession.ToUserIdentifier(), UserNotificationState.Unread, input.StartDate, input.EndDate
  41. );
  42. var notifications = await UserNotificationManager.GetUserNotificationsAsync(
  43. AbpSession.ToUserIdentifier(), input.State, input.SkipCount, input.MaxResultCount, input.StartDate, input.EndDate
  44. );
  45. return new GetNotificationsOutput(totalCount, unreadCount, notifications);
  46. }
  47. public async Task<PagedResultDto<UserNotification>> GetAll(IwbPagedRequestDto input)
  48. {
  49. UserNotificationState? state = null;
  50. DateTime? startDate = null, endDate = null;
  51. if (input.SearchList.Any())
  52. {
  53. foreach (var l in input.SearchList)
  54. {
  55. if (string.IsNullOrEmpty(l.KeyWords))
  56. {
  57. continue;
  58. }
  59. if (l.KeyField == "state")
  60. {
  61. state = l.KeyWords == "1" ? UserNotificationState.Read : UserNotificationState.Unread;
  62. continue;
  63. }
  64. if (l.KeyField == "date")
  65. {
  66. if (l.ExpType == (int)LambdaExpType.GreaterOrEqual)
  67. {
  68. startDate = Convert.ToDateTime(l.KeyWords);
  69. continue;
  70. }
  71. if (l.ExpType == (int)LambdaExpType.LessOrEqual)
  72. {
  73. endDate = Convert.ToDateTime(l.KeyWords);
  74. }
  75. }
  76. }
  77. }
  78. var totalCount = await UserNotificationManager.GetUserNotificationCountAsync(
  79. AbpSession.ToUserIdentifier(), state, startDate, endDate
  80. );
  81. var notifications = await UserNotificationManager.GetUserNotificationsAsync(
  82. AbpSession.ToUserIdentifier(), state, input.SkipCount, input.MaxResultCount, startDate, endDate
  83. );
  84. return new PagedResultDto<UserNotification>(totalCount, notifications);
  85. }
  86. [AuditLog("全部已读")]
  87. public async Task SetAllNotificationsAsRead()
  88. {
  89. await UserNotificationManager.UpdateAllUserNotificationStatesAsync(AbpSession.ToUserIdentifier(), UserNotificationState.Read);
  90. }
  91. [AuditLog("设置已读")]
  92. public async Task SetNotificationAsRead(EntityDto<Guid> input)
  93. {
  94. var userNotification = await UserNotificationManager.GetUserNotificationAsync(AbpSession.TenantId, input.Id);
  95. if (userNotification.UserId != AbpSession.GetUserId())
  96. {
  97. ThrowError(IwbLanguageMessage.NotificationNotBelong);
  98. }
  99. await UserNotificationManager.UpdateUserNotificationStateAsync(AbpSession.TenantId, input.Id, UserNotificationState.Read);
  100. }
  101. [DisableAuditing]
  102. public async Task<GetNotificationSettingsOutput> GetNotificationSettings()
  103. {
  104. var output = new GetNotificationSettingsOutput
  105. {
  106. ReceiveNotifications =
  107. await SettingManager.GetSettingValueAsync<bool>(IwbSettingNames.ReceiveNotifications)
  108. };
  109. //排除带EntityTyped的实体通知
  110. var notificationDefinitions = (await NotificationDefinitionManager.GetAllAvailableAsync(AbpSession.ToUserIdentifier())).Where(nd => nd.EntityType == null);
  111. output.Notifications = ObjectMapper.Map<List<NotificationSubscriptionWithDisplayNameDto>>(notificationDefinitions);
  112. var subscribedNotifications = (await NotificationSubscriptionManager
  113. .GetSubscribedNotificationsAsync(AbpSession.ToUserIdentifier()))
  114. .Select(ns => ns.NotificationName)
  115. .ToList();
  116. output.Notifications.ForEach(n => n.IsSubscribed = subscribedNotifications.Contains(n.Name));
  117. return output;
  118. }
  119. [AuditLog("修改订阅通知")]
  120. public async Task UpdateNotificationSettings(UpdateNotificationSettingsInput input)
  121. {
  122. await SettingManager.ChangeSettingForUserAsync(AbpSession.ToUserIdentifier(), IwbSettingNames.ReceiveNotifications, input.ReceiveNotifications.ToString());
  123. foreach (var notification in input.Notifications)
  124. {
  125. if (notification.IsSubscribed)
  126. {
  127. await NotificationSubscriptionManager.SubscribeAsync(AbpSession.ToUserIdentifier(), notification.Name);
  128. }
  129. else
  130. {
  131. await NotificationSubscriptionManager.UnsubscribeAsync(AbpSession.ToUserIdentifier(), notification.Name);
  132. }
  133. }
  134. }
  135. [AuditLog("删除消息")]
  136. public async Task DeleteNotification(EntityDto<Guid> input)
  137. {
  138. var notification = await UserNotificationManager.GetUserNotificationAsync(AbpSession.TenantId, input.Id);
  139. if (notification.UserId != AbpSession.GetUserId())
  140. {
  141. ThrowError(IwbLanguageMessage.NotificationNotBelong);
  142. }
  143. await UserNotificationManager.DeleteUserNotificationAsync(AbpSession.TenantId, input.Id);
  144. }
  145. [AuditLog("删除全部消息")]
  146. public async Task DeleteAllUserNotifications(DeleteAllUserNotificationsInput input)
  147. {
  148. await UserNotificationManager.DeleteAllUserNotificationsAsync(
  149. AbpSession.ToUserIdentifier(),
  150. input.State,
  151. input.StartDate,
  152. input.EndDate);
  153. }
  154. }
  155. }