MediaRepository.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. var id = await _repository.InsertAsync(model);
  109. if (id > 0)
  110. {
  111. foreach (var item in result.MediaLibraryResults)
  112. {
  113. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  114. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  115. }
  116. }
  117. //添加标签
  118. foreach (var item in result.MediaLableResults)
  119. {
  120. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  121. await mediaLableRepository.InsertAsync(mediaLable);
  122. }
  123. if (result.Id == 0 && result.TeacherId == 0 && !string.IsNullOrEmpty(result.Author))
  124. {
  125. //新增名师
  126. }
  127. //提交事务
  128. transactionScope.Complete();
  129. }
  130. }
  131. catch (System.Exception ex)
  132. {
  133. throw new Common.Exceptions.BusinessException(ex.Message);
  134. }
  135. return true;
  136. }
  137. public async Task<bool> UpdateAsync(Entity.tede2.Media.Media note)
  138. {
  139. return await _repository.UpdateAsync(note);
  140. }
  141. public async Task<bool> UpdateAsync(SqlKata.Query query)
  142. {
  143. return await _repository.UpdateAsync(query) > 0;
  144. }
  145. public async Task<PagedList<Entity.tede2.Media.Media>> GetAllAsync(MediaSearchRequest request)
  146. {
  147. var result = new PagedList<Entity.tede2.Media.Media>();
  148. var query = Q.NewQuery();
  149. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), request.IsDelete);
  150. query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.MediaType);
  151. result.Total = await _repository.CountAsync(query);
  152. var item = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage).OrderByDesc(nameof(Entity.tede2.Media.Media.Sort)));
  153. result.Items = item;
  154. return result;
  155. }
  156. public async Task<bool> UpdateAsync(MediaResult result)
  157. {
  158. var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  159. try
  160. {
  161. using (var transactionScope = new TransactionScope())
  162. {
  163. var categoryId = "";
  164. foreach (var item in result.CategoryId)
  165. {
  166. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  167. }
  168. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  169. model.CategoryId = categoryId;
  170. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  171. var categoryName = "";
  172. foreach (var item in result.CategoryName)
  173. {
  174. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  175. }
  176. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  177. model.CategoryName = categoryName;
  178. await _repository.UpdateAsync(model);
  179. //删除媒体库
  180. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  181. //删除媒体标签
  182. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
  183. foreach (var item in result.MediaLibraryResults)
  184. {
  185. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  186. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  187. }
  188. //添加标签
  189. foreach (var item in result.MediaLableResults)
  190. {
  191. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  192. await mediaLableRepository.InsertAsync(mediaLable);
  193. }
  194. //提交事务
  195. transactionScope.Complete();
  196. }
  197. }
  198. catch (System.Exception ex)
  199. {
  200. throw new Common.Exceptions.BusinessException(ex.Message);
  201. }
  202. return true;
  203. }
  204. }
  205. }