VisitService.cs 3.7 KB

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