MediaRepository.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. namespace GxPress.Repository.Implement.Media
  17. {
  18. public class MediaRepository : IMediaRepository
  19. {
  20. private readonly Repository<Entity.tede2.Media.Media> _repository;
  21. private readonly Repository<Entity.tede2.Media.MediaLibrary> mediaLibraryRepository;
  22. private readonly Repository<Entity.tede2.Media.MediaLable> mediaLableRepository;
  23. private readonly IMapper _mapper;
  24. private readonly string _connectionString;
  25. private readonly string _databaseTypestr;
  26. public MediaRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  27. {
  28. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  29. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  30. var databaseType =
  31. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  32. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  33. _repository = new Repository<Entity.tede2.Media.Media>(database);
  34. mediaLibraryRepository = new Repository<Entity.tede2.Media.MediaLibrary>(database);
  35. mediaLableRepository = new Repository<Entity.tede2.Media.MediaLable>(database);
  36. _mapper = mapper;
  37. }
  38. public IDatabase Database => _repository.Database;
  39. public string TableName => _repository.TableName;
  40. public List<TableColumn> TableColumns => _repository.TableColumns;
  41. public async Task<MediaResult> GetAsync(int id)
  42. {
  43. var result = new MediaResult();
  44. //获取媒体
  45. var media = await _repository.GetAsync(id);
  46. result = _mapper.Map<MediaResult>(media);
  47. result.ImageUrls = StringUtils.AddDomain(result.ImageUrls);
  48. result.CategoryId = StringUtils.StringCollectionToStringList(media.CategoryId).ToList();
  49. result.CategoryName = StringUtils.StringCollectionToStringList(media.CategoryName).ToList();
  50. result.FullCategoryId = StringUtils.StringCollectionToStringList(media.FullCategoryId).ToList();
  51. //获取媒体库资源
  52. var mediaLibrarys = await mediaLibraryRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
  53. result.MediaLibraryResults = mediaLibrarys.ToList().Select(n => _mapper.Map<MediaLibraryResult>(n)).ToList();
  54. foreach (var item in result.MediaLibraryResults)
  55. {
  56. item.FileUrl = StringUtils.AddDomain(item.FileUrl);
  57. }
  58. //获取媒体标签
  59. var mediaLables = await mediaLableRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));
  60. result.MediaLableResults = mediaLables.Select(n => _mapper.Map<MediaLableResult>(n)).ToList();
  61. return result;
  62. }
  63. /// <summary>
  64. /// 删除媒体
  65. /// </summary>
  66. /// <param name="id"></param>
  67. /// <returns></returns>
  68. public async Task<bool> DeleteAsync(int id)
  69. {
  70. try
  71. {
  72. using (var transactionScope = new TransactionScope())
  73. {
  74. // //删除媒体库
  75. // await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
  76. // //删除媒体标签
  77. // await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));
  78. await _repository.UpdateAsync(Q.Where(nameof(Entity.tede2.Media.Media.Id), id).Set(nameof(Entity.tede2.Media.Media.IsDelete), true));
  79. transactionScope.Complete();
  80. }
  81. }
  82. catch (System.Exception ex)
  83. {
  84. throw new BusinessException(ex.Message);
  85. }
  86. return true;
  87. }
  88. /// <summary>
  89. /// 添加媒体
  90. /// </summary>
  91. /// <param name="result"></param>
  92. /// <returns></returns>
  93. public async Task<bool> InsertAsync(MediaResult result)
  94. {
  95. var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  96. model.ImageUrls = StringUtils.RemoveDomain(result.ImageUrls);
  97. try
  98. {
  99. using (var transactionScope = new TransactionScope())
  100. {
  101. model.AutoNumber = SnowflakeHelper.MakeSnowflake();
  102. var categoryId = "";
  103. foreach (var item in result.CategoryId)
  104. {
  105. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  106. }
  107. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  108. model.CategoryId = categoryId;
  109. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  110. var categoryName = "";
  111. foreach (var item in result.CategoryName)
  112. {
  113. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  114. }
  115. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  116. model.CategoryName = categoryName;
  117. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  118. var id = await _repository.InsertAsync(model);
  119. if (id > 0)
  120. {
  121. foreach (var item in result.MediaLibraryResults)
  122. {
  123. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  124. mediaLibrary.MediaId = id;
  125. mediaLibrary.FileUrl = StringUtils.RemoveDomain(item.FileUrl);
  126. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  127. }
  128. }
  129. //添加标签
  130. foreach (var item in result.MediaLableResults)
  131. {
  132. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  133. mediaLable.MediaId = id;
  134. await mediaLableRepository.InsertAsync(mediaLable);
  135. }
  136. if (result.Id == 0 && result.TeacherId == 0 && !string.IsNullOrEmpty(result.Author))
  137. {
  138. //新增名师
  139. }
  140. //提交事务
  141. transactionScope.Complete();
  142. }
  143. }
  144. catch (System.Exception ex)
  145. {
  146. throw new Common.Exceptions.BusinessException(ex.Message);
  147. }
  148. return true;
  149. }
  150. public async Task<bool> UpdateAsync(Entity.tede2.Media.Media note)
  151. {
  152. return await _repository.UpdateAsync(note);
  153. }
  154. public async Task<bool> UpdateAsync(SqlKata.Query query)
  155. {
  156. return await _repository.UpdateAsync(query) > 0;
  157. }
  158. /// <summary>
  159. /// 查询
  160. /// </summary>
  161. /// <param name="request"></param>
  162. /// <returns></returns>
  163. public async Task<PagedList<Entity.tede2.Media.Media>> GetAllAsync(MediaSearchRequest request)
  164. {
  165. var result = new PagedList<Entity.tede2.Media.Media>();
  166. var query = Q.NewQuery();
  167. if (!string.IsNullOrEmpty(request.KeyWord))
  168. {
  169. query.OrWhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%");
  170. query.OrWhereLike(nameof(Entity.tede2.Media.Media.Creator), $"%{request.KeyWord}%");
  171. }
  172. if (request.IsDelete > 0)
  173. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), request.IsDelete == 1);
  174. if (request.MediaType > 0)
  175. query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.MediaType);
  176. if (request.LibraryType > 0)
  177. query.Where(nameof(Entity.tede2.Media.Media.IsLibrary), request.LibraryType == 1);
  178. if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
  179. {
  180. if (DateTime.TryParse(request.BeginTime, out var beginTime) && DateTime.TryParse(request.EndTime, out var endTime))
  181. {
  182. query.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), ">=", beginTime);
  183. query.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), "<=", endTime);
  184. }
  185. }
  186. if (request.IsChecked > 0)
  187. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), request.IsChecked == 1);
  188. result.Total = await _repository.CountAsync(query);
  189. //排序
  190. if (string.IsNullOrEmpty(request.Sort))
  191. query.OrderByDesc(nameof(Entity.tede2.Media.Media.CreatedDate));
  192. else if (request.Sort.Equals("SellAmount"))
  193. query.OrderByDesc(nameof(Entity.tede2.Media.Media.SellAmount));
  194. else if (request.Sort.Equals("ReadCount"))
  195. query.OrderByDesc(nameof(Entity.tede2.Media.Media.ReadCount));
  196. var item = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  197. result.Items = item;
  198. return result;
  199. }
  200. public async Task<bool> UpdateAsync(MediaResult result)
  201. {
  202. //var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  203. try
  204. {
  205. using (var transactionScope = new TransactionScope())
  206. {
  207. var model = await _repository.GetAsync(result.Id);
  208. if (result.CategoryId != null && result.CategoryId.Count > 0)
  209. {
  210. var categoryId = "";
  211. foreach (var item in result.CategoryId)
  212. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  213. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  214. model.CategoryId = categoryId;
  215. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  216. }
  217. if (result.CategoryName != null && result.CategoryName.Count > 0)
  218. {
  219. var categoryName = "";
  220. foreach (var item in result.CategoryName)
  221. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  222. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  223. model.CategoryName = categoryName;
  224. }
  225. if (result.AdminId > 0)
  226. model.AdminId = result.AdminId;
  227. if (!string.IsNullOrEmpty(result.Creator))
  228. model.Creator = result.Creator;
  229. if (result.MediaType > 0)
  230. model.MediaType = result.MediaType;
  231. if (!string.IsNullOrEmpty(result.Title))
  232. model.Title = result.Title;
  233. if (!string.IsNullOrEmpty(result.ImageUrls))
  234. model.ImageUrls = result.ImageUrls;
  235. if (!string.IsNullOrEmpty(result.Summary))
  236. model.Summary = result.Summary;
  237. if (!string.IsNullOrEmpty(result.Author))
  238. model.Author = result.Author;
  239. if (result.TeacherId > 0)
  240. model.TeacherId = result.TeacherId;
  241. if (!string.IsNullOrEmpty(result.Source))
  242. model.Source = result.Source;
  243. if (!string.IsNullOrEmpty(result.Blocks))
  244. model.Blocks = result.Blocks;
  245. if (result.IsChecked > 0)
  246. model.IsChecked = result.IsChecked == 1;
  247. if (!string.IsNullOrEmpty(result.AddDate))
  248. model.AddDate = Convert.ToDateTime(result.AddDate);
  249. if (model.Sort > 0)
  250. model.Sort = result.Sort;
  251. if (model.ReadCount > 0)
  252. model.ReadCount = result.ReadCount;
  253. if (result.IsTop > 0)
  254. model.IsTop = result.IsTop == 1;
  255. if (result.LableId > 0)
  256. model.LableId = result.LableId;
  257. if (result.IsRecommend > 0)
  258. model.IsRecommend = result.IsRecommend == 1;
  259. if (!string.IsNullOrEmpty(result.Press))
  260. model.Press = result.Press;
  261. if (!string.IsNullOrEmpty(result.PublishTime))
  262. model.PublishTime = result.PublishTime;
  263. if (result.IosDiscountPrice > 0)
  264. model.IosDiscountPrice = result.IosDiscountPrice;
  265. if (result.IosPrice > 0)
  266. model.IosPrice = result.IosPrice;
  267. if (result.OtherDiscountPrice > 0)
  268. model.OtherDiscountPrice = result.OtherDiscountPrice;
  269. if (result.OtherPrice > 0)
  270. model.OtherPrice = result.OtherPrice;
  271. if (result.FreeProportion > 0)
  272. model.FreeProportion = result.FreeProportion;
  273. if (result.AttachType > 0)
  274. model.AttachType = result.AttachType;
  275. if (result.AttachId > 0)
  276. model.AttachId = result.AttachId;
  277. if (result.MediaId > 0)
  278. model.MediaId = result.MediaId;
  279. if (!string.IsNullOrEmpty(result.MediaTitle))
  280. model.MediaTitle = result.MediaTitle;
  281. if (!string.IsNullOrEmpty(result.JournalsYear))
  282. model.JournalsYear = result.JournalsYear;
  283. if (result.JournalsCategory > 0)
  284. model.JournalsCategory = result.JournalsCategory;
  285. await _repository.UpdateAsync(model);
  286. if (result.MediaLibraryResults != null && result.MediaLibraryResults.Count > 0)
  287. {
  288. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  289. //删除媒体库
  290. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  291. foreach (var item in result.MediaLibraryResults)
  292. {
  293. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  294. mediaLibrary.MediaId = result.Id;
  295. mediaLibrary.FileUrl = StringUtils.RemoveDomain(mediaLibrary.FileUrl);
  296. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  297. }
  298. }
  299. if (result.MediaLableResults != null && result.MediaLableResults.Count > 0)
  300. {
  301. //删除媒体标签
  302. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
  303. //添加标签
  304. foreach (var item in result.MediaLableResults)
  305. {
  306. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  307. mediaLable.MediaId = result.Id;
  308. await mediaLableRepository.InsertAsync(mediaLable);
  309. }
  310. }
  311. //提交事务
  312. transactionScope.Complete();
  313. }
  314. }
  315. catch (System.Exception ex)
  316. {
  317. throw new Common.Exceptions.BusinessException(ex.Message);
  318. }
  319. return true;
  320. }
  321. public async Task<int> CountAsync(string beginTime, string endTime)
  322. {
  323. return await _repository.CountAsync(Q.WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), ">=", beginTime).WhereDate(nameof(Entity.tede2.Media.Media.CreatedDate), "<=", endTime));
  324. }
  325. public async Task<int> CountAsync(SqlKata.Query query)
  326. {
  327. return await _repository.CountAsync(query);
  328. }
  329. public async Task<Entity.tede2.Media.Media> GetAsync(string autoNumber)
  330. {
  331. return await _repository.GetAsync(Q.Where(nameof(Entity.tede2.Media.Media.AutoNumber), autoNumber));
  332. }
  333. /// <summary>
  334. /// 获取书籍
  335. /// </summary>
  336. /// <param name="mediaId"></param>
  337. /// <returns></returns>
  338. public async Task<BookMediaResult> GetBookMediaResultAsync(int mediaId)
  339. {
  340. 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));
  341. return _mapper.Map<BookMediaResult>(result);
  342. }
  343. /// <summary>
  344. /// 获取视频
  345. /// </summary>
  346. /// <param name="mediaId"></param>
  347. /// <returns></returns>
  348. public async Task<VideoMediaResult> GetVideoMediaResultAsync(int mediaId)
  349. {
  350. 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));
  351. return _mapper.Map<VideoMediaResult>(result);
  352. }
  353. /// <summary>
  354. /// 根据
  355. /// </summary>
  356. /// <param name="mediaIds"></param>
  357. /// <returns></returns>
  358. public async Task<IEnumerable<NavigationMediaResult>> GetNavigationMediaResults(IEnumerable<int> mediaIds)
  359. {
  360. //获取媒体
  361. var query = Q.NewQuery();
  362. query.Select(nameof(Entity.tede2.Media.Media.Id));
  363. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  364. query.Select(nameof(Entity.tede2.Media.Media.Title));
  365. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  366. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  367. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  368. query.Select(nameof(Entity.tede2.Media.Media.Author));
  369. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  370. query.WhereIn(nameof(Entity.tede2.Media.Media.Id), mediaIds);
  371. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  372. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  373. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  374. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  375. query.ForPage(1, 10);
  376. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  377. }
  378. /// <summary>
  379. /// 猜你喜欢
  380. /// </summary>
  381. /// <param name="mediaIds"></param>
  382. /// <returns></returns>
  383. public async Task<IEnumerable<NavigationMediaResult>> GetlikeMediaResults()
  384. {
  385. //获取媒体
  386. var query = Q.NewQuery();
  387. query.Select(nameof(Entity.tede2.Media.Media.Id));
  388. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  389. query.Select(nameof(Entity.tede2.Media.Media.Title));
  390. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  391. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  392. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  393. query.Select(nameof(Entity.tede2.Media.Media.Author));
  394. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  395. query.Where(nameof(Entity.tede2.Media.Media.IsTop), true);
  396. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  397. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  398. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  399. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  400. query.ForPage(1, 10);
  401. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  402. }
  403. /// <summary>
  404. /// 推荐
  405. /// </summary>
  406. /// <param name="mediaIds"></param>
  407. /// <returns></returns>
  408. public async Task<IEnumerable<NavigationMediaResult>> GetRecommendMediaResults()
  409. {
  410. //获取媒体
  411. var query = Q.NewQuery();
  412. query.Select(nameof(Entity.tede2.Media.Media.Id));
  413. query.Select(nameof(Entity.tede2.Media.Media.ImageUrls));
  414. query.Select(nameof(Entity.tede2.Media.Media.Title));
  415. query.Select(nameof(Entity.tede2.Media.Media.Summary));
  416. query.Select(nameof(Entity.tede2.Media.Media.TeacherId));
  417. query.Select(nameof(Entity.tede2.Media.Media.AutoNumber));
  418. query.Select(nameof(Entity.tede2.Media.Media.Author));
  419. query.Select(nameof(Entity.tede2.Media.Media.MediaType));
  420. query.Where(nameof(Entity.tede2.Media.Media.IsRecommend), true);
  421. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  422. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  423. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  424. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  425. query.ForPage(1, 10);
  426. return await _repository.GetAllAsync<NavigationMediaResult>(query);
  427. }
  428. }
  429. }