using System.Linq; using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Datory; using GxPress.Common.AppOptions; using GxPress.Common.Tools; using GxPress.Repository.Interface.Media; using GxPress.Result.Media; using Microsoft.Extensions.Options; using System.Transactions; using GxPress.Common.Exceptions; using GxPress.Common.Page; using GxPress.Request.Media; using System; using GxPress.EnumConst; using GxPress.Request.ArticleGroup; using Dapper; using GxPress.Request.AppChannel; using GxPress.Result.AppChannel; namespace GxPress.Repository.Implement.Media { public class MediaRepository : IMediaRepository { private readonly Repository _repository; private readonly Repository mediaLibraryRepository; private readonly Repository mediaLableRepository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypestr; public MediaRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType; _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString; var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); mediaLibraryRepository = new Repository(database); mediaLableRepository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task GetAsync(int id) { var result = new MediaResult(); //获取媒体 var media = await _repository.GetAsync(id); result = _mapper.Map(media); result.ImageUrls = StringUtils.AddDomain(result.ImageUrls); result.CategoryId = StringUtils.StringCollectionToStringList(media.CategoryId).ToList(); result.CategoryName = StringUtils.StringCollectionToStringList(media.CategoryName).ToList(); result.FullCategoryId = StringUtils.StringCollectionToStringList(media.FullCategoryId).ToList(); //获取媒体库资源 var mediaLibrarys = await mediaLibraryRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id)); result.MediaLibraryResults = mediaLibrarys.ToList().Select(n => _mapper.Map(n)).ToList(); foreach (var item in result.MediaLibraryResults) { item.FileUrl = StringUtils.AddDomain(item.FileUrl); } //获取媒体标签 var mediaLables = await mediaLableRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id)); result.MediaLableResults = mediaLables.Select(n => _mapper.Map(n)).ToList(); return result; } /// /// 删除媒体 /// /// /// public async Task DeleteAsync(int id) { try { using (var transactionScope = new TransactionScope()) { // //删除媒体库 // await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id)); // //删除媒体标签 // await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id)); await _repository.UpdateAsync(Q.Where(nameof(Entity.tede2.Media.Media.Id), id).Set(nameof(Entity.tede2.Media.Media.IsDelete), true)); transactionScope.Complete(); } } catch (System.Exception ex) { throw new BusinessException(ex.Message); } return true; } /// /// 添加媒体 /// /// /// public async Task InsertAsync(MediaResult result) { var model = _mapper.Map(result); model.ImageUrls = StringUtils.RemoveDomain(result.ImageUrls); try { using (var transactionScope = new TransactionScope()) { model.AutoNumber = SnowflakeHelper.MakeSnowflake(); var categoryId = ""; foreach (var item in result.CategoryId) { categoryId += item.Split(",")[item.Split(",").Length - 1] + ","; } categoryId = categoryId.Remove(categoryId.Length - 1, 1); model.CategoryId = categoryId; model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId); var categoryName = ""; foreach (var item in result.CategoryName) { categoryName += item.Split(",")[item.Split(",").Length - 1] + ","; } categoryName = categoryName.Remove(categoryName.Length - 1, 1); model.CategoryName = categoryName; model.IsLibrary = result.MediaLibraryResults.Count > 0; var id = await _repository.InsertAsync(model); if (id > 0) { foreach (var item in result.MediaLibraryResults) { var mediaLibrary = _mapper.Map(item); mediaLibrary.MediaId = id; mediaLibrary.FileUrl = StringUtils.RemoveDomain(item.FileUrl); await mediaLibraryRepository.InsertAsync(mediaLibrary); } } //添加标签 foreach (var item in result.MediaLableResults) { var mediaLable = _mapper.Map(item); mediaLable.MediaId = id; await mediaLableRepository.InsertAsync(mediaLable); } if (result.Id == 0 && result.TeacherId == 0 && !string.IsNullOrEmpty(result.Author)) { //新增名师 } //提交事务 transactionScope.Complete(); } } catch (System.Exception ex) { throw new Common.Exceptions.BusinessException(ex.Message); } return true; } /// /// 获取媒体 /// /// /// public async Task GetAsync(SqlKata.Query query) { query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); return await _repository.GetAsync(query); } public async Task UpdateAsync(Entity.tede2.Media.Media note) { return await _repository.UpdateAsync(note); } public async Task UpdateAsync(SqlKata.Query query) { return await _repository.UpdateAsync(query) > 0; } /// /// 查询 /// /// /// public async Task> GetAllAsync(MediaSearchRequest request) { var result = new PagedList(); var query = Q.NewQuery(); if (!string.IsNullOrEmpty(request.KeyWord)) { query.Where(n => n.OrWhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%").OrWhereLike(nameof(Entity.tede2.Media.Media.Creator), $"%{request.KeyWord}%")); } if (request.IsDelete > 0) query.Where(nameof(Entity.tede2.Media.Media.IsDelete), request.IsDelete == 1); if (request.MediaType > 0) query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.MediaType); if (request.LibraryType > 0) query.Where(nameof(Entity.tede2.Media.Media.IsLibrary), request.LibraryType == 1); if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime)) { if (DateTime.TryParse(request.BeginTime, out var beginTime) && DateTime.TryParse(request.EndTime, out var endTime)) { query.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), ">=", beginTime); query.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), "<=", endTime); } } if (request.IsChecked > 0) query.Where(nameof(Entity.tede2.Media.Media.IsChecked), request.IsChecked == 1); result.Total = await _repository.CountAsync(query); //排序 if (string.IsNullOrEmpty(request.Sort)) query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate)); else if (request.Sort.Equals("SellAmount")) query.OrderByDesc(nameof(Entity.tede2.Media.Media.SellAmount)); else if (request.Sort.Equals("ReadCount")) query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount)); var item = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage)); result.Items = item; return result; } /// /// 根据IDs /// /// public async Task> GetMediaByLableIdsAsync(MediaIdsRequest request) { if (request.Ids.Count > 0) { var str = ""; foreach (var item in request.Ids) str += item + ","; str = str.Remove(str.Length - 1, 1); var sql = $@"SELECT Id,Summary,ImageUrls,Title,Author,MediaType,ReadCount,CreatedDate FROM tede_media WHERE id IN ({str}) ORDER BY RAND() LIMIT {request.Limit}"; var databaseType = _databaseTypestr.ToEnum(DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection.QueryAsync(sql); foreach (var item in result) { item.ImageUrls = StringUtils.AddDomain(item.ImageUrls); } return result; } return null; } /// /// 根据IDs /// /// public async Task> GetMediaByLableIdAsync(MediaLableIdRequest request) { var result = new PagedList(); if (request.Ids.Count > 0) { var str = ""; foreach (var item in request.Ids) str += item + ","; str = str.Remove(str.Length - 1, 1); var sql = $@"SELECT Id,Summary,ImageUrls,Title,Author,MediaType,ReadCount,CreatedDate FROM tede_media WHERE id IN ({str}) ORDER BY CreatedDate desc LIMIT {(request.Page - 1) * request.PerPage},{request.PerPage}"; var databaseType = _databaseTypestr.ToEnum(DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); result.Items = await connection.QueryAsync(sql); sql = $@"SELECT count(1) FROM tede_media WHERE id IN ({str})"; result.Total = await connection.ExecuteScalarAsync(sql); } foreach (var item in result.Items) { item.ImageUrls = StringUtils.AddDomain(item.ImageUrls); } return result; } public async Task UpdateAsync(MediaResult result) { //var model = _mapper.Map(result); try { using (var transactionScope = new TransactionScope()) { var model = await _repository.GetAsync(result.Id); if (result.CategoryId != null && result.CategoryId.Count > 0) { var categoryId = ""; foreach (var item in result.CategoryId) categoryId += item.Split(",")[item.Split(",").Length - 1] + ","; categoryId = categoryId.Remove(categoryId.Length - 1, 1); model.CategoryId = categoryId; model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId); } if (result.CategoryName != null && result.CategoryName.Count > 0) { var categoryName = ""; foreach (var item in result.CategoryName) categoryName += item.Split(",")[item.Split(",").Length - 1] + ","; categoryName = categoryName.Remove(categoryName.Length - 1, 1); model.CategoryName = categoryName; } if (result.AdminId > 0) model.AdminId = result.AdminId; if (!string.IsNullOrEmpty(result.Creator)) model.Creator = result.Creator; if (result.MediaType > 0) model.MediaType = result.MediaType; if (!string.IsNullOrEmpty(result.Title)) model.Title = result.Title; if (!string.IsNullOrEmpty(result.ImageUrls)) model.ImageUrls = result.ImageUrls; if (!string.IsNullOrEmpty(result.Summary)) model.Summary = result.Summary; if (!string.IsNullOrEmpty(result.Author)) model.Author = result.Author; if (result.TeacherId > 0) model.TeacherId = result.TeacherId; if (!string.IsNullOrEmpty(result.Source)) model.Source = result.Source; if (!string.IsNullOrEmpty(result.Blocks)) model.Blocks = result.Blocks; if (result.IsChecked > 0) model.IsChecked = result.IsChecked == 1; if (!string.IsNullOrEmpty(result.AddDate)) model.AddDate = Convert.ToDateTime(result.AddDate); if (model.Sort > 0) model.Sort = result.Sort; if (model.ReadCount > 0) model.ReadCount = result.ReadCount; if (result.IsTop > 0) model.IsTop = result.IsTop == 1; if (result.LableId > 0) model.LableId = result.LableId; if (result.IsRecommend > 0) model.IsRecommend = result.IsRecommend == 1; if (!string.IsNullOrEmpty(result.Press)) model.Press = result.Press; if (!string.IsNullOrEmpty(result.PublishTime)) model.PublishTime = result.PublishTime; if (result.IosDiscountPrice > 0) model.IosDiscountPrice = result.IosDiscountPrice; if (result.IosPrice > 0) model.IosPrice = result.IosPrice; if (result.OtherDiscountPrice > 0) model.OtherDiscountPrice = result.OtherDiscountPrice; if (result.OtherPrice > 0) model.OtherPrice = result.OtherPrice; if (result.FreeProportion > 0) model.FreeProportion = result.FreeProportion; if (result.AttachType > 0) model.AttachType = result.AttachType; if (result.AttachId > 0) model.AttachId = result.AttachId; if (result.MediaId > 0) model.MediaId = result.MediaId; if (!string.IsNullOrEmpty(result.MediaTitle)) model.MediaTitle = result.MediaTitle; if (!string.IsNullOrEmpty(result.JournalsYear)) model.JournalsYear = result.JournalsYear; if (result.JournalsCategory > 0) model.JournalsCategory = result.JournalsCategory; if (result.IsExercises > 0) model.IsExercises = result.IsExercises == 1; if (result.AppCategoryId > 0) model.AppCategoryId = result.AppCategoryId; await _repository.UpdateAsync(model); if (result.MediaLibraryResults != null && result.MediaLibraryResults.Count > 0) { model.IsLibrary = result.MediaLibraryResults.Count > 0; //删除媒体库 await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id)); foreach (var item in result.MediaLibraryResults) { var mediaLibrary = _mapper.Map(item); mediaLibrary.MediaId = result.Id; mediaLibrary.FileUrl = StringUtils.RemoveDomain(mediaLibrary.FileUrl); await mediaLibraryRepository.InsertAsync(mediaLibrary); } } if (result.MediaLableResults != null && result.MediaLableResults.Count > 0) { //删除媒体标签 await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id)); //添加标签 foreach (var item in result.MediaLableResults) { var mediaLable = _mapper.Map(item); mediaLable.MediaId = result.Id; await mediaLableRepository.InsertAsync(mediaLable); } } //提交事务 transactionScope.Complete(); } } catch (System.Exception ex) { throw new Common.Exceptions.BusinessException(ex.Message); } return true; } public async Task CountAsync(string beginTime, string endTime) { return await _repository.CountAsync(Q.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), ">=", beginTime).WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), "<=", endTime)); } public async Task CountAsync(SqlKata.Query query) { return await _repository.CountAsync(query); } public async Task GetAsync(string autoNumber) { return await _repository.GetAsync(Q.Where(nameof(Entity.tede2.Media.Media.AutoNumber), autoNumber)); } /// /// 获取书籍 /// /// /// public async Task GetBookMediaResultAsync(int mediaId) { var result = await _repository.GetAsync(Q.Where(nameof(Entity.tede2.Media.Media.Id), mediaId).Where(nameof(Entity.tede2.Media.Media.IsChecked), true).Where(nameof(Entity.tede2.Media.Media.IsDelete), false)); return _mapper.Map(result); } /// /// 获取视频 /// /// /// public async Task GetVideoMediaResultAsync(int mediaId) { var result = await _repository.GetAsync(Q.Where(nameof(Entity.tede2.Media.Media.Id), mediaId).Where(nameof(Entity.tede2.Media.Media.IsChecked), true).Where(nameof(Entity.tede2.Media.Media.IsDelete), false)); return _mapper.Map(result); } /// /// 根据 /// /// /// public async Task> GetNavigationMediaResults(IEnumerable mediaIds, int page = 1, int pageSize = 10) { //获取媒体 var query = Q.NewQuery(); query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Summary)); query.Select(nameof(Entity.tede2.Media.Media.TeacherId)); query.Select(nameof(Entity.tede2.Media.Media.AutoNumber)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.MediaType)); query.Select(nameof(Entity.tede2.Media.Media.ReadCount)); query.Select(nameof(Entity.tede2.Media.Media.CreatedDate)); query.Select(nameof(Entity.tede2.Media.Media.CollectCount)); query.WhereIn(nameof(Entity.tede2.Media.Media.Id), mediaIds); query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true); query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.LastModifiedDate)); query.ForPage(page, pageSize); return await _repository.GetAllAsync(query); } /// /// 猜你喜欢 /// /// /// public async Task> GetlikeMediaResults() { //获取媒体 var query = Q.NewQuery(); query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Summary)); query.Select(nameof(Entity.tede2.Media.Media.TeacherId)); query.Select(nameof(Entity.tede2.Media.Media.AutoNumber)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.MediaType)); query.Where(nameof(Entity.tede2.Media.Media.IsTop), true); query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true); query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); query.ForPage(1, 10); return await _repository.GetAllAsync(query); } /// /// 推荐 /// /// /// public async Task> GetRecommendMediaResults() { //获取媒体 var query = Q.NewQuery(); query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Summary)); query.Select(nameof(Entity.tede2.Media.Media.TeacherId)); query.Select(nameof(Entity.tede2.Media.Media.AutoNumber)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.MediaType)); query.Where(nameof(Entity.tede2.Media.Media.IsRecommend), true); query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true); query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); query.ForPage(1, 10); return await _repository.GetAllAsync(query); } /// /// 排行榜 /// /// /// public async Task> GetRankingListResults(RankingListRequest request) { //获取媒体 var query = Q.NewQuery(); query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Summary)); query.Select(nameof(Entity.tede2.Media.Media.TeacherId)); query.Select(nameof(Entity.tede2.Media.Media.AutoNumber)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.MediaType)); query.Select(nameof(Entity.tede2.Media.Media.CommentScore)); query.Select(nameof(Entity.tede2.Media.Media.CreatedDate)); query.Select(nameof(Entity.tede2.Media.Media.ReadCount)); //1 热搜 2热书榜 3 课程榜 4Top 100 新书 5文献头条 6试听榜 7大众热评 8Top 100 专著 if (request.DataTypeValue > 0) { //1 热搜 if (request.DataTypeValue == RankingListConst.HotSearch.GetHashCode()) query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount)); //2热书榜 if (request.DataTypeValue == RankingListConst.ReadBook.GetHashCode()) { query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode()); query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount)); } //3 课程榜 if (request.DataTypeValue == RankingListConst.Article.GetHashCode()) { query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode()); query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount)); } //Top 100 新书 if (request.DataTypeValue == RankingListConst.NewBook.GetHashCode()) { query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode()); query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate)); } //文献头条 if (request.DataTypeValue == RankingListConst.ArticleTop.GetHashCode()) { query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode()); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); } if (request.DataTypeValue == RankingListConst.Audio.GetHashCode()) { query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Audio.GetHashCode()); query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount)); } if (request.DataTypeValue == RankingListConst.HotComment.GetHashCode()) query.OrderByDesc(nameof(Entity.tede2.Media.Media.CommentScore)); if (request.DataTypeValue == RankingListConst.CollectionBook.GetHashCode()) query.OrderByDesc(nameof(Entity.tede2.Media.Media.CollectCount)); } query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true); query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); query.ForPage(request.Page, request.PerPage); var result = await _repository.GetAllAsync(query); foreach (var item in result) item.ImageUrls = StringUtils.AddDomain(item.ImageUrls); return result; } /// /// 排行榜分页 /// /// /// public async Task> GetRankingPageListResults(RankingListRequest request) { var result = new PagedList(); //获取媒体 var query = Q.NewQuery(); query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Summary)); query.Select(nameof(Entity.tede2.Media.Media.TeacherId)); query.Select(nameof(Entity.tede2.Media.Media.AutoNumber)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.MediaType)); query.Select(nameof(Entity.tede2.Media.Media.CommentScore)); query.Select(nameof(Entity.tede2.Media.Media.CreatedDate)); query.Select(nameof(Entity.tede2.Media.Media.ReadCount)); if (!string.IsNullOrEmpty(request.KeyWord)) query.WhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%"); //1 热搜 2热书榜 3 课程榜 4Top 100 新书 5文献头条 6试听榜 7大众热评 8Top 100 专著 if (request.DataTypeValue > 0) { //1 热搜 if (request.DataTypeValue == RankingListConst.HotSearch.GetHashCode()) query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount)); //2热书榜 if (request.DataTypeValue == RankingListConst.ReadBook.GetHashCode()) { query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode()); query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount)); } //3 课程榜 if (request.DataTypeValue == RankingListConst.Article.GetHashCode()) { query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode()); query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount)); } //Top 100 新书 if (request.DataTypeValue == RankingListConst.NewBook.GetHashCode()) { query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode()); query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate)); } //文献头条 if (request.DataTypeValue == RankingListConst.ArticleTop.GetHashCode()) { query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode()); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); } if (request.DataTypeValue == RankingListConst.Audio.GetHashCode()) { query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Audio.GetHashCode()); query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount)); } if (request.DataTypeValue == RankingListConst.HotComment.GetHashCode()) query.OrderByDesc(nameof(Entity.tede2.Media.Media.CommentScore)); if (request.DataTypeValue == RankingListConst.CollectionBook.GetHashCode()) query.OrderByDesc(nameof(Entity.tede2.Media.Media.CollectCount)); } query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true); query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false); result.Total = await _repository.CountAsync(query); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); query.ForPage(request.Page, request.PerPage); result.Items = await _repository.GetAllAsync(query); foreach (var item in result.Items) item.ImageUrls = StringUtils.AddDomain(item.ImageUrls); return result; } /// /// 大咖讲座 /// /// public async Task> GetMediasByTeacherAsync(ArticleGroupRequest request) { var query = Q.NewQuery(); query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.Summary)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.MediaType)); query.Select(nameof(Entity.tede2.Media.Media.ReadCount)); query.Select(nameof(Entity.tede2.Media.Media.CreatedDate)); query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0); query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode()); if (!string.IsNullOrEmpty(request.Sort)) { query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); } else query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate)); query.ForPage(request.Page, request.PerPage); return await _repository.GetAllAsync(query); } /// /// 大咖讲座 /// /// public async Task GetMediasByTeacherCountAsync(ArticleGroupRequest request) { var query = Q.NewQuery(); query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0); query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode()); return await _repository.CountAsync(query); } /// /// 每日技能分页 /// /// public async Task> GetDaySkillsAsync(MediaDayWeBookRequest request) { var resut = new PagedList(); var query = Q.NewQuery(); query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.MediaType)); query.Select(nameof(Entity.tede2.Media.Media.ReadCount)); query.Select(nameof(Entity.tede2.Media.Media.CreatedDate)); query.Select(nameof(Entity.tede2.Media.Media.Summary)); query.Where(nameof(Entity.tede2.Media.Media.IsExercises), true); query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.TypeValue); resut.Total = await _repository.CountAsync(query); if (!string.IsNullOrEmpty(request.Sort)) { query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); } else query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate)); query.ForPage(request.Page, request.PerPage); resut.Items = await _repository.GetAllAsync(query); foreach (var item in resut.Items) item.ImageUrls = StringUtils.AddDomain(item.ImageUrls); return resut; } /// /// 每日技能 /// /// public async Task GetRandomDaySkillAsync() { var sql = $@"SELECT Id,Blocks,MediaType FROM tede_media where IsExercises=1 and MediaType={AllTypeConst.Article.GetHashCode()} ORDER BY RAND() LIMIT 1"; var databaseType = _databaseTypestr.ToEnum(DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); return await connection.QueryFirstOrDefaultAsync(sql); } /// /// 猜你喜欢 /// /// public async Task> GetUserLikeAsync() { var sql = $@"SELECT Id,ImageUrls,Title,Author,MediaType,ReadCount,CreatedDate,Summary FROM tede_media where IsChecked=1 and MediaType={AllTypeConst.Book.GetHashCode()} ORDER BY RAND() LIMIT 3"; var databaseType = _databaseTypestr.ToEnum(DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); return await connection.QueryAsync(sql); } /// /// 导航内容 /// /// public async Task> GetContentNavigationAsync(MediaContentNavigationRequest request) { var resut = new PagedList(); var query = Q.NewQuery(); query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.MediaType)); query.Select(nameof(Entity.tede2.Media.Media.ReadCount)); query.Select(nameof(Entity.tede2.Media.Media.CreatedDate)); query.Select(nameof(Entity.tede2.Media.Media.Summary)); if (request.MediaIds.Count() > 0) query.WhereIn(nameof(Entity.tede2.Media.Media.Id), request.MediaIds); if (request.TeacherId != null && request.TeacherId.Count > 0) query.WhereIn(nameof(Entity.tede2.Media.Media.TeacherId), request.TeacherId); if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime)) { query.Where(nameof(Entity.tede2.Media.Media.AddDate), ">=", request.BeginTime); query.Where(nameof(Entity.tede2.Media.Media.AddDate), "<=", request.EndTime); } if (request.Press != null && request.Press.Count > 0) { query.OrWhereIn(nameof(Entity.tede2.Media.Media.Press), request.Press); } resut.Total = await _repository.CountAsync(query); if (!string.IsNullOrEmpty(request.Sort)) { if (request.Sort.Equals("0")) { query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); } if (request.Sort.Equals("1")) { query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); } if (request.Sort.Equals("2")) { query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount)); } if (request.Sort.Equals("3")) { query.OrderByDesc(nameof(Entity.tede2.Media.Media.AddDate)); } if (request.Sort.Equals("4")) { query.OrderBy(nameof(Entity.tede2.Media.Media.AddDate)); } } else query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate)); query.ForPage(request.Page, request.PerPage); resut.Items = await _repository.GetAllAsync(query); foreach (var item in resut.Items) { item.ImageUrls = StringUtils.AddDomain(item.ImageUrls); } return resut; } /// /// 明栏分页 /// /// public async Task> GetStarLablePageAsync(MediaStarLableRequest request) { var resut = new PagedList(); var query = Q.NewQuery(); query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.MediaType)); query.Select(nameof(Entity.tede2.Media.Media.ReadCount)); query.Select(nameof(Entity.tede2.Media.Media.Summary)); query.Select(nameof(Entity.tede2.Media.Media.CreatedDate)); if (!string.IsNullOrEmpty(request.KeyWord)) query.WhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%"); query.WhereIn(nameof(Entity.tede2.Media.Media.Id), request.MediaIds); resut.Total = await _repository.CountAsync(query); if (!string.IsNullOrEmpty(request.Sort)) { query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); } else query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate)); query.ForPage(request.Page, request.PerPage); resut.Items = await _repository.GetAllAsync(query); foreach (var item in resut.Items) { item.ImageUrls = StringUtils.AddDomain(item.ImageUrls); } return resut; } /// /// 明栏 /// /// public async Task> GetStarLablesAsync(MediaStarLableRequest request) { var query = Q.NewQuery(); query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.MediaType)); query.Select(nameof(Entity.tede2.Media.Media.ReadCount)); query.Select(nameof(Entity.tede2.Media.Media.CreatedDate)); query.Select(nameof(Entity.tede2.Media.Media.Summary)); if (!string.IsNullOrEmpty(request.KeyWord)) query.WhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%"); query.WhereIn(nameof(Entity.tede2.Media.Media.Id), request.MediaIds); if (!string.IsNullOrEmpty(request.Sort)) { query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); } else query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate)); query.ForPage(request.Page, request.PerPage); var reuslt = await _repository.GetAllAsync(query); foreach (var item in reuslt) { item.ImageUrls = StringUtils.AddDomain(item.ImageUrls); } return reuslt; } /// /// app频道 /// /// public async Task> GetAppChannelsAsync(AppChannelSearchRequest request) { var resut = new PagedList(); var query = Q.NewQuery(); query.Select(nameof(Entity.tede2.Media.Media.Id)); query.Select(nameof(Entity.tede2.Media.Media.ImageUrls)); query.Select(nameof(Entity.tede2.Media.Media.Title)); query.Select(nameof(Entity.tede2.Media.Media.Author)); query.Select(nameof(Entity.tede2.Media.Media.MediaType)); query.Select(nameof(Entity.tede2.Media.Media.ReadCount)); query.Select(nameof(Entity.tede2.Media.Media.CreatedDate)); query.Select(nameof(Entity.tede2.Media.Media.Summary)); query.Select(nameof(Entity.tede2.Media.Media.CommentScore)); query.WhereIn(nameof(Entity.tede2.Media.Media.Id), request.Ids); //最热 if (request.SortSift == 1) query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount)); //最新 if (request.SortSift == 2) query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate)); //好评 if (request.SortSift == 3) query.OrderByDesc(nameof(Entity.tede2.Media.Media.CommentScore)); if (request.PressSift != null && request.PressSift.Count() > 0) query.WhereIn(nameof(Entity.tede2.Media.Media.Press), request.PressSift); if (!string.IsNullOrEmpty(request.KeyWord)) query.WhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%"); resut.Total = await _repository.CountAsync(query); if (!string.IsNullOrEmpty(request.Sort)) { query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop)); query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend)); } else query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate)); query.ForPage(request.Page, request.PerPage); resut.Items = await _repository.GetAllAsync(query); foreach (var item in resut.Items) item.ImageUrls = StringUtils.AddDomain(item.ImageUrls); return resut; } /// /// 获取出版社 /// /// public async Task> GetPresssAsync() { var query = Q.NewQuery(); query.Where(nameof(Entity.tede2.Media.Media.Id), ">", 0); query.GroupBy(nameof(Entity.tede2.Media.Media.Press)); query.Select(nameof(Entity.tede2.Media.Media.Press)); query.Where(nameof(Entity.tede2.Media.Media.Press), "<>", ""); return await _repository.GetAllAsync(query); } } }