CollectionRepository.cs 11 KB

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