PressService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using GxPress.Service.Interface.Press;
  2. using GxPress.Repository.Interface.Press;
  3. using System.Threading.Tasks;
  4. using GxPress.Result.Press;
  5. using GxPress.EnumConst;
  6. using GxPress.Service.Interface.Analyze;
  7. using GxPress.Service.Interface.VisitHistory;
  8. using GxPress.Request.Press;
  9. using System.Linq;
  10. using GxPress.Common.Tools;
  11. using GxPress.Repository.Interface.Teacher;
  12. using GxPress.Request.TeacherRequest;
  13. using GxPress.Repository.Interface.Media;
  14. using System.Collections.Generic;
  15. namespace GxPress.Service.Implement.Press
  16. {
  17. public class PressService : IPressService
  18. {
  19. private readonly IPressRepository pressRepository;
  20. private readonly IAnalyzeService _analyzeService;
  21. private readonly IVisitHistoryService visitHistoryService;
  22. private readonly ITeacherRepository teacherRepository;
  23. private readonly IMediaRepository mediaRepository;
  24. public PressService(IPressRepository pressRepository, IAnalyzeService _analyzeService,
  25. IVisitHistoryService visitHistoryService, ITeacherRepository teacherRepository,
  26. IMediaRepository mediaRepository)
  27. {
  28. this.pressRepository = pressRepository;
  29. this._analyzeService = _analyzeService;
  30. this.visitHistoryService = visitHistoryService;
  31. this.teacherRepository = teacherRepository;
  32. this.mediaRepository = mediaRepository;
  33. }
  34. /// <summary>
  35. /// 根据ID获取组织
  36. /// </summary>
  37. /// <param name="id"></param>
  38. /// <returns></returns>
  39. public async Task<PressResult> GetPressAsync(int id, int userId)
  40. {
  41. var result = await pressRepository.GetPressAsync(id);
  42. //获取老师
  43. result.TeacherResult = await teacherRepository.GetPressTeacherResult(new TeacherRequest { PressId = id, UserId = userId });
  44. var sourceLibrary = new List<int> { AllTypeConst.Book.GetHashCode(), AllTypeConst.Video.GetHashCode(), AllTypeConst.Audio.GetHashCode() };
  45. //获取共享资源
  46. result.Resource = new List<Result.AppPageSearch.AppPageSearchResult<Result.Media.MediaSearchResult>>();
  47. foreach (var item in sourceLibrary)
  48. {
  49. var items = (await mediaRepository.GetMediaByLableIdAysnc(new Request.Media.MediaLableIdRequest { Page = 1, PerPage = 3, PressId = id, MediaType = item })).Items;
  50. if (items.Count() == 0)
  51. continue;
  52. result.Resource.Add(new Result.AppPageSearch.AppPageSearchResult<Result.Media.MediaSearchResult>
  53. {
  54. LableName = ((AllTypeConst)item).GetDescriptionOriginal(),
  55. LableId = item,
  56. Items = items
  57. });
  58. }
  59. var analyzeRequest = new Request.App.Analyze.AnalyzeRequest();
  60. analyzeRequest.TypeValue = AllTypeConst.Press.GetHashCode();
  61. analyzeRequest.AnalyzeType = 1;
  62. analyzeRequest.SourceId = id;
  63. analyzeRequest.UserId = userId;
  64. //获取话题的转发数量
  65. analyzeRequest.AnalyzeType = 4;
  66. var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest);
  67. result.RetransmissionCount = retransmissionCount;
  68. result.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest);
  69. //获取话题的收藏数量
  70. analyzeRequest.AnalyzeType = 3;
  71. var collectCount = await _analyzeService.CountAsync(analyzeRequest);
  72. result.CollectCount = collectCount;
  73. //是否收藏
  74. result.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest);
  75. //获取点赞数量
  76. analyzeRequest.AnalyzeType = 1;
  77. var praiseCount = await _analyzeService.CountAsync(analyzeRequest);
  78. result.PraiseCount = praiseCount;
  79. //是否点赞
  80. result.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest);
  81. if (userId > 0)
  82. //获取用户历史记录
  83. await visitHistoryService.InsertAsync(userId, id, AllTypeConst.Press.GetHashCode());
  84. return result;
  85. }
  86. /// <summary>
  87. /// 添加修改
  88. /// </summary>
  89. /// <param name="request"></param>
  90. /// <returns></returns>
  91. public async Task<bool> AddOrModifyAsync(PressAddOrModifyRequest request)
  92. {
  93. if (request.LableName.Count() == 0 || string.IsNullOrEmpty(request.ExtendLableName))
  94. throw new Common.Exceptions.BusinessException("必要参数为空");
  95. var press = new Entity.Press.Press()
  96. {
  97. FileUrl = StringUtils.RemoveDomain(request.FileUrl),
  98. IsDisable = request.IsDisable,
  99. IsTop = request.IsTop,
  100. IsHot = request.IsHot,
  101. IsRecommend = request.IsRecommend,
  102. ImageUrl = StringUtils.RemoveDomain(request.ImageUrl),
  103. Name = request.Name,
  104. Summary = request.Summary,
  105. Sort = request.Sort,
  106. VideoCoverImage = StringUtils.RemoveDomain(request.VideoCoverImage),
  107. ExtendLableName = request.ExtendLableName
  108. };
  109. if (request.Id > 0)
  110. press = await pressRepository.GetAsync(request.Id);
  111. press.LableName = StringUtils.ObjectCollectionToString(request.LableName, ",");
  112. press.Duration = StringUtils.MediaDuration(press.FileUrl);
  113. press.VideoImage = StringUtils.MediaImage(press.FileUrl);
  114. if (request.Id > 0)
  115. return await pressRepository.UpdateAsync(press);
  116. return await pressRepository.InsertAsync(press) > 0;
  117. }
  118. }
  119. }