MediaRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.CategoryId = StringUtils.StringCollectionToStringList(media.CategoryId).ToList();
  48. result.CategoryName = StringUtils.StringCollectionToStringList(media.CategoryName).ToList();
  49. result.FullCategoryId = StringUtils.StringCollectionToStringList(media.FullCategoryId).ToList();
  50. //获取媒体库资源
  51. var mediaLibrarys = await mediaLibraryRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
  52. result.MediaLibraryResults = mediaLibrarys.ToList().Select(n => _mapper.Map<MediaLibraryResult>(n)).ToList();
  53. //获取媒体标签
  54. var mediaLables = await mediaLableRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));
  55. result.MediaLableResults = mediaLables.Select(n => _mapper.Map<MediaLableResult>(n)).ToList();
  56. return result;
  57. }
  58. /// <summary>
  59. /// 删除媒体
  60. /// </summary>
  61. /// <param name="id"></param>
  62. /// <returns></returns>
  63. public async Task<bool> DeleteAsync(int id)
  64. {
  65. try
  66. {
  67. using (var transactionScope = new TransactionScope())
  68. {
  69. // //删除媒体库
  70. // await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
  71. // //删除媒体标签
  72. // await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), id));
  73. await _repository.UpdateAsync(Q.Where(nameof(Entity.tede2.Media.Media.Id), id).Set(nameof(Entity.tede2.Media.Media.IsDelete), true));
  74. transactionScope.Complete();
  75. }
  76. }
  77. catch (System.Exception ex)
  78. {
  79. throw new BusinessException(ex.Message);
  80. }
  81. return true;
  82. }
  83. /// <summary>
  84. /// 添加媒体
  85. /// </summary>
  86. /// <param name="result"></param>
  87. /// <returns></returns>
  88. public async Task<bool> InsertAsync(MediaResult result)
  89. {
  90. var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  91. try
  92. {
  93. using (var transactionScope = new TransactionScope())
  94. {
  95. model.AutoNumber = DateTime.Now.Ticks.ToString();
  96. var categoryId = "";
  97. foreach (var item in result.CategoryId)
  98. {
  99. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  100. }
  101. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  102. model.CategoryId = categoryId;
  103. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  104. var categoryName = "";
  105. foreach (var item in result.CategoryName)
  106. {
  107. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  108. }
  109. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  110. model.CategoryName = categoryName;
  111. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  112. var id = await _repository.InsertAsync(model);
  113. if (id > 0)
  114. {
  115. foreach (var item in result.MediaLibraryResults)
  116. {
  117. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  118. mediaLibrary.MediaId = id;
  119. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  120. }
  121. }
  122. //添加标签
  123. foreach (var item in result.MediaLableResults)
  124. {
  125. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  126. mediaLable.MediaId = id;
  127. await mediaLableRepository.InsertAsync(mediaLable);
  128. }
  129. if (result.Id == 0 && result.TeacherId == 0 && !string.IsNullOrEmpty(result.Author))
  130. {
  131. //新增名师
  132. }
  133. //提交事务
  134. transactionScope.Complete();
  135. }
  136. }
  137. catch (System.Exception ex)
  138. {
  139. throw new Common.Exceptions.BusinessException(ex.Message);
  140. }
  141. return true;
  142. }
  143. public async Task<bool> UpdateAsync(Entity.tede2.Media.Media note)
  144. {
  145. return await _repository.UpdateAsync(note);
  146. }
  147. public async Task<bool> UpdateAsync(SqlKata.Query query)
  148. {
  149. return await _repository.UpdateAsync(query) > 0;
  150. }
  151. public async Task<PagedList<Entity.tede2.Media.Media>> GetAllAsync(MediaSearchRequest request)
  152. {
  153. var result = new PagedList<Entity.tede2.Media.Media>();
  154. var query = Q.NewQuery();
  155. if (!string.IsNullOrEmpty(request.KeyWord))
  156. query.WhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%");
  157. if (request.IsDelete > 0)
  158. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), request.IsDelete == 1);
  159. if (request.MediaType > 0)
  160. query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.MediaType);
  161. if (request.LibraryType > 0)
  162. query.Where(nameof(Entity.tede2.Media.Media.IsLibrary), request.LibraryType == 1);
  163. result.Total = await _repository.CountAsync(query);
  164. var item = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage).OrderByDesc(nameof(Entity.tede2.Media.Media.Sort)));
  165. result.Items = item;
  166. return result;
  167. }
  168. public async Task<bool> UpdateAsync(MediaResult result)
  169. {
  170. //var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  171. try
  172. {
  173. using (var transactionScope = new TransactionScope())
  174. {
  175. var model = await _repository.GetAsync(result.Id);
  176. if (result.CategoryId != null && result.CategoryId.Count > 0)
  177. {
  178. var categoryId = "";
  179. foreach (var item in result.CategoryId)
  180. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  181. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  182. model.CategoryId = categoryId;
  183. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  184. }
  185. if (result.CategoryName != null && result.CategoryName.Count > 0)
  186. {
  187. var categoryName = "";
  188. foreach (var item in result.CategoryName)
  189. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  190. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  191. model.CategoryName = categoryName;
  192. }
  193. if (result.AdminId > 0)
  194. model.AdminId = result.AdminId;
  195. if (!string.IsNullOrEmpty(result.Creator))
  196. model.Creator = result.Creator;
  197. if (result.MediaType > 0)
  198. model.MediaType = result.MediaType;
  199. if (!string.IsNullOrEmpty(result.Title))
  200. model.Title = result.Title;
  201. if (!string.IsNullOrEmpty(result.ImageUrls))
  202. model.ImageUrls = result.ImageUrls;
  203. if (!string.IsNullOrEmpty(result.Summary))
  204. model.Summary = result.Summary;
  205. if (!string.IsNullOrEmpty(result.Author))
  206. model.Author = result.Author;
  207. if (result.TeacherId > 0)
  208. model.TeacherId = result.TeacherId;
  209. if (!string.IsNullOrEmpty(result.Source))
  210. model.Source = result.Source;
  211. if (!string.IsNullOrEmpty(result.Blocks))
  212. model.Blocks = result.Blocks;
  213. if (result.IsChecked > 0)
  214. model.IsChecked = result.IsChecked == 1;
  215. if (!string.IsNullOrEmpty(result.AddDate))
  216. model.AddDate = Convert.ToDateTime(result.AddDate);
  217. if (model.Sort > 0)
  218. model.Sort = result.Sort;
  219. if (model.ReadCount > 0)
  220. model.ReadCount = result.ReadCount;
  221. if (result.IsTop > 0)
  222. model.IsTop = result.IsTop == 1;
  223. if (result.LableId > 0)
  224. model.LableId = result.LableId;
  225. if (result.IsRecommend > 0)
  226. model.IsRecommend = result.IsRecommend == 1;
  227. if (!string.IsNullOrEmpty(result.Press))
  228. model.Press = result.Press;
  229. if (!string.IsNullOrEmpty(result.PublishTime))
  230. model.PublishTime = result.PublishTime;
  231. if (result.IosDiscountPrice > 0)
  232. model.IosDiscountPrice = result.IosDiscountPrice;
  233. if (result.IosPrice > 0)
  234. model.IosPrice = result.IosPrice;
  235. if (result.OtherDiscountPrice > 0)
  236. model.OtherDiscountPrice = result.OtherDiscountPrice;
  237. if (result.OtherPrice > 0)
  238. model.OtherPrice = result.OtherPrice;
  239. if (result.FreeProportion > 0)
  240. model.FreeProportion = result.FreeProportion;
  241. if (result.AttachType > 0)
  242. model.AttachType = result.AttachType;
  243. if (result.AttachId > 0)
  244. model.AttachId = result.AttachId;
  245. if (result.MediaId > 0)
  246. model.MediaId = result.MediaId;
  247. if (!string.IsNullOrEmpty(result.MediaTitle))
  248. model.MediaTitle = result.MediaTitle;
  249. if (!string.IsNullOrEmpty(result.JournalsYear))
  250. model.JournalsYear = result.JournalsYear;
  251. if (result.JournalsCategory > 0)
  252. model.JournalsCategory = result.JournalsCategory;
  253. await _repository.UpdateAsync(model);
  254. if (result.MediaLibraryResults != null && result.MediaLibraryResults.Count > 0)
  255. {
  256. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  257. //删除媒体库
  258. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  259. foreach (var item in result.MediaLibraryResults)
  260. {
  261. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  262. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  263. }
  264. }
  265. if (result.MediaLableResults != null && result.MediaLableResults.Count > 0)
  266. {
  267. //删除媒体标签
  268. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
  269. //添加标签
  270. foreach (var item in result.MediaLableResults)
  271. {
  272. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  273. await mediaLableRepository.InsertAsync(mediaLable);
  274. }
  275. }
  276. //提交事务
  277. transactionScope.Complete();
  278. }
  279. }
  280. catch (System.Exception ex)
  281. {
  282. throw new Common.Exceptions.BusinessException(ex.Message);
  283. }
  284. return true;
  285. }
  286. }
  287. }