MediaRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  119. }
  120. }
  121. //添加标签
  122. foreach (var item in result.MediaLableResults)
  123. {
  124. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  125. await mediaLableRepository.InsertAsync(mediaLable);
  126. }
  127. if (result.Id == 0 && result.TeacherId == 0 && !string.IsNullOrEmpty(result.Author))
  128. {
  129. //新增名师
  130. }
  131. //提交事务
  132. transactionScope.Complete();
  133. }
  134. }
  135. catch (System.Exception ex)
  136. {
  137. throw new Common.Exceptions.BusinessException(ex.Message);
  138. }
  139. return true;
  140. }
  141. public async Task<bool> UpdateAsync(Entity.tede2.Media.Media note)
  142. {
  143. return await _repository.UpdateAsync(note);
  144. }
  145. public async Task<bool> UpdateAsync(SqlKata.Query query)
  146. {
  147. return await _repository.UpdateAsync(query) > 0;
  148. }
  149. public async Task<PagedList<Entity.tede2.Media.Media>> GetAllAsync(MediaSearchRequest request)
  150. {
  151. var result = new PagedList<Entity.tede2.Media.Media>();
  152. var query = Q.NewQuery();
  153. if (!string.IsNullOrEmpty(request.KeyWord))
  154. query.WhereLike(nameof(Entity.tede2.Media.Media.Title), $"%{request.KeyWord}%");
  155. if (request.IsDelete > 0)
  156. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), request.IsDelete == 1);
  157. if (request.MediaType > 0)
  158. query.Where(nameof(Entity.tede2.Media.Media.MediaType), request.MediaType);
  159. if (request.LibraryType > 0)
  160. query.Where(nameof(Entity.tede2.Media.Media.IsLibrary), request.LibraryType == 1);
  161. result.Total = await _repository.CountAsync(query);
  162. var item = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage).OrderByDesc(nameof(Entity.tede2.Media.Media.Sort)));
  163. result.Items = item;
  164. return result;
  165. }
  166. public async Task<bool> UpdateAsync(MediaResult result)
  167. {
  168. //var model = _mapper.Map<Entity.tede2.Media.Media>(result);
  169. try
  170. {
  171. using (var transactionScope = new TransactionScope())
  172. {
  173. var model = await _repository.GetAsync(result.Id);
  174. if (result.CategoryId != null && result.CategoryId.Count > 0)
  175. {
  176. var categoryId = "";
  177. foreach (var item in result.CategoryId)
  178. categoryId += item.Split(",")[item.Split(",").Length - 1] + ",";
  179. categoryId = categoryId.Remove(categoryId.Length - 1, 1);
  180. model.CategoryId = categoryId;
  181. model.FullCategoryId = StringUtils.ObjectCollectionToString(result.CategoryId);
  182. }
  183. if (result.CategoryName != null && result.CategoryName.Count > 0)
  184. {
  185. var categoryName = "";
  186. foreach (var item in result.CategoryName)
  187. categoryName += item.Split(",")[item.Split(",").Length - 1] + ",";
  188. categoryName = categoryName.Remove(categoryName.Length - 1, 1);
  189. model.CategoryName = categoryName;
  190. }
  191. if (result.AdminId > 0)
  192. model.AdminId = result.AdminId;
  193. if (!string.IsNullOrEmpty(result.Creator))
  194. model.Creator = result.Creator;
  195. if (result.MediaType > 0)
  196. model.MediaType = result.MediaType;
  197. if (!string.IsNullOrEmpty(result.Title))
  198. model.Title = result.Title;
  199. if (!string.IsNullOrEmpty(result.ImageUrls))
  200. model.ImageUrls = result.ImageUrls;
  201. if (!string.IsNullOrEmpty(result.Summary))
  202. model.Summary = result.Summary;
  203. if (!string.IsNullOrEmpty(result.Author))
  204. model.Author = result.Author;
  205. if (result.TeacherId > 0)
  206. model.TeacherId = result.TeacherId;
  207. if (!string.IsNullOrEmpty(result.Source))
  208. model.Source = result.Source;
  209. if (!string.IsNullOrEmpty(result.Blocks))
  210. model.Blocks = result.Blocks;
  211. if (result.IsChecked > 0)
  212. model.IsChecked = result.IsChecked == 1;
  213. if (!string.IsNullOrEmpty(result.AddDate))
  214. model.AddDate = Convert.ToDateTime(result.AddDate);
  215. if (model.Sort > 0)
  216. model.Sort = result.Sort;
  217. if (model.ReadCount > 0)
  218. model.ReadCount = result.ReadCount;
  219. if (result.IsTop > 0)
  220. model.IsTop = result.IsTop == 1;
  221. if (result.LableId > 0)
  222. model.LableId = result.LableId;
  223. if (result.IsRecommend > 0)
  224. model.IsRecommend = result.IsRecommend == 1;
  225. if (!string.IsNullOrEmpty(result.Press))
  226. model.Press = result.Press;
  227. if (!string.IsNullOrEmpty(result.PublishTime))
  228. model.PublishTime = result.PublishTime;
  229. if (result.IosDiscountPrice > 0)
  230. model.IosDiscountPrice = result.IosDiscountPrice;
  231. if (result.IosPrice > 0)
  232. model.IosPrice = result.IosPrice;
  233. if (result.OtherDiscountPrice > 0)
  234. model.OtherDiscountPrice = result.OtherDiscountPrice;
  235. if (result.OtherPrice > 0)
  236. model.OtherPrice = result.OtherPrice;
  237. if (result.FreeProportion > 0)
  238. model.FreeProportion = result.FreeProportion;
  239. if (result.AttachType > 0)
  240. model.AttachType = result.AttachType;
  241. if (result.AttachId > 0)
  242. model.AttachId = result.AttachId;
  243. if (result.MediaId > 0)
  244. model.MediaId = result.MediaId;
  245. if (!string.IsNullOrEmpty(result.MediaTitle))
  246. model.MediaTitle = result.MediaTitle;
  247. if (!string.IsNullOrEmpty(result.JournalsYear))
  248. model.JournalsYear = result.JournalsYear;
  249. if (result.JournalsCategory > 0)
  250. model.JournalsCategory = result.JournalsCategory;
  251. await _repository.UpdateAsync(model);
  252. if (result.MediaLibraryResults != null && result.MediaLibraryResults.Count > 0)
  253. {
  254. model.IsLibrary = result.MediaLibraryResults.Count > 0;
  255. //删除媒体库
  256. await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
  257. foreach (var item in result.MediaLibraryResults)
  258. {
  259. var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
  260. await mediaLibraryRepository.InsertAsync(mediaLibrary);
  261. }
  262. }
  263. if (result.MediaLableResults != null && result.MediaLableResults.Count > 0)
  264. {
  265. //删除媒体标签
  266. await mediaLableRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.MediaId), result.Id));
  267. //添加标签
  268. foreach (var item in result.MediaLableResults)
  269. {
  270. var mediaLable = _mapper.Map<Entity.tede2.Media.MediaLable>(item);
  271. await mediaLableRepository.InsertAsync(mediaLable);
  272. }
  273. }
  274. //提交事务
  275. transactionScope.Complete();
  276. }
  277. }
  278. catch (System.Exception ex)
  279. {
  280. throw new Common.Exceptions.BusinessException(ex.Message);
  281. }
  282. return true;
  283. }
  284. }
  285. }