NoteRepository.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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 IMapper _mapper;
  23. private readonly string _connectionString;
  24. private readonly string _databaseTypestr;
  25. public NoteRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  26. {
  27. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  28. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  29. var databaseType =
  30. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  31. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  32. _repository = new Repository<Entity.Note.Note>(database);
  33. _mapper = mapper;
  34. }
  35. public IDatabase Database => _repository.Database;
  36. public string TableName => _repository.TableName;
  37. public List<TableColumn> TableColumns => _repository.TableColumns;
  38. public async Task<Entity.Note.Note> GetAsync(int id)
  39. {
  40. return await _repository.GetAsync(id);
  41. }
  42. public async Task<bool> DeleteAsync(SqlKata.Query query)
  43. {
  44. return await _repository.DeleteAsync(query) > 0;
  45. }
  46. public async Task<int> InsertAsync(Entity.Note.Note note)
  47. {
  48. return await _repository.InsertAsync(note);
  49. }
  50. public async Task<bool> UpdateAsync(Entity.Note.Note note)
  51. {
  52. return await _repository.UpdateAsync(note);
  53. }
  54. public async Task<bool> UpdateAsync(SqlKata.Query query)
  55. {
  56. return await _repository.UpdateAsync(query) > 0;
  57. }
  58. /// <summary>
  59. /// 设置置顶
  60. /// </summary>
  61. /// <param name="noteId"></param>
  62. /// <returns></returns>
  63. public async Task<bool> SetIsTopAsync(int noteId)
  64. {
  65. var note = await _repository.GetAsync(noteId);
  66. note.IsTop = !note.IsTop;
  67. return await _repository.UpdateAsync(note);
  68. }
  69. /// <summary>
  70. /// 获取笔记分页
  71. /// </summary>
  72. /// <param name="request"></param>
  73. /// <returns></returns>
  74. public async Task<PagedList<NotePageListRequest>> NotePageListAsync(NoteSearchPageListRequest request)
  75. {
  76. var pagedList = new PagedList<NotePageListRequest>();
  77. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  78. var database = new Database(databaseType, _connectionString);
  79. var connection = database.GetConnection();
  80. var result =
  81. await connection
  82. .QueryAsync<NotePageListRequest>(GetCollectionSql(request));
  83. foreach (var item in result)
  84. {
  85. if (string.IsNullOrEmpty(item.FolderName))
  86. {
  87. item.FolderName = item.FolderParentName;
  88. item.FolderId = item.ParentId;
  89. }
  90. else
  91. item.FolderId = item.Id;
  92. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  93. }
  94. pagedList.Items = result;
  95. pagedList.Total = await NotePageListCountAsync(GetCollectionSqlCount(request));
  96. return pagedList;
  97. }
  98. /// <summary>
  99. /// 获取笔记/话题草稿分页
  100. /// </summary>
  101. /// <param name="request"></param>
  102. /// <returns></returns>
  103. public async Task<PagedList<NoteNotFolderPageResult>> NoteTopicDraftPageListAsync(NoteSearchPageListRequest request)
  104. {
  105. request.TypeValue = request.TypeValue == 1 ? 1 : 0;
  106. var pagedList = new PagedList<NoteNotFolderPageResult>();
  107. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  108. var database = new Database(databaseType, _connectionString);
  109. var connection = database.GetConnection();
  110. var sqlStr = string.Empty;
  111. if (!string.IsNullOrEmpty(request.SearchKey))
  112. sqlStr = $@"AND (a.Title LIKE '%{request.SearchKey}%' OR a.Content LIKE '%{request.SearchKey}%')";
  113. var sql = $@"SELECT
  114. a.*, b.Name, b.AvatarUrl,(SELECT
  115. GROUP_CONCAT(a.Name
  116. SEPARATOR '、')
  117. FROM
  118. tede_department a
  119. INNER JOIN
  120. tede_department_user b ON a.Id = b.DepartmentId
  121. WHERE
  122. b.UserId = {request.UserId}) as DepartmentName,
  123. (SELECT
  124. FolderName
  125. FROM
  126. tede_middle
  127. WHERE
  128. id = a.FolderId) AS FolderName
  129. FROM
  130. tede_note a
  131. INNER JOIN
  132. tede_user b ON a.UserId = b.Id
  133. WHERE
  134. a.UserId ={request.UserId} AND a.IsDraft = 1 and a.IsTopic={request.TypeValue}
  135. {sqlStr} order by a.LastModifiedDate desc limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  136. var result =
  137. await connection
  138. .QueryAsync<NoteNotFolderPageResult>(sql);
  139. foreach (var item in result)
  140. {
  141. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  142. }
  143. pagedList.Items = result;
  144. sql = $@"SELECT
  145. count(1)
  146. FROM
  147. tede_note a
  148. INNER JOIN
  149. tede_user b ON a.UserId = b.Id
  150. WHERE
  151. a.UserId ={request.UserId} AND a.IsDraft = 1 and a.IsTopic={ request.TypeValue}
  152. {sqlStr}";
  153. pagedList.Total = await NotePageListCountAsync(sql);
  154. return pagedList;
  155. }
  156. /// <summary>
  157. /// 集合
  158. /// </summary>
  159. /// <param name="request"></param>
  160. /// <returns></returns>
  161. public string GetCollectionSql(NoteSearchPageListRequest request)
  162. {
  163. string sql = "";
  164. var noteTypeId = request.TypeValue == 0 ? AllTypeConst.Note.GetHashCode() : AllTypeConst.TopicNote.GetHashCode();
  165. if (request.VisitUserId > 0)
  166. {
  167. var sqlStr = $"a.Id";
  168. if (request.FolderId > 0)
  169. sqlStr = "a.ParentId";
  170. sql =
  171. $@"SELECT a.Id,a.MiddleId,a.RoleId,a.ParentId,a.AttributeValue,a.UserId,
  172. a.CreatedDate,a.LastModifiedDate,a.IsTop,a.FolderName,a.FolderType,
  173. (SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType={noteTypeId}
  174. and DataSourceId=a.MiddleSonId) as EnjoyUser,
  175. (SELECT
  176. COUNT(1)
  177. FROM
  178. tede_analyze
  179. WHERE
  180. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  181. AND SourceId = a.MiddleId
  182. AND AnalyzeType = 1) AS PraiseCount,
  183. (SELECT
  184. COUNT(1)
  185. FROM
  186. tede_analyze
  187. WHERE
  188. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  189. AND SourceId = a.MiddleId
  190. AND AnalyzeType = 1
  191. LIMIT 0 , 1) AS IsPraise,
  192. (SELECT
  193. COUNT(1)
  194. FROM
  195. tede_comment
  196. WHERE
  197. UserId = {request.UserId} AND ArticleId = a.MiddleId
  198. AND TypeValue = {noteTypeId}) AS CommentCount,
  199. (SELECT
  200. COUNT(1)
  201. FROM
  202. tede_analyze
  203. WHERE
  204. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  205. AND SourceId = a.MiddleId
  206. AND AnalyzeType = 4) AS RetransmissionCount,
  207. (SELECT
  208. COUNT(1)
  209. FROM
  210. tede_analyze
  211. WHERE
  212. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  213. AND SourceId = a.MiddleId
  214. AND AnalyzeType = 4
  215. LIMIT 0 , 1) AS IsRetransmission,
  216. (SELECT
  217. COUNT(1)
  218. FROM
  219. tede_analyze
  220. WHERE
  221. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  222. AND SourceId = a.MiddleId
  223. AND AnalyzeType = 3
  224. LIMIT 0 , 1) AS IsCollect,
  225. (SELECT
  226. COUNT(1)
  227. FROM
  228. tede_analyze
  229. WHERE
  230. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  231. AND SourceId = a.MiddleId
  232. AND AnalyzeType = 3) AS IsCollect,
  233. (SELECT
  234. COUNT(1)
  235. FROM
  236. tede_middle
  237. WHERE
  238. ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,b.MediaId,b.CatalogId,
  239. b.ChapterId,c.Name,c.AvatarUrl,d.Name AS DepartmentName,
  240. (SELECT
  241. FolderName
  242. FROM
  243. tede_middle
  244. WHERE
  245. id =a.ParentId) AS FolderParentName,
  246. (SELECT
  247. Id
  248. FROM
  249. tede_middle
  250. WHERE
  251. id =a.ParentId ) AS FolderId
  252. FROM tede_middle a LEFT JOIN tede_note b ON a.MiddleId = b.Id
  253. left join tede_user c on c.Id=b.UserId
  254. left join tede_department d on d.Id=c.DepartmentId
  255. WHERE
  256. a.FolderType = {noteTypeId} AND a.userId = {request.VisitUserId}
  257. AND a.IsDelete = 0
  258. AND (a.UserId IN (SELECT
  259. Id
  260. FROM
  261. tede_user
  262. WHERE
  263. 1 = a.RoleId)
  264. OR a.UserId IN (SELECT
  265. Id
  266. FROM
  267. tede_user
  268. WHERE
  269. 0 = a.RoleId)
  270. OR {sqlStr} IN (SELECT
  271. MiddleId
  272. FROM
  273. tede_folder_user
  274. WHERE
  275. UserId = {request.UserId} AND MiddleId = {sqlStr}))";
  276. if (request.TypeValue == 0)
  277. sql += "and (a.RoleId=2 or a.AttributeValue<>2)";
  278. else
  279. sql += "and (a.RoleId in(1,3) or a.AttributeValue<>2)";
  280. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  281. sql += $@" AND (b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.TextContent LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
  282. else if (!string.IsNullOrEmpty(request.SearchKey))
  283. sql += $@" and a.ParentId={request.FolderId} AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.TextContent LIKE '%{request.SearchKey}%')";
  284. else
  285. sql += $" and a.ParentId={request.FolderId}";
  286. sql +=
  287. $@" ORDER BY a.IsSystemDefault desc,a.AttributeValue desc,a.IsTop DESC, a.LastModifiedDate DESC";
  288. if (request.Page > 0)
  289. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  290. }
  291. else
  292. {
  293. sql =
  294. $@"SELECT a.Id,a.MiddleId,a.RoleId,a.ParentId,a.AttributeValue,a.UserId,
  295. a.CreatedDate,a.LastModifiedDate,a.IsTop,a.FolderName,a.FolderType,
  296. (SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType={noteTypeId}
  297. and DataSourceId=a.MiddleSonId) as EnjoyUser,
  298. (SELECT
  299. COUNT(1)
  300. FROM
  301. tede_analyze
  302. WHERE
  303. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  304. AND SourceId = a.MiddleId
  305. AND AnalyzeType = 1) AS PraiseCount,
  306. (SELECT
  307. COUNT(1)
  308. FROM
  309. tede_analyze
  310. WHERE
  311. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  312. AND SourceId = a.MiddleId
  313. AND AnalyzeType = 1
  314. LIMIT 0 , 1) AS IsPraise,
  315. (SELECT
  316. COUNT(1)
  317. FROM
  318. tede_comment
  319. WHERE
  320. UserId = {request.UserId} AND ArticleId = a.MiddleId
  321. AND TypeValue = {noteTypeId}) AS CommentCount,
  322. (SELECT
  323. COUNT(1)
  324. FROM
  325. tede_analyze
  326. WHERE
  327. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  328. AND SourceId = a.MiddleId
  329. AND AnalyzeType = 4) AS RetransmissionCount,
  330. (SELECT
  331. COUNT(1)
  332. FROM
  333. tede_analyze
  334. WHERE
  335. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  336. AND SourceId = a.MiddleId
  337. AND AnalyzeType = 4
  338. LIMIT 0 , 1) AS IsRetransmission,
  339. (SELECT
  340. COUNT(1)
  341. FROM
  342. tede_analyze
  343. WHERE
  344. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  345. AND SourceId = a.MiddleId
  346. AND AnalyzeType = 3
  347. LIMIT 0 , 1) AS IsCollect,
  348. (SELECT
  349. COUNT(1)
  350. FROM
  351. tede_analyze
  352. WHERE
  353. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  354. AND SourceId = a.MiddleId
  355. AND AnalyzeType = 3) AS IsCollect,
  356. (SELECT
  357. COUNT(1)
  358. FROM
  359. tede_middle
  360. WHERE
  361. ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,b.MediaId,b.CatalogId,
  362. b.ChapterId,c.Name,c.AvatarUrl,d.Name AS DepartmentName,
  363. (SELECT
  364. FolderName
  365. FROM
  366. tede_middle
  367. WHERE
  368. id =a.ParentId) AS FolderParentName,
  369. (SELECT
  370. Id
  371. FROM
  372. tede_middle
  373. WHERE
  374. id =a.ParentId ) AS FolderId
  375. FROM tede_middle a LEFT JOIN tede_note b ON a.MiddleId = b.Id
  376. left join tede_user c on c.Id=b.UserId
  377. left join tede_department d on d.Id=c.DepartmentId
  378. WHERE a.FolderType = {noteTypeId} and a.UserId = {request.UserId} AND a.IsDelete = 0 ";
  379. if (request.TypeValue == 0)
  380. sql += " AND (a.RoleId = 2 OR a.AttributeValue in( 1,2))";
  381. else
  382. sql += "and (a.RoleId in(1,3) or a.AttributeValue in( 1,2))";
  383. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  384. sql += $@" AND (b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.TextContent LIKE '%{request.SearchKey}%')";
  385. else if (!string.IsNullOrEmpty(request.SearchKey))
  386. sql += $@" and a.ParentId={request.FolderId} AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.TextContent LIKE '%{request.SearchKey}%')";
  387. else
  388. sql += $" and a.ParentId={request.FolderId}";
  389. sql +=
  390. $@" ORDER BY a.AttributeValue desc,a.IsTop DESC, a.LastModifiedDate DESC";
  391. if (request.Page > 0)
  392. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  393. }
  394. return sql;
  395. }
  396. /// <summary>
  397. /// 条数
  398. /// </summary>
  399. /// <param name="request"></param>
  400. /// <returns></returns>
  401. public string GetCollectionSqlCount(NoteSearchPageListRequest request)
  402. {
  403. var noteTypeId = request.TypeValue == 0 ? AllTypeConst.Note.GetHashCode() : AllTypeConst.TopicNote.GetHashCode();
  404. string sql = "";
  405. if (request.VisitUserId > 0)
  406. {
  407. var sqlStr = $"a.Id";
  408. if (request.FolderId > 0)
  409. sqlStr = "a.ParentId";
  410. sql =
  411. $@"SELECT count(1)
  412. FROM tede_middle a LEFT JOIN tede_note b ON a.MiddleId = b.Id
  413. left join tede_user c on c.Id=b.UserId
  414. left join tede_department d on d.Id=c.DepartmentId
  415. WHERE a.FolderType = {noteTypeId} and
  416. a.userId = {request.VisitUserId}
  417. AND a.IsDelete = 0
  418. AND (a.UserId IN (SELECT
  419. Id
  420. FROM
  421. tede_user
  422. WHERE
  423. 1 = a.RoleId)
  424. OR a.UserId IN (SELECT
  425. Id
  426. FROM
  427. tede_user
  428. WHERE
  429. 0 = a.RoleId)
  430. OR {sqlStr} IN (SELECT
  431. MiddleId
  432. FROM
  433. tede_folder_user
  434. WHERE
  435. UserId = {request.UserId} AND MiddleId = {sqlStr}))";
  436. if (request.TypeValue == 0)
  437. sql += "and (a.RoleId in(2) or a.AttributeValue<>2)";
  438. else
  439. {
  440. sql += "and (a.RoleId in(1,3) or a.AttributeValue<>2)";
  441. }
  442. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  443. sql += $@" AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.TextContent LIKE '%{request.SearchKey}%')";
  444. else if (!string.IsNullOrEmpty(request.SearchKey))
  445. sql += $@" and a.ParentId={request.FolderId} AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.TextContent LIKE '%{request.SearchKey}%')";
  446. else
  447. sql += $" and a.ParentId={request.FolderId}";
  448. }
  449. else
  450. {
  451. sql =
  452. $@"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
  453. on d.Id=c.DepartmentId WHERE a.FolderType = {noteTypeId} and a.UserId = {request.UserId} AND a.IsDelete = 0";
  454. if (request.TypeValue == 0)
  455. sql += " and (a.RoleId in(2) or a.AttributeValue in(1,2))";
  456. else
  457. {
  458. sql += " and (a.RoleId in(1,3) or a.AttributeValue in(1,2))";
  459. }
  460. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  461. sql += $@" AND (b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.TextContent LIKE '%{request.SearchKey}%')";
  462. else if (!string.IsNullOrEmpty(request.SearchKey))
  463. sql += $@" and a.ParentId={request.FolderId} AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.TextContent LIKE '%{request.SearchKey}%')";
  464. else
  465. sql += $" and a.ParentId={request.FolderId}";
  466. }
  467. return sql;
  468. }
  469. public async Task<int> NotePageListCountAsync(string sql)
  470. {
  471. var pagedList = new PagedList<NotePageListRequest>();
  472. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  473. var database = new Database(databaseType, _connectionString);
  474. var connection = database.GetConnection();
  475. var result =
  476. await connection
  477. .ExecuteScalarAsync<int>(sql);
  478. return result;
  479. }
  480. /// <summary>
  481. /// 删除笔记
  482. /// </summary>
  483. /// <param name="id"></param>
  484. /// <returns></returns>
  485. public async Task<bool> DeleteAsync(int id) => await _repository.DeleteAsync(id);
  486. /// <summary>
  487. ///查询个人摘录
  488. /// </summary>
  489. /// <param name="request"></param>
  490. /// <returns></returns>
  491. public async Task<PagedList<NoteUserExtractResult>> GetAllByUserId(NoteUserRequest request)
  492. {
  493. var result = new PagedList<NoteUserExtractResult>();
  494. var sql = $@"SELECT
  495. a.Id,
  496. a.CreatedDate,
  497. a.Title,
  498. a.Content,
  499. a.UserId,
  500. a.ReadCount,
  501. a.HtmlContent,
  502. a.MediaId,
  503. a.CatalogId,
  504. a.ChapterId,
  505. (SELECT
  506. COUNT(1)
  507. FROM
  508. tede_analyze
  509. WHERE
  510. UserId = {request.UserId} AND TypeValue = 3
  511. AND SourceId = a.Id
  512. AND AnalyzeType = 1) AS PraiseCount,
  513. (SELECT
  514. COUNT(1)
  515. FROM
  516. tede_analyze
  517. WHERE
  518. UserId = {request.UserId} AND TypeValue = 3
  519. AND SourceId = a.Id
  520. AND AnalyzeType = 1
  521. LIMIT 0 , 1) AS IsPraise,
  522. (SELECT
  523. COUNT(1)
  524. FROM
  525. tede_comment
  526. WHERE
  527. ArticleId = a.Id AND pid = 0
  528. AND TypeValue = 3) AS CommentCount,
  529. (SELECT
  530. COUNT(1)
  531. FROM
  532. tede_analyze
  533. WHERE
  534. UserId = {request.UserId} AND TypeValue = 3
  535. AND SourceId = a.Id
  536. AND AnalyzeType = 4) AS RetransmissionCount,
  537. (SELECT
  538. COUNT(1)
  539. FROM
  540. tede_analyze
  541. WHERE
  542. UserId = {request.UserId} AND TypeValue = 3
  543. AND SourceId = a.Id
  544. AND AnalyzeType = 4
  545. LIMIT 0 , 1) AS IsRetransmission,
  546. b.Name,b.AvatarUrl
  547. FROM
  548. tede_note a
  549. INNER JOIN
  550. tede_user b ON a.UserId = b.Id
  551. WHERE
  552. UserId = {request.UserId} and a.MediaId='{request.MediaId}'
  553. ORDER BY CreatedDate DESC";
  554. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  555. var database = new Database(databaseType, _connectionString);
  556. var connection = database.GetConnection();
  557. result.Items =
  558. await connection
  559. .QueryAsync<NoteUserExtractResult>(sql);
  560. sql = $@"SELECT
  561. count(1)
  562. FROM
  563. tede_note a
  564. INNER JOIN
  565. tede_user b ON a.UserId = b.Id
  566. WHERE
  567. UserId = {request.UserId} and a.MediaId='{request.MediaId}' and a.CatalogId='{request.CatalogId}' and a.ChapterId='{request.ChapterId}'";
  568. result.Total =
  569. await connection.ExecuteScalarAsync<int>(sql);
  570. return result;
  571. }
  572. /// <summary>
  573. /// web页面查询话题
  574. /// </summary>
  575. /// <param name="request"></param>
  576. /// <returns></returns>
  577. public async Task<PagedList<NoteTopicResult>> GetTopicNoteAsync(NoteSearchPageListRequest request)
  578. {
  579. var noteTypeId = GxPress.EnumConst.AllTypeConst.Note.GetHashCode();
  580. var sql = $@"SELECT
  581. a.Id,
  582. a.Title,
  583. a.Content,
  584. a.HtmlContent,
  585. a.CreatedDate,
  586. b.Id as UserId,
  587. b.Name,
  588. b.AvatarUrl,
  589. c.Id,
  590. c.FolderName,
  591. (SELECT
  592. COUNT(1)
  593. FROM
  594. tede_analyze
  595. WHERE
  596. TypeValue ={noteTypeId} AND SourceId = a.Id
  597. AND AnalyzeType = 1) AS PraiseCount,
  598. (SELECT
  599. COUNT(1)
  600. FROM
  601. tede_analyze
  602. WHERE
  603. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  604. AND SourceId = a.Id
  605. AND AnalyzeType = 1
  606. LIMIT 0 , 1) AS IsPraise,
  607. (SELECT
  608. COUNT(1)
  609. FROM
  610. tede_comment
  611. WHERE
  612. ArticleId = a.Id AND TypeValue = {noteTypeId}
  613. AND Pid = 0) AS CommentCount,
  614. (SELECT
  615. COUNT(1)
  616. FROM
  617. tede_analyze
  618. WHERE
  619. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  620. AND SourceId = a.Id
  621. AND AnalyzeType = 4) AS RetransmissionCount,
  622. (SELECT
  623. COUNT(1)
  624. FROM
  625. tede_analyze
  626. WHERE
  627. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  628. AND SourceId = a.Id
  629. AND AnalyzeType = 4
  630. LIMIT 0 , 1) AS IsRetransmission,
  631. (SELECT
  632. COUNT(1)
  633. FROM
  634. tede_analyze
  635. WHERE
  636. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  637. AND SourceId = a.Id
  638. AND AnalyzeType = 3
  639. LIMIT 0 , 1) AS IsCollect
  640. FROM
  641. tede_note a
  642. INNER JOIN
  643. tede_user b ON a.UserId = b.Id
  644. INNER JOIN
  645. tede_middle c ON c.Id = a.FolderId
  646. WHERE
  647. a.IsTopic = 1";
  648. if (!string.IsNullOrEmpty(request.SearchKey))
  649. {
  650. sql += $@" and (b.Name like '%{request.SearchKey}%' or a.Title like '%11%' or a.TextContent like '%{request.SearchKey}%')";
  651. }
  652. sql += $@" AND (a.FolderId IN (SELECT
  653. Id
  654. FROM
  655. tede_middle
  656. WHERE
  657. id IN (SELECT
  658. MiddleId
  659. FROM
  660. tede_folder_user
  661. WHERE
  662. UserId = {request.UserId})
  663. AND RoleId IN (3))
  664. OR a.FolderId IN (SELECT
  665. Id
  666. FROM
  667. tede_middle
  668. WHERE
  669. RoleId IN (1))) ";
  670. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  671. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  672. var database = new Database(databaseType, _connectionString);
  673. var connection = database.GetConnection();
  674. var result = new PagedList<NoteTopicResult>();
  675. result.Items =
  676. await connection
  677. .QueryAsync<NoteTopicResult, User, Entity.Middle.Middle, NoteTopicResult>(sql,
  678. (noteTopicResult, user, middle) =>
  679. {
  680. noteTopicResult.UserName = user == null ? "" : user.Name;
  681. noteTopicResult.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  682. noteTopicResult.FolderId = middle == null ? 0 : middle.Id;
  683. noteTopicResult.FolderName = middle == null ? "" : middle.FolderName;
  684. return noteTopicResult;
  685. }, splitOn: "Id,Name,Id");
  686. sql = $@"SELECT
  687. count(1)
  688. FROM
  689. tede_note a
  690. INNER JOIN
  691. tede_user b ON a.UserId = b.Id
  692. INNER JOIN
  693. tede_middle c ON c.Id = a.FolderId
  694. WHERE
  695. a.IsTopic = 1";
  696. if (!string.IsNullOrEmpty(request.SearchKey))
  697. {
  698. sql += $@" and (b.Name like '%11%' or a.Title like '%11%' or a.TextContent like '%11%')";
  699. }
  700. sql += $@" AND (a.FolderId IN (SELECT
  701. Id
  702. FROM
  703. tede_middle
  704. WHERE
  705. id IN (SELECT
  706. MiddleId
  707. FROM
  708. tede_folder_user
  709. WHERE
  710. UserId = {request.UserId})
  711. AND RoleId IN (3))
  712. OR a.FolderId IN (SELECT
  713. Id
  714. FROM
  715. tede_middle
  716. WHERE
  717. RoleId IN (1))) ";
  718. result.Total =
  719. await connection.ExecuteScalarAsync<int>(sql);
  720. return result;
  721. }
  722. /// <summary>
  723. /// 笔记没有文件夹分页
  724. /// </summary>
  725. /// <param name="request"></param>
  726. /// <returns></returns>
  727. public async Task<PagedList<NoteNotFolderPageResult>> GetNoteNotFolderPageResult(NoteSearchPageListRequest request)
  728. {
  729. var sqlValue = "";
  730. if (!string.IsNullOrEmpty(request.SearchKey))
  731. {
  732. sqlValue += $" and (a.Title like '%{request.SearchKey}%' or a.TextContent like '%{request.SearchKey}%')";
  733. }
  734. var noteConstValue = GxPress.EnumConst.AllTypeConst.Note.GetHashCode();
  735. var sql = $@"SELECT
  736. a.Id,
  737. a.Title,
  738. a.Content,
  739. a.UserId,
  740. a.CreatedDate,
  741. a.MediaId,
  742. a.ReadCount,
  743. a.IsTop,
  744. (SELECT
  745. FolderName
  746. FROM
  747. tede_middle
  748. WHERE
  749. id = (SELECT
  750. ParentId
  751. FROM
  752. tede_middle
  753. WHERE
  754. MiddleId = a.Id AND FolderType = {noteConstValue} limit 1)) AS FolderName,
  755. (SELECT
  756. Id
  757. FROM
  758. tede_middle
  759. WHERE
  760. id = (SELECT
  761. ParentId
  762. FROM
  763. tede_middle
  764. WHERE
  765. MiddleId = a.Id AND FolderType = {noteConstValue} limit 1)) AS FolderId,
  766. (SELECT
  767. COUNT(1)
  768. FROM
  769. tede_analyze
  770. WHERE
  771. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  772. AND SourceId = a.Id
  773. AND AnalyzeType = 1) AS PraiseCount,
  774. (SELECT
  775. COUNT(1)
  776. FROM
  777. tede_analyze
  778. WHERE
  779. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  780. AND SourceId = a.Id
  781. AND AnalyzeType = 1
  782. LIMIT 0 , 1) AS IsPraise,
  783. (SELECT
  784. COUNT(1)
  785. FROM
  786. tede_comment
  787. WHERE
  788. UserId = {request.UserId} AND ArticleId = a.Id
  789. AND TypeValue = {noteConstValue}) AS CommentCount,
  790. (SELECT
  791. COUNT(1)
  792. FROM
  793. tede_analyze
  794. WHERE
  795. UserId = {request.UserId} AND TypeValue ={noteConstValue}
  796. AND SourceId = a.Id
  797. AND AnalyzeType = {noteConstValue}) AS RetransmissionCount,
  798. (SELECT
  799. COUNT(1)
  800. FROM
  801. tede_analyze
  802. WHERE
  803. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  804. AND SourceId = a.Id
  805. AND AnalyzeType = 4
  806. LIMIT 0 , 1) AS IsRetransmission,
  807. (SELECT
  808. COUNT(1)
  809. FROM
  810. tede_analyze
  811. WHERE
  812. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  813. AND SourceId = a.Id
  814. AND AnalyzeType = 3
  815. LIMIT 0 , 1) AS IsCollect,
  816. (SELECT
  817. COUNT(1)
  818. FROM
  819. tede_analyze
  820. WHERE
  821. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  822. AND SourceId = a.Id
  823. AND AnalyzeType = 3) AS IsCollect,
  824. b.Name,
  825. b.AvatarUrl,c.Id,c.MiddleId
  826. FROM
  827. tede_note a
  828. INNER JOIN
  829. tede_user b ON a.UserId = b.Id inner join tede_middle c on c.MiddleId=a.Id
  830. WHERE
  831. c.IsDelete = 0 and a.IsTopic=0 AND a.UserId = {request.UserId} and c.FolderType={noteConstValue} {sqlValue}
  832. ORDER BY a.IsTop desc ,a.LastModifiedDate DESC
  833. limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  834. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  835. var database = new Database(databaseType, _connectionString);
  836. var connection = database.GetConnection();
  837. var result = new PagedList<NoteNotFolderPageResult>();
  838. result.Items =
  839. await connection
  840. .QueryAsync<NoteNotFolderPageResult, User, Entity.Middle.Middle, NoteNotFolderPageResult>(sql,
  841. (result, user, middle) =>
  842. {
  843. result.MiddleId = middle.MiddleId;
  844. result.Id = middle.Id;
  845. result.Name = user == null ? "" : user.Name;
  846. result.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  847. return result;
  848. }, splitOn: "Id,Id,Name,Id");
  849. sql = $@"SELECT count(1)
  850. FROM
  851. tede_note a
  852. INNER JOIN
  853. tede_user b ON a.UserId = b.Id inner join tede_middle c on c.MiddleId=a.Id
  854. WHERE
  855. a.IsDelete = 0 and a.IsTopic=0 AND a.UserId = {request.UserId} and c.FolderType={noteConstValue} {sqlValue}";
  856. result.Total = await connection.ExecuteScalarAsync<int>(sql);
  857. return result;
  858. }
  859. /// <summary>
  860. /// 获取用户最近使用的文件夹
  861. /// </summary>
  862. /// <param name="userId"></param>
  863. /// <returns></returns>
  864. public async Task<IEnumerable<int>> GetLatelyFolderIdAsync(int userId, bool isTopic)
  865. {
  866. var query = Q.NewQuery();
  867. query.Select(nameof(Entity.Note.Note.FolderId));
  868. query.Where(nameof(Entity.Note.Note.UserId), userId);
  869. query.Where(nameof(Entity.Note.Note.IsTopic), isTopic);
  870. query.Where(nameof(Entity.Note.Note.FolderId), ">", 0);
  871. query.OrderByDesc(nameof(Entity.Note.Note.CreatedDate));
  872. return await _repository.GetAllAsync<int>(query);
  873. }
  874. /// <summary>
  875. /// 获取用户是否存在草稿
  876. /// </summary>
  877. /// <param name="userId"></param>
  878. /// <returns></returns>
  879. public async Task<bool> IsExistsDraftAsync(int userId, bool isTopic)
  880. {
  881. var query = Q.NewQuery();
  882. query.Where(nameof(Entity.Note.Note.UserId), userId);
  883. query.Where(nameof(Entity.Note.Note.IsTopic), isTopic);
  884. query.Where(nameof(Entity.Note.Note.IsDraft), true);
  885. return await _repository.ExistsAsync(query);
  886. }
  887. /// <summary>
  888. /// 获取用户最新的草稿Id
  889. /// </summary>
  890. /// <param name="userId"></param>
  891. /// <returns></returns>
  892. public async Task<int> GetNoteIdByDraftAsync(int userId, bool isTopic)
  893. {
  894. var query = Q.NewQuery();
  895. query.Where(nameof(Entity.Note.Note.UserId), userId);
  896. query.Where(nameof(Entity.Note.Note.IsTopic), isTopic);
  897. query.Where(nameof(Entity.Note.Note.IsDraft), true);
  898. query.OrderByDesc(nameof(Entity.Note.Note.CreatedDate));
  899. query.Select(nameof(Entity.Note.Note.Id));
  900. return await _repository.GetAsync<int>(query);
  901. }
  902. /// <summary>
  903. /// 获取用户最新的草稿Id
  904. /// </summary>
  905. /// <param name="userId"></param>
  906. /// <returns></returns>
  907. public async Task<int> GetDraftCountAsync(int userId, bool isTopic)
  908. {
  909. var query = Q.NewQuery();
  910. query.Where(nameof(Entity.Note.Note.UserId), userId);
  911. query.Where(nameof(Entity.Note.Note.IsTopic), isTopic);
  912. query.Where(nameof(Entity.Note.Note.IsDraft), true);
  913. return await _repository.CountAsync(query);
  914. }
  915. /// <summary>
  916. /// 清空草稿箱
  917. /// </summary>
  918. /// <param name="userId"></param>
  919. /// <param name="typeValue"></param>
  920. /// <returns></returns>
  921. public async Task<bool> ClearDraftAsync(int userId, int typeValue)
  922. {
  923. var query = Q.NewQuery();
  924. query.Where(nameof(Entity.Note.Note.UserId), userId);
  925. query.Where(nameof(Entity.Note.Note.IsTopic), typeValue == 1);
  926. query.Where(nameof(Entity.Note.Note.IsDraft), true);
  927. return await _repository.DeleteAsync(query) > 0;
  928. }
  929. public async Task<IEnumerable<Entity.Note.Note>> GetAllAsync(SqlKata.Query query)
  930. {
  931. return await _repository.GetAllAsync(query);
  932. }
  933. }
  934. }