MediaRepository.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. namespace GxPress.Repository.Implement.Media
  14. {
  15. public class MediaRepository : IMediaRepository
  16. {
  17. private readonly Repository<Entity.tede2.Media.Media> _repository;
  18. private readonly Repository<Entity.tede2.Media.MediaLibrary> mediaLibraryRepository;
  19. private readonly Repository<Entity.tede2.Media.MediaLable> mediaLableRepository;
  20. private readonly IMapper _mapper;
  21. private readonly string _connectionString;
  22. private readonly string _databaseTypestr;
  23. public MediaRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  24. {
  25. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  26. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  27. var databaseType =
  28. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  29. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  30. _repository = new Repository<Entity.tede2.Media.Media>(database);
  31. mediaLibraryRepository = new Repository<Entity.tede2.Media.MediaLibrary>(database);
  32. mediaLableRepository = new Repository<Entity.tede2.Media.MediaLable>(database);
  33. _mapper = mapper;
  34. }
  35. public IDatabase Database => _repository.Database;
  36. public string TableName => _repository.TableName;
  37. public List<TableColumn> TableColumns => _repository.TableColumns;
  38. public async Task<MediaResult> GetAsync(int id)
  39. {
  40. var result = new MediaResult();
  41. //获取媒体
  42. var media = await _repository.GetAsync(id);
  43. result = _mapper.Map<MediaResult>(media);
  44. //获取媒体库资源
  45. var mediaLibrarys = await mediaLibraryRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
  46. result.MediaLibraryResults = mediaLibrarys.ToList().Select(n => _mapper.Map<MediaLibraryResult>(n)).ToList();
  47. //获取媒体标签
  48. var mediaLables = await mediaLableRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));
  49. result.MediaLableResults = mediaLables.Select(n => _mapper.Map<MediaLableResult>(n)).ToList();
  50. return result;
  51. }
  52. /// <summary>
  53. /// 删除媒体
  54. /// </summary>
  55. /// <param name="id"></param>
  56. /// <returns></returns>
  57. public async Task<bool> DeleteAsync(int id)
  58. {
  59. try
  60. {
  61. using (var transactionScope = new TransactionScope())
  62. {
  63. //删除媒体库
  64. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
  65. //删除媒体标签
  66. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));
  67. await _repository.DeleteAsync(id);
  68. transactionScope.Complete();
  69. }
  70. }
  71. catch (System.Exception ex)
  72. {
  73. throw new BusinessException(ex.Message);
  74. }
  75. return true;
  76. }
  77. /// <summary>
  78. /// 添加媒体
  79. /// </summary>
  80. /// <param name="result"></param>
  81. /// <returns></returns>
  82. public async Task<bool> InsertAsync(MediaResult result)
  83. {
  84. var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  85. try
  86. {
  87. using (var transactionScope = new TransactionScope())
  88. {
  89. var id = await _repository.InsertAsync(model);
  90. if (id > 0)
  91. {
  92. foreach (var item in result.MediaLibraryResults)
  93. {
  94. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  95. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  96. }
  97. }
  98. //添加标签
  99. foreach (var item in result.MediaLableResults)
  100. {
  101. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  102. await mediaLableRepository.InsertAsync(mediaLable);
  103. }
  104. if (result.Id == 0 && result.TeacherId == 0 && !string.IsNullOrEmpty(result.Author))
  105. {
  106. //新增名师
  107. }
  108. //提交事务
  109. transactionScope.Complete();
  110. }
  111. }
  112. catch (System.Exception ex)
  113. {
  114. throw new Common.Exceptions.BusinessException(ex.Message);
  115. }
  116. return true;
  117. }
  118. public async Task<bool> UpdateAsync(Entity.tede2.Media.Media note)
  119. {
  120. return await _repository.UpdateAsync(note);
  121. }
  122. public async Task<bool> UpdateAsync(SqlKata.Query query)
  123. {
  124. return await _repository.UpdateAsync(query) > 0;
  125. }
  126. public async Task<IEnumerable<Entity.tede2.Media.Media>> GetAllAsync(int mediaType)
  127. {
  128. return await _repository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.Media.MediaType), mediaType).OrderByDesc(nameof(Entity.tede2.Media.Media.Sort)));
  129. }
  130. public async Task<bool> UpdateAsync(MediaResult result)
  131. {
  132. var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  133. try
  134. {
  135. using (var transactionScope = new TransactionScope())
  136. {
  137. await _repository.UpdateAsync(model);
  138. //删除媒体库
  139. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  140. //删除媒体标签
  141. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
  142. foreach (var item in result.MediaLibraryResults)
  143. {
  144. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  145. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  146. }
  147. //添加标签
  148. foreach (var item in result.MediaLableResults)
  149. {
  150. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  151. await mediaLableRepository.InsertAsync(mediaLable);
  152. }
  153. //提交事务
  154. transactionScope.Complete();
  155. }
  156. }
  157. catch (System.Exception ex)
  158. {
  159. throw new Common.Exceptions.BusinessException(ex.Message);
  160. }
  161. return true;
  162. }
  163. }
  164. }