MediaRepository.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 result;
  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. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  527. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  528. //1 热搜 2热书榜 3 课程榜 4Top 100 新书 5文献头条 6试听榜 7大众热评 8Top 100 专著
  529. if (request.DataTypeValue > 0)
  530. {
  531. //1 热搜
  532. if (request.DataTypeValue == RankingListConst.HotSearch.GetHashCode())
  533. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  534. //2热书榜
  535. if (request.DataTypeValue == RankingListConst.ReadBook.GetHashCode())
  536. {
  537. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  538. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  539. }
  540. //3 课程榜
  541. if (request.DataTypeValue == RankingListConst.Article.GetHashCode())
  542. {
  543. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  544. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  545. }
  546. //Top 100 新书
  547. if (request.DataTypeValue == RankingListConst.NewBook.GetHashCode())
  548. {
  549. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  550. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  551. }
  552. //文献头条
  553. if (request.DataTypeValue == RankingListConst.ArticleTop.GetHashCode())
  554. {
  555. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  556. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  557. }
  558. if (request.DataTypeValue == RankingListConst.Audio.GetHashCode())
  559. {
  560. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Audio.GetHashCode());
  561. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  562. }
  563. if (request.DataTypeValue == RankingListConst.HotComment.GetHashCode())
  564. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CommentScore));
  565. if (request.DataTypeValue == RankingListConst.CollectionBook.GetHashCode())
  566. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CollectCount));
  567. }
  568. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  569. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  570. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  571. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  572. query.ForPage(request.Page, request.PerPage);
  573. return await _repository.GetAllAsync<RankingListResult>(query);
  574. }
  575. /// <summary>
  576. /// 排行榜分页
  577. /// </summary>
  578. /// <param name="request"></param>
  579. /// <returns></returns>
  580. public async Task<PagedList<RankingListResult>> GetRankingPageListResults(RankingListRequest request)
  581. {
  582. var result = new PagedList<RankingListResult>();
  583. //获取媒体
  584. var query = Q.NewQuery();
  585. query.Select(nameof(Entity.tede2.Media.Media.Id));
  586. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  587. query.Select(nameof(Entity.tede2.Media.Media.Title));
  588. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  589. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  590. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  591. query.Select(nameof(Entity.tede2.Media.Media.Author));
  592. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  593. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  594. query.Select(nameof(Entity.tede2.Media.Media.CommentScore));
  595. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  596. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  597. if (!string.IsNullOrEmpty(request.KeyWord))
  598. query.WhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%");
  599. //1 热搜 2热书榜 3 课程榜 4Top 100 新书 5文献头条 6试听榜 7大众热评 8Top 100 专著
  600. if (request.DataTypeValue > 0)
  601. {
  602. //1 热搜
  603. if (request.DataTypeValue == RankingListConst.HotSearch.GetHashCode())
  604. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  605. //2热书榜
  606. if (request.DataTypeValue == RankingListConst.ReadBook.GetHashCode())
  607. {
  608. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  609. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  610. }
  611. //3 课程榜
  612. if (request.DataTypeValue == RankingListConst.Article.GetHashCode())
  613. {
  614. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  615. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  616. }
  617. //Top 100 新书
  618. if (request.DataTypeValue == RankingListConst.NewBook.GetHashCode())
  619. {
  620. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  621. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  622. }
  623. //文献头条
  624. if (request.DataTypeValue == RankingListConst.ArticleTop.GetHashCode())
  625. {
  626. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  627. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  628. }
  629. if (request.DataTypeValue == RankingListConst.Audio.GetHashCode())
  630. {
  631. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Audio.GetHashCode());
  632. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  633. }
  634. if (request.DataTypeValue == RankingListConst.HotComment.GetHashCode())
  635. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CommentScore));
  636. if (request.DataTypeValue == RankingListConst.CollectionBook.GetHashCode())
  637. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CollectCount));
  638. }
  639. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  640. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  641. result.Total = await _repository.CountAsync(query);
  642. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  643. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  644. query.ForPage(request.Page, request.PerPage);
  645. result.Items = await _repository.GetAllAsync<RankingListResult>(query);
  646. return result;
  647. }
  648. /// <summary>
  649. /// 大咖讲座
  650. /// </summary>
  651. /// <returns></returns>
  652. public async Task<IEnumerable<MediaCathedraResult>> GetMediasByTeacherAsync(ArticleGroupRequest request)
  653. {
  654. var query = Q.NewQuery();
  655. query.Select(nameof(Entity.tede2.Media.Media.Id));
  656. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  657. query.Select(nameof(Entity.tede2.Media.Media.Title));
  658. query.Select(nameof(Entity.tede2.Media.Media.Author));
  659. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  660. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  661. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  662. query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0);
  663. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode());
  664. if (!string.IsNullOrEmpty(request.Sort))
  665. {
  666. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  667. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  668. }
  669. else
  670. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  671. query.ForPage(request.Page, request.PerPage);
  672. return await _repository.GetAllAsync<MediaCathedraResult>(query);
  673. }
  674. /// <summary>
  675. /// 大咖讲座
  676. /// </summary>
  677. /// <returns></returns>
  678. public async Task<int> GetMediasByTeacherCountAsync(ArticleGroupRequest request)
  679. {
  680. var query = Q.NewQuery();
  681. query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0);
  682. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode());
  683. return await _repository.CountAsync(query);
  684. }
  685. /// <summary>
  686. /// 每日技能分页
  687. /// </summary>
  688. /// <returns></returns>
  689. public async Task<PagedList<MediaCathedraResult>> GetDaySkillsAsync(MediaDayWeBookRequest request)
  690. {
  691. var resut = new PagedList<MediaCathedraResult>();
  692. var query = Q.NewQuery();
  693. query.Select(nameof(Entity.tede2.Media.Media.Id));
  694. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  695. query.Select(nameof(Entity.tede2.Media.Media.Title));
  696. query.Select(nameof(Entity.tede2.Media.Media.Author));
  697. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  698. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  699. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  700. query.Where(nameof(Entity.tede2.Media.Media.IsExercises), true);
  701. query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.TypeValue);
  702. resut.Total = await _repository.CountAsync(query);
  703. if (!string.IsNullOrEmpty(request.Sort))
  704. {
  705. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  706. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  707. }
  708. else
  709. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  710. query.ForPage(request.Page, request.PerPage);
  711. resut.Items = await _repository.GetAllAsync<MediaCathedraResult>(query);
  712. return resut;
  713. }
  714. /// <summary>
  715. /// 每日技能
  716. /// </summary>
  717. /// <returns></returns>
  718. public async Task<SkillInfo> GetRandomDaySkillAsync()
  719. {
  720. var sql = $@"SELECT
  721. Id,Blocks,MediaType
  722. FROM
  723. tede_media where IsExercises=1 and MediaType={AllTypeConst.Article.GetHashCode()}
  724. ORDER BY RAND() LIMIT 1";
  725. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  726. var database = new Database(databaseType, _connectionString);
  727. var connection = database.GetConnection();
  728. return await connection.QueryFirstOrDefaultAsync<SkillInfo>(sql);
  729. }
  730. /// <summary>
  731. /// 猜你喜欢
  732. /// </summary>
  733. /// <returns></returns>
  734. public async Task<IEnumerable<MediaCathedraResult>> GetUserLikeAsync()
  735. {
  736. var sql = $@"SELECT
  737. Id,ImageUrls,Title,Author,MediaType,ReadCount,CreatedDate
  738. FROM
  739. tede_media where IsChecked=1 and MediaType={AllTypeConst.Book.GetHashCode()}
  740. ORDER BY RAND() LIMIT 3";
  741. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  742. var database = new Database(databaseType, _connectionString);
  743. var connection = database.GetConnection();
  744. return await connection.QueryAsync<MediaCathedraResult>(sql);
  745. }
  746. /// <summary>
  747. /// 导航内容
  748. /// </summary>
  749. /// <returns></returns>
  750. public async Task<PagedList<MediaCathedraResult>> GetContentNavigationAsync(MediaContentNavigationRequest request)
  751. {
  752. var resut = new PagedList<MediaCathedraResult>();
  753. var query = Q.NewQuery();
  754. query.Select(nameof(Entity.tede2.Media.Media.Id));
  755. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  756. query.Select(nameof(Entity.tede2.Media.Media.Title));
  757. query.Select(nameof(Entity.tede2.Media.Media.Author));
  758. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  759. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  760. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  761. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Book.GetHashCode());
  762. if (request.MediaIds.Count() > 0)
  763. query.WhereIn(nameof(Entity.tede2.Media.Media.Id), request.MediaIds);
  764. resut.Total = await _repository.CountAsync(query);
  765. if (!string.IsNullOrEmpty(request.Sort))
  766. {
  767. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  768. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  769. }
  770. else
  771. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  772. query.ForPage(request.Page, request.PerPage);
  773. resut.Items = await _repository.GetAllAsync<MediaCathedraResult>(query);
  774. return resut;
  775. }
  776. }
  777. }