NoteRepository.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 ={noteTypeId} 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.TypeValue == 0)
  266. sql += "and (a.RoleId in(2) or a.AttributeValue=1)";
  267. else
  268. sql += "and (a.RoleId in(1,3) or a.AttributeValue=1)";
  269. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  270. 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}%'))";
  271. else if (!string.IsNullOrEmpty(request.SearchKey))
  272. sql += $@" and a.ParentId={request.FolderId} AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%')";
  273. else
  274. sql += $" and a.ParentId={request.FolderId}";
  275. sql +=
  276. $@" ORDER BY a.AttributeValue desc,a.IsTop DESC,a.Sort DESC , a.LastModifiedDate DESC";
  277. if (request.Page > 0)
  278. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  279. }
  280. return sql;
  281. }
  282. /// <summary>
  283. /// 条数
  284. /// </summary>
  285. /// <param name="request"></param>
  286. /// <returns></returns>
  287. public string GetCollectionSqlCount(NoteSearchPageListRequest request)
  288. {
  289. string sql = "";
  290. if (request.VisitUserId > 0)
  291. {
  292. sql =
  293. $@"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";
  294. if (request.FolderId == 0)
  295. sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
  296. else
  297. sql += $" and a.ParentId={request.FolderId}";
  298. if (!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. sql +=
  301. $@" OR a.id IN(SELECT MiddleId FROM tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType = 4 AND a.UserId = {request.VisitUserId}";
  302. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  303. 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}%'))";
  304. else if (!string.IsNullOrEmpty(request.SearchKey))
  305. 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}%'))";
  306. else
  307. sql += $" and a.ParentId={request.FolderId}";
  308. }
  309. else
  310. {
  311. sql =
  312. $@"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";
  313. if (request.TypeValue == 0)
  314. sql += " and (a.RoleId in(2) or a.AttributeValue=1)";
  315. else
  316. sql += " and (a.RoleId in(1,3) or a.AttributeValue=1)";
  317. if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
  318. 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}%'))";
  319. else if (!string.IsNullOrEmpty(request.SearchKey))
  320. 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}%'))";
  321. else
  322. sql += $" and a.ParentId={request.FolderId}";
  323. }
  324. return sql;
  325. }
  326. public async Task<int> NotePageListCountAsync(string sql)
  327. {
  328. var pagedList = new PagedList<NotePageListRequest>();
  329. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  330. var database = new Database(databaseType, _connectionString);
  331. var connection = database.GetConnection();
  332. var result =
  333. await connection
  334. .ExecuteScalarAsync<int>(sql);
  335. return result;
  336. }
  337. /// <summary>
  338. /// 删除笔记
  339. /// </summary>
  340. /// <param name="id"></param>
  341. /// <returns></returns>
  342. public async Task<bool> DeleteAsync(int id) => await _repository.DeleteAsync(id);
  343. /// <summary>
  344. ///查询个人摘录
  345. /// </summary>
  346. /// <param name="request"></param>
  347. /// <returns></returns>
  348. public async Task<PagedList<NoteUserExtractResult>> GetAllByUserId(NoteUserRequest request)
  349. {
  350. var result = new PagedList<NoteUserExtractResult>();
  351. var sql = $@"SELECT
  352. a.Id,
  353. a.CreatedDate,
  354. a.Title,
  355. a.Content,
  356. a.UserId,
  357. a.ReadCount,
  358. a.HtmlContent,
  359. a.MediaId,
  360. a.CatalogId,
  361. a.ChapterId,
  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) AS PraiseCount,
  370. (SELECT
  371. COUNT(1)
  372. FROM
  373. tede_analyze
  374. WHERE
  375. UserId = {request.UserId} AND TypeValue = 3
  376. AND SourceId = a.Id
  377. AND AnalyzeType = 1
  378. LIMIT 0 , 1) AS IsPraise,
  379. (SELECT
  380. COUNT(1)
  381. FROM
  382. tede_comment
  383. WHERE
  384. ArticleId = a.Id AND pid = 0
  385. AND TypeValue = 3) AS CommentCount,
  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) AS RetransmissionCount,
  394. (SELECT
  395. COUNT(1)
  396. FROM
  397. tede_analyze
  398. WHERE
  399. UserId = {request.UserId} AND TypeValue = 3
  400. AND SourceId = a.Id
  401. AND AnalyzeType = 4
  402. LIMIT 0 , 1) AS IsRetransmission,
  403. b.Name,b.AvatarUrl
  404. FROM
  405. tede_note a
  406. INNER JOIN
  407. tede_user b ON a.UserId = b.Id
  408. WHERE
  409. UserId = {request.UserId} and a.MediaId='{request.MediaId}'
  410. ORDER BY CreatedDate DESC";
  411. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  412. var database = new Database(databaseType, _connectionString);
  413. var connection = database.GetConnection();
  414. result.Items =
  415. await connection
  416. .QueryAsync<NoteUserExtractResult>(sql);
  417. sql = $@"SELECT
  418. count(1)
  419. FROM
  420. tede_note a
  421. INNER JOIN
  422. tede_user b ON a.UserId = b.Id
  423. WHERE
  424. UserId = {request.UserId} and a.MediaId='{request.MediaId}' and a.CatalogId='{request.CatalogId}' and a.ChapterId='{request.ChapterId}'";
  425. result.Total =
  426. await connection.ExecuteScalarAsync<int>(sql);
  427. return result;
  428. }
  429. /// <summary>
  430. /// web页面查询话题
  431. /// </summary>
  432. /// <param name="request"></param>
  433. /// <returns></returns>
  434. public async Task<PagedList<NoteTopicResult>> GetTopicNoteAsync(NoteSearchPageListRequest request)
  435. {
  436. var noteTypeId = GxPress.EnumConst.AllTypeConst.Note.GetHashCode();
  437. var sql = $@"SELECT
  438. a.Id,
  439. a.Title,
  440. a.Content,
  441. a.HtmlContent,
  442. a.CreatedDate,
  443. b.Id as UserId,
  444. b.Name,
  445. b.AvatarUrl,
  446. c.Id,
  447. c.FolderName,
  448. (SELECT
  449. COUNT(1)
  450. FROM
  451. tede_analyze
  452. WHERE
  453. TypeValue ={noteTypeId} AND SourceId = a.Id
  454. AND AnalyzeType = 1) AS PraiseCount,
  455. (SELECT
  456. COUNT(1)
  457. FROM
  458. tede_analyze
  459. WHERE
  460. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  461. AND SourceId = a.Id
  462. AND AnalyzeType = 1
  463. LIMIT 0 , 1) AS IsPraise,
  464. (SELECT
  465. COUNT(1)
  466. FROM
  467. tede_comment
  468. WHERE
  469. ArticleId = a.Id AND TypeValue = {noteTypeId}
  470. AND Pid = 0) AS CommentCount,
  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) AS RetransmissionCount,
  479. (SELECT
  480. COUNT(1)
  481. FROM
  482. tede_analyze
  483. WHERE
  484. UserId = {request.UserId} AND TypeValue ={noteTypeId}
  485. AND SourceId = a.Id
  486. AND AnalyzeType = 4
  487. LIMIT 0 , 1) AS IsRetransmission,
  488. (SELECT
  489. COUNT(1)
  490. FROM
  491. tede_analyze
  492. WHERE
  493. UserId = {request.UserId} AND TypeValue = {noteTypeId}
  494. AND SourceId = a.Id
  495. AND AnalyzeType = 3
  496. LIMIT 0 , 1) AS IsCollect
  497. FROM
  498. tede_note a
  499. INNER JOIN
  500. tede_user b ON a.UserId = b.Id
  501. INNER JOIN
  502. tede_middle c ON c.Id = a.FolderId
  503. WHERE
  504. a.IsTopic = 1";
  505. if (!string.IsNullOrEmpty(request.SearchKey))
  506. {
  507. sql += $@" and (b.Name like '%11%' or a.Title like '%11%' or a.Content like '%11%')";
  508. }
  509. sql += $@" AND (a.FolderId IN (SELECT
  510. Id
  511. FROM
  512. tede_middle
  513. WHERE
  514. id IN (SELECT
  515. MiddleId
  516. FROM
  517. tede_folder_user
  518. WHERE
  519. UserId = {request.UserId})
  520. AND RoleId IN (3))
  521. OR a.FolderId IN (SELECT
  522. Id
  523. FROM
  524. tede_middle
  525. WHERE
  526. RoleId IN (1))) ";
  527. sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  528. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  529. var database = new Database(databaseType, _connectionString);
  530. var connection = database.GetConnection();
  531. var result = new PagedList<NoteTopicResult>();
  532. result.Items =
  533. await connection
  534. .QueryAsync<NoteTopicResult, User, Entity.Middle.Middle, NoteTopicResult>(sql,
  535. (noteTopicResult, user, middle) =>
  536. {
  537. noteTopicResult.UserName = user == null ? "" : user.Name;
  538. noteTopicResult.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  539. noteTopicResult.FolderId = middle == null ? 0 : middle.Id;
  540. noteTopicResult.FolderName = middle == null ? "" : middle.FolderName;
  541. return noteTopicResult;
  542. }, splitOn: "Id,Name,Id");
  543. sql = $@"SELECT
  544. count(1)
  545. FROM
  546. tede_note a
  547. INNER JOIN
  548. tede_user b ON a.UserId = b.Id
  549. INNER JOIN
  550. tede_middle c ON c.Id = a.FolderId
  551. WHERE
  552. a.IsTopic = 1";
  553. if (!string.IsNullOrEmpty(request.SearchKey))
  554. {
  555. sql += $@" and (b.Name like '%11%' or a.Title like '%11%' or a.Content like '%11%')";
  556. }
  557. sql += $@" AND (a.FolderId IN (SELECT
  558. Id
  559. FROM
  560. tede_middle
  561. WHERE
  562. id IN (SELECT
  563. MiddleId
  564. FROM
  565. tede_folder_user
  566. WHERE
  567. UserId = {request.UserId})
  568. AND RoleId IN (3))
  569. OR a.FolderId IN (SELECT
  570. Id
  571. FROM
  572. tede_middle
  573. WHERE
  574. RoleId IN (1))) ";
  575. result.Total =
  576. await connection.ExecuteScalarAsync<int>(sql);
  577. return result;
  578. }
  579. /// <summary>
  580. /// 笔记没有文件夹分页
  581. /// </summary>
  582. /// <param name="request"></param>
  583. /// <returns></returns>
  584. public async Task<PagedList<NoteNotFolderPageResult>> GetNoteNotFolderPageResult(NoteSearchPageListRequest request)
  585. {
  586. var sqlValue = "";
  587. if (!string.IsNullOrEmpty(request.SearchKey))
  588. {
  589. sqlValue += $" and (a.Title like '%{request.SearchKey}%' or a.Content like '%{request.SearchKey}%')";
  590. }
  591. var noteConstValue = GxPress.EnumConst.AllTypeConst.Note.GetHashCode();
  592. var sql = $@"SELECT
  593. a.Id,
  594. a.Title,
  595. a.Content,
  596. a.UserId,
  597. a.CreatedDate,
  598. a.MediaId,
  599. a.ReadCount,
  600. a.IsTop,
  601. (SELECT
  602. FolderName
  603. FROM
  604. tede_middle
  605. WHERE
  606. id = (SELECT
  607. ParentId
  608. FROM
  609. tede_middle
  610. WHERE
  611. MiddleId = a.Id AND FolderType = {noteConstValue})) AS FolderName,
  612. (SELECT
  613. Id
  614. FROM
  615. tede_middle
  616. WHERE
  617. id = (SELECT
  618. ParentId
  619. FROM
  620. tede_middle
  621. WHERE
  622. MiddleId = a.Id AND FolderType = {noteConstValue})) AS FolderId,
  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) AS PraiseCount,
  631. (SELECT
  632. COUNT(1)
  633. FROM
  634. tede_analyze
  635. WHERE
  636. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  637. AND SourceId = a.Id
  638. AND AnalyzeType = 1
  639. LIMIT 0 , 1) AS IsPraise,
  640. (SELECT
  641. COUNT(1)
  642. FROM
  643. tede_comment
  644. WHERE
  645. UserId = {request.UserId} AND ArticleId = a.Id
  646. AND TypeValue = {noteConstValue}) AS CommentCount,
  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 = {noteConstValue}) AS RetransmissionCount,
  655. (SELECT
  656. COUNT(1)
  657. FROM
  658. tede_analyze
  659. WHERE
  660. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  661. AND SourceId = a.Id
  662. AND AnalyzeType = 4
  663. LIMIT 0 , 1) AS IsRetransmission,
  664. (SELECT
  665. COUNT(1)
  666. FROM
  667. tede_analyze
  668. WHERE
  669. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  670. AND SourceId = a.Id
  671. AND AnalyzeType = 3
  672. LIMIT 0 , 1) AS IsCollect,
  673. (SELECT
  674. COUNT(1)
  675. FROM
  676. tede_analyze
  677. WHERE
  678. UserId = {request.UserId} AND TypeValue = {noteConstValue}
  679. AND SourceId = a.Id
  680. AND AnalyzeType = 3) AS IsCollect,
  681. b.Name,
  682. b.AvatarUrl,c.Id,c.MiddleId
  683. FROM
  684. tede_note a
  685. INNER JOIN
  686. tede_user b ON a.UserId = b.Id inner join tede_middle c on c.MiddleId=a.Id
  687. WHERE
  688. a.IsDelete = 0 AND a.UserId = {request.UserId} and c.FolderType={noteConstValue} {sqlValue}
  689. ORDER BY a.IsTop desc ,a.CreatedDate DESC
  690. limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  691. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  692. var database = new Database(databaseType, _connectionString);
  693. var connection = database.GetConnection();
  694. var result = new PagedList<NoteNotFolderPageResult>();
  695. result.Items =
  696. await connection
  697. .QueryAsync<NoteNotFolderPageResult, User, Entity.Middle.Middle, NoteNotFolderPageResult>(sql,
  698. (result, user, middle) =>
  699. {
  700. result.MiddleId = middle.MiddleId;
  701. result.Id = middle.Id;
  702. result.Name = user == null ? "" : user.Name;
  703. result.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
  704. return result;
  705. }, splitOn: "Id,Id,Name,Id");
  706. sql = $@"SELECT count(1)
  707. FROM
  708. tede_note a
  709. INNER JOIN
  710. tede_user b ON a.UserId = b.Id inner join tede_middle c on c.MiddleId=a.Id
  711. WHERE
  712. a.IsDelete = 0 AND a.UserId = {request.UserId} and c.FolderType={noteConstValue} {sqlValue}";
  713. result.Total = await connection.ExecuteScalarAsync<int>(sql);
  714. return result;
  715. }
  716. /// <summary>
  717. /// 获取用户最近使用的文件夹
  718. /// </summary>
  719. /// <param name="userId"></param>
  720. /// <returns></returns>
  721. public async Task<IEnumerable<int>> GetLatelyFolderIdAsync(int userId)
  722. {
  723. var query = Q.NewQuery();
  724. query.Select(nameof(Entity.Note.Note.FolderId));
  725. query.Where(nameof(Entity.Note.Note.UserId), userId);
  726. query.Where(nameof(Entity.Note.Note.IsTop), true);
  727. query.Where(nameof(Entity.Note.Note.FolderId), ">", 0);
  728. query.OrderByDesc(nameof(Entity.Note.Note.CreatedDate));
  729. return await _repository.GetAllAsync<int>(query);
  730. }
  731. }
  732. }