NotificationDomainService.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Exam.Infrastructure.Extensions;
  2. using Hotline.Share.Dtos.Snapshot;
  3. using Hotline.Snapshot.Contracts;
  4. using Hotline.Snapshot.IRepository;
  5. using Mapster;
  6. using Microsoft.Extensions.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using XF.Domain.Dependency;
  13. namespace Hotline.Snapshot.Services;
  14. public class NotificationDomainService : INotificationDomainService, IScopeDependency
  15. {
  16. private readonly INotificationRepository _notificationRepository;
  17. private readonly INotificationReceiverRepository _notificationReceiverRepository;
  18. private readonly ILogger<NotificationDomainService> _logger;
  19. public NotificationDomainService(INotificationRepository notificationRepository, INotificationReceiverRepository notificationReceiverRepository, ILogger<NotificationDomainService> logger)
  20. {
  21. _notificationRepository = notificationRepository;
  22. _notificationReceiverRepository = notificationReceiverRepository;
  23. _logger = logger;
  24. }
  25. public async Task AddNotifyAsync(AddNotifyInDto inDto, CancellationToken token)
  26. {
  27. var entity = inDto.Adapt<Notification>();
  28. var hasOld = await _notificationRepository.Queryable()
  29. .Where(m => m.ExternalId == inDto.ExternalId)
  30. .AnyAsync();
  31. if (hasOld)
  32. {
  33. _logger.LogInformation("通知消息已存在, 不重复通知用户. " + inDto.ToJson());
  34. return;
  35. }
  36. var id = await _notificationRepository.AddAsync(entity, token);
  37. var items = new List<NotificationReceiver>();
  38. foreach (var userId in inDto.UserIds)
  39. {
  40. items.Add(new NotificationReceiver {
  41. NotificationId = id,
  42. ReceiverId = userId,
  43. IsRead = false
  44. });
  45. }
  46. await _notificationReceiverRepository.AddRangeAsync(items, token);
  47. }
  48. }