MediaRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 == 1);
  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 model = await _repository.GetAsync(result.Id);
  171. if (result.CategoryId != null && result.CategoryId.Count > 0)
  172. {
  173. var categoryId = "";
  174. foreach (var item in result.CategoryId)
  175. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  176. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  177. model.CategoryId = categoryId;
  178. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  179. }
  180. if (result.CategoryName != null && result.CategoryName.Count > 0)
  181. {
  182. var categoryName = "";
  183. foreach (var item in result.CategoryName)
  184. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  185. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  186. model.CategoryName = categoryName;
  187. }
  188. if (result.AdminId > 0)
  189. model.AdminId = result.AdminId;
  190. if (!string.IsNullOrEmpty(result.Creator))
  191. model.Creator = result.Creator;
  192. if (result.MediaType > 0)
  193. model.MediaType = result.MediaType;
  194. if (!string.IsNullOrEmpty(result.Title))
  195. model.Title = result.Title;
  196. if (!string.IsNullOrEmpty(result.ImageUrls))
  197. model.ImageUrls = result.ImageUrls;
  198. if (!string.IsNullOrEmpty(result.Summary))
  199. model.Summary = result.Summary;
  200. if (!string.IsNullOrEmpty(result.Author))
  201. model.Author = result.Author;
  202. if (result.TeacherId > 0)
  203. model.TeacherId = result.TeacherId;
  204. if (!string.IsNullOrEmpty(result.Source))
  205. model.Source = result.Source;
  206. if (!string.IsNullOrEmpty(result.Blocks))
  207. model.Blocks = result.Blocks;
  208. if (result.IsChecked > 0)
  209. model.IsChecked = result.IsChecked == 1;
  210. if (!string.IsNullOrEmpty(result.AddDate))
  211. model.AddDate = Convert.ToDateTime(result.AddDate);
  212. if (model.Sort > 0)
  213. model.Sort = result.Sort;
  214. if (model.ReadCount > 0)
  215. model.ReadCount = result.ReadCount;
  216. if (result.IsTop > 0)
  217. model.IsTop = result.IsTop == 1;
  218. if (result.LableId > 0)
  219. model.LableId = result.LableId;
  220. if (result.IsRecommend > 0)
  221. model.IsRecommend = result.IsRecommend == 1;
  222. if (!string.IsNullOrEmpty(result.Press))
  223. model.Press = result.Press;
  224. if (!string.IsNullOrEmpty(result.PublishTime))
  225. model.PublishTime = result.PublishTime;
  226. if (result.IosDiscountPrice > 0)
  227. model.IosDiscountPrice = result.IosDiscountPrice;
  228. if (result.IosPrice > 0)
  229. model.IosPrice = result.IosPrice;
  230. if (result.OtherDiscountPrice > 0)
  231. model.OtherDiscountPrice = result.OtherDiscountPrice;
  232. if (result.OtherPrice > 0)
  233. model.OtherPrice = result.OtherPrice;
  234. if (result.FreeProportion > 0)
  235. model.FreeProportion = result.FreeProportion;
  236. if (result.AttachType > 0)
  237. model.AttachType = result.AttachType;
  238. if (result.AttachId > 0)
  239. model.AttachId = result.AttachId;
  240. if (result.MediaId > 0)
  241. model.MediaId = result.MediaId;
  242. if (!string.IsNullOrEmpty(result.MediaTitle))
  243. model.MediaTitle = result.MediaTitle;
  244. if (!string.IsNullOrEmpty(result.JournalsYear))
  245. model.JournalsYear = result.JournalsYear;
  246. if (result.JournalsCategory > 0)
  247. model.JournalsCategory = result.JournalsCategory;
  248. await _repository.UpdateAsync(model);
  249. if (result.MediaLibraryResults != null && result.MediaLibraryResults.Count > 0)
  250. {
  251. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  252. //删除媒体库
  253. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  254. foreach (var item in result.MediaLibraryResults)
  255. {
  256. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  257. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  258. }
  259. }
  260. if (result.MediaLableResults != null && result.MediaLableResults.Count > 0)
  261. {
  262. //删除媒体标签
  263. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
  264. //添加标签
  265. foreach (var item in result.MediaLableResults)
  266. {
  267. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  268. await mediaLableRepository.InsertAsync(mediaLable);
  269. }
  270. }
  271. //提交事务
  272. transactionScope.Complete();
  273. }
  274. }
  275. catch (System.Exception ex)
  276. {
  277. throw new Common.Exceptions.BusinessException(ex.Message);
  278. }
  279. return true;
  280. }
  281. }
  282. }