MediaRepository.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. //获取媒体库资源
  48. var mediaLibrarys = await mediaLibraryRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
  49. result.MediaLibraryResults = mediaLibrarys.ToList().Select(n => _mapper.Map<MediaLibraryResult>(n)).ToList();
  50. //获取媒体标签
  51. var mediaLables = await mediaLableRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));
  52. result.MediaLableResults = mediaLables.Select(n => _mapper.Map<MediaLableResult>(n)).ToList();
  53. return result;
  54. }
  55. /// <summary>
  56. /// 删除媒体
  57. /// </summary>
  58. /// <param name="id"></param>
  59. /// <returns></returns>
  60. public async Task<bool> DeleteAsync(int id)
  61. {
  62. try
  63. {
  64. using (var transactionScope = new TransactionScope())
  65. {
  66. // //删除媒体库
  67. // await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
  68. // //删除媒体标签
  69. // await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));
  70. await _repository.UpdateAsync(Q.Where(nameof(Entity.tede2.Media.Media.Id), id).Set(nameof(Entity.tede2.Media.Media.IsDelete), true));
  71. transactionScope.Complete();
  72. }
  73. }
  74. catch (System.Exception ex)
  75. {
  76. throw new BusinessException(ex.Message);
  77. }
  78. return true;
  79. }
  80. /// <summary>
  81. /// 添加媒体
  82. /// </summary>
  83. /// <param name="result"></param>
  84. /// <returns></returns>
  85. public async Task<bool> InsertAsync(MediaResult result)
  86. {
  87. var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  88. try
  89. {
  90. using (var transactionScope = new TransactionScope())
  91. {
  92. model.AutoNumber = DateTime.Now.Ticks.ToString();
  93. var categoryId = "";
  94. foreach (var item in result.CategoryId)
  95. {
  96. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  97. }
  98. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  99. model.CategoryId = categoryId;
  100. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  101. var categoryName = "";
  102. foreach (var item in result.CategoryName)
  103. {
  104. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  105. }
  106. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  107. model.CategoryName = categoryName;
  108. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  109. var id = await _repository.InsertAsync(model);
  110. if (id > 0)
  111. {
  112. foreach (var item in result.MediaLibraryResults)
  113. {
  114. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  115. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  116. }
  117. }
  118. //添加标签
  119. foreach (var item in result.MediaLableResults)
  120. {
  121. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  122. await mediaLableRepository.InsertAsync(mediaLable);
  123. }
  124. if (result.Id == 0 && result.TeacherId == 0 && !string.IsNullOrEmpty(result.Author))
  125. {
  126. //新增名师
  127. }
  128. //提交事务
  129. transactionScope.Complete();
  130. }
  131. }
  132. catch (System.Exception ex)
  133. {
  134. throw new Common.Exceptions.BusinessException(ex.Message);
  135. }
  136. return true;
  137. }
  138. public async Task<bool> UpdateAsync(Entity.tede2.Media.Media note)
  139. {
  140. return await _repository.UpdateAsync(note);
  141. }
  142. public async Task<bool> UpdateAsync(SqlKata.Query query)
  143. {
  144. return await _repository.UpdateAsync(query) > 0;
  145. }
  146. public async Task<PagedList<Entity.tede2.Media.Media>> GetAllAsync(MediaSearchRequest request)
  147. {
  148. var result = new PagedList<Entity.tede2.Media.Media>();
  149. var query = Q.NewQuery();
  150. if(!string.IsNullOrEmpty(request.KeyWord))
  151. query.WhereLike(nameof(Entity.tede2.Media.Media.Title),$"%{request.KeyWord}%");
  152. if (request.IsDelete > 0)
  153. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), request.IsDelete);
  154. if (request.MediaType > 0)
  155. query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.MediaType);
  156. if (request.LibraryType > 0)
  157. query.Where(nameof(Entity.tede2.Media.Media.IsLibrary), request.LibraryType == 1);
  158. result.Total = await _repository.CountAsync(query);
  159. var item = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage).OrderByDesc(nameof(Entity.tede2.Media.Media.Sort)));
  160. result.Items = item;
  161. return result;
  162. }
  163. public async Task<bool> UpdateAsync(MediaResult result)
  164. {
  165. var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  166. try
  167. {
  168. using (var transactionScope = new TransactionScope())
  169. {
  170. var categoryId = "";
  171. foreach (var item in result.CategoryId)
  172. {
  173. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  174. }
  175. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  176. model.CategoryId = categoryId;
  177. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  178. var categoryName = "";
  179. foreach (var item in result.CategoryName)
  180. {
  181. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  182. }
  183. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  184. model.CategoryName = categoryName;
  185. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  186. await _repository.UpdateAsync(model);
  187. //删除媒体库
  188. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  189. //删除媒体标签
  190. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
  191. foreach (var item in result.MediaLibraryResults)
  192. {
  193. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  194. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  195. }
  196. //添加标签
  197. foreach (var item in result.MediaLableResults)
  198. {
  199. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  200. await mediaLableRepository.InsertAsync(mediaLable);
  201. }
  202. //提交事务
  203. transactionScope.Complete();
  204. }
  205. }
  206. catch (System.Exception ex)
  207. {
  208. throw new Common.Exceptions.BusinessException(ex.Message);
  209. }
  210. return true;
  211. }
  212. }
  213. }