123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- 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<bool> InsertAsync(MediaResult result)
- {
- // if (result.CategoryId > 0)
- // {
- // result.CategoryName = await categoryRepository.GetCategoryParentAsync(result.CategoryId, result.CategoryName);
- // }
- return await mediaRepository.InsertAsync(result);
- }
- public async Task<MediaResult> 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<CategoryResult>(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<CategoryResult>(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<CategoryResult>(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;
- }
- /// <summary>
- /// 获取书籍详情
- /// </summary>
- /// <param name="id"></param>
- /// <param name="userId"></param>
- /// <returns></returns>
- public async Task<BookMediaResult> 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;
- }
- /// <summary>
- /// 获取视频详情
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<VideoMediaResult> 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;
- }
- /// <summary>
- /// 获取音频
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<AudioMediaResult> 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;
- }
- /// <summary>
- /// 获取章节内容
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<string> 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);
- }
- /// <summary>
- /// 根据labeId获取媒体
- /// </summary>
- /// <param name="mediaIds"></param>
- /// <returns></returns>
- public async Task<PagedList<MediaSearchResult>> GetMediaByLableIdAysnc(MediaLableIdRequest request)
- {
- var result = new PagedList<MediaSearchResult>();
- 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<MediaSearchResult>();
- 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<MediaSearchResult>();
- 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<MediaSearchResult>();
- 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);
- }
- /// <summary>
- /// 排行榜
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<AppRankingResult> 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<MediaRankingResult>();
- 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;
- }
- /// <summary>
- /// 根据关键字搜索榜单
- /// </summary>
- /// <param name="keyWord"></param>
- /// <param name="lableId"></param>
- /// <returns></returns>
- public async Task<PagedList<MediaSearchResult>> SearchRankingAsync(Common.Page.PageParameter request)
- {
- return await mediaRepository.SearchRankingAsync(request);
- }
- }
- }
|