NoteRepository.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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.Request.Web;
  13. using GxPress.Result.App.Note;
  14. using GxPress.Result.Web;
  15. using Microsoft.Extensions.Options;
  16. namespace GxPress.Repository.Implement.Note
  17. {
  18. public class NoteRepository : INoteRepository
  19. {
  20. private readonly Repository<Entity.Note.Note> _repository;
  21. private readonly IMapper _mapper;
  22. private readonly string _connectionString;
  23. private readonly string _databaseTypestr;
  24. public NoteRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  25. {
  26. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  27. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  28. var databaseType =
  29. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  30. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  31. _repository = new Repository<Entity.Note.Note>(database);
  32. _mapper = mapper;
  33. }
  34. public IDatabase Database => _repository.Database;
  35. public string TableName => _repository.TableName;
  36. public List<TableColumn> TableColumns => _repository.TableColumns;
  37. public async Task<Entity.Note.Note> GetAsync(int id)
  38. {
  39. return await _repository.GetAsync(id);
  40. }
  41. public async Task<bool> DeleteAsync(SqlKata.Query query)
  42. {
  43. return await _repository.DeleteAsync(query) > 0;
  44. }
  45. public async Task<int> InsertAsync(Entity.Note.Note note)
  46. {
  47. return await _repository.InsertAsync(note);
  48. }
  49. public async Task<bool> UpdateAsync(Entity.Note.Note note)
  50. {
  51. return await _repository.UpdateAsync(note);
  52. }
  53. public async Task<bool> UpdateAsync(SqlKata.Query query)
  54. {
  55. return await _repository.UpdateAsync(query) > 0;
  56. }
  57. /// <summary>
  58. /// 获取笔记分页
  59. /// </summary>
  60. /// <param name="request"></param>
  61. /// <returns></returns>
  62. public async Task<PagedList<NotePageListRequest>> NotePageListAsync(NoteSearchPageListRequest request)
  63. {
  64. var pagedList = new PagedList<NotePageListRequest>();
  65. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  66. var database = new Database(databaseType, _connectionString);
  67. var connection = database.GetConnection();
  68. var result =
  69. await connection
  70. .QueryAsync<NotePageListRequest, Entity.Note.Note, User, Department, NotePageListRequest>(GetCollectionSql(request),
  71. (notePageListRequest, note, user, department) =>
  72. {
  73. notePageListRequest.Title = note == null ? "" : note.Title;
  74. notePageListRequest.Content = note == null ? "" : note.Content;
  75. notePageListRequest.MediaId = note == null ? 0 : note.MediaId;
  76. notePageListRequest.CatalogId = note == null ? "" : note.CatalogId;
  77. notePageListRequest.ChapterId = note == null ? "" : note.ChapterId;
  78. notePageListRequest.Name = user == null ? "" : user.Name;
  79. notePageListRequest.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  80. notePageListRequest.DepartmentName = department == null ? "" : department.Name;
  81. notePageListRequest.ReadCount = note == null ? 0 : note.ReadCount;
  82. notePageListRequest.NickName = user == null ? "" : user.Nick;
  83. return notePageListRequest;
  84. }, splitOn: "Title,Name,Name");
  85. pagedList.Items = result;
  86. pagedList.Total = await NotePageListCountAsync(GetCollectionSqlCount(request));
  87. return pagedList;
  88. }
  89. /// <summary>
  90. /// 集合
  91. /// </summary>
  92. /// <param name="request"></param>
  93. /// <returns></returns>
  94. public string GetCollectionSql(NoteSearchPageListRequest request)
  95. {
  96. string sql = "";
  97. var noteTypeId= GxPress.EnumConst.AllTypeConst.Note.GetHashCode();
  98. if (request.VisitUserId > 0)
  99. {
  100. sql =
  101. $@"SELECT a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType={noteTypeId} and DataSourceId=a.MiddleSonId) as EnjoyUser,(SELECT
  102. COUNT(1)
  103. FROM
  104. tede_analyze
  105. WHERE
  106. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  107. AND SourceId = a.MiddleId
  108. AND AnalyzeType = 1) AS PraiseCount,
  109. (SELECT
  110. COUNT(1)
  111. FROM
  112. tede_analyze
  113. WHERE
  114. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  115. AND SourceId = a.MiddleId
  116. AND AnalyzeType = 1
  117. LIMIT 0 , 1) AS IsPraise,
  118. (SELECT
  119. COUNT(1)
  120. FROM
  121. tede_comment
  122. WHERE
  123. ArticleId = a.MiddleId and pid=0
  124. AND TypeValue = {noteTypeId}) AS CommentCount,
  125. (SELECT
  126. COUNT(1)
  127. FROM
  128. tede_analyze
  129. WHERE
  130. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  131. AND SourceId = a.MiddleId
  132. AND AnalyzeType = 4) AS RetransmissionCount,
  133. (SELECT
  134. COUNT(1)
  135. FROM
  136. tede_analyze
  137. WHERE
  138. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  139. AND SourceId = a.MiddleId
  140. AND AnalyzeType = 4
  141. LIMIT 0 , 1) AS IsRetransmission,
  142. (SELECT
  143. COUNT(1)
  144. FROM
  145. tede_analyze
  146. WHERE
  147. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  148. AND SourceId = a.MiddleId
  149. AND AnalyzeType = 4
  150. LIMIT 0 , 1) AS IsCollect,
  151. (SELECT
  152. COUNT(1)
  153. FROM
  154. tede_analyze
  155. WHERE
  156. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  157. AND SourceId = a.MiddleId
  158. AND AnalyzeType = 4) AS CollectCount,
  159. (SELECT
  160. COUNT(1)
  161. FROM
  162. tede_middle
  163. WHERE
  164. ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,b.MediaId,b.CatalogId,
  165. b.ChapterId,c.Name,c.AvatarUrl,c.Nick,d.Name FROM tede_middle a LEFT JOIN tede_note b ON a.MiddleId = b.Id left join tede_user c on c.Id=b.UserId left join tede_department d on d.Id=c.DepartmentId
  166. WHERE a.UserId = {request.VisitUserId} AND a.FolderType = 4 AND a.IsDelete = 0";
  167. if (request.FolderId == 0)
  168. sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
  169. else
  170. sql += $" and a.ParentId={request.FolderId}";
  171. if (!string.IsNullOrEmpty(request.SearchKey))
  172. 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}%'))";
  173. sql +=
  174. $@" OR a.id IN(SELECT MiddleId FROM tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType ={noteTypeId} AND a.UserId = {request.VisitUserId}";
  175. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  176. 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}%'))";
  177. else if (!string.IsNullOrEmpty(request.SearchKey))
  178. 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}%'))";
  179. else
  180. sql += $" and a.ParentId={request.FolderId}";
  181. sql +=
  182. $@" ORDER BY a.IsTop DESC,a.AttributeValue desc, a.LastModifiedDate DESC";
  183. if (request.Page > 0)
  184. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  185. }
  186. else
  187. {
  188. sql =
  189. $@"SELECT a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType={noteTypeId} and DataSourceId=a.MiddleSonId) as EnjoyUser,(SELECT
  190. COUNT(1)
  191. FROM
  192. tede_analyze
  193. WHERE
  194. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  195. AND SourceId = a.MiddleId
  196. AND AnalyzeType = 1) AS PraiseCount,
  197. (SELECT
  198. COUNT(1)
  199. FROM
  200. tede_analyze
  201. WHERE
  202. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  203. AND SourceId = a.MiddleId
  204. AND AnalyzeType = 1
  205. LIMIT 0 , 1) AS IsPraise,
  206. (SELECT
  207. COUNT(1)
  208. FROM
  209. tede_comment
  210. WHERE
  211. UserId = {request.UserId} AND ArticleId = a.MiddleId
  212. AND TypeValue = {noteTypeId}) AS CommentCount,
  213. (SELECT
  214. COUNT(1)
  215. FROM
  216. tede_analyze
  217. WHERE
  218. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  219. AND SourceId = a.MiddleId
  220. AND AnalyzeType = 4) AS RetransmissionCount,
  221. (SELECT
  222. COUNT(1)
  223. FROM
  224. tede_analyze
  225. WHERE
  226. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  227. AND SourceId = a.MiddleId
  228. AND AnalyzeType = 4
  229. LIMIT 0 , 1) AS IsRetransmission,
  230. (SELECT
  231. COUNT(1)
  232. FROM
  233. tede_analyze
  234. WHERE
  235. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  236. AND SourceId = a.MiddleId
  237. AND AnalyzeType = 4
  238. LIMIT 0 , 1) AS IsCollect,
  239. (SELECT
  240. COUNT(1)
  241. FROM
  242. tede_analyze
  243. WHERE
  244. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  245. AND SourceId = a.MiddleId
  246. AND AnalyzeType = 4) AS IsCollect,
  247. (SELECT
  248. COUNT(1)
  249. FROM
  250. tede_middle
  251. WHERE
  252. ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,b.MediaId,b.CatalogId,
  253. b.ChapterId,c.Name,c.AvatarUrl,d.Name FROM tede_middle a LEFT JOIN tede_note b ON a.MiddleId = b.Id left join tede_user c on c.Id=b.UserId left join tede_department d on d.Id=c.DepartmentId WHERE a.UserId = {request.UserId} AND a.FolderType = {noteTypeId} AND a.IsDelete = 0";
  254. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  255. 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}%'))";
  256. else if (!string.IsNullOrEmpty(request.SearchKey))
  257. sql += $@" and a.ParentId={request.FolderId} AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%')";
  258. else
  259. sql += $" and a.ParentId={request.FolderId}";
  260. sql +=
  261. $@" ORDER BY a.IsTop DESC,a.Sort DESC ,a.AttributeValue desc , a.LastModifiedDate DESC";
  262. if (request.Page > 0)
  263. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  264. }
  265. return sql;
  266. }
  267. /// <summary>
  268. /// 条数
  269. /// </summary>
  270. /// <param name="request"></param>
  271. /// <returns></returns>
  272. public string GetCollectionSqlCount(NoteSearchPageListRequest request)
  273. {
  274. string sql = "";
  275. if (request.VisitUserId > 0)
  276. {
  277. sql =
  278. $@"SELECT count(1) FROM tede_middle a LEFT JOIN tede_note b ON a.MiddleId = b.Id WHERE a.UserId = {request.VisitUserId} AND a.FolderType = 4 AND a.IsDelete = 0";
  279. if (request.FolderId == 0)
  280. sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
  281. else
  282. sql += $" and a.ParentId={request.FolderId}";
  283. if (!string.IsNullOrEmpty(request.SearchKey))
  284. 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}%'))";
  285. sql +=
  286. $@" OR a.id IN(SELECT MiddleId FROM tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType = 4 AND a.UserId = {request.VisitUserId}";
  287. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  288. 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}%'))";
  289. else if (!string.IsNullOrEmpty(request.SearchKey))
  290. 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}%'))";
  291. else
  292. sql += $" and a.ParentId={request.FolderId}";
  293. }
  294. else
  295. {
  296. sql =
  297. $@"SELECT count(1) FROM tede_middle a LEFT JOIN tede_note b ON a.MiddleId = b.Id left join tede_user c on c.Id=b.UserId left join tede_department d on d.Id=c.DepartmentId WHERE a.UserId = {request.UserId} AND a.FolderType = 4 AND a.IsDelete = 0";
  298. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  299. 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}%'))";
  300. else if (!string.IsNullOrEmpty(request.SearchKey))
  301. 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}%'))";
  302. else
  303. sql += $" and a.ParentId={request.FolderId}";
  304. }
  305. return sql;
  306. }
  307. public async Task<int> NotePageListCountAsync(string sql)
  308. {
  309. var pagedList = new PagedList<NotePageListRequest>();
  310. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  311. var database = new Database(databaseType, _connectionString);
  312. var connection = database.GetConnection();
  313. var result =
  314. await connection
  315. .ExecuteScalarAsync<int>(sql);
  316. return result;
  317. }
  318. /// <summary>
  319. /// 删除笔记
  320. /// </summary>
  321. /// <param name="id"></param>
  322. /// <returns></returns>
  323. public async Task<bool> DeleteAsync(int id) => await _repository.DeleteAsync(id);
  324. /// <summary>
  325. ///查询个人摘录
  326. /// </summary>
  327. /// <param name="request"></param>
  328. /// <returns></returns>
  329. public async Task<PagedList<NoteUserExtractResult>> GetAllByUserId(NoteUserRequest request)
  330. {
  331. var result = new PagedList<NoteUserExtractResult>();
  332. var sql = $@"SELECT
  333. a.Id,
  334. a.CreatedDate,
  335. a.Title,
  336. a.Content,
  337. a.UserId,
  338. a.ReadCount,
  339. a.HtmlContent,
  340. a.MediaId,
  341. a.CatalogId,
  342. a.ChapterId,
  343. (SELECT
  344. COUNT(1)
  345. FROM
  346. tede_analyze
  347. WHERE
  348. UserId = {request.UserId} AND TypeValue = 3
  349. AND SourceId = a.Id
  350. AND AnalyzeType = 1) AS PraiseCount,
  351. (SELECT
  352. COUNT(1)
  353. FROM
  354. tede_analyze
  355. WHERE
  356. UserId = {request.UserId} AND TypeValue = 3
  357. AND SourceId = a.Id
  358. AND AnalyzeType = 1
  359. LIMIT 0 , 1) AS IsPraise,
  360. (SELECT
  361. COUNT(1)
  362. FROM
  363. tede_comment
  364. WHERE
  365. ArticleId = a.Id AND pid = 0
  366. AND TypeValue = 3) AS CommentCount,
  367. (SELECT
  368. COUNT(1)
  369. FROM
  370. tede_analyze
  371. WHERE
  372. UserId = {request.UserId} AND TypeValue = 3
  373. AND SourceId = a.Id
  374. AND AnalyzeType = 4) AS RetransmissionCount,
  375. (SELECT
  376. COUNT(1)
  377. FROM
  378. tede_analyze
  379. WHERE
  380. UserId = {request.UserId} AND TypeValue = 3
  381. AND SourceId = a.Id
  382. AND AnalyzeType = 4
  383. LIMIT 0 , 1) AS IsRetransmission,
  384. b.Name,b.AvatarUrl
  385. FROM
  386. tede_note a
  387. INNER JOIN
  388. tede_user b ON a.UserId = b.Id
  389. WHERE
  390. UserId = {request.UserId} and a.MediaId='{request.MediaId}'
  391. ORDER BY CreatedDate DESC";
  392. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  393. var database = new Database(databaseType, _connectionString);
  394. var connection = database.GetConnection();
  395. result.Items =
  396. await connection
  397. .QueryAsync<NoteUserExtractResult>(sql);
  398. sql = $@"SELECT
  399. count(1)
  400. FROM
  401. tede_note a
  402. INNER JOIN
  403. tede_user b ON a.UserId = b.Id
  404. WHERE
  405. UserId = {request.UserId} and a.MediaId='{request.MediaId}' and a.CatalogId='{request.CatalogId}' and a.ChapterId='{request.ChapterId}'";
  406. result.Total =
  407. await connection.ExecuteScalarAsync<int>(sql);
  408. return result;
  409. }
  410. /// <summary>
  411. /// web页面查询话题
  412. /// </summary>
  413. /// <param name="request"></param>
  414. /// <returns></returns>
  415. public async Task<PagedList<NoteTopicResult>> GetTopicNoteAsync(NoteSearchPageListRequest request)
  416. {
  417. var noteTypeId= GxPress.EnumConst.AllTypeConst.Note.GetHashCode();
  418. var sql = $@"SELECT
  419. a.Id,
  420. a.Title,
  421. a.Content,
  422. a.HtmlContent,
  423. a.CreatedDate,
  424. b.Name,
  425. b.AvatarUrl,
  426. c.Id,
  427. c.FolderName,
  428. (SELECT
  429. COUNT(1)
  430. FROM
  431. tede_analyze
  432. WHERE
  433. TypeValue ={noteTypeId} AND SourceId = a.Id
  434. AND AnalyzeType = 1) AS PraiseCount,
  435. (SELECT
  436. COUNT(1)
  437. FROM
  438. tede_analyze
  439. WHERE
  440. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  441. AND SourceId = a.Id
  442. AND AnalyzeType = 1
  443. LIMIT 0 , 1) AS IsPraise,
  444. (SELECT
  445. COUNT(1)
  446. FROM
  447. tede_comment
  448. WHERE
  449. ArticleId = a.Id AND TypeValue = {noteTypeId}
  450. AND Pid = 0) AS CommentCount,
  451. (SELECT
  452. COUNT(1)
  453. FROM
  454. tede_analyze
  455. WHERE
  456. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  457. AND SourceId = a.Id
  458. AND AnalyzeType = 4) AS RetransmissionCount,
  459. (SELECT
  460. COUNT(1)
  461. FROM
  462. tede_analyze
  463. WHERE
  464. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  465. AND SourceId = a.Id
  466. AND AnalyzeType = 4
  467. LIMIT 0 , 1) AS IsRetransmission,
  468. (SELECT
  469. COUNT(1)
  470. FROM
  471. tede_analyze
  472. WHERE
  473. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  474. AND SourceId = a.Id
  475. AND AnalyzeType = 3
  476. LIMIT 0 , 1) AS IsCollect
  477. FROM
  478. tede_note a
  479. INNER JOIN
  480. tede_user b ON a.UserId = b.Id
  481. INNER JOIN
  482. tede_middle c ON c.Id = a.FolderId
  483. WHERE
  484. a.IsTopic = 1";
  485. if (!string.IsNullOrEmpty(request.SearchKey))
  486. {
  487. sql += $@" and (b.Name like '%11%' or a.Title like '%11%' or a.Content like '%11%')";
  488. }
  489. sql += $@" AND (a.FolderId IN (SELECT
  490. Id
  491. FROM
  492. tede_middle
  493. WHERE
  494. id IN (SELECT
  495. MiddleId
  496. FROM
  497. tede_folder_user
  498. WHERE
  499. UserId = {request.UserId})
  500. AND RoleId IN (3))
  501. OR a.FolderId IN (SELECT
  502. Id
  503. FROM
  504. tede_middle
  505. WHERE
  506. RoleId IN (1))) ";
  507. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  508. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  509. var database = new Database(databaseType, _connectionString);
  510. var connection = database.GetConnection();
  511. var result = new PagedList<NoteTopicResult>();
  512. result.Items =
  513. await connection
  514. .QueryAsync<NoteTopicResult, User, Entity.Middle.Middle, NoteTopicResult>(sql,
  515. (noteTopicResult, user, middle) =>
  516. {
  517. noteTopicResult.UserName = user == null ? "" : user.Name;
  518. noteTopicResult.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  519. noteTopicResult.FolderId = middle == null ? 0 : middle.Id;
  520. noteTopicResult.FolderName = middle == null ? "" : middle.FolderName;
  521. return noteTopicResult;
  522. }, splitOn: "Id,Name,Id");
  523. sql = $@"SELECT
  524. count(1)
  525. FROM
  526. tede_note a
  527. INNER JOIN
  528. tede_user b ON a.UserId = b.Id
  529. INNER JOIN
  530. tede_middle c ON c.Id = a.FolderId
  531. WHERE
  532. a.IsTopic = 1";
  533. if (!string.IsNullOrEmpty(request.SearchKey))
  534. {
  535. sql += $@" and (b.Name like '%11%' or a.Title like '%11%' or a.Content like '%11%')";
  536. }
  537. sql += $@" AND (a.FolderId IN (SELECT
  538. Id
  539. FROM
  540. tede_middle
  541. WHERE
  542. id IN (SELECT
  543. MiddleId
  544. FROM
  545. tede_folder_user
  546. WHERE
  547. UserId = {request.UserId})
  548. AND RoleId IN (3))
  549. OR a.FolderId IN (SELECT
  550. Id
  551. FROM
  552. tede_middle
  553. WHERE
  554. RoleId IN (1))) ";
  555. result.Total =
  556. await connection.ExecuteScalarAsync<int>(sql);
  557. return result;
  558. }
  559. }
  560. }