MediaRepository.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. using System.Linq;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Datory;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Repository.Interface.Media;
  9. using GxPress.Result.Media;
  10. using Microsoft.Extensions.Options;
  11. using System.Transactions;
  12. using GxPress.Common.Exceptions;
  13. using GxPress.Common.Page;
  14. using GxPress.Request.Media;
  15. using System;
  16. using GxPress.EnumConst;
  17. using GxPress.Request.ArticleGroup;
  18. using Dapper;
  19. namespace GxPress.Repository.Implement.Media
  20. {
  21. public class MediaRepository : IMediaRepository
  22. {
  23. private readonly Repository<Entity.tede2.Media.Media> _repository;
  24. private readonly Repository<Entity.tede2.Media.MediaLibrary> mediaLibraryRepository;
  25. private readonly Repository<Entity.tede2.Media.MediaLable> mediaLableRepository;
  26. private readonly IMapper _mapper;
  27. private readonly string _connectionString;
  28. private readonly string _databaseTypestr;
  29. public MediaRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  30. {
  31. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  32. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  33. var databaseType =
  34. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  35. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  36. _repository = new Repository<Entity.tede2.Media.Media>(database);
  37. mediaLibraryRepository = new Repository<Entity.tede2.Media.MediaLibrary>(database);
  38. mediaLableRepository = new Repository<Entity.tede2.Media.MediaLable>(database);
  39. _mapper = mapper;
  40. }
  41. public IDatabase Database => _repository.Database;
  42. public string TableName => _repository.TableName;
  43. public List<TableColumn> TableColumns => _repository.TableColumns;
  44. public async Task<MediaResult> GetAsync(int id)
  45. {
  46. var result = new MediaResult();
  47. //获取媒体
  48. var media = await _repository.GetAsync(id);
  49. result = _mapper.Map<MediaResult>(media);
  50. result.ImageUrls = StringUtils.AddDomain(result.ImageUrls);
  51. result.CategoryId = StringUtils.StringCollectionToStringList(media.CategoryId).ToList();
  52. result.CategoryName = StringUtils.StringCollectionToStringList(media.CategoryName).ToList();
  53. result.FullCategoryId = StringUtils.StringCollectionToStringList(media.FullCategoryId).ToList();
  54. //获取媒体库资源
  55. var mediaLibrarys = await mediaLibraryRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
  56. result.MediaLibraryResults = mediaLibrarys.ToList().Select(n => _mapper.Map<MediaLibraryResult>(n)).ToList();
  57. foreach (var item in result.MediaLibraryResults)
  58. {
  59. item.FileUrl = StringUtils.AddDomain(item.FileUrl);
  60. }
  61. //获取媒体标签
  62. var mediaLables = await mediaLableRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));
  63. result.MediaLableResults = mediaLables.Select(n => _mapper.Map<MediaLableResult>(n)).ToList();
  64. return result;
  65. }
  66. /// <summary>
  67. /// 删除媒体
  68. /// </summary>
  69. /// <param name="id"></param>
  70. /// <returns></returns>
  71. public async Task<bool> DeleteAsync(int id)
  72. {
  73. try
  74. {
  75. using (var transactionScope = new TransactionScope())
  76. {
  77. // //删除媒体库
  78. // await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
  79. // //删除媒体标签
  80. // await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));
  81. await _repository.UpdateAsync(Q.Where(nameof(Entity.tede2.Media.Media.Id), id).Set(nameof(Entity.tede2.Media.Media.IsDelete), true));
  82. transactionScope.Complete();
  83. }
  84. }
  85. catch (System.Exception ex)
  86. {
  87. throw new BusinessException(ex.Message);
  88. }
  89. return true;
  90. }
  91. /// <summary>
  92. /// 添加媒体
  93. /// </summary>
  94. /// <param name="result"></param>
  95. /// <returns></returns>
  96. public async Task<bool> InsertAsync(MediaResult result)
  97. {
  98. var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  99. model.ImageUrls = StringUtils.RemoveDomain(result.ImageUrls);
  100. try
  101. {
  102. using (var transactionScope = new TransactionScope())
  103. {
  104. model.AutoNumber = SnowflakeHelper.MakeSnowflake();
  105. var categoryId = "";
  106. foreach (var item in result.CategoryId)
  107. {
  108. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  109. }
  110. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  111. model.CategoryId = categoryId;
  112. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  113. var categoryName = "";
  114. foreach (var item in result.CategoryName)
  115. {
  116. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  117. }
  118. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  119. model.CategoryName = categoryName;
  120. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  121. var id = await _repository.InsertAsync(model);
  122. if (id > 0)
  123. {
  124. foreach (var item in result.MediaLibraryResults)
  125. {
  126. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  127. mediaLibrary.MediaId = id;
  128. mediaLibrary.FileUrl = StringUtils.RemoveDomain(item.FileUrl);
  129. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  130. }
  131. }
  132. //添加标签
  133. foreach (var item in result.MediaLableResults)
  134. {
  135. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  136. mediaLable.MediaId = id;
  137. await mediaLableRepository.InsertAsync(mediaLable);
  138. }
  139. if (result.Id == 0 && result.TeacherId == 0 && !string.IsNullOrEmpty(result.Author))
  140. {
  141. //新增名师
  142. }
  143. //提交事务
  144. transactionScope.Complete();
  145. }
  146. }
  147. catch (System.Exception ex)
  148. {
  149. throw new Common.Exceptions.BusinessException(ex.Message);
  150. }
  151. return true;
  152. }
  153. /// <summary>
  154. /// 获取媒体
  155. /// </summary>
  156. /// <param name="query"></param>
  157. /// <returns></returns>
  158. public async Task<MediaNoteResult> GetAsync(SqlKata.Query query)
  159. {
  160. query.Select(nameof(Entity.tede2.Media.Media.Id));
  161. query.Select(nameof(Entity.tede2.Media.Media.Title));
  162. query.Select(nameof(Entity.tede2.Media.Media.Author));
  163. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  164. return await _repository.GetAsync<MediaNoteResult>(query);
  165. }
  166. public async Task<bool> UpdateAsync(Entity.tede2.Media.Media note)
  167. {
  168. return await _repository.UpdateAsync(note);
  169. }
  170. public async Task<bool> UpdateAsync(SqlKata.Query query)
  171. {
  172. return await _repository.UpdateAsync(query) > 0;
  173. }
  174. /// <summary>
  175. /// 查询
  176. /// </summary>
  177. /// <param name="request"></param>
  178. /// <returns></returns>
  179. public async Task<PagedList<Entity.tede2.Media.Media>> GetAllAsync(MediaSearchRequest request)
  180. {
  181. var result = new PagedList<Entity.tede2.Media.Media>();
  182. var query = Q.NewQuery();
  183. if (!string.IsNullOrEmpty(request.KeyWord))
  184. {
  185. query.OrWhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%");
  186. query.OrWhereLike(nameof(Entity.tede2.Media.Media.Creator), $"%{request.KeyWord}%");
  187. }
  188. if (request.IsDelete > 0)
  189. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), request.IsDelete == 1);
  190. if (request.MediaType > 0)
  191. query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.MediaType);
  192. if (request.LibraryType > 0)
  193. query.Where(nameof(Entity.tede2.Media.Media.IsLibrary), request.LibraryType == 1);
  194. if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
  195. {
  196. if (DateTime.TryParse(request.BeginTime, out var beginTime) && DateTime.TryParse(request.EndTime, out var endTime))
  197. {
  198. query.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), ">=", beginTime);
  199. query.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), "<=", endTime);
  200. }
  201. }
  202. if (request.IsChecked > 0)
  203. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), request.IsChecked == 1);
  204. result.Total = await _repository.CountAsync(query);
  205. //排序
  206. if (string.IsNullOrEmpty(request.Sort))
  207. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  208. else if (request.Sort.Equals("SellAmount"))
  209. query.OrderByDesc(nameof(Entity.tede2.Media.Media.SellAmount));
  210. else if (request.Sort.Equals("ReadCount"))
  211. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  212. var item = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  213. result.Items = item;
  214. return result;
  215. }
  216. /// <summary>
  217. /// 根据IDs
  218. /// </summary>
  219. /// <returns></returns>
  220. public async Task<IEnumerable<MediaCathedraResult>> GetMediaByLableIdsAsync(MediaIdsRequest request)
  221. {
  222. if (request.Ids.Count > 0)
  223. {
  224. var str = "";
  225. foreach (var item in request.Ids)
  226. str += item + ",";
  227. str = str.Remove(str.Length - 1, 1);
  228. var sql = $@"SELECT
  229. Id,ImageUrls,Title,Author,MediaType,ReadCount,CreatedDate
  230. FROM
  231. tede_media
  232. WHERE
  233. id IN ({str})
  234. ORDER BY RAND() LIMIT {request.Limit}";
  235. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  236. var database = new Database(databaseType, _connectionString);
  237. var connection = database.GetConnection();
  238. return await connection.QueryAsync<MediaCathedraResult>(sql);
  239. }
  240. return null;
  241. }
  242. /// <summary>
  243. /// 根据IDs
  244. /// </summary>
  245. /// <returns></returns>
  246. public async Task<PagedList<MediaCathedraResult>> GetMediaByLableIdAsync(MediaLableIdRequest request)
  247. {
  248. var result = new PagedList<MediaCathedraResult>();
  249. if (request.Ids.Count > 0)
  250. {
  251. var str = "";
  252. foreach (var item in request.Ids)
  253. str += item + ",";
  254. str = str.Remove(str.Length - 1, 1);
  255. var sql = $@"SELECT
  256. Id,ImageUrls,Title,Author,MediaType,ReadCount,CreatedDate
  257. FROM
  258. tede_media
  259. WHERE
  260. id IN ({str})
  261. ORDER BY CreatedDate desc LIMIT {(request.Page - 1) * request.PerPage},{request.PerPage}";
  262. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  263. var database = new Database(databaseType, _connectionString);
  264. var connection = database.GetConnection();
  265. result.Items = await connection.QueryAsync<MediaCathedraResult>(sql);
  266. sql = $@"SELECT count(1) FROM tede_media WHERE id IN ({str})";
  267. result.Total = await connection.ExecuteScalarAsync<int>(sql);
  268. }
  269. return null;
  270. }
  271. public async Task<bool> UpdateAsync(MediaResult result)
  272. {
  273. //var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  274. try
  275. {
  276. using (var transactionScope = new TransactionScope())
  277. {
  278. var model = await _repository.GetAsync(result.Id);
  279. if (result.CategoryId != null && result.CategoryId.Count > 0)
  280. {
  281. var categoryId = "";
  282. foreach (var item in result.CategoryId)
  283. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  284. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  285. model.CategoryId = categoryId;
  286. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  287. }
  288. if (result.CategoryName != null && result.CategoryName.Count > 0)
  289. {
  290. var categoryName = "";
  291. foreach (var item in result.CategoryName)
  292. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  293. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  294. model.CategoryName = categoryName;
  295. }
  296. if (result.AdminId > 0)
  297. model.AdminId = result.AdminId;
  298. if (!string.IsNullOrEmpty(result.Creator))
  299. model.Creator = result.Creator;
  300. if (result.MediaType > 0)
  301. model.MediaType = result.MediaType;
  302. if (!string.IsNullOrEmpty(result.Title))
  303. model.Title = result.Title;
  304. if (!string.IsNullOrEmpty(result.ImageUrls))
  305. model.ImageUrls = result.ImageUrls;
  306. if (!string.IsNullOrEmpty(result.Summary))
  307. model.Summary = result.Summary;
  308. if (!string.IsNullOrEmpty(result.Author))
  309. model.Author = result.Author;
  310. if (result.TeacherId > 0)
  311. model.TeacherId = result.TeacherId;
  312. if (!string.IsNullOrEmpty(result.Source))
  313. model.Source = result.Source;
  314. if (!string.IsNullOrEmpty(result.Blocks))
  315. model.Blocks = result.Blocks;
  316. if (result.IsChecked > 0)
  317. model.IsChecked = result.IsChecked == 1;
  318. if (!string.IsNullOrEmpty(result.AddDate))
  319. model.AddDate = Convert.ToDateTime(result.AddDate);
  320. if (model.Sort > 0)
  321. model.Sort = result.Sort;
  322. if (model.ReadCount > 0)
  323. model.ReadCount = result.ReadCount;
  324. if (result.IsTop > 0)
  325. model.IsTop = result.IsTop == 1;
  326. if (result.LableId > 0)
  327. model.LableId = result.LableId;
  328. if (result.IsRecommend > 0)
  329. model.IsRecommend = result.IsRecommend == 1;
  330. if (!string.IsNullOrEmpty(result.Press))
  331. model.Press = result.Press;
  332. if (!string.IsNullOrEmpty(result.PublishTime))
  333. model.PublishTime = result.PublishTime;
  334. if (result.IosDiscountPrice > 0)
  335. model.IosDiscountPrice = result.IosDiscountPrice;
  336. if (result.IosPrice > 0)
  337. model.IosPrice = result.IosPrice;
  338. if (result.OtherDiscountPrice > 0)
  339. model.OtherDiscountPrice = result.OtherDiscountPrice;
  340. if (result.OtherPrice > 0)
  341. model.OtherPrice = result.OtherPrice;
  342. if (result.FreeProportion > 0)
  343. model.FreeProportion = result.FreeProportion;
  344. if (result.AttachType > 0)
  345. model.AttachType = result.AttachType;
  346. if (result.AttachId > 0)
  347. model.AttachId = result.AttachId;
  348. if (result.MediaId > 0)
  349. model.MediaId = result.MediaId;
  350. if (!string.IsNullOrEmpty(result.MediaTitle))
  351. model.MediaTitle = result.MediaTitle;
  352. if (!string.IsNullOrEmpty(result.JournalsYear))
  353. model.JournalsYear = result.JournalsYear;
  354. if (result.JournalsCategory > 0)
  355. model.JournalsCategory = result.JournalsCategory;
  356. await _repository.UpdateAsync(model);
  357. if (result.MediaLibraryResults != null && result.MediaLibraryResults.Count > 0)
  358. {
  359. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  360. //删除媒体库
  361. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  362. foreach (var item in result.MediaLibraryResults)
  363. {
  364. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  365. mediaLibrary.MediaId = result.Id;
  366. mediaLibrary.FileUrl = StringUtils.RemoveDomain(mediaLibrary.FileUrl);
  367. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  368. }
  369. }
  370. if (result.MediaLableResults != null && result.MediaLableResults.Count > 0)
  371. {
  372. //删除媒体标签
  373. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
  374. //添加标签
  375. foreach (var item in result.MediaLableResults)
  376. {
  377. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  378. mediaLable.MediaId = result.Id;
  379. await mediaLableRepository.InsertAsync(mediaLable);
  380. }
  381. }
  382. //提交事务
  383. transactionScope.Complete();
  384. }
  385. }
  386. catch (System.Exception ex)
  387. {
  388. throw new Common.Exceptions.BusinessException(ex.Message);
  389. }
  390. return true;
  391. }
  392. public async Task<int> CountAsync(string beginTime, string endTime)
  393. {
  394. return await _repository.CountAsync(Q.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), ">=", beginTime).WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), "<=", endTime));
  395. }
  396. public async Task<int> CountAsync(SqlKata.Query query)
  397. {
  398. return await _repository.CountAsync(query);
  399. }
  400. public async Task<Entity.tede2.Media.Media> GetAsync(string autoNumber)
  401. {
  402. return await _repository.GetAsync(Q.Where(nameof(Entity.tede2.Media.Media.AutoNumber), autoNumber));
  403. }
  404. /// <summary>
  405. /// 获取书籍
  406. /// </summary>
  407. /// <param name="mediaId"></param>
  408. /// <returns></returns>
  409. public async Task<BookMediaResult> GetBookMediaResultAsync(int mediaId)
  410. {
  411. 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));
  412. return _mapper.Map<BookMediaResult>(result);
  413. }
  414. /// <summary>
  415. /// 获取视频
  416. /// </summary>
  417. /// <param name="mediaId"></param>
  418. /// <returns></returns>
  419. public async Task<VideoMediaResult> GetVideoMediaResultAsync(int mediaId)
  420. {
  421. 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));
  422. return _mapper.Map<VideoMediaResult>(result);
  423. }
  424. /// <summary>
  425. /// 根据
  426. /// </summary>
  427. /// <param name="mediaIds"></param>
  428. /// <returns></returns>
  429. public async Task<IEnumerable<NavigationMediaResult>> GetNavigationMediaResults(IEnumerable<int> mediaIds, int pageSize = 10)
  430. {
  431. //获取媒体
  432. var query = Q.NewQuery();
  433. query.Select(nameof(Entity.tede2.Media.Media.Id));
  434. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  435. query.Select(nameof(Entity.tede2.Media.Media.Title));
  436. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  437. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  438. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  439. query.Select(nameof(Entity.tede2.Media.Media.Author));
  440. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  441. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  442. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  443. query.Select(nameof(Entity.tede2.Media.Media.CollectCount));
  444. query.WhereIn(nameof(Entity.tede2.Media.Media.Id), mediaIds);
  445. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  446. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  447. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  448. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  449. query.OrderByDesc(nameof(Entity.tede2.Media.Media.LastModifiedDate));
  450. query.ForPage(1, pageSize);
  451. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  452. }
  453. /// <summary>
  454. /// 猜你喜欢
  455. /// </summary>
  456. /// <param name="mediaIds"></param>
  457. /// <returns></returns>
  458. public async Task<IEnumerable<NavigationMediaResult>> GetlikeMediaResults()
  459. {
  460. //获取媒体
  461. var query = Q.NewQuery();
  462. query.Select(nameof(Entity.tede2.Media.Media.Id));
  463. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  464. query.Select(nameof(Entity.tede2.Media.Media.Title));
  465. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  466. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  467. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  468. query.Select(nameof(Entity.tede2.Media.Media.Author));
  469. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  470. query.Where(nameof(Entity.tede2.Media.Media.IsTop), true);
  471. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  472. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  473. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  474. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  475. query.ForPage(1, 10);
  476. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  477. }
  478. /// <summary>
  479. /// 推荐
  480. /// </summary>
  481. /// <param name="mediaIds"></param>
  482. /// <returns></returns>
  483. public async Task<IEnumerable<NavigationMediaResult>> GetRecommendMediaResults()
  484. {
  485. //获取媒体
  486. var query = Q.NewQuery();
  487. query.Select(nameof(Entity.tede2.Media.Media.Id));
  488. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  489. query.Select(nameof(Entity.tede2.Media.Media.Title));
  490. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  491. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  492. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  493. query.Select(nameof(Entity.tede2.Media.Media.Author));
  494. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  495. query.Where(nameof(Entity.tede2.Media.Media.IsRecommend), true);
  496. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  497. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  498. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  499. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  500. query.ForPage(1, 10);
  501. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  502. }
  503. /// <summary>
  504. /// 排行榜
  505. /// </summary>
  506. /// <param name="request"></param>
  507. /// <returns></returns>
  508. public async Task<IEnumerable<RankingListResult>> GetRankingListResults(RankingListRequest request)
  509. {
  510. //获取媒体
  511. var query = Q.NewQuery();
  512. query.Select(nameof(Entity.tede2.Media.Media.Id));
  513. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  514. query.Select(nameof(Entity.tede2.Media.Media.Title));
  515. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  516. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  517. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  518. query.Select(nameof(Entity.tede2.Media.Media.Author));
  519. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  520. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  521. query.Select(nameof(Entity.tede2.Media.Media.CommentScore));
  522. //1 热搜 2热书榜 3 课程榜 4Top 100 新书 5文献头条 6试听榜 7大众热评 8Top 100 专著
  523. if (request.DataTypeValue > 0)
  524. {
  525. //1 热搜
  526. if (request.DataTypeValue == RankingListConst.HotSearch.GetHashCode())
  527. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  528. //2热书榜
  529. if (request.DataTypeValue == RankingListConst.ReadBook.GetHashCode())
  530. {
  531. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  532. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  533. }
  534. //3 课程榜
  535. if (request.DataTypeValue == RankingListConst.Article.GetHashCode())
  536. {
  537. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  538. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  539. }
  540. //Top 100 新书
  541. if (request.DataTypeValue == RankingListConst.NewBook.GetHashCode())
  542. {
  543. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  544. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  545. }
  546. //文献头条
  547. if (request.DataTypeValue == RankingListConst.ArticleTop.GetHashCode())
  548. {
  549. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  550. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  551. }
  552. if (request.DataTypeValue == RankingListConst.Audio.GetHashCode())
  553. {
  554. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Audio.GetHashCode());
  555. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  556. }
  557. if (request.DataTypeValue == RankingListConst.HotComment.GetHashCode())
  558. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CommentScore));
  559. if (request.DataTypeValue == RankingListConst.CollectionBook.GetHashCode())
  560. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CollectCount));
  561. }
  562. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  563. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  564. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  565. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  566. query.ForPage(request.Page, request.PerPage);
  567. return await _repository.GetAllAsync<RankingListResult>(query);
  568. }
  569. /// <summary>
  570. /// 大咖讲座
  571. /// </summary>
  572. /// <returns></returns>
  573. public async Task<IEnumerable<MediaCathedraResult>> GetMediasByTeacherAsync(ArticleGroupRequest request)
  574. {
  575. var query = Q.NewQuery();
  576. query.Select(nameof(Entity.tede2.Media.Media.Id));
  577. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  578. query.Select(nameof(Entity.tede2.Media.Media.Title));
  579. query.Select(nameof(Entity.tede2.Media.Media.Author));
  580. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  581. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  582. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  583. query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0);
  584. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode());
  585. if (!string.IsNullOrEmpty(request.Sort))
  586. {
  587. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  588. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  589. }
  590. else
  591. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  592. query.ForPage(request.Page, request.PerPage);
  593. return await _repository.GetAllAsync<MediaCathedraResult>(query);
  594. }
  595. /// <summary>
  596. /// 大咖讲座
  597. /// </summary>
  598. /// <returns></returns>
  599. public async Task<int> GetMediasByTeacherCountAsync(ArticleGroupRequest request)
  600. {
  601. var query = Q.NewQuery();
  602. query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0);
  603. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode());
  604. return await _repository.CountAsync(query);
  605. }
  606. }
  607. }