MediaService.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using GxPress.Service.Interface.Media;
  2. using GxPress.Repository.Interface.Category;
  3. using GxPress.Repository.Interface.Media;
  4. using System.Threading.Tasks;
  5. using GxPress.Result.Media;
  6. using AutoMapper;
  7. using System.Linq;
  8. using GxPress.Result.Category;
  9. using GxPress.Service.Interface.Epub;
  10. using GxPress.Request.Media;
  11. using System;
  12. using GxPress.Repository.Interface;
  13. using GxPress.Repository.Interface.Order;
  14. namespace GxPress.Service.Implement.Media
  15. {
  16. public class MediaService : IMediaService
  17. {
  18. private readonly ICategoryRepository categoryRepository;
  19. private readonly IMediaRepository mediaRepository;
  20. private readonly IMapper _mapper;
  21. private readonly IEpubService epubService;
  22. private readonly IMediaLibraryRepository mediaLibraryRepository;
  23. private readonly IUserRepository userRepository;
  24. private readonly IOrderRepository orderRepository;
  25. public MediaService(ICategoryRepository categoryRepository, IMediaRepository mediaRepository, IMapper _mapper, IEpubService epubService, IMediaLibraryRepository mediaLibraryRepository, IUserRepository userRepository, IOrderRepository orderRepository)
  26. {
  27. this.categoryRepository = categoryRepository;
  28. this.mediaRepository = mediaRepository;
  29. this._mapper = _mapper;
  30. this.epubService = epubService;
  31. this.mediaLibraryRepository = mediaLibraryRepository;
  32. this.userRepository = userRepository;
  33. this.orderRepository = orderRepository;
  34. }
  35. public async Task<bool> InsertAsync(MediaResult result)
  36. {
  37. // if (result.CategoryId > 0)
  38. // {
  39. // result.CategoryName = await categoryRepository.GetCategoryParentAsync(result.CategoryId, result.CategoryName);
  40. // }
  41. return await mediaRepository.InsertAsync(result);
  42. }
  43. public async Task<MediaResult> GetAsync(int id)
  44. {
  45. var result = new MediaResult();
  46. //获取媒体
  47. result = await mediaRepository.GetAsync(id);
  48. //获取类别
  49. var categoryAll = await categoryRepository.GetAllAsync(0);
  50. result.CategoryResults = categoryAll.Where(n => n.ParentId == 0).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  51. foreach (var item in result.CategoryResults)
  52. {
  53. item.IsChildren = categoryAll.Any(n => n.ParentId == item.Id);
  54. item.Children = categoryAll.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  55. if (item.IsChildren)
  56. {
  57. foreach (var chidren in item.Children)
  58. {
  59. chidren.IsChildren = categoryAll.Any(n => n.ParentId == chidren.Id);
  60. chidren.Children = categoryAll.Where(n => n.ParentId == chidren.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  61. }
  62. }
  63. }
  64. return result;
  65. }
  66. /// <summary>
  67. /// 获取书籍详情
  68. /// </summary>
  69. /// <param name="id"></param>
  70. /// <param name="userId"></param>
  71. /// <returns></returns>
  72. public async Task<BookMediaResult> GetBookMediaResultAsync(int id, int userId)
  73. {
  74. var result = await mediaRepository.GetBookMediaResultAsync(id);
  75. if (result == null)
  76. throw new Common.Exceptions.BusinessException("不存在");
  77. if (result.MediaType != GxPress.EnumConst.ResourceTypeConst.Book.GetHashCode())
  78. throw new Common.Exceptions.BusinessException("非书籍");
  79. //获取书籍文件
  80. var mediaLibrary = await mediaLibraryRepository.GetTaskAsync(id);
  81. if (!mediaLibrary.FileUrl.Contains("epub"))
  82. throw new Common.Exceptions.BusinessException("书籍不存在epub文件");
  83. //书籍
  84. //获取media
  85. var media = await mediaRepository.GetAsync(id);
  86. if (userId > 0)
  87. {
  88. var user = await userRepository.GetAsync(userId);
  89. if (user.IsVip && user.EndTime > DateTime.Now)
  90. media.FreeProportion = 0;
  91. else
  92. {
  93. if (await orderRepository.GetExistsAsync(userId, id))
  94. media.FreeProportion = 0;
  95. }
  96. }
  97. var sectionValue = 0;
  98. result.BookCatalogResults = epubService.GetCatalog("wwwroot" + mediaLibrary.FileUrl);
  99. if (media.FreeProportion > 0)
  100. {
  101. var freeProportion = media.FreeProportion / 100;
  102. sectionValue = Convert.ToInt32(result.BookCatalogResults.Count * freeProportion);
  103. foreach (var item in result.BookCatalogResults)
  104. {
  105. if (item.Id <= sectionValue)
  106. item.IsRead = true;
  107. if (item.IsChildren)
  108. {
  109. foreach (var bookCatalogResult in item.Children)
  110. {
  111. if (bookCatalogResult.Id <= sectionValue)
  112. bookCatalogResult.IsRead = true;
  113. }
  114. }
  115. }
  116. }
  117. else
  118. {
  119. foreach (var item in result.BookCatalogResults)
  120. {
  121. item.IsRead = true;
  122. foreach (var bookCatalogResult in item.Children)
  123. bookCatalogResult.IsRead = true;
  124. }
  125. }
  126. //计算免费占比
  127. return result;
  128. }
  129. /// <summary>
  130. /// 获取视频详情
  131. /// </summary>
  132. /// <param name="id"></param>
  133. /// <returns></returns>
  134. public async Task<VideoMediaResult> GetVideoMediaResultAsync(int id)
  135. {
  136. var result = await mediaRepository.GetVideoMediaResultAsync(id);
  137. if (result == null)
  138. throw new Common.Exceptions.BusinessException("不存在");
  139. if (result.MediaType != GxPress.EnumConst.ResourceTypeConst.Video.GetHashCode())
  140. throw new Common.Exceptions.BusinessException("非视频");
  141. //获取视频
  142. result.MediaLibraryResults = await mediaLibraryRepository.GetAllAsync(id);
  143. return result;
  144. }
  145. /// <summary>
  146. /// 获取章节内容
  147. /// </summary>
  148. /// <param name="request"></param>
  149. /// <returns></returns>
  150. public async Task<string> GetBookMediaContentResultAsync(BookCatalogRequest request)
  151. {
  152. if (request.MediaId == 0)
  153. throw new Common.Exceptions.BusinessException("请检查参数");
  154. if (string.IsNullOrEmpty(request.CatalogNameg))
  155. throw new Common.Exceptions.BusinessException("请检查参数");
  156. if (string.IsNullOrEmpty(request.CatalogId))
  157. throw new Common.Exceptions.BusinessException("请检查参数");
  158. //获取书籍文件
  159. var mediaLibrary = await mediaLibraryRepository.GetTaskAsync(request.MediaId);
  160. if (!mediaLibrary.FileUrl.Contains("epub"))
  161. throw new Common.Exceptions.BusinessException("书籍不存在epub文件");
  162. request.Path = "wwwroot" + mediaLibrary.FileUrl;
  163. return await epubService.GetBookCatalogContent(request);
  164. }
  165. }
  166. }