MediaService.cs 8.0 KB

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