NoteRepository.cs 38 KB

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