MediaRepository.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. if (result.IsExercises > 0)
  357. model.IsExercises = result.IsExercises == 1;
  358. if (result.AppCategoryId > 0)
  359. model.AppCategoryId = result.AppCategoryId;
  360. await _repository.UpdateAsync(model);
  361. if (result.MediaLibraryResults != null && result.MediaLibraryResults.Count > 0)
  362. {
  363. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  364. //删除媒体库
  365. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  366. foreach (var item in result.MediaLibraryResults)
  367. {
  368. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  369. mediaLibrary.MediaId = result.Id;
  370. mediaLibrary.FileUrl = StringUtils.RemoveDomain(mediaLibrary.FileUrl);
  371. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  372. }
  373. }
  374. if (result.MediaLableResults != null && result.MediaLableResults.Count > 0)
  375. {
  376. //删除媒体标签
  377. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
  378. //添加标签
  379. foreach (var item in result.MediaLableResults)
  380. {
  381. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  382. mediaLable.MediaId = result.Id;
  383. await mediaLableRepository.InsertAsync(mediaLable);
  384. }
  385. }
  386. //提交事务
  387. transactionScope.Complete();
  388. }
  389. }
  390. catch (System.Exception ex)
  391. {
  392. throw new Common.Exceptions.BusinessException(ex.Message);
  393. }
  394. return true;
  395. }
  396. public async Task<int> CountAsync(string beginTime, string endTime)
  397. {
  398. return await _repository.CountAsync(Q.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), ">=", beginTime).WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), "<=", endTime));
  399. }
  400. public async Task<int> CountAsync(SqlKata.Query query)
  401. {
  402. return await _repository.CountAsync(query);
  403. }
  404. public async Task<Entity.tede2.Media.Media> GetAsync(string autoNumber)
  405. {
  406. return await _repository.GetAsync(Q.Where(nameof(Entity.tede2.Media.Media.AutoNumber), autoNumber));
  407. }
  408. /// <summary>
  409. /// 获取书籍
  410. /// </summary>
  411. /// <param name="mediaId"></param>
  412. /// <returns></returns>
  413. public async Task<BookMediaResult> GetBookMediaResultAsync(int mediaId)
  414. {
  415. 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));
  416. return _mapper.Map<BookMediaResult>(result);
  417. }
  418. /// <summary>
  419. /// 获取视频
  420. /// </summary>
  421. /// <param name="mediaId"></param>
  422. /// <returns></returns>
  423. public async Task<VideoMediaResult> GetVideoMediaResultAsync(int mediaId)
  424. {
  425. 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));
  426. return _mapper.Map<VideoMediaResult>(result);
  427. }
  428. /// <summary>
  429. /// 根据
  430. /// </summary>
  431. /// <param name="mediaIds"></param>
  432. /// <returns></returns>
  433. public async Task<IEnumerable<NavigationMediaResult>> GetNavigationMediaResults(IEnumerable<int> mediaIds, int page = 1, int pageSize = 10)
  434. {
  435. //获取媒体
  436. var query = Q.NewQuery();
  437. query.Select(nameof(Entity.tede2.Media.Media.Id));
  438. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  439. query.Select(nameof(Entity.tede2.Media.Media.Title));
  440. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  441. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  442. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  443. query.Select(nameof(Entity.tede2.Media.Media.Author));
  444. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  445. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  446. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  447. query.Select(nameof(Entity.tede2.Media.Media.CollectCount));
  448. query.WhereIn(nameof(Entity.tede2.Media.Media.Id), mediaIds);
  449. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  450. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  451. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  452. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  453. query.OrderByDesc(nameof(Entity.tede2.Media.Media.LastModifiedDate));
  454. query.ForPage(page, pageSize);
  455. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  456. }
  457. /// <summary>
  458. /// 猜你喜欢
  459. /// </summary>
  460. /// <param name="mediaIds"></param>
  461. /// <returns></returns>
  462. public async Task<IEnumerable<NavigationMediaResult>> GetlikeMediaResults()
  463. {
  464. //获取媒体
  465. var query = Q.NewQuery();
  466. query.Select(nameof(Entity.tede2.Media.Media.Id));
  467. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  468. query.Select(nameof(Entity.tede2.Media.Media.Title));
  469. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  470. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  471. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  472. query.Select(nameof(Entity.tede2.Media.Media.Author));
  473. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  474. query.Where(nameof(Entity.tede2.Media.Media.IsTop), true);
  475. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  476. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  477. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  478. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  479. query.ForPage(1, 10);
  480. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  481. }
  482. /// <summary>
  483. /// 推荐
  484. /// </summary>
  485. /// <param name="mediaIds"></param>
  486. /// <returns></returns>
  487. public async Task<IEnumerable<NavigationMediaResult>> GetRecommendMediaResults()
  488. {
  489. //获取媒体
  490. var query = Q.NewQuery();
  491. query.Select(nameof(Entity.tede2.Media.Media.Id));
  492. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  493. query.Select(nameof(Entity.tede2.Media.Media.Title));
  494. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  495. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  496. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  497. query.Select(nameof(Entity.tede2.Media.Media.Author));
  498. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  499. query.Where(nameof(Entity.tede2.Media.Media.IsRecommend), true);
  500. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  501. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  502. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  503. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  504. query.ForPage(1, 10);
  505. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  506. }
  507. /// <summary>
  508. /// 排行榜
  509. /// </summary>
  510. /// <param name="request"></param>
  511. /// <returns></returns>
  512. public async Task<IEnumerable<RankingListResult>> GetRankingListResults(RankingListRequest request)
  513. {
  514. //获取媒体
  515. var query = Q.NewQuery();
  516. query.Select(nameof(Entity.tede2.Media.Media.Id));
  517. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  518. query.Select(nameof(Entity.tede2.Media.Media.Title));
  519. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  520. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  521. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  522. query.Select(nameof(Entity.tede2.Media.Media.Author));
  523. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  524. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  525. query.Select(nameof(Entity.tede2.Media.Media.CommentScore));
  526. //1 热搜 2热书榜 3 课程榜 4Top 100 新书 5文献头条 6试听榜 7大众热评 8Top 100 专著
  527. if (request.DataTypeValue > 0)
  528. {
  529. //1 热搜
  530. if (request.DataTypeValue == RankingListConst.HotSearch.GetHashCode())
  531. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  532. //2热书榜
  533. if (request.DataTypeValue == RankingListConst.ReadBook.GetHashCode())
  534. {
  535. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  536. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  537. }
  538. //3 课程榜
  539. if (request.DataTypeValue == RankingListConst.Article.GetHashCode())
  540. {
  541. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  542. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  543. }
  544. //Top 100 新书
  545. if (request.DataTypeValue == RankingListConst.NewBook.GetHashCode())
  546. {
  547. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  548. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  549. }
  550. //文献头条
  551. if (request.DataTypeValue == RankingListConst.ArticleTop.GetHashCode())
  552. {
  553. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  554. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  555. }
  556. if (request.DataTypeValue == RankingListConst.Audio.GetHashCode())
  557. {
  558. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Audio.GetHashCode());
  559. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  560. }
  561. if (request.DataTypeValue == RankingListConst.HotComment.GetHashCode())
  562. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CommentScore));
  563. if (request.DataTypeValue == RankingListConst.CollectionBook.GetHashCode())
  564. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CollectCount));
  565. }
  566. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  567. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  568. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  569. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  570. query.ForPage(request.Page, request.PerPage);
  571. return await _repository.GetAllAsync<RankingListResult>(query);
  572. }
  573. /// <summary>
  574. /// 大咖讲座
  575. /// </summary>
  576. /// <returns></returns>
  577. public async Task<IEnumerable<MediaCathedraResult>> GetMediasByTeacherAsync(ArticleGroupRequest request)
  578. {
  579. var query = Q.NewQuery();
  580. query.Select(nameof(Entity.tede2.Media.Media.Id));
  581. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  582. query.Select(nameof(Entity.tede2.Media.Media.Title));
  583. query.Select(nameof(Entity.tede2.Media.Media.Author));
  584. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  585. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  586. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  587. query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0);
  588. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode());
  589. if (!string.IsNullOrEmpty(request.Sort))
  590. {
  591. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  592. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  593. }
  594. else
  595. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  596. query.ForPage(request.Page, request.PerPage);
  597. return await _repository.GetAllAsync<MediaCathedraResult>(query);
  598. }
  599. /// <summary>
  600. /// 大咖讲座
  601. /// </summary>
  602. /// <returns></returns>
  603. public async Task<int> GetMediasByTeacherCountAsync(ArticleGroupRequest request)
  604. {
  605. var query = Q.NewQuery();
  606. query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0);
  607. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode());
  608. return await _repository.CountAsync(query);
  609. }
  610. /// <summary>
  611. /// 每日技能分页
  612. /// </summary>
  613. /// <returns></returns>
  614. public async Task<PagedList<MediaCathedraResult>> GetDaySkillsAsync(MediaDayWeBookRequest request)
  615. {
  616. var resut = new PagedList<MediaCathedraResult>();
  617. var query = Q.NewQuery();
  618. query.Select(nameof(Entity.tede2.Media.Media.Id));
  619. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  620. query.Select(nameof(Entity.tede2.Media.Media.Title));
  621. query.Select(nameof(Entity.tede2.Media.Media.Author));
  622. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  623. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  624. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  625. query.Where(nameof(Entity.tede2.Media.Media.IsExercises), true);
  626. query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.TypeValue);
  627. resut.Total = await _repository.CountAsync(query);
  628. if (!string.IsNullOrEmpty(request.Sort))
  629. {
  630. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  631. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  632. }
  633. else
  634. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  635. query.ForPage(request.Page, request.PerPage);
  636. resut.Items = await _repository.GetAllAsync<MediaCathedraResult>(query);
  637. return resut;
  638. }
  639. /// <summary>
  640. /// 每日技能
  641. /// </summary>
  642. /// <returns></returns>
  643. public async Task<SkillInfo> GetRandomDaySkillAsync()
  644. {
  645. var sql = $@"SELECT
  646. Id,Blocks,MediaType
  647. FROM
  648. tede_media where IsExercises=1 and MediaType={AllTypeConst.Article.GetHashCode()}
  649. ORDER BY RAND() LIMIT 1";
  650. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  651. var database = new Database(databaseType, _connectionString);
  652. var connection = database.GetConnection();
  653. return await connection.QueryFirstOrDefaultAsync<SkillInfo>(sql);
  654. }
  655. /// <summary>
  656. /// 猜你喜欢
  657. /// </summary>
  658. /// <returns></returns>
  659. public async Task<IEnumerable<MediaCathedraResult>> GetUserLikeAsync()
  660. {
  661. var sql = $@"SELECT
  662. Id,ImageUrls,Title,Author,MediaType,ReadCount,CreatedDate
  663. FROM
  664. tede_media where IsExercises=1 and MediaType={AllTypeConst.Book.GetHashCode()}
  665. ORDER BY RAND() LIMIT 3";
  666. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  667. var database = new Database(databaseType, _connectionString);
  668. var connection = database.GetConnection();
  669. return await connection.QueryAsync<MediaCathedraResult>(sql);
  670. }
  671. }
  672. }