TopicRepository.cs 36 KB

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