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; using GxPress.Common.Tools; using GxPress.Service.Interface.Analyze; using GxPress.EnumConst; using System.IO; using GxPress.Request.Analyze; using GxPress.Service.Interface.VisitHistory; using GxPress.Common.Page; using System.Collections.Generic; using GxPress.Repository.Interface.Press; using GxPress.Repository.Interface.Teacher; using GxPress.Request.TeacherRequest; 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; private readonly IAnalyzeService _analyzeService; private readonly ICommentRepository _commentRepository; private readonly IVisitHistoryService visitHistoryService; private readonly IArticleRepository articleRepository; private readonly IPressRepository pressRepository; private readonly ITeacherRepository teacherRepository; public MediaService(ICategoryRepository categoryRepository, IMediaRepository mediaRepository, IMapper _mapper, IEpubService epubService, IMediaLibraryRepository mediaLibraryRepository, IUserRepository userRepository, IOrderRepository orderRepository, IAttachRepository attachRepository, ISystemLabelRepository systemLabelRepository, ISystemLableMediaRepository systemLableMediaRepository, IAnalyzeRepository analyzeRepository, IAnalyzeService _analyzeService, ICommentRepository _commentRepository, IVisitHistoryService visitHistoryService, IArticleRepository articleRepository, IPressRepository pressRepository, ITeacherRepository teacherRepository) { 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; this._analyzeService = _analyzeService; this._commentRepository = _commentRepository; this.visitHistoryService = visitHistoryService; this.articleRepository = articleRepository; this.pressRepository = pressRepository; this.teacherRepository = teacherRepository; } 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, int userId) { 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(); } } } if (userId > 0) //获取用户历史记录 await visitHistoryService.InsertAsync(userId, id, result.MediaType); var analyzeRequest = new Request.App.Analyze.AnalyzeRequest() { TypeValue = result.MediaType, AnalyzeType = 1, SourceId = id, UserId = userId }; //点赞数量 result.PraiseCount = await _analyzeService.CountAsync(analyzeRequest); //是否点赞 result.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest); //获取话题的评论数量 var commentCount = await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), id).Where(nameof(Entity.Comment.TypeValue), analyzeRequest.TypeValue).Where(nameof(Entity.Comment.Pid), 0)); result.CommentCount = commentCount; //获取话题的转发数量 analyzeRequest.AnalyzeType = 4; var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest); result.RetransmissionCount = retransmissionCount; result.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest); //获取话题的收藏数量 analyzeRequest.AnalyzeType = 3; var collectCount = await _analyzeService.CountAsync(analyzeRequest); result.CollectCount = collectCount; //是否收藏 result.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest); //是否购买 if (userId > 0 && result.FreeProportion == 0) result.IsBuy = true; else if (userId > 0) result.IsBuy = await orderRepository.GetExistsAsync(userId, id); //点赞数据 var praisePageSearchRequest = new PraisePageSearchRequest { SourceId = id, TypeValue = analyzeRequest.TypeValue, Page = 1, PerPage = 3 }; result.PraisePagedList = await _analyzeService.GetPraisePageAsync(praisePageSearchRequest); result.CommentScore = await _commentRepository.GetCommentScoreAsync(id, result.MediaType); 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("非书籍"); //获取用户历史记录 await visitHistoryService.InsertAsync(userId, id, AllTypeConst.Book.GetHashCode()); //获取书籍文件 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((Directory.GetCurrentDirectory() + "\\wwwroot" + StringUtils.RemoveDomain(mediaLibrary.FileUrl).Replace("/", "\\"))); 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.ImageUrls = StringUtils.AddDomain(result.ImageUrls); var analyzeRequest = new Request.App.Analyze.AnalyzeRequest() { TypeValue = result.MediaType, AnalyzeType = 1, SourceId = id, UserId = userId }; //获取老师 if (result.TeacherId > 0) { var teacher = await teacherRepository.GetAsync(result.TeacherId); result.Job = teacher != null ? teacher.Job : string.Empty; } //点赞数量 result.PraiseCount = await _analyzeService.CountAsync(analyzeRequest); //是否点赞 result.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest); //获取话题的评论数量 var commentCount = await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), id).Where(nameof(Entity.Comment.TypeValue), analyzeRequest.TypeValue).Where(nameof(Entity.Comment.Pid), 0)); result.CommentCount = commentCount; //获取话题的转发数量 analyzeRequest.AnalyzeType = 4; var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest); result.RetransmissionCount = retransmissionCount; result.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest); //获取话题的收藏数量 analyzeRequest.AnalyzeType = 3; var collectCount = await _analyzeService.CountAsync(analyzeRequest); result.CollectCount = collectCount; //是否收藏 result.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest); //是否购买 if (userId > 0 && result.FreeProportion == 0) result.IsBuy = true; else if (userId > 0) result.IsBuy = await orderRepository.GetExistsAsync(userId, id); //点赞数据 var praisePageSearchRequest = new PraisePageSearchRequest { SourceId = id, TypeValue = analyzeRequest.TypeValue, Page = 1, PerPage = 3 }; result.PraisePagedList = await _analyzeService.GetPraisePageAsync(praisePageSearchRequest); //获取评分 result.CommentScore = await _commentRepository.GetCommentScoreAsync(id, result.MediaType); return result; } /// /// 获取视频详情 /// /// /// public async Task GetVideoMediaResultAsync(int id, int userId) { 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("非视频"); //获取用户历史记录 await visitHistoryService.InsertAsync(userId, id, AllTypeConst.Video.GetHashCode()); //获取视频 result.MediaLibraryResults = await mediaLibraryRepository.GetAllAsync(id); var analyzeRequest = new Request.App.Analyze.AnalyzeRequest(); analyzeRequest.TypeValue = result.MediaType; analyzeRequest.AnalyzeType = 1; analyzeRequest.SourceId = id; analyzeRequest.UserId = userId; //点赞数量 result.PraiseCount = await _analyzeService.CountAsync(analyzeRequest); //是否点赞 result.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest); //获取话题的评论数量 var commentCount = await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), id).Where(nameof(Entity.Comment.TypeValue), analyzeRequest.TypeValue).Where(nameof(Entity.Comment.Pid), 0)); result.CommentCount = commentCount; //获取话题的转发数量 analyzeRequest.AnalyzeType = 4; var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest); result.RetransmissionCount = retransmissionCount; result.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest); //获取话题的收藏数量 analyzeRequest.AnalyzeType = 3; var collectCount = await _analyzeService.CountAsync(analyzeRequest); result.CollectCount = collectCount; //是否收藏 result.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest); //点赞数据 var praisePageSearchRequest = new PraisePageSearchRequest { SourceId = id, TypeValue = analyzeRequest.TypeValue, Page = 1, PerPage = 3 }; result.PraisePagedList = await _analyzeService.GetPraisePageAsync(praisePageSearchRequest); result.CommentScore = await _commentRepository.GetCommentScoreAsync(id, result.MediaType); return result; } /// /// 获取音频 /// /// /// public async Task GetAudioMediaResultAsync(int id, int userId) { var result = await mediaRepository.GetAudioMediaResultAsync(id); if (result == null) throw new Common.Exceptions.BusinessException("不存在"); if (result.MediaType != GxPress.EnumConst.ResourceTypeConst.Audio.GetHashCode()) throw new Common.Exceptions.BusinessException("非音频"); //获取用户历史记录 await visitHistoryService.InsertAsync(userId, id, AllTypeConst.Audio.GetHashCode()); //获取音频地址 result.MediaLibraryResults = await mediaLibraryRepository.GetAudioTaskAsync(id); var query = Q.NewQuery(); query.Where(nameof(Entity.Analyze.Analyze.TypeValue), GxPress.EnumConst.AllTypeConst.Book.GetHashCode()); query.Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3); query.Where(nameof(Entity.Analyze.Analyze.SourceId), id); query.Where(nameof(Entity.Analyze.Analyze.UserId), userId); var analyzeRequest = new Request.App.Analyze.AnalyzeRequest(); analyzeRequest.TypeValue = result.MediaType; analyzeRequest.AnalyzeType = 1; analyzeRequest.SourceId = id; analyzeRequest.UserId = userId; //点赞数量 result.PraiseCount = await _analyzeService.CountAsync(analyzeRequest); //是否点赞 result.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest); //获取话题的评论数量 var commentCount = await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), id).Where(nameof(Entity.Comment.TypeValue), analyzeRequest.TypeValue).Where(nameof(Entity.Comment.Pid), 0)); result.CommentCount = commentCount; //获取话题的转发数量 analyzeRequest.AnalyzeType = 4; var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest); result.RetransmissionCount = retransmissionCount; result.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest); //获取话题的收藏数量 analyzeRequest.AnalyzeType = 3; var collectCount = await _analyzeService.CountAsync(analyzeRequest); result.CollectCount = collectCount; //是否收藏 result.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest); //是否购买 if (userId > 0 && result.FreeProportion == 0) result.IsBuy = true; else if (userId > 0) result.IsBuy = await orderRepository.GetExistsAsync(userId, id); //点赞数据 var praisePageSearchRequest = new PraisePageSearchRequest { SourceId = id, TypeValue = analyzeRequest.TypeValue, Page = 1, PerPage = 3 }; result.PraisePagedList = await _analyzeService.GetPraisePageAsync(praisePageSearchRequest); result.CommentScore = await _commentRepository.GetCommentScoreAsync(id, result.MediaType); 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" + StringUtils.RemoveDomain(mediaLibrary.FileUrl); return await epubService.GetBookCatalogContent(request); } /// /// 根据labeId获取媒体 /// /// /// public async Task> GetMediaByLableIdAysnc(MediaLableIdRequest request) { var result = new PagedList(); var systemLabel = await systemLabelRepository.GetAsync(request.LableId); if (systemLabel.ResourceType > 0) { if (systemLabel.ResourceType == AllTypeConst.Teacher.GetHashCode()) { var teacherResults = await teacherRepository.GetTeacherResult(new TeacherRequest { KeyWord = request.KeyWord, Page = request.Page, PerPage = request.PerPage, UserId = request.UserId }); var mediaSearchResults = new List(); foreach (var teacher in teacherResults.Items) { mediaSearchResults.Add(new MediaSearchResult { Id = teacher.Id, Title = teacher.Name, ImageUrls = !string.IsNullOrEmpty(teacher.ImageUrl) ? StringUtils.AddDomain(teacher.ImageUrl) : "", Summary = teacher.Summary, Author = teacher.Name, CollectCount = teacher.CollectCount, MediaType = AllTypeConst.Teacher.GetHashCode() }); } result.Items = mediaSearchResults; result.Total = teacherResults.Total; return result; } if (systemLabel.ResourceType == AllTypeConst.Article.GetHashCode()) { var articleResult = await articleRepository.GetArticleResults(new PageParameter { Page = request.Page, PerPage = request.PerPage, KeyWord = request.KeyWord }); var mediaSearchResults = new List(); foreach (var article in articleResult.Items) { var jsonContent = article.ArticleContent; var textContent = string.Empty; //新版json if (jsonContent != null && jsonContent.type.Count() > 0) { foreach (var content in jsonContent.content) { foreach (var contentItem in content.content) { if (contentItem.type == "text") textContent += contentItem.text; } } } var imageUrl = article.ImageUrls != null ? article.ImageUrls.FirstOrDefault() : ""; mediaSearchResults.Add(new MediaSearchResult { Id = article.Id, Title = article.Title, ImageUrls = !string.IsNullOrEmpty(imageUrl) ? StringUtils.AddDomain(imageUrl) : "", Summary = textContent, Author = article.Author, //Source = article.Source, CreatedDate = article.AddDate, //CollectCount = article.CollectCount, ReadCount = article.ReadCount, MediaType = AllTypeConst.Article.GetHashCode() }); } result.Items = mediaSearchResults; result.Total = articleResult.Total; return result; } if (systemLabel.ResourceType == AllTypeConst.Press.GetHashCode()) { var pressResults = await pressRepository.GetAllListAsync(new PageParameter { Page = request.Page, PerPage = request.PerPage, KeyWord = request.KeyWord }); var mediaSearchResults = new List(); foreach (var press in pressResults.Items) { mediaSearchResults.Add(new MediaSearchResult { Id = press.Id, Title = press.Name, Summary = press.Summary, ImageUrls = press.ImageUrl, MediaType = AllTypeConst.Press.GetHashCode() }); } result.Items = mediaSearchResults; result.Total = pressResults.Total; return result; } } return await mediaRepository.GetMediaByLableIdAysnc(request); } /// /// 排行榜 /// /// /// public async Task GetRankingListResults() { var query = Q.NewQuery(); query.Where(nameof(Entity.SystemLabel.SystemLabel.LableType), 2); query.Where(nameof(Entity.SystemLabel.SystemLabel.IsDisable), false); query.OrderByDesc(nameof(Entity.SystemLabel.SystemLabel.Sort)); var systemLabels = await systemLabelRepository.GetAllAsync(query); var result = new AppRankingResult(); result.Labels = systemLabels.Where(n => !n.IsRecommend); result.Items = new List(); foreach (var item in systemLabels.Where(n => n.IsRecommend)) { var data = await mediaRepository.SearchLableKeyWordAsync(new LableIdMediaRequest { LableId = item.Id, KeyWord = string.Empty, Page = 1, PerPage = item.PageSize }); result.Items.Add(new MediaRankingResult { Id = item.Id, MediaType = item.ResourceType, Name = item.LabelName, Items = data.Items.ToList() }); } return result; } /// /// 根据关键字搜索榜单 /// /// /// /// public async Task> SearchRankingAsync(Common.Page.PageParameter request) { return await mediaRepository.SearchRankingAsync(request); } } }