MediaService.cs 6.1 KB

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