NoteRepository.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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="noteId"></param>
  61. /// <returns></returns>
  62. public async Task<bool> SetIsTopAsync(int noteId)
  63. {
  64. var note = await _repository.GetAsync(noteId);
  65. note.IsTop = !note.IsTop;
  66. return await _repository.UpdateAsync(note);
  67. }
  68. /// <summary>
  69. /// 获取笔记分页
  70. /// </summary>
  71. /// <param name="request"></param>
  72. /// <returns></returns>
  73. public async Task<PagedList<NotePageListRequest>> NotePageListAsync(NoteSearchPageListRequest request)
  74. {
  75. var pagedList = new PagedList<NotePageListRequest>();
  76. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  77. var database = new Database(databaseType, _connectionString);
  78. var connection = database.GetConnection();
  79. var result =
  80. await connection
  81. .QueryAsync<NotePageListRequest, Entity.Note.Note, User, Department, NotePageListRequest>(GetCollectionSql(request),
  82. (notePageListRequest, note, user, department) =>
  83. {
  84. notePageListRequest.Title = note == null ? "" : note.Title;
  85. notePageListRequest.Content = note == null ? "" : note.Content;
  86. notePageListRequest.MediaId = note == null ? 0 : note.MediaId;
  87. notePageListRequest.CatalogId = note == null ? "" : note.CatalogId;
  88. notePageListRequest.ChapterId = note == null ? "" : note.ChapterId;
  89. notePageListRequest.Name = user == null ? "" : user.Name;
  90. notePageListRequest.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  91. notePageListRequest.DepartmentName = department == null ? "" : department.Name;
  92. notePageListRequest.ReadCount = note == null ? 0 : note.ReadCount;
  93. notePageListRequest.NickName = user == null ? "" : user.Nick;
  94. return notePageListRequest;
  95. }, splitOn: "Title,Name,Name");
  96. pagedList.Items = result;
  97. pagedList.Total = await NotePageListCountAsync(GetCollectionSqlCount(request));
  98. return pagedList;
  99. }
  100. /// <summary>
  101. /// 集合
  102. /// </summary>
  103. /// <param name="request"></param>
  104. /// <returns></returns>
  105. public string GetCollectionSql(NoteSearchPageListRequest request)
  106. {
  107. string sql = "";
  108. var noteTypeId = GxPress.EnumConst.AllTypeConst.Note.GetHashCode();
  109. if (request.VisitUserId > 0)
  110. {
  111. sql =
  112. $@"SELECT a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType={noteTypeId} and DataSourceId=a.MiddleSonId) as EnjoyUser,(SELECT
  113. COUNT(1)
  114. FROM
  115. tede_analyze
  116. WHERE
  117. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  118. AND SourceId = a.MiddleId
  119. AND AnalyzeType = 1) AS PraiseCount,
  120. (SELECT
  121. COUNT(1)
  122. FROM
  123. tede_analyze
  124. WHERE
  125. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  126. AND SourceId = a.MiddleId
  127. AND AnalyzeType = 1
  128. LIMIT 0 , 1) AS IsPraise,
  129. (SELECT
  130. COUNT(1)
  131. FROM
  132. tede_comment
  133. WHERE
  134. ArticleId = a.MiddleId and pid=0
  135. AND TypeValue = {noteTypeId}) AS CommentCount,
  136. (SELECT
  137. COUNT(1)
  138. FROM
  139. tede_analyze
  140. WHERE
  141. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  142. AND SourceId = a.MiddleId
  143. AND AnalyzeType = 4) AS RetransmissionCount,
  144. (SELECT
  145. COUNT(1)
  146. FROM
  147. tede_analyze
  148. WHERE
  149. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  150. AND SourceId = a.MiddleId
  151. AND AnalyzeType = 4
  152. LIMIT 0 , 1) AS IsRetransmission,
  153. (SELECT
  154. COUNT(1)
  155. FROM
  156. tede_analyze
  157. WHERE
  158. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  159. AND SourceId = a.MiddleId
  160. AND AnalyzeType = 3
  161. LIMIT 0 , 1) AS IsCollect,
  162. (SELECT
  163. COUNT(1)
  164. FROM
  165. tede_analyze
  166. WHERE
  167. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  168. AND SourceId = a.MiddleId
  169. AND AnalyzeType = 3) AS CollectCount,
  170. (SELECT
  171. COUNT(1)
  172. FROM
  173. tede_middle
  174. WHERE
  175. ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,b.MediaId,b.CatalogId,
  176. 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
  177. WHERE a.UserId = {request.VisitUserId} AND a.FolderType = 4 AND a.IsDelete = 0";
  178. if (request.FolderId == 0)
  179. sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
  180. else
  181. sql += $" and a.ParentId={request.FolderId}";
  182. if (!string.IsNullOrEmpty(request.SearchKey))
  183. 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}%'))";
  184. sql +=
  185. $@" OR a.id IN(SELECT MiddleId FROM tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType ={noteTypeId} AND a.UserId = {request.VisitUserId}";
  186. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  187. 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}%'))";
  188. else if (!string.IsNullOrEmpty(request.SearchKey))
  189. 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}%'))";
  190. else
  191. sql += $" and a.ParentId={request.FolderId}";
  192. sql +=
  193. $@" ORDER BY a.AttributeValue desc,a.IsTop DESC, a.LastModifiedDate DESC";
  194. if (request.Page > 0)
  195. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  196. }
  197. else
  198. {
  199. sql =
  200. $@"SELECT a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType={noteTypeId} and DataSourceId=a.MiddleSonId) as EnjoyUser,(SELECT
  201. COUNT(1)
  202. FROM
  203. tede_analyze
  204. WHERE
  205. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  206. AND SourceId = a.MiddleId
  207. AND AnalyzeType = 1) AS PraiseCount,
  208. (SELECT
  209. COUNT(1)
  210. FROM
  211. tede_analyze
  212. WHERE
  213. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  214. AND SourceId = a.MiddleId
  215. AND AnalyzeType = 1
  216. LIMIT 0 , 1) AS IsPraise,
  217. (SELECT
  218. COUNT(1)
  219. FROM
  220. tede_comment
  221. WHERE
  222. UserId = {request.UserId} AND ArticleId = a.MiddleId
  223. AND TypeValue = {noteTypeId}) AS CommentCount,
  224. (SELECT
  225. COUNT(1)
  226. FROM
  227. tede_analyze
  228. WHERE
  229. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  230. AND SourceId = a.MiddleId
  231. AND AnalyzeType = 4) AS RetransmissionCount,
  232. (SELECT
  233. COUNT(1)
  234. FROM
  235. tede_analyze
  236. WHERE
  237. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  238. AND SourceId = a.MiddleId
  239. AND AnalyzeType = 4
  240. LIMIT 0 , 1) AS IsRetransmission,
  241. (SELECT
  242. COUNT(1)
  243. FROM
  244. tede_analyze
  245. WHERE
  246. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  247. AND SourceId = a.MiddleId
  248. AND AnalyzeType = 3
  249. LIMIT 0 , 1) AS IsCollect,
  250. (SELECT
  251. COUNT(1)
  252. FROM
  253. tede_analyze
  254. WHERE
  255. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  256. AND SourceId = a.MiddleId
  257. AND AnalyzeType = 3) AS IsCollect,
  258. (SELECT
  259. COUNT(1)
  260. FROM
  261. tede_middle
  262. WHERE
  263. ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,b.MediaId,b.CatalogId,
  264. 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";
  265. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  266. 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}%'))";
  267. else if (!string.IsNullOrEmpty(request.SearchKey))
  268. sql += $@" and a.ParentId={request.FolderId} AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%')";
  269. else
  270. sql += $" and a.ParentId={request.FolderId}";
  271. sql +=
  272. $@" ORDER BY a.AttributeValue desc,a.IsTop DESC,a.Sort DESC , a.LastModifiedDate DESC";
  273. if (request.Page > 0)
  274. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  275. }
  276. return sql;
  277. }
  278. /// <summary>
  279. /// 条数
  280. /// </summary>
  281. /// <param name="request"></param>
  282. /// <returns></returns>
  283. public string GetCollectionSqlCount(NoteSearchPageListRequest request)
  284. {
  285. string sql = "";
  286. if (request.VisitUserId > 0)
  287. {
  288. sql =
  289. $@"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";
  290. if (request.FolderId == 0)
  291. sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
  292. else
  293. sql += $" and a.ParentId={request.FolderId}";
  294. if (!string.IsNullOrEmpty(request.SearchKey))
  295. 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}%'))";
  296. sql +=
  297. $@" OR a.id IN(SELECT MiddleId FROM tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType = 4 AND a.UserId = {request.VisitUserId}";
  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. else
  306. {
  307. sql =
  308. $@"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";
  309. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  310. 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}%'))";
  311. else if (!string.IsNullOrEmpty(request.SearchKey))
  312. 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}%'))";
  313. else
  314. sql += $" and a.ParentId={request.FolderId}";
  315. }
  316. return sql;
  317. }
  318. public async Task<int> NotePageListCountAsync(string sql)
  319. {
  320. var pagedList = new PagedList<NotePageListRequest>();
  321. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  322. var database = new Database(databaseType, _connectionString);
  323. var connection = database.GetConnection();
  324. var result =
  325. await connection
  326. .ExecuteScalarAsync<int>(sql);
  327. return result;
  328. }
  329. /// <summary>
  330. /// 删除笔记
  331. /// </summary>
  332. /// <param name="id"></param>
  333. /// <returns></returns>
  334. public async Task<bool> DeleteAsync(int id) => await _repository.DeleteAsync(id);
  335. /// <summary>
  336. ///查询个人摘录
  337. /// </summary>
  338. /// <param name="request"></param>
  339. /// <returns></returns>
  340. public async Task<PagedList<NoteUserExtractResult>> GetAllByUserId(NoteUserRequest request)
  341. {
  342. var result = new PagedList<NoteUserExtractResult>();
  343. var sql = $@"SELECT
  344. a.Id,
  345. a.CreatedDate,
  346. a.Title,
  347. a.Content,
  348. a.UserId,
  349. a.ReadCount,
  350. a.HtmlContent,
  351. a.MediaId,
  352. a.CatalogId,
  353. a.ChapterId,
  354. (SELECT
  355. COUNT(1)
  356. FROM
  357. tede_analyze
  358. WHERE
  359. UserId = {request.UserId} AND TypeValue = 3
  360. AND SourceId = a.Id
  361. AND AnalyzeType = 1) AS PraiseCount,
  362. (SELECT
  363. COUNT(1)
  364. FROM
  365. tede_analyze
  366. WHERE
  367. UserId = {request.UserId} AND TypeValue = 3
  368. AND SourceId = a.Id
  369. AND AnalyzeType = 1
  370. LIMIT 0 , 1) AS IsPraise,
  371. (SELECT
  372. COUNT(1)
  373. FROM
  374. tede_comment
  375. WHERE
  376. ArticleId = a.Id AND pid = 0
  377. AND TypeValue = 3) AS CommentCount,
  378. (SELECT
  379. COUNT(1)
  380. FROM
  381. tede_analyze
  382. WHERE
  383. UserId = {request.UserId} AND TypeValue = 3
  384. AND SourceId = a.Id
  385. AND AnalyzeType = 4) AS RetransmissionCount,
  386. (SELECT
  387. COUNT(1)
  388. FROM
  389. tede_analyze
  390. WHERE
  391. UserId = {request.UserId} AND TypeValue = 3
  392. AND SourceId = a.Id
  393. AND AnalyzeType = 4
  394. LIMIT 0 , 1) AS IsRetransmission,
  395. b.Name,b.AvatarUrl
  396. FROM
  397. tede_note a
  398. INNER JOIN
  399. tede_user b ON a.UserId = b.Id
  400. WHERE
  401. UserId = {request.UserId} and a.MediaId='{request.MediaId}'
  402. ORDER BY CreatedDate DESC";
  403. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  404. var database = new Database(databaseType, _connectionString);
  405. var connection = database.GetConnection();
  406. result.Items =
  407. await connection
  408. .QueryAsync<NoteUserExtractResult>(sql);
  409. sql = $@"SELECT
  410. count(1)
  411. FROM
  412. tede_note a
  413. INNER JOIN
  414. tede_user b ON a.UserId = b.Id
  415. WHERE
  416. UserId = {request.UserId} and a.MediaId='{request.MediaId}' and a.CatalogId='{request.CatalogId}' and a.ChapterId='{request.ChapterId}'";
  417. result.Total =
  418. await connection.ExecuteScalarAsync<int>(sql);
  419. return result;
  420. }
  421. /// <summary>
  422. /// web页面查询话题
  423. /// </summary>
  424. /// <param name="request"></param>
  425. /// <returns></returns>
  426. public async Task<PagedList<NoteTopicResult>> GetTopicNoteAsync(NoteSearchPageListRequest request)
  427. {
  428. var noteTypeId = GxPress.EnumConst.AllTypeConst.Note.GetHashCode();
  429. var sql = $@"SELECT
  430. a.Id,
  431. a.Title,
  432. a.Content,
  433. a.HtmlContent,
  434. a.CreatedDate,
  435. b.Id as UserId,
  436. b.Name,
  437. b.AvatarUrl,
  438. c.Id,
  439. c.FolderName,
  440. (SELECT
  441. COUNT(1)
  442. FROM
  443. tede_analyze
  444. WHERE
  445. TypeValue ={noteTypeId} AND SourceId = a.Id
  446. AND AnalyzeType = 1) AS PraiseCount,
  447. (SELECT
  448. COUNT(1)
  449. FROM
  450. tede_analyze
  451. WHERE
  452. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  453. AND SourceId = a.Id
  454. AND AnalyzeType = 1
  455. LIMIT 0 , 1) AS IsPraise,
  456. (SELECT
  457. COUNT(1)
  458. FROM
  459. tede_comment
  460. WHERE
  461. ArticleId = a.Id AND TypeValue = {noteTypeId}
  462. AND Pid = 0) AS CommentCount,
  463. (SELECT
  464. COUNT(1)
  465. FROM
  466. tede_analyze
  467. WHERE
  468. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  469. AND SourceId = a.Id
  470. AND AnalyzeType = 4) AS RetransmissionCount,
  471. (SELECT
  472. COUNT(1)
  473. FROM
  474. tede_analyze
  475. WHERE
  476. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  477. AND SourceId = a.Id
  478. AND AnalyzeType = 4
  479. LIMIT 0 , 1) AS IsRetransmission,
  480. (SELECT
  481. COUNT(1)
  482. FROM
  483. tede_analyze
  484. WHERE
  485. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  486. AND SourceId = a.Id
  487. AND AnalyzeType = 3
  488. LIMIT 0 , 1) AS IsCollect
  489. FROM
  490. tede_note a
  491. INNER JOIN
  492. tede_user b ON a.UserId = b.Id
  493. INNER JOIN
  494. tede_middle c ON c.Id = a.FolderId
  495. WHERE
  496. a.IsTopic = 1";
  497. if (!string.IsNullOrEmpty(request.SearchKey))
  498. {
  499. sql += $@" and (b.Name like '%11%' or a.Title like '%11%' or a.Content like '%11%')";
  500. }
  501. sql += $@" AND (a.FolderId IN (SELECT
  502. Id
  503. FROM
  504. tede_middle
  505. WHERE
  506. id IN (SELECT
  507. MiddleId
  508. FROM
  509. tede_folder_user
  510. WHERE
  511. UserId = {request.UserId})
  512. AND RoleId IN (3))
  513. OR a.FolderId IN (SELECT
  514. Id
  515. FROM
  516. tede_middle
  517. WHERE
  518. RoleId IN (1))) ";
  519. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  520. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  521. var database = new Database(databaseType, _connectionString);
  522. var connection = database.GetConnection();
  523. var result = new PagedList<NoteTopicResult>();
  524. result.Items =
  525. await connection
  526. .QueryAsync<NoteTopicResult, User, Entity.Middle.Middle, NoteTopicResult>(sql,
  527. (noteTopicResult, user, middle) =>
  528. {
  529. noteTopicResult.UserName = user == null ? "" : user.Name;
  530. noteTopicResult.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  531. noteTopicResult.FolderId = middle == null ? 0 : middle.Id;
  532. noteTopicResult.FolderName = middle == null ? "" : middle.FolderName;
  533. return noteTopicResult;
  534. }, splitOn: "Id,Name,Id");
  535. sql = $@"SELECT
  536. count(1)
  537. FROM
  538. tede_note a
  539. INNER JOIN
  540. tede_user b ON a.UserId = b.Id
  541. INNER JOIN
  542. tede_middle c ON c.Id = a.FolderId
  543. WHERE
  544. a.IsTopic = 1";
  545. if (!string.IsNullOrEmpty(request.SearchKey))
  546. {
  547. sql += $@" and (b.Name like '%11%' or a.Title like '%11%' or a.Content like '%11%')";
  548. }
  549. sql += $@" AND (a.FolderId IN (SELECT
  550. Id
  551. FROM
  552. tede_middle
  553. WHERE
  554. id IN (SELECT
  555. MiddleId
  556. FROM
  557. tede_folder_user
  558. WHERE
  559. UserId = {request.UserId})
  560. AND RoleId IN (3))
  561. OR a.FolderId IN (SELECT
  562. Id
  563. FROM
  564. tede_middle
  565. WHERE
  566. RoleId IN (1))) ";
  567. result.Total =
  568. await connection.ExecuteScalarAsync<int>(sql);
  569. return result;
  570. }
  571. /// <summary>
  572. /// 笔记没有文件夹分页
  573. /// </summary>
  574. /// <param name="request"></param>
  575. /// <returns></returns>
  576. public async Task<PagedList<NoteNotFolderPageResult>> GetNoteNotFolderPageResult(NoteSearchPageListRequest request)
  577. {
  578. var sqlValue = "";
  579. if (!string.IsNullOrEmpty(request.SearchKey))
  580. {
  581. sqlValue += $" and (a.Title like '%{request.SearchKey}%' or a.Content like '%{request.SearchKey}%')";
  582. }
  583. var noteConstValue = GxPress.EnumConst.AllTypeConst.Note.GetHashCode();
  584. var sql = $@"SELECT
  585. a.Id,
  586. a.Title,
  587. a.Content,
  588. a.UserId,
  589. a.CreatedDate,
  590. a.MediaId,
  591. a.ReadCount,
  592. a.IsTop,
  593. (SELECT
  594. FolderName
  595. FROM
  596. tede_middle
  597. WHERE
  598. id = (SELECT
  599. ParentId
  600. FROM
  601. tede_middle
  602. WHERE
  603. MiddleId = a.Id AND FolderType = {noteConstValue})) AS FolderName,
  604. (SELECT
  605. Id
  606. FROM
  607. tede_middle
  608. WHERE
  609. id = (SELECT
  610. ParentId
  611. FROM
  612. tede_middle
  613. WHERE
  614. MiddleId = a.Id AND FolderType = {noteConstValue})) AS FolderId,
  615. (SELECT
  616. COUNT(1)
  617. FROM
  618. tede_analyze
  619. WHERE
  620. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  621. AND SourceId = a.Id
  622. AND AnalyzeType = 1) AS PraiseCount,
  623. (SELECT
  624. COUNT(1)
  625. FROM
  626. tede_analyze
  627. WHERE
  628. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  629. AND SourceId = a.Id
  630. AND AnalyzeType = 1
  631. LIMIT 0 , 1) AS IsPraise,
  632. (SELECT
  633. COUNT(1)
  634. FROM
  635. tede_comment
  636. WHERE
  637. UserId = {request.UserId} AND ArticleId = a.Id
  638. AND TypeValue = {noteConstValue}) AS CommentCount,
  639. (SELECT
  640. COUNT(1)
  641. FROM
  642. tede_analyze
  643. WHERE
  644. UserId = {request.UserId} AND TypeValue ={noteConstValue}
  645. AND SourceId = a.Id
  646. AND AnalyzeType = {noteConstValue}) AS RetransmissionCount,
  647. (SELECT
  648. COUNT(1)
  649. FROM
  650. tede_analyze
  651. WHERE
  652. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  653. AND SourceId = a.Id
  654. AND AnalyzeType = 4
  655. LIMIT 0 , 1) AS IsRetransmission,
  656. (SELECT
  657. COUNT(1)
  658. FROM
  659. tede_analyze
  660. WHERE
  661. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  662. AND SourceId = a.Id
  663. AND AnalyzeType = 3
  664. LIMIT 0 , 1) AS IsCollect,
  665. (SELECT
  666. COUNT(1)
  667. FROM
  668. tede_analyze
  669. WHERE
  670. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  671. AND SourceId = a.Id
  672. AND AnalyzeType = 3) AS IsCollect,
  673. b.Name,
  674. b.AvatarUrl,c.Id,c.MiddleId
  675. FROM
  676. tede_note a
  677. INNER JOIN
  678. tede_user b ON a.UserId = b.Id inner join tede_middle c on c.MiddleId=a.Id
  679. WHERE
  680. a.IsDelete = 0 AND a.UserId = {request.UserId} and c.FolderType={noteConstValue} {sqlValue}
  681. ORDER BY a.IsTop desc ,a.CreatedDate DESC
  682. limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  683. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  684. var database = new Database(databaseType, _connectionString);
  685. var connection = database.GetConnection();
  686. var result = new PagedList<NoteNotFolderPageResult>();
  687. result.Items =
  688. await connection
  689. .QueryAsync<NoteNotFolderPageResult, User, Entity.Middle.Middle, NoteNotFolderPageResult>(sql,
  690. (result, user, middle) =>
  691. {
  692. result.MiddleId = middle.MiddleId;
  693. result.Id = middle.Id;
  694. result.Name = user == null ? "" : user.Name;
  695. result.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  696. return result;
  697. }, splitOn: "Id,Id,Name,Id");
  698. sql = $@"SELECT count(1)
  699. FROM
  700. tede_note a
  701. INNER JOIN
  702. tede_user b ON a.UserId = b.Id inner join tede_middle c on c.MiddleId=a.Id
  703. WHERE
  704. a.IsDelete = 0 AND a.UserId = {request.UserId} and c.FolderType={noteConstValue} {sqlValue}";
  705. result.Total = await connection.ExecuteScalarAsync<int>(sql);
  706. return result;
  707. }
  708. /// <summary>
  709. /// 获取用户最近使用的文件夹
  710. /// </summary>
  711. /// <param name="userId"></param>
  712. /// <returns></returns>
  713. public async Task<IEnumerable<int>> GetLatelyFolderIdAsync(int userId)
  714. {
  715. var query = Q.NewQuery();
  716. query.Select(nameof(Entity.Note.Note.FolderId));
  717. query.Where(nameof(Entity.Note.Note.UserId), userId);
  718. query.Where(nameof(Entity.Note.Note.IsTop), true);
  719. query.Where(nameof(Entity.Note.Note.FolderId), ">", 0);
  720. query.OrderByDesc(nameof(Entity.Note.Note.CreatedDate));
  721. return await _repository.GetAllAsync<int>(query);
  722. }
  723. }
  724. }