CollectionRepository.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.Repository.Interface.Collection;
  11. using GxPress.Request.App.Collection;
  12. using GxPress.Result.App.Collection;
  13. using Microsoft.Extensions.Options;
  14. using SqlKata;
  15. namespace GxPress.Repository.Implement.Collection
  16. {
  17. public class CollectionRepository : ICollectionRepository
  18. {
  19. private readonly Repository<Entity.Collection> _repository;
  20. private readonly Repository<Entity.Middle.Middle> _middleRepository;
  21. private readonly Repository<Entity.Analyze.Analyze> _analyzeRepository;
  22. private readonly IMapper _mapper;
  23. private readonly string _connectionString;
  24. private readonly string _databaseTypestr;
  25. public CollectionRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  26. {
  27. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  28. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  29. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  30. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  31. _repository = new Repository<Entity.Collection>(database);
  32. _middleRepository = new Repository<Entity.Middle.Middle>(database);
  33. _analyzeRepository = new Repository<Entity.Analyze.Analyze>(database);
  34. _mapper = mapper;
  35. }
  36. public IDatabase Database => _repository.Database;
  37. public string TableName => _repository.TableName;
  38. public List<TableColumn> TableColumns => _repository.TableColumns;
  39. /// <summary>
  40. /// 添加收藏
  41. /// </summary>
  42. /// <param name="collection"></param>
  43. /// <returns></returns>
  44. public async Task<int> InsertAsync(Entity.Collection collection)
  45. {
  46. return await _repository.InsertAsync(collection);
  47. }
  48. /// <summary>
  49. /// 查询结果
  50. /// </summary>
  51. /// <param name="request"></param>
  52. /// <returns></returns>
  53. public async Task<PagedList<CollectionListPageResult>> PageListAsync(CollectionPageSearchRequest request)
  54. {
  55. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  56. var database = new Database(databaseType, _connectionString);
  57. var connection = database.GetConnection();
  58. var items = await connection
  59. .QueryAsync<CollectionListPageResult, Entity.Collection, CollectionListPageResult>(GetCollectionSql(request),
  60. (collectionListPageResult, collection) =>
  61. {
  62. if (collectionListPageResult == null)
  63. throw new ArgumentNullException(nameof(collectionListPageResult));
  64. //collectionListPageResult= _mapper.Map<CollectionListPageResult>(middle);
  65. collectionListPageResult.Title = collection == null ? "" : collection.Title;
  66. collectionListPageResult.CollectionDataId = collection?.CollectionDataId ?? 0;
  67. collectionListPageResult.CollectionType = collection?.CollectionType ?? 0;
  68. collectionListPageResult.SourceUserId = collection?.SourceUserId ?? 0;
  69. collectionListPageResult.CollectionId = collection?.Id ?? 0;
  70. return collectionListPageResult;
  71. },
  72. splitOn: "MiddleId,Id");
  73. PagedList<CollectionListPageResult> result = new PagedList<CollectionListPageResult>
  74. {
  75. Total = await CountAsync(GetCollectionSqlCount(request)),
  76. Items = items
  77. };
  78. return result;
  79. }
  80. /// <summary>
  81. /// 集合
  82. /// </summary>
  83. /// <param name="request"></param>
  84. /// <returns></returns>
  85. public string GetCollectionSql(CollectionPageSearchRequest request)
  86. {
  87. string sql = "";
  88. if (request.VisitUserId > 0)
  89. {
  90. sql =
  91. $@"SELECT a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType=3 and DataSourceId=a.MiddleSonId) as EnjoyUser,(select count(1) from tede_middle where ParentId=a.Id) as FileCount, b.* FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_collection b ON a.MiddleId = b.Id WHERE a.UserId = {request.VisitUserId} AND a.FolderType = 3 AND a.IsDelete = 0";
  92. if (request.FolderId == 0)
  93. sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
  94. else
  95. sql += $" and a.ParentId={request.FolderId}";
  96. if (!string.IsNullOrEmpty(request.SearchKey))
  97. sql += $@" AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%')";
  98. sql +=
  99. $@" OR a.id IN(SELECT MiddleId FROM ccpph.tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType = 3 AND a.UserId = {request.VisitUserId}";
  100. if (request.FolderId > 0)
  101. sql += $" and a.ParentId={request.FolderId}";
  102. if (!string.IsNullOrEmpty(request.SearchKey))
  103. sql += $@" AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%')";
  104. sql +=
  105. $@" ORDER BY a.IsTop DESC,a.Sort desc,a.AttributeValue desc, a.LastModifiedDate DESC";
  106. if (request.Page > 0)
  107. sql += $" LIMIT {(request.Page - 1) * request.PerPage},{request.PerPage}";
  108. }
  109. else
  110. {
  111. sql =
  112. $@"SELECT a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType=3 and DataSourceId=a.MiddleSonId) as EnjoyUser,(select count(1) from tede_middle where ParentId=a.Id) as FileCount, b.* FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_collection b ON a.MiddleId = b.Id WHERE a.UserId = {request.UserId} AND a.FolderType = 3 AND a.IsDelete = 0";
  113. if (!string.IsNullOrEmpty(request.SearchKey) && request.FolderId > 0)
  114. {
  115. sql += $" AND a.ParentId={request.FolderId}";
  116. sql += $@" AND (b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%')";
  117. }
  118. else if (!string.IsNullOrEmpty(request.SearchKey))
  119. {
  120. sql += $@" AND (b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%')";
  121. }
  122. else
  123. sql += $" and a.ParentId={request.FolderId}";
  124. sql += $@" ORDER BY a.IsTop DESC,a.Sort desc,a.AttributeValue desc, a.LastModifiedDate DESC";
  125. if (request.Page > 0)
  126. sql += $" LIMIT {(request.Page - 1) * request.PerPage},{request.PerPage}";
  127. }
  128. return sql;
  129. }
  130. /// <summary>
  131. /// 条数
  132. /// </summary>
  133. /// <param name="request"></param>
  134. /// <returns></returns>
  135. public string GetCollectionSqlCount(CollectionPageSearchRequest request)
  136. {
  137. string sql = "";
  138. if (request.VisitUserId > 0)
  139. {
  140. sql =
  141. $@"SELECT count(1) FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_collection b ON a.MiddleId = b.Id WHERE a.UserId = {request.VisitUserId} AND a.FolderType = 3 AND a.IsDelete = 0";
  142. if (request.FolderId == 0)
  143. sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
  144. else
  145. sql += $" and a.ParentId={request.FolderId}";
  146. if (!string.IsNullOrEmpty(request.SearchKey))
  147. sql += $@" AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%')";
  148. sql +=
  149. $@" OR a.id IN(SELECT MiddleId FROM ccpph.tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType = 3 AND a.UserId = {request.VisitUserId}";
  150. if (request.FolderId > 0)
  151. sql += $" and a.ParentId={request.FolderId}";
  152. if (!string.IsNullOrEmpty(request.SearchKey))
  153. sql += $@" AND (b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%')";
  154. }
  155. else
  156. {
  157. sql =
  158. $@"SELECT count(1) FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_collection b ON a.MiddleId = b.Id WHERE a.UserId = {request.UserId} AND a.FolderType = 3 AND a.IsDelete = 0";
  159. if (!string.IsNullOrEmpty(request.SearchKey) && request.FolderId > 0)
  160. {
  161. sql += $" AND a.ParentId={request.FolderId}";
  162. sql += $@" AND (b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%')";
  163. }
  164. else if (!string.IsNullOrEmpty(request.SearchKey))
  165. {
  166. sql += $@" AND (b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%')";
  167. }
  168. else
  169. sql += $" and a.ParentId={request.FolderId}";
  170. }
  171. return sql;
  172. }
  173. public async Task<int> CountAsync(string sql)
  174. {
  175. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  176. var database = new Database(databaseType, _connectionString);
  177. var connection = database.GetConnection();
  178. var items = await connection.ExecuteScalarAsync<int>(sql);
  179. return items;
  180. }
  181. /// <summary>
  182. /// 根据ID获取收藏数据
  183. /// </summary>
  184. /// <param name="id"></param>
  185. /// <returns></returns>
  186. public async Task<Entity.Collection> GetAsync(int id)
  187. {
  188. return await _repository.GetAsync(id);
  189. }
  190. public async Task<Entity.Collection> GetAsync(SqlKata.Query query)
  191. {
  192. return await _repository.GetAsync(query);
  193. }
  194. public async Task<bool> DeleteAsync(Query query)
  195. {
  196. return await _repository.DeleteAsync(query) > 0;
  197. }
  198. /// <summary>
  199. /// 递归删除收藏包括文件夹下面的数据
  200. /// </summary>
  201. /// <param name="middleIds"></param>
  202. /// <returns></returns>
  203. public async Task<bool> RecursionDeleteAsync(List<int> middleIds)
  204. {
  205. foreach (var item in middleIds)
  206. {
  207. await DeleteParendIdAsync(item);
  208. }
  209. return true;
  210. }
  211. public async Task<bool> DeleteParendIdAsync(int parentId)
  212. {
  213. var middle = await _middleRepository.GetAsync(parentId);
  214. if (middle == null)
  215. return true;
  216. //删除middle
  217. await _middleRepository.DeleteAsync(middle.Id);
  218. //删除coll
  219. await _repository.DeleteAsync(middle.MiddleId);
  220. //删除收藏
  221. var query = new SqlKata.Query();
  222. query.Where(nameof(Entity.Analyze.Analyze.SourceId), middle.MiddleId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3).Where(nameof(Entity.Analyze.Analyze.UserId), middle.UserId);
  223. await _analyzeRepository.DeleteAsync(query);
  224. var middles = await _middleRepository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), parentId));
  225. if (middles.Count == 0)
  226. return true;
  227. foreach (var item in middles)
  228. {
  229. //删除middle
  230. await _middleRepository.DeleteAsync(item.Id);
  231. //删除coll
  232. await _repository.DeleteAsync(item.MiddleId);
  233. //删除收藏
  234. query = new SqlKata.Query();
  235. query.Where(nameof(Entity.Analyze.Analyze.SourceId), item.MiddleId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3).Where(nameof(Entity.Analyze.Analyze.UserId), middle.UserId);
  236. await _analyzeRepository.DeleteAsync(query);
  237. await DeleteParendIdAsync(item.Id);
  238. }
  239. return true;
  240. }
  241. public async Task<bool> DeleteAsync(int id)
  242. {
  243. return await _repository.DeleteAsync(id);
  244. }
  245. public async Task<IEnumerable<Entity.Collection>> GetAllAsync(Query query)
  246. {
  247. return await _repository.GetAllAsync(query);
  248. }
  249. }
  250. }