CollectionRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. public async Task<bool> UpdateAsync(Entity.Collection collection)
  61. {
  62. return await _repository.UpdateAsync(collection);
  63. }
  64. /// <summary>
  65. /// 查询结果
  66. /// </summary>
  67. /// <param name="request"></param>
  68. /// <returns></returns>
  69. public async Task<PagedList<CollectionListPageResult>> PageListAsync(CollectionPageSearchRequest request)
  70. {
  71. var result = new PagedList<CollectionListPageResult>();
  72. var query = new SqlKata.Query().From("tede_middle");
  73. query.LeftJoin("tede_collection", "tede_collection.Id", "tede_middle.MiddleId");
  74. query.Where("tede_middle.UserId", request.VisitUserId > 0 ? request.VisitUserId : request.UserId);
  75. query.Where("tede_middle.FolderType", GxPress.EnumConst.AllTypeConst.Collect.GetHashCode());
  76. query.Where("tede_middle.IsDelete", false);
  77. if (request.VisitUserId > 0)
  78. {
  79. var folderUserQuery = new SqlKata.Query("tede_folder_user");
  80. folderUserQuery.Where("tede_folder_user.UserId", request.UserId);
  81. folderUserQuery.WhereColumns("tede_folder_user.MiddleId", "=", "tede_middle.Id");
  82. folderUserQuery.Select("tede_folder_user.MiddleId");
  83. query.Where(n => n.WhereIn("tede_middle.RoleId", new List<int> { MiddleRoleConst.Publicity.GetHashCode(), MiddleRoleConst.Share.GetHashCode() }).OrWhereIn("tede_middle.Id", folderUserQuery));
  84. }
  85. if (!string.IsNullOrEmpty(request.SearchKey) && request.FolderId == 0)
  86. query.Where(n => n.OrWhereLike("tede_collection.Title", $"%{request.SearchKey}%")
  87. .OrWhereLike("tede_middle.FolderName", $"%{request.SearchKey}%")
  88. .OrWhereLike("tede_collection.ResultJson", $"%{request.SearchKey}%"));
  89. else
  90. {
  91. query.Where("tede_middle.ParentId", request.FolderId);
  92. if (!string.IsNullOrEmpty(request.SearchKey))
  93. query.Where(n => n.OrWhereLike("tede_collection.Title", $"%{request.SearchKey}%")
  94. .OrWhereLike("tede_middle.FolderName", $"%{request.SearchKey}%")
  95. .OrWhereLike("tede_collection.ResultJson", $"%{request.SearchKey}%"));
  96. }
  97. result.Total = await _middleRepository.CountAsync(query);
  98. query.ForPage(request.Page, request.PerPage);
  99. query.OrderByDesc("tede_middle.AttributeValue");
  100. query.OrderByDesc("tede_middle.IsTop");
  101. query.OrderByDesc("tede_middle.Sort");
  102. query.OrderByDesc("tede_middle.LastModifiedDate");
  103. var enjoyUser = new SqlKata.Query("tede_user_middle");
  104. enjoyUser.Where("MiddleType", AllTypeConst.Collect.GetHashCode());
  105. enjoyUser.WhereColumns("DataSourceId", "=", "tede_middle.Id");
  106. enjoyUser.SelectRaw("GROUP_CONCAT(SourceName Separator'、')");
  107. query.Select(enjoyUser, "EnjoyUser");
  108. var fileCount = new SqlKata.Query("tede_middle as a");
  109. fileCount.WhereColumns("a.ParentId", "=", "tede_middle.Id");
  110. fileCount.AsCount();
  111. query.Select(fileCount, "FileCount");
  112. query.Select(
  113. "tede_middle.{Id,MiddleId,UserId,FolderName,FolderType,IsTop,ParentId,AttributeValue,RoleId}",
  114. "tede_collection.{Title,CollectionDataId,CollectionType,SourceUserId,Id as CollectionId,CreatedDate,ResultJson}"
  115. );
  116. result.Items = await _middleRepository.GetAllAsync<CollectionListPageResult>(query);
  117. return result;
  118. }
  119. /// <summary>
  120. /// 根据ID获取收藏数据
  121. /// </summary>
  122. /// <param name="id"></param>
  123. /// <returns></returns>
  124. public async Task<Entity.Collection> GetAsync(int id)
  125. {
  126. return await _repository.GetAsync(id);
  127. }
  128. public async Task<Entity.Collection> GetAsync(SqlKata.Query query)
  129. {
  130. return await _repository.GetAsync(query);
  131. }
  132. public async Task<bool> DeleteAsync(Query query)
  133. {
  134. return await _repository.DeleteAsync(query) > 0;
  135. }
  136. /// <summary>
  137. /// 递归删除收藏包括文件夹下面的数据
  138. /// </summary>
  139. /// <param name="middleIds"></param>
  140. /// <returns></returns>
  141. public async Task<bool> RecursionDeleteAsync(List<int> middleIds)
  142. {
  143. foreach (var item in middleIds)
  144. {
  145. await DeleteParendIdAsync(item);
  146. }
  147. return true;
  148. }
  149. public async Task<bool> DeleteParendIdAsync(int parentId)
  150. {
  151. var middle = await _middleRepository.GetAsync(parentId);
  152. if (middle == null)
  153. return true;
  154. //删除middle
  155. await _middleRepository.DeleteAsync(middle.Id);
  156. //删除coll
  157. await _repository.DeleteAsync(middle.MiddleId);
  158. //删除收藏
  159. var query = new SqlKata.Query();
  160. query.Where(nameof(Entity.Analyze.Analyze.SourceId), middle.MiddleId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3).Where(nameof(Entity.Analyze.Analyze.UserId), middle.UserId);
  161. await _analyzeRepository.DeleteAsync(query);
  162. var middles = await _middleRepository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), parentId));
  163. if (middles.Count == 0)
  164. return true;
  165. foreach (var item in middles)
  166. {
  167. //删除middle
  168. await _middleRepository.DeleteAsync(item.Id);
  169. //删除coll
  170. await _repository.DeleteAsync(item.MiddleId);
  171. //删除收藏
  172. query = new SqlKata.Query();
  173. query.Where(nameof(Entity.Analyze.Analyze.SourceId), item.MiddleId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3).Where(nameof(Entity.Analyze.Analyze.UserId), middle.UserId);
  174. await _analyzeRepository.DeleteAsync(query);
  175. await DeleteParendIdAsync(item.Id);
  176. }
  177. return true;
  178. }
  179. public async Task<bool> DeleteAsync(int id)
  180. {
  181. return await _repository.DeleteAsync(id);
  182. }
  183. public async Task<IEnumerable<Entity.Collection>> GetAllAsync(Query query)
  184. {
  185. return await _repository.GetAllAsync(query);
  186. }
  187. /// <summary>
  188. /// 收藏无文件夹分页列表
  189. /// </summary>
  190. /// <param name="request"></param>
  191. /// <returns></returns>
  192. public async Task<PagedList<CollectionNoFolderResult>> GetCollectionNoFolderPageListAsync(CollectionPageSearchRequest request)
  193. {
  194. return await GetNewCollectionNoFolderPageListAsync(request);
  195. }
  196. /// <summary>
  197. /// 收藏无文件夹分页列表
  198. /// </summary>
  199. /// <param name="request"></param>
  200. /// <returns></returns>
  201. public async Task<PagedList<CollectionNoFolderResult>> GetNewCollectionNoFolderPageListAsync(CollectionPageSearchRequest request)
  202. {
  203. var folderNameQuery = new SqlKata.Query("tede_middle");
  204. folderNameQuery.WhereColumns("Id", "=", "a.ParentId");
  205. folderNameQuery.Select("FolderName");
  206. var result = new PagedList<CollectionNoFolderResult>();
  207. var collectionContsValue = GxPress.EnumConst.AllTypeConst.Collect.GetHashCode();
  208. var query = new SqlKata.Query("tede_collection");
  209. query.Join("tede_middle as a", "a.MiddleId", "tede_collection.Id");
  210. if (!string.IsNullOrEmpty(request.SearchKey))
  211. query.Where(n => n.WhereLike("tede_collection.Title", $"%{request.SearchKey}%")
  212. .OrWhereLike("tede_collection.ResultJson", $"%{request.SearchKey}%"));
  213. query.Where("a.FolderType", collectionContsValue);
  214. query.Where("tede_collection.UserId", request.UserId);
  215. query.Where("a.IsDelete", false);
  216. //条数
  217. result.Total = await _repository.CountAsync(query);
  218. query.OrderByDesc("a.IsTop");
  219. query.OrderByDesc("a.LastModifiedDate");
  220. query.ForPage(request.Page, request.PerPage);
  221. query.Select(
  222. "tede_collection.{UserId,Title,CollectionType,CollectionDataId,SourceUserId,ResultJson}",
  223. "a.{IsTop,Id,MiddleId,ParentId as FolderId}"
  224. );
  225. query.Select(folderNameQuery, "FolderName");
  226. result.Items = await _repository.GetAllAsync<CollectionNoFolderResult>(query);
  227. return result;
  228. }
  229. }
  230. }