MediaService.cs 5.4 KB

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