NotificationAppServiceBase.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using Abp.Application.Services.Dto;
  2. using Abp.Auditing;
  3. using Abp.Authorization;
  4. using Abp.Configuration;
  5. using Abp.Notifications;
  6. using Abp.Runtime.Session;
  7. using VberZero.AppService.Base;
  8. using VberZero.AppService.Base.Dto;
  9. using VberZero.AppService.Notifications.Dto;
  10. using VberZero.Auditing;
  11. using VberZero.DomainService.Notifications;
  12. using VberZero.Settings;
  13. using VberZero.Tools.Lambda;
  14. using VberZero.Tools.StringModel;
  15. namespace VberZero.AppService.Notifications;
  16. [AbpAuthorize, AuditLog("消息通知管理", "消息")]
  17. public class NotificationAppServiceBase : VzAppServiceBase, INotificationAppServiceBase
  18. {
  19. //private INotificationDefinitionManager NotificationDefinitionManager { get; }
  20. private IUserNotificationManager UserNotificationManager { get; }
  21. private INotificationSubscriptionManager NotificationSubscriptionManager { get; }
  22. private readonly IAppNotifier _appNotifier;
  23. public NotificationAppServiceBase(
  24. IUserNotificationManager userNotificationManager,
  25. INotificationSubscriptionManager notificationSubscriptionManager, IAppNotifier appNotifier, ISettingManager settingManager)
  26. {
  27. UserNotificationManager = userNotificationManager;
  28. NotificationSubscriptionManager = notificationSubscriptionManager;
  29. SettingManager = settingManager;
  30. _appNotifier = appNotifier;
  31. }
  32. [DisableAuditing]
  33. public async Task<UserNotification> GetUserNotificationById(Guid id)
  34. {
  35. var notification = await UserNotificationManager.GetUserNotificationAsync(AbpSession.TenantId, id);
  36. return notification;
  37. }
  38. [DisableAuditing]
  39. public async Task<GetNotificationsOutput> GetUserNotifications(GetUserNotificationsInput input)
  40. {
  41. var totalCount = await UserNotificationManager.GetUserNotificationCountAsync(
  42. AbpSession.ToUserIdentifier(), input.State, input.StartDate, input.EndDate
  43. );
  44. var unreadCount = await UserNotificationManager.GetUserNotificationCountAsync(
  45. AbpSession.ToUserIdentifier(), UserNotificationState.Unread, input.StartDate, input.EndDate
  46. );
  47. var notifications = await UserNotificationManager.GetUserNotificationsAsync(
  48. AbpSession.ToUserIdentifier(), input.State, input.SkipCount, input.MaxResultCount, input.StartDate, input.EndDate
  49. );
  50. return new GetNotificationsOutput(totalCount, unreadCount, notifications);
  51. }
  52. [DisableAuditing]
  53. public async Task<PagedResultDto<UserNotification>> GetAll(VzPagedRequestDto input)
  54. {
  55. UserNotificationState? state = null;
  56. DateTime? startDate = null, endDate = null;
  57. if (input.SearchList.Any())
  58. {
  59. foreach (var l in input.SearchList)
  60. {
  61. if (string.IsNullOrEmpty(l.KeyWords))
  62. {
  63. continue;
  64. }
  65. if (l.KeyField == "state")
  66. {
  67. state = l.KeyWords == "1" ? UserNotificationState.Read : UserNotificationState.Unread;
  68. continue;
  69. }
  70. if (l.KeyField == "date")
  71. {
  72. if (l.ExpType == (int)LambdaExpType.GreaterOrEqual)
  73. {
  74. startDate = Convert.ToDateTime(l.KeyWords);
  75. continue;
  76. }
  77. if (l.ExpType == (int)LambdaExpType.LessOrEqual)
  78. {
  79. endDate = Convert.ToDateTime(l.KeyWords);
  80. }
  81. }
  82. }
  83. }
  84. var totalCount = await UserNotificationManager.GetUserNotificationCountAsync(AbpSession.ToUserIdentifier(), state, startDate, endDate);
  85. var notifications = await UserNotificationManager.GetUserNotificationsAsync(AbpSession.ToUserIdentifier(), state, input.SkipCount, input.MaxResultCount, startDate, endDate);
  86. return new PagedResultDto<UserNotification>(totalCount, notifications);
  87. }
  88. [AuditLog("全部已读")]
  89. public async Task SetAllNotificationsAsRead()
  90. {
  91. await UserNotificationManager.UpdateAllUserNotificationStatesAsync(AbpSession.ToUserIdentifier(), UserNotificationState.Read);
  92. }
  93. [AuditLog("已读")]
  94. public async Task SetNotificationAsRead(EntityDto<Guid> input)
  95. {
  96. var userNotification = await UserNotificationManager.GetUserNotificationAsync(AbpSession.TenantId, input.Id);
  97. if (userNotification.UserId != AbpSession.GetUserId())
  98. {
  99. ThrowError("ThisNotificationDoesntBelongToYou");
  100. }
  101. await UserNotificationManager.UpdateUserNotificationStateAsync(AbpSession.TenantId, input.Id, UserNotificationState.Read);
  102. }
  103. [AuditLog("发送通知")]
  104. public async Task SendSysMessage(MsgDto input)
  105. {
  106. await _appNotifier.SendMsgAsync(VzNotificationNames.SystemMessage, input.Message, input.Severity);
  107. }
  108. [AuditLog("发送通知")]
  109. public async Task SendSysNotification(MsgDto input)
  110. {
  111. await _appNotifier.SendMsgAsync(VzNotificationNames.SystemNotification, input.Message, input.Severity);
  112. }
  113. [DisableAuditing]
  114. public async Task<NotificationSettingsDto> GetNotificationSettings()
  115. {
  116. var output = new NotificationSettingsDto
  117. {
  118. ReceiveNotifications =
  119. (await SettingManager.GetSettingValueAsync(VzSettingNames.ReceiveNotifications)).ValB(),
  120. //var notificationDefinitions = (await NotificationDefinitionManager.GetAllAvailableAsync(AbpSession.ToUserIdentifier())).Where(nd => nd.EntityType == null);
  121. //排除带EntityTyped的实体通知
  122. Notifications = new List<NotificationSubscriptionWithDisplayNameDto>()
  123. };
  124. foreach (var name in VzNotificationNames.CanBeSubscriptionName)
  125. {
  126. output.Notifications.Add(new NotificationSubscriptionWithDisplayNameDto()
  127. {
  128. Name = name,
  129. IsSubscribed = false,
  130. DisplayName = L("Notification_" + name)
  131. });
  132. }
  133. var subscribedNotifications = (await NotificationSubscriptionManager
  134. .GetSubscribedNotificationsAsync(AbpSession.ToUserIdentifier()))
  135. .Select(ns => ns.NotificationName)
  136. .ToList();
  137. output.Notifications.ForEach(n => n.IsSubscribed = subscribedNotifications.Contains(n.Name));
  138. return output;
  139. }
  140. [AuditLog("变更通知设置")]
  141. public async Task UpdateNotificationSettings(UpdateNotificationSettingsInput input)
  142. {
  143. await SettingManager.ChangeSettingForUserAsync(AbpSession.ToUserIdentifier(), VzSettingNames.ReceiveNotifications, input.ReceiveNotifications.ToString());
  144. foreach (var notification in input.Notifications)
  145. {
  146. if (notification.IsSubscribed)
  147. {
  148. await NotificationSubscriptionManager.SubscribeAsync(AbpSession.ToUserIdentifier(), notification.Name);
  149. }
  150. else
  151. {
  152. await NotificationSubscriptionManager.UnsubscribeAsync(AbpSession.ToUserIdentifier(), notification.Name);
  153. }
  154. }
  155. }
  156. [AuditLog("变更通知设置")]
  157. public async Task UpdateNotificationSetting(NotificationSubscriptionDto input)
  158. {
  159. if (input.Name == "receiveNotifications")
  160. {
  161. await SettingManager.ChangeSettingForUserAsync(AbpSession.ToUserIdentifier(), VzSettingNames.ReceiveNotifications, input.IsSubscribed.ToString());
  162. }
  163. else
  164. {
  165. if (input.IsSubscribed)
  166. {
  167. await NotificationSubscriptionManager.SubscribeAsync(AbpSession.ToUserIdentifier(), input.Name);
  168. }
  169. else
  170. {
  171. await NotificationSubscriptionManager.UnsubscribeAsync(AbpSession.ToUserIdentifier(), input.Name);
  172. }
  173. }
  174. }
  175. [AuditLog("删除通知")]
  176. public async Task DeleteNotification(EntityDto<Guid> input)
  177. {
  178. var notification = await UserNotificationManager.GetUserNotificationAsync(AbpSession.TenantId, input.Id);
  179. if (notification.UserId != AbpSession.GetUserId())
  180. {
  181. ThrowError("ThisNotificationDoesntBelongToYou");
  182. }
  183. await UserNotificationManager.DeleteUserNotificationAsync(AbpSession.TenantId, input.Id);
  184. }
  185. [AuditLog("删除全部通知")]
  186. public async Task DeleteAllUserNotifications(DeleteAllUserNotificationsInput input)
  187. {
  188. await UserNotificationManager.DeleteAllUserNotificationsAsync(AbpSession.ToUserIdentifier(), input.State, input.StartDate, input.EndDate);
  189. }
  190. }