TopicRepository.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Dapper;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Page;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity;
  10. using GxPress.Repository.Interface;
  11. using GxPress.Request.App.Topic;
  12. using GxPress.Result.App.Topic;
  13. using Microsoft.Extensions.Options;
  14. using Datory;
  15. using GxPress.Entity.Topic;
  16. using GxPress.Result;
  17. using SqlKata;
  18. using GxPress.Result.Job;
  19. using GxPress.EnumConst;
  20. namespace GxPress.Repository.Implement
  21. {
  22. public class TopicRepository : ITopicRepository
  23. {
  24. private readonly Repository<Entity.Topic.Topic> _repository;
  25. private readonly Repository<TopicAddressee> _topicAddresseeRepository;
  26. private readonly Repository<User> _userRepository;
  27. private readonly IMapper _mapper;
  28. private readonly string _connectionString;
  29. private readonly string _databaseTypeStr;
  30. private readonly Repository<GroupUser> _groupUserRepository;
  31. private readonly Repository<Entity.Visit.Visit> _visitRepository;
  32. public TopicRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  33. {
  34. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  35. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  36. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  37. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  38. _repository = new Repository<Entity.Topic.Topic>(database);
  39. _topicAddresseeRepository = new Repository<TopicAddressee>(database);
  40. _userRepository = new Repository<User>(database);
  41. _groupUserRepository = new Repository<GroupUser>(database);
  42. _visitRepository = new Repository<Entity.Visit.Visit>(database);
  43. _mapper = mapper;
  44. }
  45. public TopicRepository()
  46. {
  47. }
  48. public IDatabase Database => _repository.Database;
  49. public string TableName => _repository.TableName;
  50. public List<TableColumn> TableColumns => _repository.TableColumns;
  51. /// <summary>
  52. /// 根据userId获取话题
  53. /// </summary>
  54. /// <param name="request"></param>
  55. /// <returns></returns>
  56. public async Task<PagedList<Entity.Topic.Topic>> GetTopicByUserIdAsync(TopicPageSearchRequest request)
  57. {
  58. var query = Q.NewQuery();
  59. if (request.UserId > 0)
  60. {
  61. query.Where(nameof(Entity.Topic.Topic.UserId), request.UserId);
  62. }
  63. if (!string.IsNullOrEmpty(request.Keyword))
  64. {
  65. var like = $"%{request.Keyword}%";
  66. query.WhereLike(nameof(Entity.Topic.Topic.Title), like);
  67. }
  68. var pagedList = new PagedList<Entity.Topic.Topic>
  69. {
  70. Total = await _repository.CountAsync(query)
  71. };
  72. var list = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  73. pagedList.Items = list;
  74. return pagedList;
  75. }
  76. /// <summary>
  77. /// APP列表显示用
  78. /// </summary>
  79. /// <param name="contentJsonDataList"></param>
  80. /// <returns></returns>
  81. public List<ContentJsonData> GetListAsync(List<ContentJsonData> contentJsonDataList)
  82. {
  83. var result = new List<ContentJsonData>();
  84. if (contentJsonDataList.Count > 0)
  85. {
  86. //查询文本
  87. var txtType = contentJsonDataList.FirstOrDefault(n => n.Type == 1);
  88. if (txtType != null)
  89. result.Add(txtType);
  90. //拼接图片
  91. var imgType = contentJsonDataList.FindAll(n => n.Type == 2);
  92. if (imgType.Any())
  93. {
  94. foreach (var item in imgType)
  95. {
  96. //判断图片数量
  97. if (result.Count(n => n.Type == 2) > 8)
  98. break;
  99. result.Add(item);
  100. }
  101. }
  102. }
  103. return result;
  104. }
  105. /// <summary>
  106. /// 根据GroupId获取通知
  107. /// </summary>
  108. /// <param name="request"></param>
  109. /// <returns></returns>
  110. public async Task<PagedList<TopicListPageResult>> FindTopicByGroupIdAsync(TopicDetailListRequest request)
  111. {
  112. var topicConts = GxPress.EnumConst.AllTypeConst.Topic.GetHashCode();
  113. //用户收件人
  114. var resultData = new PagedList<TopicListPageResult>();
  115. var showKey = $@"SELECT a.*,(SELECT
  116. COUNT(1)
  117. FROM
  118. tede_analyze
  119. WHERE
  120. TypeValue ={topicConts}
  121. AND SourceId = a.Id
  122. AND AnalyzeType = 1) AS PraiseCount,
  123. (SELECT
  124. COUNT(1)
  125. FROM
  126. tede_analyze
  127. WHERE
  128. UserId = {request.UserId} AND TypeValue = {topicConts}
  129. AND SourceId = a.Id
  130. AND AnalyzeType = 1
  131. LIMIT 0 , 1) AS IsPraise,
  132. (SELECT
  133. COUNT(1)
  134. FROM
  135. tede_comment
  136. WHERE
  137. ArticleId = a.Id and pid=0
  138. AND TypeValue = 1) AS CommentCount,
  139. (SELECT
  140. COUNT(1)
  141. FROM
  142. tede_analyze
  143. WHERE
  144. UserId = {request.UserId} AND TypeValue = {topicConts}
  145. AND SourceId = a.Id
  146. AND AnalyzeType = 4) AS RetransmissionCount,
  147. (SELECT
  148. COUNT(1)
  149. FROM
  150. tede_analyze
  151. WHERE
  152. UserId = {request.UserId} AND TypeValue = {topicConts}
  153. AND SourceId = a.Id
  154. AND AnalyzeType = 4
  155. LIMIT 0 , 1) AS IsRetransmission,
  156. (SELECT
  157. COUNT(1)
  158. FROM
  159. tede_analyze
  160. WHERE
  161. UserId = {request.UserId} AND TypeValue = {topicConts}
  162. AND SourceId = a.Id
  163. AND AnalyzeType = 4
  164. LIMIT 0 , 1) AS IsCollect,
  165. (SELECT
  166. COUNT(1)
  167. FROM
  168. tede_analyze
  169. WHERE
  170. UserId = {request.UserId} AND TypeValue = {topicConts}
  171. AND SourceId = a.Id
  172. AND AnalyzeType = 4) AS CollectCount,b.Name,b.AvatarUrl,c.Name FROM";
  173. var countSql = $"SELECT count(1) FROM tede_topic a inner join tede_user b on a.UserId=b.Id inner join tede_Department c on c.Id=b.DepartmentId where a.GroupId={request.GroupId}";
  174. var sql =
  175. $"{showKey} tede_topic a inner join tede_user b on a.UserId=b.Id inner join tede_Department c on c.Id=b.DepartmentId where a.GroupId={request.GroupId}";
  176. var strValue = "";
  177. if (!string.IsNullOrEmpty(request.Key))
  178. strValue += $" and (a.Title like '%{request.Key}%' or a.Content like '%{request.Key}%' or b.Name like '%{request.Key}%')";
  179. sql += $"{strValue} order by a.CreatedDate DESC LIMIT @page , @pageSize";
  180. countSql += $"{strValue}";
  181. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  182. var database = new Database(databaseType, _connectionString);
  183. var connection = database.GetConnection();
  184. var result =
  185. connection
  186. .Query<TopicListPageResult, User, Department, TopicListPageResult>(sql,
  187. (topicListPageResult, user, department) =>
  188. {
  189. topicListPageResult.DepartmentName = department.Name;
  190. topicListPageResult.UserName = user.Name;
  191. topicListPageResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  192. return topicListPageResult;
  193. }, new { page = (request.Page - 1) * request.PerPage, pageSize = request.PerPage }, splitOn: "Name");
  194. resultData.Total = await connection.ExecuteScalarAsync<int>(countSql);
  195. var topicListPageResults = result as TopicListPageResult[] ?? result.ToArray();
  196. resultData.Items = topicListPageResults;
  197. return resultData;
  198. }
  199. /// <summary>
  200. ///最新 小组话题分页列表
  201. /// </summary>
  202. /// <param name="request"></param>
  203. /// <returns></returns>
  204. public async Task<PagedList<TopicListPageResult>> GetGroupTopicPageAsync(TopicDetailListRequest request)
  205. {
  206. var topicConst = AllTypeConst.Topic.GetHashCode();
  207. var result = new PagedList<TopicListPageResult>();
  208. var sqlStr = "";
  209. if (request.GroupIds.Count() == 0)
  210. {
  211. sqlStr += $@" AND a.GroupId IN (SELECT
  212. GroupId
  213. FROM
  214. tede_group_user
  215. WHERE
  216. UserId ={request.UserId})";
  217. }
  218. if (request.GroupIds.Count > 0)
  219. {
  220. var groupIds = new List<int>();
  221. foreach (var item in request.GroupIds)
  222. {
  223. if (item > 0)
  224. groupIds.Add(item);
  225. //全网
  226. if (item == -1)
  227. groupIds.AddRange(await GetPublicGroupAsync());
  228. //我的小组
  229. if (item == -2)
  230. groupIds.AddRange(await GetIntoGroupAsync(request.UserId));
  231. }
  232. var groupIdStr = string.Empty;
  233. foreach (var item in groupIds)
  234. groupIdStr += $"{item},";
  235. if (!string.IsNullOrEmpty(groupIdStr))
  236. {
  237. groupIdStr = groupIdStr.Remove(groupIdStr.Length - 1, 1);
  238. sqlStr += $@" AND a.GroupId IN (SELECT
  239. GroupId
  240. FROM
  241. tede_group_user
  242. WHERE
  243. UserId ={request.UserId} AND GroupId In ({groupIdStr}))";
  244. }
  245. }
  246. if (!string.IsNullOrEmpty(request.Key))
  247. {
  248. sqlStr += $@"AND (a.Title LIKE '%{request.Key}%'
  249. OR a.Content LIKE '%{request.Key}%')";
  250. }
  251. string sql = $@"SELECT
  252. a.Id,
  253. a.Title,
  254. a.UserId,
  255. a.ReadCount,
  256. a.CreatedDate,
  257. a.Content,
  258. a.GroupId,
  259. (SELECT
  260. COUNT(1)
  261. FROM
  262. tede_analyze
  263. WHERE
  264. TypeValue ={topicConst} AND SourceId = a.Id
  265. AND AnalyzeType = 1) AS PraiseCount,
  266. (SELECT
  267. COUNT(1)
  268. FROM
  269. tede_analyze
  270. WHERE
  271. UserId ={request.UserId} AND TypeValue = {topicConst}
  272. AND SourceId = a.Id
  273. AND AnalyzeType = 1
  274. LIMIT 0 , 1) AS IsPraise,
  275. (SELECT
  276. COUNT(1)
  277. FROM
  278. tede_comment
  279. WHERE
  280. ArticleId = a.Id AND pid = 0
  281. AND TypeValue = 1) AS CommentCount,
  282. (SELECT
  283. COUNT(1)
  284. FROM
  285. tede_analyze
  286. WHERE
  287. UserId ={request.UserId} AND TypeValue ={topicConst}
  288. AND SourceId = a.Id
  289. AND AnalyzeType = 4) AS RetransmissionCount,
  290. (SELECT
  291. COUNT(1)
  292. FROM
  293. tede_analyze
  294. WHERE
  295. UserId ={request.UserId} AND TypeValue ={topicConst}
  296. AND SourceId = a.Id
  297. AND AnalyzeType = 4
  298. LIMIT 0 , 1) AS IsRetransmission,
  299. (SELECT
  300. COUNT(1)
  301. FROM
  302. tede_analyze
  303. WHERE
  304. UserId ={request.UserId} AND TypeValue = {topicConst}
  305. AND SourceId = a.Id
  306. AND AnalyzeType = 4
  307. LIMIT 0 , 1) AS IsCollect,
  308. (SELECT
  309. COUNT(1)
  310. FROM
  311. tede_analyze
  312. WHERE
  313. UserId ={request.UserId} AND TypeValue = {topicConst}
  314. AND SourceId = a.Id
  315. AND AnalyzeType = 4) AS CollectCount,
  316. b.Name,
  317. c.Name,
  318. c.AvatarUrl,
  319. d.Id,
  320. d.Name
  321. FROM
  322. tede_topic a
  323. INNER JOIN
  324. tede_group b ON a.GroupId = b.Id
  325. INNER JOIN
  326. tede_user c ON c.Id = a.UserId
  327. INNER JOIN
  328. tede_department d ON d.Id = c.DepartmentId
  329. WHERE
  330. a.GroupId > 0
  331. {sqlStr}
  332. ORDER BY a.CreatedDate DESC";
  333. string countSql = $@"SELECT
  334. count(1)
  335. FROM
  336. tede_topic a
  337. INNER JOIN
  338. tede_group b ON a.GroupId = b.Id
  339. INNER JOIN
  340. tede_user c ON c.Id = a.UserId
  341. WHERE
  342. a.GroupId > 0
  343. {sqlStr}
  344. ORDER BY a.CreatedDate DESC";
  345. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  346. var database = new Database(databaseType, _connectionString);
  347. var connection = database.GetConnection();
  348. result.Items =
  349. await connection
  350. .QueryAsync<TopicListPageResult, Entity.Group, User, Department, TopicListPageResult>(sql,
  351. (topicListPageResult, group, user, department) =>
  352. {
  353. topicListPageResult.GroupName = group != null ? group.Name : "";
  354. topicListPageResult.DepartmentName = department != null ? department.Name : "";
  355. topicListPageResult.DepartmentId = department != null ? department.Id : 0;
  356. topicListPageResult.UserName = user != null ? user.Name : "";
  357. topicListPageResult.AvatarUrl = user != null ? StringUtils.AddDomainMin(user.AvatarUrl) : "";
  358. return topicListPageResult;
  359. }, new { page = (request.Page - 1) * request.PerPage, pageSize = request.PerPage }, splitOn: "Id,Name,Name,Id");
  360. result.Total = await connection.ExecuteScalarAsync<int>(countSql);
  361. return result;
  362. }
  363. /// <summary>
  364. /// 获取共享小组
  365. /// </summary>
  366. /// <returns></returns>
  367. public async Task<IEnumerable<int>> GetPublicGroupAsync()
  368. {
  369. string sql = @"select Id from tede_group where IsShow=1";
  370. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  371. var database = new Database(databaseType, _connectionString);
  372. var connection = database.GetConnection();
  373. var result = await connection.QueryAsync<int>(sql);
  374. return result;
  375. }
  376. /// <summary>
  377. /// 我加入的小组
  378. /// </summary>
  379. /// <param name="userId"></param>
  380. /// <returns></returns>
  381. public async Task<IEnumerable<int>> GetIntoGroupAsync(int userId)
  382. {
  383. string sql = $"select a.Id from tede_group a inner join tede_group_user b on a.Id=b.GroupId where b.UserId={userId}";
  384. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  385. var database = new Database(databaseType, _connectionString);
  386. var connection = database.GetConnection();
  387. return await connection.QueryAsync<int>(sql);
  388. }
  389. /// <summary>
  390. /// 根据小组ID获取话题数量
  391. /// </summary>
  392. /// <param name="groupId"></param>
  393. /// <returns></returns>
  394. public async Task<int> GetGroupTopicCountAsync(int groupId)
  395. {
  396. return await _repository.CountAsync(Q.Where(nameof(Entity.Topic.Topic.GroupId), groupId));
  397. }
  398. /// <summary>
  399. /// 根据ID获取话题
  400. /// </summary>
  401. /// <param name="id"></param>
  402. /// <returns></returns>
  403. public async Task<Entity.Topic.Topic> GetAsync(int id)
  404. {
  405. return await _repository.GetAsync(id);
  406. }
  407. public async Task<int> CountAsync(Query query)
  408. {
  409. return await _repository.CountAsync(query);
  410. }
  411. public async Task<IEnumerable<Entity.Topic.Topic>> GetAllAsync(Query query)
  412. {
  413. return await _repository.GetAllAsync(query);
  414. }
  415. public async Task<int> InsertAsync(Entity.Topic.Topic topic)
  416. {
  417. return await _repository.InsertAsync(topic);
  418. }
  419. public async Task<bool> DeleteAsync(int id)
  420. {
  421. return await _repository.DeleteAsync(id);
  422. }
  423. public async Task<bool> UpdateAsync(Entity.Topic.Topic topic)
  424. {
  425. return await _repository.UpdateAsync(topic);
  426. }
  427. /// <summary>
  428. /// 执行话题
  429. /// </summary>
  430. /// <returns></returns>
  431. public async Task<List<JobTopicResult>> ExecuteTopic()
  432. {
  433. string sql = "select a.*,b.UserId,b.Id,c.Name,d.AvatarUrl from tede_topic a inner join tede_topic_addressee b on a.Id=b.TopicId inner join tede_user c on c.Id=b.UserId inner join tede_user d on d.Id=a.UserId where b.IsUpload=0 order by a.CreatedDate desc limit 0,100";
  434. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  435. var database = new Database(databaseType, _connectionString);
  436. var connection = database.GetConnection();
  437. var result = await connection.QueryAsync<JobTopicResult, TopicAddressee, User, User, JobTopicResult>(sql, (jobTopicResult, topicAddressee, user, userDto) =>
  438. {
  439. jobTopicResult.UserId = topicAddressee.UserId;
  440. jobTopicResult.Name = user.Name;
  441. jobTopicResult.AvatarUrl = userDto.AvatarUrl;
  442. jobTopicResult.TopicAddresseeId = topicAddressee.Id;
  443. return jobTopicResult;
  444. }, splitOn: "UserId,Name,AvatarUrl");
  445. connection.Dispose();
  446. return result.ToList();
  447. }
  448. /// <summary>
  449. /// 新版 获取笔记话题列表
  450. /// </summary>
  451. /// <param name="request"></param>
  452. /// <returns></returns>
  453. public async Task<PagedList<TopicListPageResult>> GetTopicPage(TopicPageSearchRequest request, string sql, string countSql)
  454. {
  455. var result = new PagedList<TopicListPageResult>();
  456. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  457. var database = new Database(databaseType, _connectionString);
  458. var connection = database.GetConnection();
  459. result.Items = await connection.QueryAsync<TopicListPageResult>(sql);
  460. result.Total = await connection.ExecuteScalarAsync<int>(countSql);
  461. connection.Dispose();
  462. foreach (var item in result.Items)
  463. {
  464. item.FolderResult.FolderName = item.FolderName;
  465. item.FolderResult.Id = item.ParentId;
  466. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  467. }
  468. return result;
  469. }
  470. public async Task<PagedList<TopicListPageResult>> GetTopicByGroupAsync(TopicPageSearchRequest request)
  471. {
  472. var topicConstValue = AllTypeConst.Topic.GetHashCode();
  473. var sql = $@"SELECT
  474. a.*,
  475. (SELECT
  476. COUNT(1)
  477. FROM
  478. tede_analyze
  479. WHERE
  480. TypeValue ={topicConstValue} AND SourceId = a.Id
  481. AND AnalyzeType = 1) AS PraiseCount,
  482. (SELECT
  483. COUNT(1)
  484. FROM
  485. tede_analyze
  486. WHERE
  487. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  488. AND SourceId = a.Id
  489. AND AnalyzeType = 1
  490. LIMIT 0 , 1) AS IsPraise,
  491. (SELECT
  492. COUNT(1)
  493. FROM
  494. tede_comment
  495. WHERE
  496. ArticleId = a.Id AND TypeValue = {topicConstValue}
  497. AND Pid = 0) AS CommentCount,
  498. (SELECT
  499. COUNT(1)
  500. FROM
  501. tede_analyze
  502. WHERE
  503. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  504. AND SourceId = a.Id
  505. AND AnalyzeType = 4) AS RetransmissionCount,
  506. (SELECT
  507. COUNT(1)
  508. FROM
  509. tede_analyze
  510. WHERE
  511. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  512. AND SourceId = a.Id
  513. AND AnalyzeType = 4
  514. LIMIT 0 , 1) AS IsRetransmission,
  515. (SELECT
  516. COUNT(1)
  517. FROM
  518. tede_analyze
  519. WHERE
  520. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  521. AND SourceId = a.Id
  522. AND AnalyzeType = 3
  523. LIMIT 0 , 1) AS IsCollect,
  524. b.Id as GroupId,
  525. b.Name,
  526. c.Name,
  527. c.AvatarUrl
  528. FROM
  529. tede_topic a
  530. INNER JOIN
  531. tede_group b ON a.GroupId = b.id
  532. INNER JOIN
  533. tede_user c ON a.UserId = c.Id
  534. WHERE 1=1 ";
  535. if (request.TopicGroupIds.Count > 0)
  536. {
  537. var groupStr = "(";
  538. foreach (var item in request.TopicGroupIds)
  539. {
  540. groupStr += $"{item},";
  541. }
  542. groupStr = groupStr.Remove(groupStr.Length - 1, 1);
  543. groupStr += ")";
  544. sql += $@" and a.GroupId IN {groupStr}";
  545. }
  546. else
  547. sql += $@" and a.GroupId > 0";
  548. if (request.IsAdmin)
  549. {
  550. sql += $@" AND a.UserId={request.UserId}";
  551. }
  552. else
  553. {
  554. sql += $@" AND b.Id IN (SELECT
  555. GroupId
  556. FROM
  557. tede_group_user
  558. WHERE
  559. UserId = {request.UserId})";
  560. }
  561. if (!string.IsNullOrWhiteSpace(request.Keyword))
  562. {
  563. sql += $@" AND (c.Name LIKE '%{request.Keyword}%'
  564. OR a.Title LIKE '%{request.Keyword}%'
  565. OR a.HtmlContent LIKE '%{request.Keyword}%')";
  566. }
  567. if (!string.IsNullOrEmpty(request.Sort))
  568. sql += $@" ORDER BY a.ReadCount DESC";
  569. else
  570. sql += $@" ORDER BY a.CreatedDate DESC";
  571. sql += $@" LIMIT {(request.Page - 1) * request.PerPage} , {request.PerPage}";
  572. var result = new PagedList<TopicListPageResult>();
  573. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  574. var database = new Database(databaseType, _connectionString);
  575. var connection = database.GetConnection();
  576. result.Items = await connection.QueryAsync<TopicListPageResult, Entity.Group, User, TopicListPageResult>(sql, (topicListPageResult, group, user) =>
  577. {
  578. topicListPageResult.AvatarUrl = user != null ? StringUtils.AddDomainMin(user.AvatarUrl) : "";
  579. topicListPageResult.GroupName = group != null ? group.Name : "";
  580. topicListPageResult.UserName = user != null ? user.Name : "";
  581. return topicListPageResult;
  582. }, splitOn: "Id,GroupId,Name");
  583. sql = $@"SELECT
  584. count(1)
  585. FROM
  586. tede_topic a
  587. INNER JOIN
  588. tede_group b ON a.GroupId = b.id
  589. INNER JOIN
  590. tede_user c ON a.UserId = c.Id
  591. WHERE 1=1 ";
  592. if (request.TopicGroupIds.Count > 0)
  593. {
  594. var groupStr = "(";
  595. foreach (var item in request.TopicGroupIds)
  596. {
  597. groupStr += $"{item},";
  598. }
  599. groupStr = groupStr.Remove(groupStr.Length - 1, 1);
  600. groupStr += ")";
  601. sql += $@" and a.GroupId IN {groupStr}";
  602. }
  603. else
  604. sql += $@" and a.GroupId > 0";
  605. if (request.IsAdmin)
  606. {
  607. sql += $@" AND a.UserId={request.UserId}";
  608. }
  609. else
  610. {
  611. sql += $@" AND b.Id IN (SELECT
  612. GroupId
  613. FROM
  614. tede_group_user
  615. WHERE
  616. UserId = {request.UserId})";
  617. }
  618. if (!string.IsNullOrWhiteSpace(request.Keyword))
  619. {
  620. sql += $@" AND (c.Name LIKE '%{request.Keyword}%'
  621. OR a.Title LIKE '%{request.Keyword}%'
  622. OR a.HtmlContent LIKE '%{request.Keyword}%')";
  623. }
  624. result.Total = await connection.ExecuteScalarAsync<int>(sql);
  625. return result;
  626. }
  627. }
  628. }