VisitService.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Threading.Tasks;
  2. using Datory;
  3. using GxPress.Entity.Topic;
  4. using GxPress.EnumConst;
  5. using GxPress.Repository.Interface;
  6. using GxPress.Repository.Interface.Note;
  7. using GxPress.Repository.Interface.Visit;
  8. using GxPress.Service.Interface.Visit;
  9. namespace GxPress.Service.Implement.Visit
  10. {
  11. /// <summary>
  12. /// 访问服务
  13. /// </summary>
  14. public class VisitService : IVisitService
  15. {
  16. private readonly IVisitRepository _visitRepository;
  17. private readonly ITopicRepository _topicRepository;
  18. private readonly INoteRepository _noteRepository;
  19. private readonly ITopicAddresseeRepository _topicAddresseeRepository;
  20. private readonly IArticleRepository _articleRepository;
  21. public VisitService(IVisitRepository visitRepository, ITopicRepository topicRepository, INoteRepository noteRepository, ITopicAddresseeRepository topicAddresseeRepository, IArticleRepository articleRepository)
  22. {
  23. _visitRepository = visitRepository;
  24. _topicRepository = topicRepository;
  25. _noteRepository = noteRepository;
  26. _topicAddresseeRepository = topicAddresseeRepository;
  27. _articleRepository = articleRepository;
  28. }
  29. /// <summary>
  30. /// /// 添加/数据条数
  31. /// </summary>
  32. /// <param name="userId"></param>
  33. /// <param name="typeId">类型ID 类型1文章 2话题 3 收藏 4笔记 5通知 6站内信 7小组 8 会议详情</param>
  34. /// <param name="sourceId"></param>
  35. /// <returns></returns>
  36. public async Task<bool> AddVisit(int userId, int typeId, int sourceId)
  37. {
  38. var visitBool = await _visitRepository.ExistsAsync(Q.Where(nameof(Entity.Visit.Visit.TypeId), typeId).Where(nameof(Entity.Visit.Visit.SourceId), sourceId).Where(nameof(Entity.Visit.Visit.UserId), userId));
  39. if (visitBool == false)
  40. {
  41. var visit = new Entity.Visit.Visit();
  42. visit.TypeId = typeId;
  43. visit.UserId = userId;
  44. visit.SourceId = sourceId;
  45. await _visitRepository.InsertAsync(visit);
  46. //修改访问量数量
  47. if (typeId == AllTypeConst.Article.GetHashCode())
  48. {
  49. var article = await _articleRepository.GetAsync(sourceId);
  50. article.ReadCount++;
  51. await _articleRepository.UpdateAsync(article);
  52. }
  53. //修改访问量数量
  54. if (typeId == AllTypeConst.Topic.GetHashCode())
  55. {
  56. var topic = await _topicRepository.GetAsync(sourceId);
  57. topic.ReadCount++;
  58. await _topicRepository.UpdateAsync(topic);
  59. //修改
  60. await _topicAddresseeRepository.UpdateAsync(Q.Set(nameof(TopicAddressee.IsRead), true)
  61. .Where(nameof(TopicAddressee.TopicId), sourceId)
  62. .Where(nameof(TopicAddressee.UserId), userId));
  63. }
  64. if (typeId == AllTypeConst.Note.GetHashCode())
  65. {
  66. var note = await _noteRepository.GetAsync(sourceId);
  67. note.ReadCount++;
  68. await _noteRepository.UpdateAsync(note);
  69. }
  70. if (typeId == AllTypeConst.TopicNote.GetHashCode())
  71. {
  72. var note = await _noteRepository.GetAsync(sourceId);
  73. note.ReadCount++;
  74. await _noteRepository.UpdateAsync(note);
  75. }
  76. return true;
  77. }
  78. return false;
  79. }
  80. /// <summary>
  81. /// 获取访问数量
  82. /// </summary>
  83. /// <param name="typeId"></param>
  84. /// <param name="sourceId"></param>
  85. /// <returns></returns>
  86. public async Task<int> GetCountAsync(int typeId, int sourceId)
  87. {
  88. return await _visitRepository.CountAsync(Q.Where(nameof(Entity.Visit.Visit.TypeId), typeId).Where(nameof(Entity.Visit.Visit.SourceId), sourceId));
  89. }
  90. }
  91. }