NoteRepository.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Dapper;
  5. using Datory;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Page;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity;
  10. using GxPress.Repository.Interface.Note;
  11. using GxPress.Request.App.Note;
  12. using GxPress.Result.App.Note;
  13. using Microsoft.Extensions.Options;
  14. namespace GxPress.Repository.Implement.Note
  15. {
  16. public class NoteRepository : INoteRepository
  17. {
  18. private readonly Repository<Entity.Note.Note> _repository;
  19. private readonly IMapper _mapper;
  20. private readonly string _connectionString;
  21. private readonly string _databaseTypestr;
  22. public NoteRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  23. {
  24. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  25. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  26. var databaseType =
  27. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  28. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  29. _repository = new Repository<Entity.Note.Note>(database);
  30. _mapper = mapper;
  31. }
  32. public IDatabase Database => _repository.Database;
  33. public string TableName => _repository.TableName;
  34. public List<TableColumn> TableColumns => _repository.TableColumns;
  35. public async Task<Entity.Note.Note> GetAsync(int id)
  36. {
  37. return await _repository.GetAsync(id);
  38. }
  39. public async Task<bool> DeleteAsync(SqlKata.Query query)
  40. {
  41. return await _repository.DeleteAsync(query) > 0;
  42. }
  43. public async Task<int> InsertAsync(Entity.Note.Note note)
  44. {
  45. return await _repository.InsertAsync(note);
  46. }
  47. public async Task<bool> UpdateAsync(Entity.Note.Note note)
  48. {
  49. return await _repository.UpdateAsync(note);
  50. }
  51. public async Task<bool> UpdateAsync(SqlKata.Query query)
  52. {
  53. return await _repository.UpdateAsync(query) > 0;
  54. }
  55. /// <summary>
  56. /// 获取笔记分页
  57. /// </summary>
  58. /// <param name="request"></param>
  59. /// <returns></returns>
  60. public async Task<PagedList<NotePageListRequest>> NotePageListAsync(NoteSearchPageListRequest request)
  61. {
  62. var pagedList = new PagedList<NotePageListRequest>();
  63. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  64. var database = new Database(databaseType, _connectionString);
  65. var connection = database.GetConnection();
  66. var result =
  67. await connection
  68. .QueryAsync<NotePageListRequest, Entity.Note.Note, User, Department, NotePageListRequest>(GetCollectionSql(request),
  69. (notePageListRequest, note, user, department) =>
  70. {
  71. notePageListRequest.Title = note == null ? "" : note.Title;
  72. notePageListRequest.Content = note == null ? "" : note.Content;
  73. notePageListRequest.Name = user == null ? "" : user.Name;
  74. notePageListRequest.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  75. notePageListRequest.DepartmentName = department == null ? "" : department.Name;
  76. notePageListRequest.ReadCount = note == null ? 0 : note.ReadCount;
  77. notePageListRequest.NickName = user == null ? "" : user.Nick;
  78. return notePageListRequest;
  79. }, splitOn: "Title,Name,Name");
  80. pagedList.Items = result;
  81. pagedList.Total = await NotePageListCountAsync(GetCollectionSqlCount(request));
  82. return pagedList;
  83. }
  84. /// <summary>
  85. /// 集合
  86. /// </summary>
  87. /// <param name="request"></param>
  88. /// <returns></returns>
  89. public string GetCollectionSql(NoteSearchPageListRequest request)
  90. {
  91. string sql = "";
  92. if (request.VisitUserId > 0)
  93. {
  94. sql =
  95. $@"SELECT a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType=4 and DataSourceId=a.MiddleSonId) as EnjoyUser,(SELECT
  96. COUNT(1)
  97. FROM
  98. ccpph.tede_analyze
  99. WHERE
  100. UserId = {request.UserId} AND TypeValue = 3
  101. AND SourceId = a.MiddleId
  102. AND AnalyzeType = 1) AS PraiseCount,
  103. (SELECT
  104. COUNT(1)
  105. FROM
  106. ccpph.tede_analyze
  107. WHERE
  108. UserId = {request.UserId} AND TypeValue = 3
  109. AND SourceId = a.MiddleId
  110. AND AnalyzeType = 1
  111. LIMIT 0 , 1) AS IsPraise,
  112. (SELECT
  113. COUNT(1)
  114. FROM
  115. ccpph.tede_comment
  116. WHERE
  117. ArticleId = a.MiddleId and pid=0
  118. AND TypeValue = 3) AS CommentCount,
  119. (SELECT
  120. COUNT(1)
  121. FROM
  122. ccpph.tede_analyze
  123. WHERE
  124. UserId = {request.UserId} AND TypeValue = 3
  125. AND SourceId = a.MiddleId
  126. AND AnalyzeType = 4) AS RetransmissionCount,
  127. (SELECT
  128. COUNT(1)
  129. FROM
  130. ccpph.tede_analyze
  131. WHERE
  132. UserId = {request.UserId} AND TypeValue = 3
  133. AND SourceId = a.MiddleId
  134. AND AnalyzeType = 4
  135. LIMIT 0 , 1) AS IsRetransmission,
  136. (SELECT
  137. COUNT(1)
  138. FROM
  139. ccpph.tede_analyze
  140. WHERE
  141. UserId = {request.UserId} AND TypeValue = 3
  142. AND SourceId = a.MiddleId
  143. AND AnalyzeType = 4
  144. LIMIT 0 , 1) AS IsCollect,
  145. (SELECT
  146. COUNT(1)
  147. FROM
  148. ccpph.tede_analyze
  149. WHERE
  150. UserId = {request.UserId} AND TypeValue = 3
  151. AND SourceId = a.MiddleId
  152. AND AnalyzeType = 4) AS CollectCount,
  153. (SELECT
  154. COUNT(1)
  155. FROM
  156. ccpph.tede_middle
  157. WHERE
  158. ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,c.Name,c.AvatarUrl,c.Nick,d.Name FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_note b ON a.MiddleId = b.Id left join ccpph.tede_user c on c.Id=b.UserId left join ccpph.tede_department d on d.Id=c.DepartmentId
  159. WHERE a.UserId = {request.VisitUserId} AND a.FolderType = 4 AND a.IsDelete = 0";
  160. if (request.FolderId == 0)
  161. sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
  162. else
  163. sql += $" and a.ParentId={request.FolderId}";
  164. if (!string.IsNullOrEmpty(request.SearchKey))
  165. sql += $@" AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
  166. sql +=
  167. $@" OR a.id IN(SELECT MiddleId FROM ccpph.tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType = 4 AND a.UserId = {request.VisitUserId}";
  168. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  169. sql += $@" AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
  170. else if (!string.IsNullOrEmpty(request.SearchKey))
  171. sql += $@" and a.ParentId={request.FolderId} AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
  172. else
  173. sql += $" and a.ParentId={request.FolderId}";
  174. sql +=
  175. $@" ORDER BY a.IsTop DESC,a.AttributeValue desc, a.LastModifiedDate DESC";
  176. if (request.Page > 0)
  177. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  178. }
  179. else
  180. {
  181. sql =
  182. $@"SELECT a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType=4 and DataSourceId=a.MiddleSonId) as EnjoyUser,(SELECT
  183. COUNT(1)
  184. FROM
  185. ccpph.tede_analyze
  186. WHERE
  187. UserId = {request.UserId} AND TypeValue = 3
  188. AND SourceId = a.MiddleId
  189. AND AnalyzeType = 1) AS PraiseCount,
  190. (SELECT
  191. COUNT(1)
  192. FROM
  193. ccpph.tede_analyze
  194. WHERE
  195. UserId = {request.UserId} AND TypeValue = 3
  196. AND SourceId = a.MiddleId
  197. AND AnalyzeType = 1
  198. LIMIT 0 , 1) AS IsPraise,
  199. (SELECT
  200. COUNT(1)
  201. FROM
  202. ccpph.tede_comment
  203. WHERE
  204. UserId = {request.UserId} AND ArticleId = a.MiddleId
  205. AND TypeValue = 3) AS CommentCount,
  206. (SELECT
  207. COUNT(1)
  208. FROM
  209. ccpph.tede_analyze
  210. WHERE
  211. UserId = {request.UserId} AND TypeValue = 3
  212. AND SourceId = a.MiddleId
  213. AND AnalyzeType = 4) AS RetransmissionCount,
  214. (SELECT
  215. COUNT(1)
  216. FROM
  217. ccpph.tede_analyze
  218. WHERE
  219. UserId = {request.UserId} AND TypeValue = 3
  220. AND SourceId = a.MiddleId
  221. AND AnalyzeType = 4
  222. LIMIT 0 , 1) AS IsRetransmission,
  223. (SELECT
  224. COUNT(1)
  225. FROM
  226. ccpph.tede_analyze
  227. WHERE
  228. UserId = {request.UserId} AND TypeValue = 3
  229. AND SourceId = a.MiddleId
  230. AND AnalyzeType = 4
  231. LIMIT 0 , 1) AS IsCollect,
  232. (SELECT
  233. COUNT(1)
  234. FROM
  235. ccpph.tede_analyze
  236. WHERE
  237. UserId = {request.UserId} AND TypeValue = 3
  238. AND SourceId = a.MiddleId
  239. AND AnalyzeType = 4) AS IsCollect,
  240. (SELECT
  241. COUNT(1)
  242. FROM
  243. ccpph.tede_middle
  244. WHERE
  245. ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,c.Name,c.AvatarUrl,d.Name FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_note b ON a.MiddleId = b.Id left join ccpph.tede_user c on c.Id=b.UserId left join ccpph.tede_department d on d.Id=c.DepartmentId WHERE a.UserId = {request.UserId} AND a.FolderType = 4 AND a.IsDelete = 0";
  246. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  247. sql += $@" AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
  248. else if (!string.IsNullOrEmpty(request.SearchKey))
  249. sql += $@" and a.ParentId={request.FolderId} AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%')";
  250. else
  251. sql += $" and a.ParentId={request.FolderId}";
  252. sql +=
  253. $@" ORDER BY a.IsTop DESC,a.Sort DESC ,a.AttributeValue desc , a.LastModifiedDate DESC";
  254. if (request.Page > 0)
  255. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  256. }
  257. return sql;
  258. }
  259. /// <summary>
  260. /// 条数
  261. /// </summary>
  262. /// <param name="request"></param>
  263. /// <returns></returns>
  264. public string GetCollectionSqlCount(NoteSearchPageListRequest request)
  265. {
  266. string sql = "";
  267. if (request.VisitUserId > 0)
  268. {
  269. sql =
  270. $@"SELECT count(1) FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_note b ON a.MiddleId = b.Id WHERE a.UserId = {request.VisitUserId} AND a.FolderType = 4 AND a.IsDelete = 0";
  271. if (request.FolderId == 0)
  272. sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
  273. else
  274. sql += $" and a.ParentId={request.FolderId}";
  275. if (!string.IsNullOrEmpty(request.SearchKey))
  276. sql += $@" AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
  277. sql +=
  278. $@" OR a.id IN(SELECT MiddleId FROM ccpph.tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType = 4 AND a.UserId = {request.VisitUserId}";
  279. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  280. sql += $@" AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
  281. else if (!string.IsNullOrEmpty(request.SearchKey))
  282. sql += $@" and a.ParentId={request.FolderId} AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
  283. else
  284. sql += $" and a.ParentId={request.FolderId}";
  285. }
  286. else
  287. {
  288. sql =
  289. $@"SELECT count(1) FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_note b ON a.MiddleId = b.Id left join ccpph.tede_user c on c.Id=b.UserId left join ccpph.tede_department d on d.Id=c.DepartmentId WHERE a.UserId = {request.UserId} AND a.FolderType = 4 AND a.IsDelete = 0";
  290. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  291. sql += $@" AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
  292. else if (!string.IsNullOrEmpty(request.SearchKey))
  293. sql += $@" and a.ParentId={request.FolderId} AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
  294. else
  295. sql += $" and a.ParentId={request.FolderId}";
  296. }
  297. return sql;
  298. }
  299. public async Task<int> NotePageListCountAsync(string sql)
  300. {
  301. var pagedList = new PagedList<NotePageListRequest>();
  302. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  303. var database = new Database(databaseType, _connectionString);
  304. var connection = database.GetConnection();
  305. var result =
  306. await connection
  307. .ExecuteScalarAsync<int>(sql);
  308. return result;
  309. }
  310. /// <summary>
  311. /// 删除笔记
  312. /// </summary>
  313. /// <param name="id"></param>
  314. /// <returns></returns>
  315. public async Task<bool> DeleteAsync(int id) => await _repository.DeleteAsync(id);
  316. }
  317. }