CollectionRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Dapper;
  6. using Datory;
  7. using GxPress.Common.AppOptions;
  8. using GxPress.Common.Page;
  9. using GxPress.Common.Tools;
  10. using GxPress.EnumConst;
  11. using GxPress.Repository.Interface.Collection;
  12. using GxPress.Request.App.Collection;
  13. using GxPress.Result.App.Collection;
  14. using Microsoft.Extensions.Options;
  15. using SqlKata;
  16. namespace GxPress.Repository.Implement.Collection
  17. {
  18. public class CollectionRepository : ICollectionRepository
  19. {
  20. private readonly Repository<Entity.Collection> _repository;
  21. private readonly Repository<Entity.Middle.Middle> _middleRepository;
  22. private readonly Repository<Entity.Analyze.Analyze> _analyzeRepository;
  23. private readonly IMapper _mapper;
  24. private readonly string _connectionString;
  25. private readonly string _databaseTypestr;
  26. public CollectionRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  27. {
  28. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  29. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  30. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  31. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  32. _repository = new Repository<Entity.Collection>(database);
  33. _middleRepository = new Repository<Entity.Middle.Middle>(database);
  34. _analyzeRepository = new Repository<Entity.Analyze.Analyze>(database);
  35. _mapper = mapper;
  36. }
  37. public IDatabase Database => _repository.Database;
  38. public string TableName => _repository.TableName;
  39. public List<TableColumn> TableColumns => _repository.TableColumns;
  40. /// <summary>
  41. /// 添加收藏
  42. /// </summary>
  43. /// <param name="collection"></param>
  44. /// <returns></returns>
  45. public async Task<int> InsertAsync(Entity.Collection collection)
  46. {
  47. return await _repository.InsertAsync(collection);
  48. }
  49. /// <summary>
  50. /// 设置收藏置顶
  51. /// </summary>
  52. /// <param name="collection"></param>
  53. /// <returns></returns>
  54. public async Task<bool> SetTopAsync(int id)
  55. {
  56. var collction = await _repository.GetAsync(id);
  57. collction.IsTop = !collction.IsTop;
  58. return await _repository.UpdateAsync(collction);
  59. }
  60. /// <summary>
  61. /// 查询结果
  62. /// </summary>
  63. /// <param name="request"></param>
  64. /// <returns></returns>
  65. public async Task<PagedList<CollectionListPageResult>> PageListAsync(CollectionPageSearchRequest request)
  66. {
  67. var result = new PagedList<CollectionListPageResult>();
  68. var query = new SqlKata.Query().From("tede_middle");
  69. query.LeftJoin("tede_collection", "tede_collection.Id", "tede_middle.MiddleId");
  70. query.Where("tede_middle.UserId", request.VisitUserId > 0 ? request.VisitUserId : request.UserId);
  71. query.Where("tede_middle.FolderType", GxPress.EnumConst.AllTypeConst.Collect.GetHashCode());
  72. query.Where("tede_middle.IsDelete", false);
  73. if (request.VisitUserId > 0)
  74. {
  75. var folderUserQuery = new SqlKata.Query("tede_folder_user");
  76. folderUserQuery.Where("tede_folder_user.UserId", request.UserId);
  77. folderUserQuery.WhereColumns("tede_folder_user.MiddleId", "=", "tede_middle.Id");
  78. folderUserQuery.Select("tede_folder_user.MiddleId");
  79. query.Where(n => n.WhereIn("tede_middle.RoleId", new List<int> { MiddleRoleConst.Publicity.GetHashCode(), MiddleRoleConst.Share.GetHashCode() }).OrWhereIn("tede_middle.Id", folderUserQuery));
  80. }
  81. if (!string.IsNullOrEmpty(request.SearchKey) && request.FolderId == 0)
  82. query.Where(n => n.OrWhereLike("tede_collection.Title", $"%{request.SearchKey}%").OrWhereLike("tede_middle.FolderName", $"%{request.SearchKey}%"));
  83. else
  84. {
  85. query.Where("tede_middle.ParentId", request.FolderId);
  86. if (!string.IsNullOrEmpty(request.SearchKey))
  87. query.Where(n => n.OrWhereLike("tede_collection.Title", $"%{request.SearchKey}%").OrWhereLike("tede_middle.FolderName", $"%{request.SearchKey}%"));
  88. }
  89. result.Total = await _middleRepository.CountAsync(query);
  90. query.ForPage(request.Page, request.PerPage);
  91. query.OrderByDesc("tede_middle.AttributeValue");
  92. query.OrderByDesc("tede_middle.IsTop");
  93. query.OrderByDesc("tede_middle.Sort");
  94. query.OrderByDesc("tede_middle.LastModifiedDate");
  95. var enjoyUser = new SqlKata.Query("tede_user_middle");
  96. enjoyUser.Where("MiddleType", AllTypeConst.Collect.GetHashCode());
  97. enjoyUser.WhereColumns("DataSourceId", "=", "tede_middle.Id");
  98. enjoyUser.SelectRaw("GROUP_CONCAT(SourceName Separator'、')");
  99. query.Select(enjoyUser, "EnjoyUser");
  100. var fileCount = new SqlKata.Query("tede_middle as a");
  101. fileCount.WhereColumns("a.ParentId", "=", "tede_middle.Id");
  102. fileCount.AsCount();
  103. query.Select(fileCount, "FileCount");
  104. query.Select(
  105. "tede_middle.{Id,MiddleId,UserId,FolderName,FolderType,IsTop,ParentId,AttributeValue,RoleId}",
  106. "tede_collection.{Title,CollectionDataId,CollectionType,SourceUserId,Id as CollectionId,CreatedDate,ResultJson}"
  107. );
  108. result.Items = await _middleRepository.GetAllAsync<CollectionListPageResult>(query);
  109. return result;
  110. }
  111. /// <summary>
  112. /// 根据ID获取收藏数据
  113. /// </summary>
  114. /// <param name="id"></param>
  115. /// <returns></returns>
  116. public async Task<Entity.Collection> GetAsync(int id)
  117. {
  118. return await _repository.GetAsync(id);
  119. }
  120. public async Task<Entity.Collection> GetAsync(SqlKata.Query query)
  121. {
  122. return await _repository.GetAsync(query);
  123. }
  124. public async Task<bool> DeleteAsync(Query query)
  125. {
  126. return await _repository.DeleteAsync(query) > 0;
  127. }
  128. /// <summary>
  129. /// 递归删除收藏包括文件夹下面的数据
  130. /// </summary>
  131. /// <param name="middleIds"></param>
  132. /// <returns></returns>
  133. public async Task<bool> RecursionDeleteAsync(List<int> middleIds)
  134. {
  135. foreach (var item in middleIds)
  136. {
  137. await DeleteParendIdAsync(item);
  138. }
  139. return true;
  140. }
  141. public async Task<bool> DeleteParendIdAsync(int parentId)
  142. {
  143. var middle = await _middleRepository.GetAsync(parentId);
  144. if (middle == null)
  145. return true;
  146. //删除middle
  147. await _middleRepository.DeleteAsync(middle.Id);
  148. //删除coll
  149. await _repository.DeleteAsync(middle.MiddleId);
  150. //删除收藏
  151. var query = new SqlKata.Query();
  152. query.Where(nameof(Entity.Analyze.Analyze.SourceId), middle.MiddleId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3).Where(nameof(Entity.Analyze.Analyze.UserId), middle.UserId);
  153. await _analyzeRepository.DeleteAsync(query);
  154. var middles = await _middleRepository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), parentId));
  155. if (middles.Count == 0)
  156. return true;
  157. foreach (var item in middles)
  158. {
  159. //删除middle
  160. await _middleRepository.DeleteAsync(item.Id);
  161. //删除coll
  162. await _repository.DeleteAsync(item.MiddleId);
  163. //删除收藏
  164. query = new SqlKata.Query();
  165. query.Where(nameof(Entity.Analyze.Analyze.SourceId), item.MiddleId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3).Where(nameof(Entity.Analyze.Analyze.UserId), middle.UserId);
  166. await _analyzeRepository.DeleteAsync(query);
  167. await DeleteParendIdAsync(item.Id);
  168. }
  169. return true;
  170. }
  171. public async Task<bool> DeleteAsync(int id)
  172. {
  173. return await _repository.DeleteAsync(id);
  174. }
  175. public async Task<IEnumerable<Entity.Collection>> GetAllAsync(Query query)
  176. {
  177. return await _repository.GetAllAsync(query);
  178. }
  179. /// <summary>
  180. /// 收藏无文件夹分页列表
  181. /// </summary>
  182. /// <param name="request"></param>
  183. /// <returns></returns>
  184. public async Task<PagedList<CollectionNoFolderResult>> GetCollectionNoFolderPageListAsync(CollectionPageSearchRequest request)
  185. {
  186. return await GetNewCollectionNoFolderPageListAsync(request);
  187. }
  188. /// <summary>
  189. /// 收藏无文件夹分页列表
  190. /// </summary>
  191. /// <param name="request"></param>
  192. /// <returns></returns>
  193. public async Task<PagedList<CollectionNoFolderResult>> GetNewCollectionNoFolderPageListAsync(CollectionPageSearchRequest request)
  194. {
  195. var folderNameQuery = new SqlKata.Query("tede_middle");
  196. folderNameQuery.WhereColumns("Id", "=", "a.ParentId");
  197. folderNameQuery.Select("FolderName");
  198. var result = new PagedList<CollectionNoFolderResult>();
  199. var collectionContsValue = GxPress.EnumConst.AllTypeConst.Collect.GetHashCode();
  200. var query = new SqlKata.Query("tede_collection");
  201. query.Join("tede_middle as a", "a.MiddleId", "tede_collection.Id");
  202. if (!string.IsNullOrEmpty(request.SearchKey))
  203. query.WhereLike("tede_collection.Title", $"%{request.SearchKey}%");
  204. query.Where("a.FolderType", collectionContsValue);
  205. query.Where("tede_collection.UserId", request.UserId);
  206. query.Where("a.IsDelete", false);
  207. //条数
  208. result.Total = await _repository.CountAsync(query);
  209. query.OrderByDesc("a.IsTop");
  210. query.OrderByDesc("a.LastModifiedDate");
  211. query.ForPage(request.Page, request.PerPage);
  212. query.Select(
  213. "tede_collection.{UserId,Title,CollectionType,CollectionDataId,SourceUserId}",
  214. "a.{IsTop,Id,MiddleId,ParentId as FolderId}"
  215. );
  216. query.Select(folderNameQuery, "FolderName");
  217. result.Items = await _repository.GetAllAsync<CollectionNoFolderResult>(query);
  218. return result;
  219. }
  220. }
  221. }