NoteRepository.cs 31 KB

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