TopicRepository.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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>> GetAnonymousGroupTopicPageAsync(TopicDetailListRequest request)
  205. {
  206. var topicConst = AllTypeConst.Topic.GetHashCode();
  207. var result = new PagedList<TopicListPageResult>();
  208. var sqlStr = @" AND a.GroupId IN (SELECT
  209. Id
  210. FROM
  211. tede_group
  212. WHERE
  213. IsShow = 1) ";
  214. if (!string.IsNullOrEmpty(request.Key))
  215. {
  216. sqlStr += $@"AND (a.Title LIKE '%{request.Key}%'
  217. OR a.Content LIKE '%{request.Key}%')";
  218. }
  219. string sql = $@"SELECT
  220. a.Id,
  221. a.Title,
  222. a.UserId,
  223. a.ReadCount,
  224. a.CreatedDate,
  225. a.Content,
  226. a.GroupId,
  227. (SELECT
  228. COUNT(1)
  229. FROM
  230. tede_analyze
  231. WHERE
  232. TypeValue ={topicConst} AND SourceId = a.Id
  233. AND AnalyzeType = 1) AS PraiseCount,
  234. (SELECT
  235. COUNT(1)
  236. FROM
  237. tede_analyze
  238. WHERE
  239. UserId ={request.UserId} AND TypeValue = {topicConst}
  240. AND SourceId = a.Id
  241. AND AnalyzeType = 1
  242. LIMIT 0 , 1) AS IsPraise,
  243. (SELECT
  244. COUNT(1)
  245. FROM
  246. tede_comment
  247. WHERE
  248. ArticleId = a.Id AND pid = 0
  249. AND TypeValue = 1) AS CommentCount,
  250. (SELECT
  251. COUNT(1)
  252. FROM
  253. tede_analyze
  254. WHERE
  255. UserId ={request.UserId} AND TypeValue ={topicConst}
  256. AND SourceId = a.Id
  257. AND AnalyzeType = 4) AS RetransmissionCount,
  258. (SELECT
  259. COUNT(1)
  260. FROM
  261. tede_analyze
  262. WHERE
  263. UserId ={request.UserId} AND TypeValue ={topicConst}
  264. AND SourceId = a.Id
  265. AND AnalyzeType = 4
  266. LIMIT 0 , 1) AS IsRetransmission,
  267. (SELECT
  268. COUNT(1)
  269. FROM
  270. tede_analyze
  271. WHERE
  272. UserId ={request.UserId} AND TypeValue = {topicConst}
  273. AND SourceId = a.Id
  274. AND AnalyzeType = 4
  275. LIMIT 0 , 1) AS IsCollect,
  276. (SELECT
  277. COUNT(1)
  278. FROM
  279. tede_analyze
  280. WHERE
  281. UserId ={request.UserId} AND TypeValue = {topicConst}
  282. AND SourceId = a.Id
  283. AND AnalyzeType = 4) AS CollectCount,
  284. b.Name as GroupName,
  285. c.Name as UserName,
  286. c.AvatarUrl,
  287. d.Id as DepartmentId,
  288. d.Name as DepartmentName
  289. FROM
  290. tede_topic a
  291. INNER JOIN
  292. tede_group b ON a.GroupId = b.Id
  293. INNER JOIN
  294. tede_user c ON c.Id = a.UserId
  295. INNER JOIN
  296. tede_department d ON d.Id = c.DepartmentId
  297. WHERE
  298. a.GroupId > 0
  299. {sqlStr}
  300. ORDER BY a.CreatedDate DESC limit @page,@pageSize";
  301. string countSql = $@"SELECT
  302. count(1)
  303. FROM
  304. tede_topic a
  305. INNER JOIN
  306. tede_group b ON a.GroupId = b.Id
  307. INNER JOIN
  308. tede_user c ON c.Id = a.UserId
  309. WHERE
  310. a.GroupId > 0
  311. {sqlStr}
  312. ORDER BY a.CreatedDate DESC";
  313. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  314. var database = new Database(databaseType, _connectionString);
  315. var connection = database.GetConnection();
  316. result.Items =
  317. await connection
  318. .QueryAsync<TopicListPageResult>(sql, new { page = (request.Page - 1) * request.PerPage, pageSize = request.PerPage });
  319. foreach (var item in result.Items)
  320. {
  321. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  322. }
  323. result.Total = await connection.ExecuteScalarAsync<int>(countSql);
  324. return result;
  325. }
  326. /// <summary>
  327. ///最新 小组话题分页列表
  328. /// </summary>
  329. /// <param name="request"></param>
  330. /// <returns></returns>
  331. public async Task<PagedList<TopicListPageResult>> GetGroupTopicPageAsync(TopicDetailListRequest request)
  332. {
  333. var topicConst = AllTypeConst.Topic.GetHashCode();
  334. var result = new PagedList<TopicListPageResult>();
  335. var sqlStr = "";
  336. if (request.GroupIds.Count() == 0)
  337. {
  338. sqlStr += $@" AND a.GroupId IN (SELECT
  339. GroupId
  340. FROM
  341. tede_group_user
  342. WHERE
  343. UserId ={request.UserId})";
  344. }
  345. if (request.GroupIds.Count > 0)
  346. {
  347. var groupIds = new List<int>();
  348. foreach (var item in request.GroupIds)
  349. {
  350. if (item > 0)
  351. groupIds.Add(item);
  352. //全网
  353. if (item == -1)
  354. groupIds.AddRange(await GetPublicGroupAsync());
  355. //我的小组
  356. if (item == -2)
  357. groupIds.AddRange(await GetIntoGroupAsync(request.UserId));
  358. }
  359. var groupIdStr = string.Empty;
  360. foreach (var item in groupIds)
  361. groupIdStr += $"{item},";
  362. if (!string.IsNullOrEmpty(groupIdStr))
  363. {
  364. groupIdStr = groupIdStr.Remove(groupIdStr.Length - 1, 1);
  365. sqlStr += $@" AND a.GroupId IN (SELECT
  366. GroupId
  367. FROM
  368. tede_group_user
  369. WHERE
  370. UserId ={request.UserId} AND GroupId In ({groupIdStr}))";
  371. }
  372. }
  373. if (!string.IsNullOrEmpty(request.Key))
  374. {
  375. sqlStr += $@"AND (a.Title LIKE '%{request.Key}%'
  376. OR a.Content LIKE '%{request.Key}%')";
  377. }
  378. string sql = $@"SELECT
  379. a.Id,
  380. a.Title,
  381. a.UserId,
  382. a.ReadCount,
  383. a.CreatedDate,
  384. a.Content,
  385. a.GroupId,
  386. (SELECT
  387. COUNT(1)
  388. FROM
  389. tede_analyze
  390. WHERE
  391. TypeValue ={topicConst} AND SourceId = a.Id
  392. AND AnalyzeType = 1) AS PraiseCount,
  393. (SELECT
  394. COUNT(1)
  395. FROM
  396. tede_analyze
  397. WHERE
  398. UserId ={request.UserId} AND TypeValue = {topicConst}
  399. AND SourceId = a.Id
  400. AND AnalyzeType = 1
  401. LIMIT 0 , 1) AS IsPraise,
  402. (SELECT
  403. COUNT(1)
  404. FROM
  405. tede_comment
  406. WHERE
  407. ArticleId = a.Id AND pid = 0
  408. AND TypeValue = 1) AS CommentCount,
  409. (SELECT
  410. COUNT(1)
  411. FROM
  412. tede_analyze
  413. WHERE
  414. UserId ={request.UserId} AND TypeValue ={topicConst}
  415. AND SourceId = a.Id
  416. AND AnalyzeType = 4) AS RetransmissionCount,
  417. (SELECT
  418. COUNT(1)
  419. FROM
  420. tede_analyze
  421. WHERE
  422. UserId ={request.UserId} AND TypeValue ={topicConst}
  423. AND SourceId = a.Id
  424. AND AnalyzeType = 4
  425. LIMIT 0 , 1) AS IsRetransmission,
  426. (SELECT
  427. COUNT(1)
  428. FROM
  429. tede_analyze
  430. WHERE
  431. UserId ={request.UserId} AND TypeValue = {topicConst}
  432. AND SourceId = a.Id
  433. AND AnalyzeType = 4
  434. LIMIT 0 , 1) AS IsCollect,
  435. (SELECT
  436. COUNT(1)
  437. FROM
  438. tede_analyze
  439. WHERE
  440. UserId ={request.UserId} AND TypeValue = {topicConst}
  441. AND SourceId = a.Id
  442. AND AnalyzeType = 4) AS CollectCount,
  443. b.Name as GroupName,
  444. c.Name as UserName,
  445. c.AvatarUrl,
  446. d.Id as DepartmentId,
  447. d.Name as DepartmentName
  448. FROM
  449. tede_topic a
  450. INNER JOIN
  451. tede_group b ON a.GroupId = b.Id
  452. INNER JOIN
  453. tede_user c ON c.Id = a.UserId
  454. INNER JOIN
  455. tede_department d ON d.Id = c.DepartmentId
  456. WHERE
  457. a.GroupId > 0
  458. {sqlStr}
  459. ORDER BY a.CreatedDate DESC limit @page,@pageSize";
  460. string countSql = $@"SELECT
  461. count(1)
  462. FROM
  463. tede_topic a
  464. INNER JOIN
  465. tede_group b ON a.GroupId = b.Id
  466. INNER JOIN
  467. tede_user c ON c.Id = a.UserId
  468. WHERE
  469. a.GroupId > 0
  470. {sqlStr}
  471. ORDER BY a.CreatedDate DESC";
  472. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  473. var database = new Database(databaseType, _connectionString);
  474. var connection = database.GetConnection();
  475. result.Items =
  476. await connection
  477. .QueryAsync<TopicListPageResult>(sql, new { page = (request.Page - 1) * request.PerPage, pageSize = request.PerPage });
  478. foreach (var item in result.Items)
  479. {
  480. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  481. }
  482. result.Total = await connection.ExecuteScalarAsync<int>(countSql);
  483. return result;
  484. }
  485. /// <summary>
  486. /// 获取共享小组
  487. /// </summary>
  488. /// <returns></returns>
  489. public async Task<IEnumerable<int>> GetPublicGroupAsync()
  490. {
  491. string sql = @"select Id from tede_group where IsShow=1";
  492. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  493. var database = new Database(databaseType, _connectionString);
  494. var connection = database.GetConnection();
  495. var result = await connection.QueryAsync<int>(sql);
  496. return result;
  497. }
  498. /// <summary>
  499. /// 我加入的小组
  500. /// </summary>
  501. /// <param name="userId"></param>
  502. /// <returns></returns>
  503. public async Task<IEnumerable<int>> GetIntoGroupAsync(int userId)
  504. {
  505. string sql = $"select a.Id from tede_group a inner join tede_group_user b on a.Id=b.GroupId where b.UserId={userId}";
  506. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  507. var database = new Database(databaseType, _connectionString);
  508. var connection = database.GetConnection();
  509. return await connection.QueryAsync<int>(sql);
  510. }
  511. /// <summary>
  512. /// 根据小组ID获取话题数量
  513. /// </summary>
  514. /// <param name="groupId"></param>
  515. /// <returns></returns>
  516. public async Task<int> GetGroupTopicCountAsync(int groupId)
  517. {
  518. return await _repository.CountAsync(Q.Where(nameof(Entity.Topic.Topic.GroupId), groupId));
  519. }
  520. /// <summary>
  521. /// 根据ID获取话题
  522. /// </summary>
  523. /// <param name="id"></param>
  524. /// <returns></returns>
  525. public async Task<Entity.Topic.Topic> GetAsync(int id)
  526. {
  527. return await _repository.GetAsync(id);
  528. }
  529. public async Task<int> CountAsync(Query query)
  530. {
  531. return await _repository.CountAsync(query);
  532. }
  533. public async Task<IEnumerable<Entity.Topic.Topic>> GetAllAsync(Query query)
  534. {
  535. return await _repository.GetAllAsync(query);
  536. }
  537. public async Task<int> InsertAsync(Entity.Topic.Topic topic)
  538. {
  539. return await _repository.InsertAsync(topic);
  540. }
  541. public async Task<bool> DeleteAsync(int id)
  542. {
  543. return await _repository.DeleteAsync(id);
  544. }
  545. public async Task<bool> UpdateAsync(Entity.Topic.Topic topic)
  546. {
  547. return await _repository.UpdateAsync(topic);
  548. }
  549. /// <summary>
  550. /// 执行话题
  551. /// </summary>
  552. /// <returns></returns>
  553. public async Task<List<JobTopicResult>> ExecuteTopic()
  554. {
  555. 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";
  556. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  557. var database = new Database(databaseType, _connectionString);
  558. var connection = database.GetConnection();
  559. var result = await connection.QueryAsync<JobTopicResult, TopicAddressee, User, User, JobTopicResult>(sql, (jobTopicResult, topicAddressee, user, userDto) =>
  560. {
  561. jobTopicResult.UserId = topicAddressee.UserId;
  562. jobTopicResult.Name = user.Name;
  563. jobTopicResult.AvatarUrl = userDto.AvatarUrl;
  564. jobTopicResult.TopicAddresseeId = topicAddressee.Id;
  565. return jobTopicResult;
  566. }, splitOn: "UserId,Name,AvatarUrl");
  567. connection.Dispose();
  568. return result.ToList();
  569. }
  570. /// <summary>
  571. /// 新版 获取笔记话题列表
  572. /// </summary>
  573. /// <param name="request"></param>
  574. /// <returns></returns>
  575. public async Task<PagedList<TopicListPageResult>> GetTopicPage(TopicPageSearchRequest request, string sql, string countSql)
  576. {
  577. var result = new PagedList<TopicListPageResult>();
  578. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  579. var database = new Database(databaseType, _connectionString);
  580. var connection = database.GetConnection();
  581. result.Items = await connection.QueryAsync<TopicListPageResult>(sql);
  582. result.Total = await connection.ExecuteScalarAsync<int>(countSql);
  583. connection.Dispose();
  584. foreach (var item in result.Items)
  585. {
  586. item.FolderResult.FolderName = item.FolderName;
  587. item.FolderResult.Id = item.ParentId;
  588. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  589. }
  590. return result;
  591. }
  592. public async Task<PagedList<TopicListPageResult>> GetTopicByGroupAsync(TopicPageSearchRequest request)
  593. {
  594. var topicConstValue = AllTypeConst.Topic.GetHashCode();
  595. var sql = $@"SELECT
  596. a.*,
  597. (SELECT
  598. COUNT(1)
  599. FROM
  600. tede_analyze
  601. WHERE
  602. TypeValue ={topicConstValue} AND SourceId = a.Id
  603. AND AnalyzeType = 1) AS PraiseCount,
  604. (SELECT
  605. COUNT(1)
  606. FROM
  607. tede_analyze
  608. WHERE
  609. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  610. AND SourceId = a.Id
  611. AND AnalyzeType = 1
  612. LIMIT 0 , 1) AS IsPraise,
  613. (SELECT
  614. COUNT(1)
  615. FROM
  616. tede_comment
  617. WHERE
  618. ArticleId = a.Id AND TypeValue = {topicConstValue}
  619. AND Pid = 0) AS CommentCount,
  620. (SELECT
  621. COUNT(1)
  622. FROM
  623. tede_analyze
  624. WHERE
  625. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  626. AND SourceId = a.Id
  627. AND AnalyzeType = 4) AS RetransmissionCount,
  628. (SELECT
  629. COUNT(1)
  630. FROM
  631. tede_analyze
  632. WHERE
  633. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  634. AND SourceId = a.Id
  635. AND AnalyzeType = 4
  636. LIMIT 0 , 1) AS IsRetransmission,
  637. (SELECT
  638. COUNT(1)
  639. FROM
  640. tede_analyze
  641. WHERE
  642. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  643. AND SourceId = a.Id
  644. AND AnalyzeType = 3
  645. LIMIT 0 , 1) AS IsCollect,
  646. b.Id as GroupId,
  647. b.Name,
  648. c.Name,
  649. c.AvatarUrl
  650. FROM
  651. tede_topic a
  652. INNER JOIN
  653. tede_group b ON a.GroupId = b.id
  654. INNER JOIN
  655. tede_user c ON a.UserId = c.Id
  656. WHERE 1=1 ";
  657. if (request.TopicGroupIds.Count > 0)
  658. {
  659. var groupStr = "(";
  660. foreach (var item in request.TopicGroupIds)
  661. {
  662. groupStr += $"{item},";
  663. }
  664. groupStr = groupStr.Remove(groupStr.Length - 1, 1);
  665. groupStr += ")";
  666. sql += $@" and a.GroupId IN {groupStr}";
  667. }
  668. else
  669. sql += $@" and a.GroupId > 0";
  670. if (request.IsAdmin)
  671. {
  672. sql += $@" AND a.UserId={request.UserId}";
  673. }
  674. else
  675. {
  676. sql += $@" AND b.Id IN (SELECT
  677. GroupId
  678. FROM
  679. tede_group_user
  680. WHERE
  681. UserId = {request.UserId})";
  682. }
  683. if (!string.IsNullOrWhiteSpace(request.Keyword))
  684. {
  685. sql += $@" AND (c.Name LIKE '%{request.Keyword}%'
  686. OR a.Title LIKE '%{request.Keyword}%'
  687. OR a.HtmlContent LIKE '%{request.Keyword}%')";
  688. }
  689. if (!string.IsNullOrEmpty(request.Sort))
  690. sql += $@" ORDER BY a.ReadCount DESC";
  691. else
  692. sql += $@" ORDER BY a.CreatedDate DESC";
  693. sql += $@" LIMIT {(request.Page - 1) * request.PerPage} , {request.PerPage}";
  694. var result = new PagedList<TopicListPageResult>();
  695. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  696. var database = new Database(databaseType, _connectionString);
  697. var connection = database.GetConnection();
  698. result.Items = await connection.QueryAsync<TopicListPageResult, Entity.Group, User, TopicListPageResult>(sql, (topicListPageResult, group, user) =>
  699. {
  700. topicListPageResult.AvatarUrl = user != null ? StringUtils.AddDomainMin(user.AvatarUrl) : "";
  701. topicListPageResult.GroupName = group != null ? group.Name : "";
  702. topicListPageResult.UserName = user != null ? user.Name : "";
  703. return topicListPageResult;
  704. }, splitOn: "Id,GroupId,Name");
  705. sql = $@"SELECT
  706. count(1)
  707. FROM
  708. tede_topic a
  709. INNER JOIN
  710. tede_group b ON a.GroupId = b.id
  711. INNER JOIN
  712. tede_user c ON a.UserId = c.Id
  713. WHERE 1=1 ";
  714. if (request.TopicGroupIds.Count > 0)
  715. {
  716. var groupStr = "(";
  717. foreach (var item in request.TopicGroupIds)
  718. {
  719. groupStr += $"{item},";
  720. }
  721. groupStr = groupStr.Remove(groupStr.Length - 1, 1);
  722. groupStr += ")";
  723. sql += $@" and a.GroupId IN {groupStr}";
  724. }
  725. else
  726. sql += $@" and a.GroupId > 0";
  727. if (request.IsAdmin)
  728. {
  729. sql += $@" AND a.UserId={request.UserId}";
  730. }
  731. else
  732. {
  733. sql += $@" AND b.Id IN (SELECT
  734. GroupId
  735. FROM
  736. tede_group_user
  737. WHERE
  738. UserId = {request.UserId})";
  739. }
  740. if (!string.IsNullOrWhiteSpace(request.Keyword))
  741. {
  742. sql += $@" AND (c.Name LIKE '%{request.Keyword}%'
  743. OR a.Title LIKE '%{request.Keyword}%'
  744. OR a.HtmlContent LIKE '%{request.Keyword}%')";
  745. }
  746. result.Total = await connection.ExecuteScalarAsync<int>(sql);
  747. return result;
  748. }
  749. }
  750. }