MediaRepository.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. foreach (var item in result.Items)
  270. {
  271. item.ImageUrls = StringUtils.AddDomain(item.ImageUrls);
  272. }
  273. return result;
  274. }
  275. public async Task<bool> UpdateAsync(MediaResult result)
  276. {
  277. //var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  278. try
  279. {
  280. using (var transactionScope = new TransactionScope())
  281. {
  282. var model = await _repository.GetAsync(result.Id);
  283. if (result.CategoryId != null && result.CategoryId.Count > 0)
  284. {
  285. var categoryId = "";
  286. foreach (var item in result.CategoryId)
  287. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  288. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  289. model.CategoryId = categoryId;
  290. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  291. }
  292. if (result.CategoryName != null && result.CategoryName.Count > 0)
  293. {
  294. var categoryName = "";
  295. foreach (var item in result.CategoryName)
  296. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  297. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  298. model.CategoryName = categoryName;
  299. }
  300. if (result.AdminId > 0)
  301. model.AdminId = result.AdminId;
  302. if (!string.IsNullOrEmpty(result.Creator))
  303. model.Creator = result.Creator;
  304. if (result.MediaType > 0)
  305. model.MediaType = result.MediaType;
  306. if (!string.IsNullOrEmpty(result.Title))
  307. model.Title = result.Title;
  308. if (!string.IsNullOrEmpty(result.ImageUrls))
  309. model.ImageUrls = result.ImageUrls;
  310. if (!string.IsNullOrEmpty(result.Summary))
  311. model.Summary = result.Summary;
  312. if (!string.IsNullOrEmpty(result.Author))
  313. model.Author = result.Author;
  314. if (result.TeacherId > 0)
  315. model.TeacherId = result.TeacherId;
  316. if (!string.IsNullOrEmpty(result.Source))
  317. model.Source = result.Source;
  318. if (!string.IsNullOrEmpty(result.Blocks))
  319. model.Blocks = result.Blocks;
  320. if (result.IsChecked > 0)
  321. model.IsChecked = result.IsChecked == 1;
  322. if (!string.IsNullOrEmpty(result.AddDate))
  323. model.AddDate = Convert.ToDateTime(result.AddDate);
  324. if (model.Sort > 0)
  325. model.Sort = result.Sort;
  326. if (model.ReadCount > 0)
  327. model.ReadCount = result.ReadCount;
  328. if (result.IsTop > 0)
  329. model.IsTop = result.IsTop == 1;
  330. if (result.LableId > 0)
  331. model.LableId = result.LableId;
  332. if (result.IsRecommend > 0)
  333. model.IsRecommend = result.IsRecommend == 1;
  334. if (!string.IsNullOrEmpty(result.Press))
  335. model.Press = result.Press;
  336. if (!string.IsNullOrEmpty(result.PublishTime))
  337. model.PublishTime = result.PublishTime;
  338. if (result.IosDiscountPrice > 0)
  339. model.IosDiscountPrice = result.IosDiscountPrice;
  340. if (result.IosPrice > 0)
  341. model.IosPrice = result.IosPrice;
  342. if (result.OtherDiscountPrice > 0)
  343. model.OtherDiscountPrice = result.OtherDiscountPrice;
  344. if (result.OtherPrice > 0)
  345. model.OtherPrice = result.OtherPrice;
  346. if (result.FreeProportion > 0)
  347. model.FreeProportion = result.FreeProportion;
  348. if (result.AttachType > 0)
  349. model.AttachType = result.AttachType;
  350. if (result.AttachId > 0)
  351. model.AttachId = result.AttachId;
  352. if (result.MediaId > 0)
  353. model.MediaId = result.MediaId;
  354. if (!string.IsNullOrEmpty(result.MediaTitle))
  355. model.MediaTitle = result.MediaTitle;
  356. if (!string.IsNullOrEmpty(result.JournalsYear))
  357. model.JournalsYear = result.JournalsYear;
  358. if (result.JournalsCategory > 0)
  359. model.JournalsCategory = result.JournalsCategory;
  360. if (result.IsExercises > 0)
  361. model.IsExercises = result.IsExercises == 1;
  362. if (result.AppCategoryId > 0)
  363. model.AppCategoryId = result.AppCategoryId;
  364. await _repository.UpdateAsync(model);
  365. if (result.MediaLibraryResults != null && result.MediaLibraryResults.Count > 0)
  366. {
  367. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  368. //删除媒体库
  369. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  370. foreach (var item in result.MediaLibraryResults)
  371. {
  372. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  373. mediaLibrary.MediaId = result.Id;
  374. mediaLibrary.FileUrl = StringUtils.RemoveDomain(mediaLibrary.FileUrl);
  375. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  376. }
  377. }
  378. if (result.MediaLableResults != null && result.MediaLableResults.Count > 0)
  379. {
  380. //删除媒体标签
  381. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
  382. //添加标签
  383. foreach (var item in result.MediaLableResults)
  384. {
  385. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  386. mediaLable.MediaId = result.Id;
  387. await mediaLableRepository.InsertAsync(mediaLable);
  388. }
  389. }
  390. //提交事务
  391. transactionScope.Complete();
  392. }
  393. }
  394. catch (System.Exception ex)
  395. {
  396. throw new Common.Exceptions.BusinessException(ex.Message);
  397. }
  398. return true;
  399. }
  400. public async Task<int> CountAsync(string beginTime, string endTime)
  401. {
  402. return await _repository.CountAsync(Q.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), ">=", beginTime).WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), "<=", endTime));
  403. }
  404. public async Task<int> CountAsync(SqlKata.Query query)
  405. {
  406. return await _repository.CountAsync(query);
  407. }
  408. public async Task<Entity.tede2.Media.Media> GetAsync(string autoNumber)
  409. {
  410. return await _repository.GetAsync(Q.Where(nameof(Entity.tede2.Media.Media.AutoNumber), autoNumber));
  411. }
  412. /// <summary>
  413. /// 获取书籍
  414. /// </summary>
  415. /// <param name="mediaId"></param>
  416. /// <returns></returns>
  417. public async Task<BookMediaResult> GetBookMediaResultAsync(int mediaId)
  418. {
  419. 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));
  420. return _mapper.Map<BookMediaResult>(result);
  421. }
  422. /// <summary>
  423. /// 获取视频
  424. /// </summary>
  425. /// <param name="mediaId"></param>
  426. /// <returns></returns>
  427. public async Task<VideoMediaResult> GetVideoMediaResultAsync(int mediaId)
  428. {
  429. 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));
  430. return _mapper.Map<VideoMediaResult>(result);
  431. }
  432. /// <summary>
  433. /// 根据
  434. /// </summary>
  435. /// <param name="mediaIds"></param>
  436. /// <returns></returns>
  437. public async Task<IEnumerable<NavigationMediaResult>> GetNavigationMediaResults(IEnumerable<int> mediaIds, int page = 1, int pageSize = 10)
  438. {
  439. //获取媒体
  440. var query = Q.NewQuery();
  441. query.Select(nameof(Entity.tede2.Media.Media.Id));
  442. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  443. query.Select(nameof(Entity.tede2.Media.Media.Title));
  444. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  445. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  446. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  447. query.Select(nameof(Entity.tede2.Media.Media.Author));
  448. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  449. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  450. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  451. query.Select(nameof(Entity.tede2.Media.Media.CollectCount));
  452. query.WhereIn(nameof(Entity.tede2.Media.Media.Id), mediaIds);
  453. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  454. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  455. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  456. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  457. query.OrderByDesc(nameof(Entity.tede2.Media.Media.LastModifiedDate));
  458. query.ForPage(page, pageSize);
  459. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  460. }
  461. /// <summary>
  462. /// 猜你喜欢
  463. /// </summary>
  464. /// <param name="mediaIds"></param>
  465. /// <returns></returns>
  466. public async Task<IEnumerable<NavigationMediaResult>> GetlikeMediaResults()
  467. {
  468. //获取媒体
  469. var query = Q.NewQuery();
  470. query.Select(nameof(Entity.tede2.Media.Media.Id));
  471. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  472. query.Select(nameof(Entity.tede2.Media.Media.Title));
  473. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  474. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  475. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  476. query.Select(nameof(Entity.tede2.Media.Media.Author));
  477. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  478. query.Where(nameof(Entity.tede2.Media.Media.IsTop), true);
  479. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  480. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  481. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  482. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  483. query.ForPage(1, 10);
  484. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  485. }
  486. /// <summary>
  487. /// 推荐
  488. /// </summary>
  489. /// <param name="mediaIds"></param>
  490. /// <returns></returns>
  491. public async Task<IEnumerable<NavigationMediaResult>> GetRecommendMediaResults()
  492. {
  493. //获取媒体
  494. var query = Q.NewQuery();
  495. query.Select(nameof(Entity.tede2.Media.Media.Id));
  496. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  497. query.Select(nameof(Entity.tede2.Media.Media.Title));
  498. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  499. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  500. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  501. query.Select(nameof(Entity.tede2.Media.Media.Author));
  502. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  503. query.Where(nameof(Entity.tede2.Media.Media.IsRecommend), true);
  504. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  505. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  506. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  507. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  508. query.ForPage(1, 10);
  509. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  510. }
  511. /// <summary>
  512. /// 排行榜
  513. /// </summary>
  514. /// <param name="request"></param>
  515. /// <returns></returns>
  516. public async Task<IEnumerable<RankingListResult>> GetRankingListResults(RankingListRequest request)
  517. {
  518. //获取媒体
  519. var query = Q.NewQuery();
  520. query.Select(nameof(Entity.tede2.Media.Media.Id));
  521. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  522. query.Select(nameof(Entity.tede2.Media.Media.Title));
  523. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  524. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  525. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  526. query.Select(nameof(Entity.tede2.Media.Media.Author));
  527. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  528. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  529. query.Select(nameof(Entity.tede2.Media.Media.CommentScore));
  530. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  531. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  532. //1 热搜 2热书榜 3 课程榜 4Top 100 新书 5文献头条 6试听榜 7大众热评 8Top 100 专著
  533. if (request.DataTypeValue > 0)
  534. {
  535. //1 热搜
  536. if (request.DataTypeValue == RankingListConst.HotSearch.GetHashCode())
  537. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  538. //2热书榜
  539. if (request.DataTypeValue == RankingListConst.ReadBook.GetHashCode())
  540. {
  541. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  542. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  543. }
  544. //3 课程榜
  545. if (request.DataTypeValue == RankingListConst.Article.GetHashCode())
  546. {
  547. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  548. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  549. }
  550. //Top 100 新书
  551. if (request.DataTypeValue == RankingListConst.NewBook.GetHashCode())
  552. {
  553. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  554. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  555. }
  556. //文献头条
  557. if (request.DataTypeValue == RankingListConst.ArticleTop.GetHashCode())
  558. {
  559. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  560. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  561. }
  562. if (request.DataTypeValue == RankingListConst.Audio.GetHashCode())
  563. {
  564. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Audio.GetHashCode());
  565. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  566. }
  567. if (request.DataTypeValue == RankingListConst.HotComment.GetHashCode())
  568. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CommentScore));
  569. if (request.DataTypeValue == RankingListConst.CollectionBook.GetHashCode())
  570. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CollectCount));
  571. }
  572. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  573. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  574. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  575. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  576. query.ForPage(request.Page, request.PerPage);
  577. var result = await _repository.GetAllAsync<RankingListResult>(query);
  578. foreach (var item in result)
  579. item.ImageUrls = StringUtils.AddDomain(item.ImageUrls);
  580. return result;
  581. }
  582. /// <summary>
  583. /// 排行榜分页
  584. /// </summary>
  585. /// <param name="request"></param>
  586. /// <returns></returns>
  587. public async Task<PagedList<RankingListResult>> GetRankingPageListResults(RankingListRequest request)
  588. {
  589. var result = new PagedList<RankingListResult>();
  590. //获取媒体
  591. var query = Q.NewQuery();
  592. query.Select(nameof(Entity.tede2.Media.Media.Id));
  593. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  594. query.Select(nameof(Entity.tede2.Media.Media.Title));
  595. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  596. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  597. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  598. query.Select(nameof(Entity.tede2.Media.Media.Author));
  599. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  600. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  601. query.Select(nameof(Entity.tede2.Media.Media.CommentScore));
  602. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  603. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  604. if (!string.IsNullOrEmpty(request.KeyWord))
  605. query.WhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%");
  606. //1 热搜 2热书榜 3 课程榜 4Top 100 新书 5文献头条 6试听榜 7大众热评 8Top 100 专著
  607. if (request.DataTypeValue > 0)
  608. {
  609. //1 热搜
  610. if (request.DataTypeValue == RankingListConst.HotSearch.GetHashCode())
  611. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  612. //2热书榜
  613. if (request.DataTypeValue == RankingListConst.ReadBook.GetHashCode())
  614. {
  615. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  616. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  617. }
  618. //3 课程榜
  619. if (request.DataTypeValue == RankingListConst.Article.GetHashCode())
  620. {
  621. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  622. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  623. }
  624. //Top 100 新书
  625. if (request.DataTypeValue == RankingListConst.NewBook.GetHashCode())
  626. {
  627. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Book.GetHashCode());
  628. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  629. }
  630. //文献头条
  631. if (request.DataTypeValue == RankingListConst.ArticleTop.GetHashCode())
  632. {
  633. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Article.GetHashCode());
  634. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  635. }
  636. if (request.DataTypeValue == RankingListConst.Audio.GetHashCode())
  637. {
  638. query.Where(nameof(Entity.tede2.Media.Media.MediaType), ResourceTypeConst.Audio.GetHashCode());
  639. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  640. }
  641. if (request.DataTypeValue == RankingListConst.HotComment.GetHashCode())
  642. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CommentScore));
  643. if (request.DataTypeValue == RankingListConst.CollectionBook.GetHashCode())
  644. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CollectCount));
  645. }
  646. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  647. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  648. result.Total = await _repository.CountAsync(query);
  649. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  650. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  651. query.ForPage(request.Page, request.PerPage);
  652. result.Items = await _repository.GetAllAsync<RankingListResult>(query);
  653. foreach (var item in result.Items)
  654. item.ImageUrls = StringUtils.AddDomain(item.ImageUrls);
  655. return result;
  656. }
  657. /// <summary>
  658. /// 大咖讲座
  659. /// </summary>
  660. /// <returns></returns>
  661. public async Task<IEnumerable<MediaCathedraResult>> GetMediasByTeacherAsync(ArticleGroupRequest request)
  662. {
  663. var query = Q.NewQuery();
  664. query.Select(nameof(Entity.tede2.Media.Media.Id));
  665. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  666. query.Select(nameof(Entity.tede2.Media.Media.Title));
  667. query.Select(nameof(Entity.tede2.Media.Media.Author));
  668. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  669. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  670. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  671. query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0);
  672. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode());
  673. if (!string.IsNullOrEmpty(request.Sort))
  674. {
  675. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  676. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  677. }
  678. else
  679. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  680. query.ForPage(request.Page, request.PerPage);
  681. return await _repository.GetAllAsync<MediaCathedraResult>(query);
  682. }
  683. /// <summary>
  684. /// 大咖讲座
  685. /// </summary>
  686. /// <returns></returns>
  687. public async Task<int> GetMediasByTeacherCountAsync(ArticleGroupRequest request)
  688. {
  689. var query = Q.NewQuery();
  690. query.Where(nameof(Entity.tede2.Media.Media.TeacherId), ">", 0);
  691. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Video.GetHashCode());
  692. return await _repository.CountAsync(query);
  693. }
  694. /// <summary>
  695. /// 每日技能分页
  696. /// </summary>
  697. /// <returns></returns>
  698. public async Task<PagedList<MediaCathedraResult>> GetDaySkillsAsync(MediaDayWeBookRequest request)
  699. {
  700. var resut = new PagedList<MediaCathedraResult>();
  701. var query = Q.NewQuery();
  702. query.Select(nameof(Entity.tede2.Media.Media.Id));
  703. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  704. query.Select(nameof(Entity.tede2.Media.Media.Title));
  705. query.Select(nameof(Entity.tede2.Media.Media.Author));
  706. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  707. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  708. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  709. query.Where(nameof(Entity.tede2.Media.Media.IsExercises), true);
  710. query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.TypeValue);
  711. resut.Total = await _repository.CountAsync(query);
  712. if (!string.IsNullOrEmpty(request.Sort))
  713. {
  714. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  715. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  716. }
  717. else
  718. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  719. query.ForPage(request.Page, request.PerPage);
  720. resut.Items = await _repository.GetAllAsync<MediaCathedraResult>(query);
  721. return resut;
  722. }
  723. /// <summary>
  724. /// 每日技能
  725. /// </summary>
  726. /// <returns></returns>
  727. public async Task<SkillInfo> GetRandomDaySkillAsync()
  728. {
  729. var sql = $@"SELECT
  730. Id,Blocks,MediaType
  731. FROM
  732. tede_media where IsExercises=1 and MediaType={AllTypeConst.Article.GetHashCode()}
  733. ORDER BY RAND() LIMIT 1";
  734. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  735. var database = new Database(databaseType, _connectionString);
  736. var connection = database.GetConnection();
  737. return await connection.QueryFirstOrDefaultAsync<SkillInfo>(sql);
  738. }
  739. /// <summary>
  740. /// 猜你喜欢
  741. /// </summary>
  742. /// <returns></returns>
  743. public async Task<IEnumerable<MediaCathedraResult>> GetUserLikeAsync()
  744. {
  745. var sql = $@"SELECT
  746. Id,ImageUrls,Title,Author,MediaType,ReadCount,CreatedDate
  747. FROM
  748. tede_media where IsChecked=1 and MediaType={AllTypeConst.Book.GetHashCode()}
  749. ORDER BY RAND() LIMIT 3";
  750. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  751. var database = new Database(databaseType, _connectionString);
  752. var connection = database.GetConnection();
  753. return await connection.QueryAsync<MediaCathedraResult>(sql);
  754. }
  755. /// <summary>
  756. /// 导航内容
  757. /// </summary>
  758. /// <returns></returns>
  759. public async Task<PagedList<MediaCathedraResult>> GetContentNavigationAsync(MediaContentNavigationRequest request)
  760. {
  761. var resut = new PagedList<MediaCathedraResult>();
  762. var query = Q.NewQuery();
  763. query.Select(nameof(Entity.tede2.Media.Media.Id));
  764. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  765. query.Select(nameof(Entity.tede2.Media.Media.Title));
  766. query.Select(nameof(Entity.tede2.Media.Media.Author));
  767. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  768. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  769. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  770. query.Where(nameof(Entity.tede2.Media.Media.MediaType), GxPress.EnumConst.AllTypeConst.Book.GetHashCode());
  771. if (request.MediaIds.Count() > 0)
  772. query.WhereIn(nameof(Entity.tede2.Media.Media.Id), request.MediaIds);
  773. resut.Total = await _repository.CountAsync(query);
  774. if (!string.IsNullOrEmpty(request.Sort))
  775. {
  776. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  777. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  778. }
  779. else
  780. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  781. query.ForPage(request.Page, request.PerPage);
  782. resut.Items = await _repository.GetAllAsync<MediaCathedraResult>(query);
  783. return resut;
  784. }
  785. /// <summary>
  786. /// 明栏分页
  787. /// </summary>
  788. /// <returns></returns>
  789. public async Task<PagedList<MediaCathedraResult>> GetStarLablePageAsync(MediaStarLableRequest request)
  790. {
  791. var resut = new PagedList<MediaCathedraResult>();
  792. var query = Q.NewQuery();
  793. query.Select(nameof(Entity.tede2.Media.Media.Id));
  794. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  795. query.Select(nameof(Entity.tede2.Media.Media.Title));
  796. query.Select(nameof(Entity.tede2.Media.Media.Author));
  797. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  798. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  799. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  800. if (!string.IsNullOrEmpty(request.KeyWord))
  801. query.WhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%");
  802. query.Where(nameof(Entity.tede2.Media.Media.AttachId), request.AttachId);
  803. resut.Total = await _repository.CountAsync(query);
  804. if (!string.IsNullOrEmpty(request.Sort))
  805. {
  806. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  807. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  808. }
  809. else
  810. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  811. query.ForPage(request.Page, request.PerPage);
  812. resut.Items = await _repository.GetAllAsync<MediaCathedraResult>(query);
  813. return resut;
  814. }
  815. /// <summary>
  816. /// 明栏分页
  817. /// </summary>
  818. /// <returns></returns>
  819. public async Task<IEnumerable<MediaCathedraResult>> GetStarLablesAsync(MediaStarLableRequest request)
  820. {
  821. var query = Q.NewQuery();
  822. query.Select(nameof(Entity.tede2.Media.Media.Id));
  823. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  824. query.Select(nameof(Entity.tede2.Media.Media.Title));
  825. query.Select(nameof(Entity.tede2.Media.Media.Author));
  826. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  827. query.Select(nameof(Entity.tede2.Media.Media.ReadCount));
  828. query.Select(nameof(Entity.tede2.Media.Media.CreatedDate));
  829. if (!string.IsNullOrEmpty(request.KeyWord))
  830. query.WhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%");
  831. query.Where(nameof(Entity.tede2.Media.Media.AttachId), request.AttachId);
  832. if (!string.IsNullOrEmpty(request.Sort))
  833. {
  834. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  835. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  836. }
  837. else
  838. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  839. query.ForPage(request.Page, request.PerPage);
  840. return await _repository.GetAllAsync<MediaCathedraResult>(query); ;
  841. }
  842. }
  843. }