MediaRepository.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. public async Task<bool> UpdateAsync(MediaResult result)
  243. {
  244. //var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  245. try
  246. {
  247. using (var transactionScope = new TransactionScope())
  248. {
  249. var model = await _repository.GetAsync(result.Id);
  250. if (result.CategoryId != null && result.CategoryId.Count > 0)
  251. {
  252. var categoryId = "";
  253. foreach (var item in result.CategoryId)
  254. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  255. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  256. model.CategoryId = categoryId;
  257. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  258. }
  259. if (result.CategoryName != null && result.CategoryName.Count > 0)
  260. {
  261. var categoryName = "";
  262. foreach (var item in result.CategoryName)
  263. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  264. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  265. model.CategoryName = categoryName;
  266. }
  267. if (result.AdminId > 0)
  268. model.AdminId = result.AdminId;
  269. if (!string.IsNullOrEmpty(result.Creator))
  270. model.Creator = result.Creator;
  271. if (result.MediaType > 0)
  272. model.MediaType = result.MediaType;
  273. if (!string.IsNullOrEmpty(result.Title))
  274. model.Title = result.Title;
  275. if (!string.IsNullOrEmpty(result.ImageUrls))
  276. model.ImageUrls = result.ImageUrls;
  277. if (!string.IsNullOrEmpty(result.Summary))
  278. model.Summary = result.Summary;
  279. if (!string.IsNullOrEmpty(result.Author))
  280. model.Author = result.Author;
  281. if (result.TeacherId > 0)
  282. model.TeacherId = result.TeacherId;
  283. if (!string.IsNullOrEmpty(result.Source))
  284. model.Source = result.Source;
  285. if (!string.IsNullOrEmpty(result.Blocks))
  286. model.Blocks = result.Blocks;
  287. if (result.IsChecked > 0)
  288. model.IsChecked = result.IsChecked == 1;
  289. if (!string.IsNullOrEmpty(result.AddDate))
  290. model.AddDate = Convert.ToDateTime(result.AddDate);
  291. if (model.Sort > 0)
  292. model.Sort = result.Sort;
  293. if (model.ReadCount > 0)
  294. model.ReadCount = result.ReadCount;
  295. if (result.IsTop > 0)
  296. model.IsTop = result.IsTop == 1;
  297. if (result.LableId > 0)
  298. model.LableId = result.LableId;
  299. if (result.IsRecommend > 0)
  300. model.IsRecommend = result.IsRecommend == 1;
  301. if (!string.IsNullOrEmpty(result.Press))
  302. model.Press = result.Press;
  303. if (!string.IsNullOrEmpty(result.PublishTime))
  304. model.PublishTime = result.PublishTime;
  305. if (result.IosDiscountPrice > 0)
  306. model.IosDiscountPrice = result.IosDiscountPrice;
  307. if (result.IosPrice > 0)
  308. model.IosPrice = result.IosPrice;
  309. if (result.OtherDiscountPrice > 0)
  310. model.OtherDiscountPrice = result.OtherDiscountPrice;
  311. if (result.OtherPrice > 0)
  312. model.OtherPrice = result.OtherPrice;
  313. if (result.FreeProportion > 0)
  314. model.FreeProportion = result.FreeProportion;
  315. if (result.AttachType > 0)
  316. model.AttachType = result.AttachType;
  317. if (result.AttachId > 0)
  318. model.AttachId = result.AttachId;
  319. if (result.MediaId > 0)
  320. model.MediaId = result.MediaId;
  321. if (!string.IsNullOrEmpty(result.MediaTitle))
  322. model.MediaTitle = result.MediaTitle;
  323. if (!string.IsNullOrEmpty(result.JournalsYear))
  324. model.JournalsYear = result.JournalsYear;
  325. if (result.JournalsCategory > 0)
  326. model.JournalsCategory = result.JournalsCategory;
  327. await _repository.UpdateAsync(model);
  328. if (result.MediaLibraryResults != null && result.MediaLibraryResults.Count > 0)
  329. {
  330. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  331. //删除媒体库
  332. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  333. foreach (var item in result.MediaLibraryResults)
  334. {
  335. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  336. mediaLibrary.MediaId = result.Id;
  337. mediaLibrary.FileUrl = StringUtils.RemoveDomain(mediaLibrary.FileUrl);
  338. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  339. }
  340. }
  341. if (result.MediaLableResults != null && result.MediaLableResults.Count > 0)
  342. {
  343. //删除媒体标签
  344. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
  345. //添加标签
  346. foreach (var item in result.MediaLableResults)
  347. {
  348. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  349. mediaLable.MediaId = result.Id;
  350. await mediaLableRepository.InsertAsync(mediaLable);
  351. }
  352. }
  353. //提交事务
  354. transactionScope.Complete();
  355. }
  356. }
  357. catch (System.Exception ex)
  358. {
  359. throw new Common.Exceptions.BusinessException(ex.Message);
  360. }
  361. return true;
  362. }
  363. public async Task<int> CountAsync(string beginTime, string endTime)
  364. {
  365. return await _repository.CountAsync(Q.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), ">=", beginTime).WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), "<=", endTime));
  366. }
  367. public async Task<int> CountAsync(SqlKata.Query query)
  368. {
  369. return await _repository.CountAsync(query);
  370. }
  371. public async Task<Entity.tede2.Media.Media> GetAsync(string autoNumber)
  372. {
  373. return await _repository.GetAsync(Q.Where(nameof(Entity.tede2.Media.Media.AutoNumber), autoNumber));
  374. }
  375. /// <summary>
  376. /// 获取书籍
  377. /// </summary>
  378. /// <param name="mediaId"></param>
  379. /// <returns></returns>
  380. public async Task<BookMediaResult> GetBookMediaResultAsync(int mediaId)
  381. {
  382. 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));
  383. return _mapper.Map<BookMediaResult>(result);
  384. }
  385. /// <summary>
  386. /// 获取视频
  387. /// </summary>
  388. /// <param name="mediaId"></param>
  389. /// <returns></returns>
  390. public async Task<VideoMediaResult> GetVideoMediaResultAsync(int mediaId)
  391. {
  392. 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));
  393. return _mapper.Map<VideoMediaResult>(result);
  394. }
  395. /// <summary>
  396. /// 根据
  397. /// </summary>
  398. /// <param name="mediaIds"></param>
  399. /// <returns></returns>
  400. public async Task<IEnumerable<NavigationMediaResult>> GetNavigationMediaResults(IEnumerable<int> mediaIds, int pageSize = 10)
  401. {
  402. //获取媒体
  403. var query = Q.NewQuery();
  404. query.Select(nameof(Entity.tede2.Media.Media.Id));
  405. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  406. query.Select(nameof(Entity.tede2.Media.Media.Title));
  407. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  408. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  409. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  410. query.Select(nameof(Entity.tede2.Media.Media.Author));
  411. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  412. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  413. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  414. query.Select(nameof(Entity.tede2.Media.Media.CollectCount));
  415. query.WhereIn(nameof(Entity.tede2.Media.Media.Id), mediaIds);
  416. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  417. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  418. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  419. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  420. query.OrderByDesc(nameof(Entity.tede2.Media.Media.LastModifiedDate));
  421. query.ForPage(1, pageSize);
  422. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  423. }
  424. /// <summary>
  425. /// 猜你喜欢
  426. /// </summary>
  427. /// <param name="mediaIds"></param>
  428. /// <returns></returns>
  429. public async Task<IEnumerable<NavigationMediaResult>> GetlikeMediaResults()
  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.Where(nameof(Entity.tede2.Media.Media.IsTop), true);
  442. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  443. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  444. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  445. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  446. query.ForPage(1, 10);
  447. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  448. }
  449. /// <summary>
  450. /// 推荐
  451. /// </summary>
  452. /// <param name="mediaIds"></param>
  453. /// <returns></returns>
  454. public async Task<IEnumerable<NavigationMediaResult>> GetRecommendMediaResults()
  455. {
  456. //获取媒体
  457. var query = Q.NewQuery();
  458. query.Select(nameof(Entity.tede2.Media.Media.Id));
  459. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  460. query.Select(nameof(Entity.tede2.Media.Media.Title));
  461. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  462. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  463. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  464. query.Select(nameof(Entity.tede2.Media.Media.Author));
  465. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  466. query.Where(nameof(Entity.tede2.Media.Media.IsRecommend), true);
  467. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  468. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  469. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  470. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  471. query.ForPage(1, 10);
  472. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  473. }
  474. /// <summary>
  475. /// 排行榜
  476. /// </summary>
  477. /// <param name="request"></param>
  478. /// <returns></returns>
  479. public async Task<IEnumerable<RankingListResult>> GetRankingListResults(RankingListRequest request)
  480. {
  481. //获取媒体
  482. var query = Q.NewQuery();
  483. query.Select(nameof(Entity.tede2.Media.Media.Id));
  484. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  485. query.Select(nameof(Entity.tede2.Media.Media.Title));
  486. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  487. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  488. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  489. query.Select(nameof(Entity.tede2.Media.Media.Author));
  490. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  491. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  492. query.Select(nameof(Entity.tede2.Media.Media.CommentScore));
  493. //1 热搜 2热书榜 3 课程榜 4Top 100 新书 5文献头条 6试听榜 7大众热评 8Top 100 专著
  494. if (request.DataTypeValue > 0)
  495. {
  496. //1 热搜
  497. if (request.DataTypeValue == RankingListConst.HotSearch.GetHashCode())
  498. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  499. //2热书榜
  500. if (request.DataTypeValue == RankingListConst.ReadBook.GetHashCode())
  501. {
  502. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  503. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  504. }
  505. //3 课程榜
  506. if (request.DataTypeValue == RankingListConst.Article.GetHashCode())
  507. {
  508. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  509. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  510. }
  511. //Top 100 新书
  512. if (request.DataTypeValue == RankingListConst.NewBook.GetHashCode())
  513. {
  514. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  515. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  516. }
  517. //文献头条
  518. if (request.DataTypeValue == RankingListConst.ArticleTop.GetHashCode())
  519. {
  520. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  521. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  522. }
  523. if (request.DataTypeValue == RankingListConst.Audio.GetHashCode())
  524. {
  525. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Audio.GetHashCode());
  526. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  527. }
  528. if (request.DataTypeValue == RankingListConst.HotComment.GetHashCode())
  529. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CommentScore));
  530. if (request.DataTypeValue == RankingListConst.CollectionBook.GetHashCode())
  531. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CollectCount));
  532. }
  533. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  534. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  535. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  536. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  537. query.ForPage(request.Page, request.PerPage);
  538. return await _repository.GetAllAsync<RankingListResult>(query);
  539. }
  540. /// <summary>
  541. /// 大咖讲座
  542. /// </summary>
  543. /// <returns></returns>
  544. public async Task<IEnumerable<MediaCathedraResult>> GetMediasByTeacherAsync(ArticleGroupRequest request)
  545. {
  546. var query = Q.NewQuery();
  547. query.Select(nameof(Entity.tede2.Media.Media.Id));
  548. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  549. query.Select(nameof(Entity.tede2.Media.Media.Title));
  550. query.Select(nameof(Entity.tede2.Media.Media.Author));
  551. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  552. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  553. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  554. query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0);
  555. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode());
  556. if (!string.IsNullOrEmpty(request.Sort))
  557. {
  558. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  559. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  560. }
  561. else
  562. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  563. query.ForPage(request.Page, request.PerPage);
  564. return await _repository.GetAllAsync<MediaCathedraResult>(query);
  565. }
  566. /// <summary>
  567. /// 大咖讲座
  568. /// </summary>
  569. /// <returns></returns>
  570. public async Task<int> GetMediasByTeacherCountAsync(ArticleGroupRequest request)
  571. {
  572. var query = Q.NewQuery();
  573. query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0);
  574. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode());
  575. return await _repository.CountAsync(query);
  576. }
  577. }
  578. }