NoteRepository.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 = 3
  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 = 3) 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 = 3
  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 = 3) 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.Id as UserId,
  425. b.Name,
  426. b.AvatarUrl,
  427. c.Id,
  428. c.FolderName,
  429. (SELECT
  430. COUNT(1)
  431. FROM
  432. tede_analyze
  433. WHERE
  434. TypeValue ={noteTypeId} AND SourceId = a.Id
  435. AND AnalyzeType = 1) AS PraiseCount,
  436. (SELECT
  437. COUNT(1)
  438. FROM
  439. tede_analyze
  440. WHERE
  441. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  442. AND SourceId = a.Id
  443. AND AnalyzeType = 1
  444. LIMIT 0 , 1) AS IsPraise,
  445. (SELECT
  446. COUNT(1)
  447. FROM
  448. tede_comment
  449. WHERE
  450. ArticleId = a.Id AND TypeValue = {noteTypeId}
  451. AND Pid = 0) AS CommentCount,
  452. (SELECT
  453. COUNT(1)
  454. FROM
  455. tede_analyze
  456. WHERE
  457. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  458. AND SourceId = a.Id
  459. AND AnalyzeType = 4) AS RetransmissionCount,
  460. (SELECT
  461. COUNT(1)
  462. FROM
  463. tede_analyze
  464. WHERE
  465. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  466. AND SourceId = a.Id
  467. AND AnalyzeType = 4
  468. LIMIT 0 , 1) AS IsRetransmission,
  469. (SELECT
  470. COUNT(1)
  471. FROM
  472. tede_analyze
  473. WHERE
  474. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  475. AND SourceId = a.Id
  476. AND AnalyzeType = 3
  477. LIMIT 0 , 1) AS IsCollect
  478. FROM
  479. tede_note a
  480. INNER JOIN
  481. tede_user b ON a.UserId = b.Id
  482. INNER JOIN
  483. tede_middle c ON c.Id = a.FolderId
  484. WHERE
  485. a.IsTopic = 1";
  486. if (!string.IsNullOrEmpty(request.SearchKey))
  487. {
  488. sql += $@" and (b.Name like '%11%' or a.Title like '%11%' or a.Content like '%11%')";
  489. }
  490. sql += $@" AND (a.FolderId IN (SELECT
  491. Id
  492. FROM
  493. tede_middle
  494. WHERE
  495. id IN (SELECT
  496. MiddleId
  497. FROM
  498. tede_folder_user
  499. WHERE
  500. UserId = {request.UserId})
  501. AND RoleId IN (3))
  502. OR a.FolderId IN (SELECT
  503. Id
  504. FROM
  505. tede_middle
  506. WHERE
  507. RoleId IN (1))) ";
  508. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  509. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  510. var database = new Database(databaseType, _connectionString);
  511. var connection = database.GetConnection();
  512. var result = new PagedList<NoteTopicResult>();
  513. result.Items =
  514. await connection
  515. .QueryAsync<NoteTopicResult, User, Entity.Middle.Middle, NoteTopicResult>(sql,
  516. (noteTopicResult, user, middle) =>
  517. {
  518. noteTopicResult.UserName = user == null ? "" : user.Name;
  519. noteTopicResult.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  520. noteTopicResult.FolderId = middle == null ? 0 : middle.Id;
  521. noteTopicResult.FolderName = middle == null ? "" : middle.FolderName;
  522. return noteTopicResult;
  523. }, splitOn: "Id,Name,Id");
  524. sql = $@"SELECT
  525. count(1)
  526. FROM
  527. tede_note a
  528. INNER JOIN
  529. tede_user b ON a.UserId = b.Id
  530. INNER JOIN
  531. tede_middle c ON c.Id = a.FolderId
  532. WHERE
  533. a.IsTopic = 1";
  534. if (!string.IsNullOrEmpty(request.SearchKey))
  535. {
  536. sql += $@" and (b.Name like '%11%' or a.Title like '%11%' or a.Content like '%11%')";
  537. }
  538. sql += $@" AND (a.FolderId IN (SELECT
  539. Id
  540. FROM
  541. tede_middle
  542. WHERE
  543. id IN (SELECT
  544. MiddleId
  545. FROM
  546. tede_folder_user
  547. WHERE
  548. UserId = {request.UserId})
  549. AND RoleId IN (3))
  550. OR a.FolderId IN (SELECT
  551. Id
  552. FROM
  553. tede_middle
  554. WHERE
  555. RoleId IN (1))) ";
  556. result.Total =
  557. await connection.ExecuteScalarAsync<int>(sql);
  558. return result;
  559. }
  560. /// <summary>
  561. /// 笔记没有文件夹分页
  562. /// </summary>
  563. /// <param name="request"></param>
  564. /// <returns></returns>
  565. public async Task<PagedList<NoteNotFolderPageResult>> GetNoteNotFolderPageResult(NoteSearchPageListRequest request)
  566. {
  567. var sqlValue = "";
  568. if (!string.IsNullOrEmpty(request.SearchKey))
  569. {
  570. sqlValue += $" and (a.Title like '%{request.SearchKey}%' or a.Content like '%{request.SearchKey}%')";
  571. }
  572. var noteConstValue = GxPress.EnumConst.AllTypeConst.Note.GetHashCode();
  573. var sql = $@"SELECT
  574. a.Id,
  575. a.Title,
  576. a.Content,
  577. a.UserId,
  578. a.CreatedDate,
  579. a.MediaId,
  580. (SELECT
  581. COUNT(1)
  582. FROM
  583. tede_analyze
  584. WHERE
  585. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  586. AND SourceId = a.Id
  587. AND AnalyzeType = 1) AS PraiseCount,
  588. (SELECT
  589. COUNT(1)
  590. FROM
  591. tede_analyze
  592. WHERE
  593. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  594. AND SourceId = a.Id
  595. AND AnalyzeType = 1
  596. LIMIT 0 , 1) AS IsPraise,
  597. (SELECT
  598. COUNT(1)
  599. FROM
  600. tede_comment
  601. WHERE
  602. UserId = {request.UserId} AND ArticleId = a.Id
  603. AND TypeValue = {noteConstValue}) AS CommentCount,
  604. (SELECT
  605. COUNT(1)
  606. FROM
  607. tede_analyze
  608. WHERE
  609. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  610. AND SourceId = a.Id
  611. AND AnalyzeType = 4) AS RetransmissionCount,
  612. (SELECT
  613. COUNT(1)
  614. FROM
  615. tede_analyze
  616. WHERE
  617. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  618. AND SourceId = a.Id
  619. AND AnalyzeType = 4
  620. LIMIT 0 , 1) AS IsRetransmission,
  621. (SELECT
  622. COUNT(1)
  623. FROM
  624. tede_analyze
  625. WHERE
  626. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  627. AND SourceId = a.Id
  628. AND AnalyzeType = 3
  629. LIMIT 0 , 1) AS IsCollect,
  630. (SELECT
  631. COUNT(1)
  632. FROM
  633. tede_analyze
  634. WHERE
  635. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  636. AND SourceId = a.Id
  637. AND AnalyzeType = 3) AS IsCollect
  638. ,b.Name,b.AvatarUrl
  639. FROM
  640. tede_note a inner join tede_user b on a.UserId=b.Id
  641. WHERE
  642. a.UserId = {request.UserId} {sqlValue}
  643. ORDER BY a.CreatedDate DESC limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  644. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  645. var database = new Database(databaseType, _connectionString);
  646. var connection = database.GetConnection();
  647. var result = new PagedList<NoteNotFolderPageResult>();
  648. result.Items =
  649. await connection
  650. .QueryAsync<NoteNotFolderPageResult, User, NoteNotFolderPageResult>(sql,
  651. (result, user) =>
  652. {
  653. result.UserName = user == null ? "" : user.Name;
  654. result.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  655. return result;
  656. }, splitOn: "Id,Name");
  657. sql = $@"SELECT count(1)
  658. FROM
  659. tede_note a inner join tede_user b on a.UserId=b.Id
  660. WHERE
  661. a.UserId = {request.UserId} {sqlValue}";
  662. result.Total = await connection.ExecuteScalarAsync<int>(sql);
  663. return result;
  664. }
  665. }
  666. }