using GxPress.Service.Interface.Media; using GxPress.Repository.Interface.Category; using GxPress.Repository.Interface.Media; using System.Threading.Tasks; using GxPress.Result.Media; using AutoMapper; using System.Linq; using GxPress.Result.Category; using GxPress.Service.Interface.Epub; using GxPress.Request.Media; using System; using GxPress.Repository.Interface; using GxPress.Repository.Interface.Order; using GxPress.Repository.Interface.Attach; using GxPress.Repository.Interface.SystemLabel; using GxPress.Repository.Interface.Analyze; using Datory; namespace GxPress.Service.Implement.Media { public partial class MediaService : IMediaService { private readonly ICategoryRepository categoryRepository; private readonly IMediaRepository mediaRepository; private readonly IMapper _mapper; private readonly IEpubService epubService; private readonly IMediaLibraryRepository mediaLibraryRepository; private readonly IUserRepository userRepository; private readonly IOrderRepository orderRepository; private readonly IAttachRepository attachRepository; private readonly ISystemLabelRepository systemLabelRepository; private readonly ISystemLableMediaRepository systemLableMediaRepository; private readonly IAnalyzeRepository analyzeRepository; public MediaService(ICategoryRepository categoryRepository, IMediaRepository mediaRepository, IMapper _mapper, IEpubService epubService, IMediaLibraryRepository mediaLibraryRepository, IUserRepository userRepository, IOrderRepository orderRepository, IAttachRepository attachRepository, ISystemLabelRepository systemLabelRepository, ISystemLableMediaRepository systemLableMediaRepository, IAnalyzeRepository analyzeRepository) { this.categoryRepository = categoryRepository; this.mediaRepository = mediaRepository; this._mapper = _mapper; this.epubService = epubService; this.mediaLibraryRepository = mediaLibraryRepository; this.userRepository = userRepository; this.orderRepository = orderRepository; this.attachRepository = attachRepository; this.systemLabelRepository = systemLabelRepository; this.systemLableMediaRepository = systemLableMediaRepository; this.analyzeRepository = analyzeRepository; } public async Task InsertAsync(MediaResult result) { // if (result.CategoryId > 0) // { // result.CategoryName = await categoryRepository.GetCategoryParentAsync(result.CategoryId, result.CategoryName); // } return await mediaRepository.InsertAsync(result); } public async Task GetAsync(int id) { var result = new MediaResult(); //获取媒体 result = await mediaRepository.GetAsync(id); //获取类别 var categoryAll = await categoryRepository.GetAllAsync(0); result.CategoryResults = categoryAll.Where(n => n.ParentId == 0).Select(n => _mapper.Map(n)).ToList(); foreach (var item in result.CategoryResults) { item.IsChildren = categoryAll.Any(n => n.ParentId == item.Id); item.Children = categoryAll.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map(n)).ToList(); if (item.IsChildren) { foreach (var chidren in item.Children) { chidren.IsChildren = categoryAll.Any(n => n.ParentId == chidren.Id); chidren.Children = categoryAll.Where(n => n.ParentId == chidren.Id).Select(n => _mapper.Map(n)).ToList(); } } } return result; } /// /// 获取书籍详情 /// /// /// /// public async Task GetBookMediaResultAsync(int id, int userId) { var result = await mediaRepository.GetBookMediaResultAsync(id); if (result == null) throw new Common.Exceptions.BusinessException("不存在"); if (result.MediaType != GxPress.EnumConst.ResourceTypeConst.Book.GetHashCode()) throw new Common.Exceptions.BusinessException("非书籍"); //获取书籍文件 var mediaLibrary = await mediaLibraryRepository.GetTaskAsync(id); if (!mediaLibrary.FileUrl.Contains("epub")) throw new Common.Exceptions.BusinessException("书籍不存在epub文件"); //书籍 //获取media var media = await mediaRepository.GetAsync(id); if (userId > 0) { var user = await userRepository.GetAsync(userId); if (user.IsVip && user.EndTime > DateTime.Now) media.FreeProportion = 0; else { if (await orderRepository.GetExistsAsync(userId, id)) media.FreeProportion = 0; } } var sectionValue = 0; result.BookCatalogResults = epubService.GetCatalog("wwwroot" + mediaLibrary.FileUrl); if (media.FreeProportion > 0) { var freeProportion = media.FreeProportion / 100; sectionValue = Convert.ToInt32(result.BookCatalogResults.Count * freeProportion); foreach (var item in result.BookCatalogResults) { if (item.Id <= sectionValue) item.IsRead = true; if (item.IsChildren) { foreach (var bookCatalogResult in item.Children) { if (bookCatalogResult.Id <= sectionValue) bookCatalogResult.IsRead = true; } } } } else { foreach (var item in result.BookCatalogResults) { item.IsRead = true; foreach (var bookCatalogResult in item.Children) bookCatalogResult.IsRead = true; } } //是否收藏 result.IsCollect = await analyzeRepository.ExistsAsync(Q.Where(nameof(Entity.Analyze.Analyze.TypeValue), GxPress.EnumConst.AllTypeConst.Book.GetHashCode()).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3).Where(nameof(Entity.Analyze.Analyze.SourceId), id).Where(nameof(Entity.Analyze.Analyze.UserId), userId)); //计算免费占比 return result; } /// /// 获取视频详情 /// /// /// public async Task GetVideoMediaResultAsync(int id) { var result = await mediaRepository.GetVideoMediaResultAsync(id); if (result == null) throw new Common.Exceptions.BusinessException("不存在"); if (result.MediaType != GxPress.EnumConst.ResourceTypeConst.Video.GetHashCode()) throw new Common.Exceptions.BusinessException("非视频"); //获取视频 result.MediaLibraryResults = await mediaLibraryRepository.GetAllAsync(id); return result; } /// /// 获取章节内容 /// /// /// public async Task GetBookMediaContentResultAsync(BookCatalogRequest request) { if (request.MediaId == 0) throw new Common.Exceptions.BusinessException("请检查参数"); if (string.IsNullOrEmpty(request.CatalogNameg)) throw new Common.Exceptions.BusinessException("请检查参数"); if (string.IsNullOrEmpty(request.CatalogId)) throw new Common.Exceptions.BusinessException("请检查参数"); //获取书籍文件 var mediaLibrary = await mediaLibraryRepository.GetTaskAsync(request.MediaId); if (!mediaLibrary.FileUrl.Contains("epub")) throw new Common.Exceptions.BusinessException("书籍不存在epub文件"); request.Path = "wwwroot" + mediaLibrary.FileUrl; return await epubService.GetBookCatalogContent(request); } } }