NoteRepository.cs 43 KB

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