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