TopicRepository.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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 and a.IsDraft=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 and a.IsDraft=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.IsDraft)
  338. sqlStr += $" and a.UserId={request.UserId}";
  339. if (request.GroupIds.Count() == 0)
  340. {
  341. sqlStr += $@" AND a.GroupId IN (SELECT
  342. GroupId
  343. FROM
  344. tede_group_user
  345. WHERE
  346. UserId ={request.UserId})";
  347. }
  348. if (request.GroupIds.Count > 0)
  349. {
  350. var groupIds = new List<int>();
  351. foreach (var item in request.GroupIds)
  352. {
  353. if (item > 0)
  354. groupIds.Add(item);
  355. //全网
  356. if (item == -1)
  357. groupIds.AddRange(await GetPublicGroupAsync());
  358. //我的小组
  359. if (item == -2)
  360. groupIds.AddRange(await GetIntoGroupAsync(request.UserId));
  361. }
  362. var groupIdStr = string.Empty;
  363. foreach (var item in groupIds)
  364. groupIdStr += $"{item},";
  365. if (!string.IsNullOrEmpty(groupIdStr))
  366. {
  367. groupIdStr = groupIdStr.Remove(groupIdStr.Length - 1, 1);
  368. sqlStr += $@" AND a.GroupId IN (SELECT
  369. GroupId
  370. FROM
  371. tede_group_user
  372. WHERE
  373. UserId ={request.UserId} AND GroupId In ({groupIdStr}))";
  374. }
  375. }
  376. if (!string.IsNullOrEmpty(request.Key))
  377. {
  378. sqlStr += $@"AND (a.Title LIKE '%{request.Key}%'
  379. OR a.Content LIKE '%{request.Key}%')";
  380. }
  381. string sql = $@"SELECT
  382. a.Id,
  383. a.Title,
  384. a.UserId,
  385. a.ReadCount,
  386. a.CreatedDate,
  387. a.Content,
  388. a.GroupId,
  389. (SELECT
  390. COUNT(1)
  391. FROM
  392. tede_analyze
  393. WHERE
  394. TypeValue ={topicConst} AND SourceId = a.Id
  395. AND AnalyzeType = 1) AS PraiseCount,
  396. (SELECT
  397. COUNT(1)
  398. FROM
  399. tede_analyze
  400. WHERE
  401. UserId ={request.UserId} AND TypeValue = {topicConst}
  402. AND SourceId = a.Id
  403. AND AnalyzeType = 1
  404. LIMIT 0 , 1) AS IsPraise,
  405. (SELECT
  406. COUNT(1)
  407. FROM
  408. tede_comment
  409. WHERE
  410. ArticleId = a.Id AND pid = 0
  411. AND TypeValue = 1) AS CommentCount,
  412. (SELECT
  413. COUNT(1)
  414. FROM
  415. tede_analyze
  416. WHERE
  417. UserId ={request.UserId} AND TypeValue ={topicConst}
  418. AND SourceId = a.Id
  419. AND AnalyzeType = 4) AS RetransmissionCount,
  420. (SELECT
  421. COUNT(1)
  422. FROM
  423. tede_analyze
  424. WHERE
  425. UserId ={request.UserId} AND TypeValue ={topicConst}
  426. AND SourceId = a.Id
  427. AND AnalyzeType = 4
  428. LIMIT 0 , 1) AS IsRetransmission,
  429. (SELECT
  430. COUNT(1)
  431. FROM
  432. tede_analyze
  433. WHERE
  434. UserId ={request.UserId} AND TypeValue = {topicConst}
  435. AND SourceId = a.Id
  436. AND AnalyzeType = 4
  437. LIMIT 0 , 1) AS IsCollect,
  438. (SELECT
  439. COUNT(1)
  440. FROM
  441. tede_analyze
  442. WHERE
  443. UserId ={request.UserId} AND TypeValue = {topicConst}
  444. AND SourceId = a.Id
  445. AND AnalyzeType = 4) AS CollectCount,
  446. b.Name as GroupName,
  447. c.Name as UserName,
  448. c.AvatarUrl,
  449. d.Id as DepartmentId,
  450. d.Name as DepartmentName
  451. FROM
  452. tede_topic a
  453. INNER JOIN
  454. tede_group b ON a.GroupId = b.Id
  455. INNER JOIN
  456. tede_user c ON c.Id = a.UserId
  457. INNER JOIN
  458. tede_department d ON d.Id = c.DepartmentId
  459. WHERE
  460. a.GroupId > 0
  461. {sqlStr}
  462. ORDER BY a.CreatedDate DESC limit @page,@pageSize";
  463. string countSql = $@"SELECT
  464. count(1)
  465. FROM
  466. tede_topic a
  467. INNER JOIN
  468. tede_group b ON a.GroupId = b.Id
  469. INNER JOIN
  470. tede_user c ON c.Id = a.UserId
  471. WHERE
  472. a.GroupId > 0
  473. {sqlStr}
  474. ORDER BY a.CreatedDate DESC";
  475. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  476. var database = new Database(databaseType, _connectionString);
  477. var connection = database.GetConnection();
  478. result.Items =
  479. await connection
  480. .QueryAsync<TopicListPageResult>(sql, new { page = (request.Page - 1) * request.PerPage, pageSize = request.PerPage });
  481. foreach (var item in result.Items)
  482. {
  483. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  484. }
  485. result.Total = await connection.ExecuteScalarAsync<int>(countSql);
  486. //回去小组草稿数量
  487. var query = Q.NewQuery();
  488. query.WhereIn(nameof(Entity.Topic.Topic.GroupId), request.GroupIds);
  489. query.Where(nameof(Entity.Topic.Topic.IsDraft), true);
  490. query.Where(nameof(Entity.Topic.Topic.UserId), request.UserId);
  491. result.DraftCount = await CountAsync(query);
  492. return result;
  493. }
  494. /// <summary>
  495. /// 获取共享小组
  496. /// </summary>
  497. /// <returns></returns>
  498. public async Task<IEnumerable<int>> GetPublicGroupAsync()
  499. {
  500. string sql = @"select Id from tede_group where IsShow=1";
  501. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  502. var database = new Database(databaseType, _connectionString);
  503. var connection = database.GetConnection();
  504. var result = await connection.QueryAsync<int>(sql);
  505. return result;
  506. }
  507. /// <summary>
  508. /// 我加入的小组
  509. /// </summary>
  510. /// <param name="userId"></param>
  511. /// <returns></returns>
  512. public async Task<IEnumerable<int>> GetIntoGroupAsync(int userId)
  513. {
  514. string sql = $"select a.Id from tede_group a inner join tede_group_user b on a.Id=b.GroupId where b.UserId={userId}";
  515. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  516. var database = new Database(databaseType, _connectionString);
  517. var connection = database.GetConnection();
  518. return await connection.QueryAsync<int>(sql);
  519. }
  520. /// <summary>
  521. /// 根据小组ID获取话题数量
  522. /// </summary>
  523. /// <param name="groupId"></param>
  524. /// <returns></returns>
  525. public async Task<int> GetGroupTopicCountAsync(int groupId)
  526. {
  527. return await _repository.CountAsync(Q.Where(nameof(Entity.Topic.Topic.GroupId), groupId));
  528. }
  529. /// <summary>
  530. /// 根据ID获取话题
  531. /// </summary>
  532. /// <param name="id"></param>
  533. /// <returns></returns>
  534. public async Task<Entity.Topic.Topic> GetAsync(int id)
  535. {
  536. return await _repository.GetAsync(id);
  537. }
  538. public async Task<int> CountAsync(Query query)
  539. {
  540. return await _repository.CountAsync(query);
  541. }
  542. public async Task<IEnumerable<Entity.Topic.Topic>> GetAllAsync(Query query)
  543. {
  544. return await _repository.GetAllAsync(query);
  545. }
  546. public async Task<int> InsertAsync(Entity.Topic.Topic topic)
  547. {
  548. return await _repository.InsertAsync(topic);
  549. }
  550. public async Task<bool> DeleteAsync(int id)
  551. {
  552. return await _repository.DeleteAsync(id);
  553. }
  554. public async Task<bool> UpdateAsync(Entity.Topic.Topic topic)
  555. {
  556. return await _repository.UpdateAsync(topic);
  557. }
  558. /// <summary>
  559. /// 执行话题
  560. /// </summary>
  561. /// <returns></returns>
  562. public async Task<List<JobTopicResult>> ExecuteTopic()
  563. {
  564. string sql = @"select a.*,b.UserId,b.Id,c.Name,d.AvatarUrl from tede_topic a
  565. inner join tede_topic_addressee b on a.Id=b.TopicId
  566. inner join tede_user c on c.Id=b.UserId
  567. inner join tede_user d on d.Id=a.UserId
  568. where b.IsUpload=0
  569. order by a.CreatedDate desc limit 0,100";
  570. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  571. var database = new Database(databaseType, _connectionString);
  572. var connection = database.GetConnection();
  573. var result = await connection.QueryAsync<JobTopicResult, TopicAddressee, User, User, JobTopicResult>(sql, (jobTopicResult, topicAddressee, user, userDto) =>
  574. {
  575. jobTopicResult.UserId = topicAddressee.UserId;
  576. jobTopicResult.Name = user.Name;
  577. jobTopicResult.AvatarUrl = userDto.AvatarUrl;
  578. jobTopicResult.TopicAddresseeId = topicAddressee.Id;
  579. return jobTopicResult;
  580. }, splitOn: "UserId,Name,AvatarUrl");
  581. connection.Dispose();
  582. return result.ToList();
  583. }
  584. /// <summary>
  585. /// 新版 获取笔记话题列表
  586. /// </summary>
  587. /// <param name="request"></param>
  588. /// <returns></returns>
  589. public async Task<PagedList<TopicListPageResult>> GetTopicPage(TopicPageSearchRequest request, string sql, string countSql)
  590. {
  591. var result = new PagedList<TopicListPageResult>();
  592. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  593. var database = new Database(databaseType, _connectionString);
  594. var connection = database.GetConnection();
  595. result.Items = await connection.QueryAsync<TopicListPageResult>(sql);
  596. result.Total = await connection.ExecuteScalarAsync<int>(countSql);
  597. connection.Dispose();
  598. foreach (var item in result.Items)
  599. {
  600. item.FolderResult.FolderName = item.FolderName;
  601. item.FolderResult.Id = item.ParentId;
  602. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  603. }
  604. return result;
  605. }
  606. public async Task<PagedList<TopicListPageResult>> GetTopicByGroupAsync(TopicPageSearchRequest request)
  607. {
  608. var topicConstValue = AllTypeConst.Topic.GetHashCode();
  609. var sql = $@"SELECT
  610. a.*,
  611. (SELECT
  612. COUNT(1)
  613. FROM
  614. tede_analyze
  615. WHERE
  616. TypeValue ={topicConstValue} AND SourceId = a.Id
  617. AND AnalyzeType = 1) AS PraiseCount,
  618. (SELECT
  619. COUNT(1)
  620. FROM
  621. tede_analyze
  622. WHERE
  623. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  624. AND SourceId = a.Id
  625. AND AnalyzeType = 1
  626. LIMIT 0 , 1) AS IsPraise,
  627. (SELECT
  628. COUNT(1)
  629. FROM
  630. tede_comment
  631. WHERE
  632. ArticleId = a.Id AND TypeValue = {topicConstValue}
  633. AND Pid = 0) AS CommentCount,
  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) AS RetransmissionCount,
  642. (SELECT
  643. COUNT(1)
  644. FROM
  645. tede_analyze
  646. WHERE
  647. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  648. AND SourceId = a.Id
  649. AND AnalyzeType = 4
  650. LIMIT 0 , 1) AS IsRetransmission,
  651. (SELECT
  652. COUNT(1)
  653. FROM
  654. tede_analyze
  655. WHERE
  656. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  657. AND SourceId = a.Id
  658. AND AnalyzeType = 3
  659. LIMIT 0 , 1) AS IsCollect,
  660. b.Id as GroupId,
  661. b.Name,
  662. c.Name,
  663. c.AvatarUrl
  664. FROM
  665. tede_topic a
  666. INNER JOIN
  667. tede_group b ON a.GroupId = b.id
  668. INNER JOIN
  669. tede_user c ON a.UserId = c.Id
  670. WHERE 1=1 ";
  671. if (request.TopicGroupIds.Count > 0)
  672. {
  673. var groupStr = "(";
  674. foreach (var item in request.TopicGroupIds)
  675. {
  676. groupStr += $"{item},";
  677. }
  678. groupStr = groupStr.Remove(groupStr.Length - 1, 1);
  679. groupStr += ")";
  680. sql += $@" and a.GroupId IN {groupStr}";
  681. }
  682. else
  683. sql += $@" and a.GroupId > 0";
  684. if (request.IsAdmin)
  685. {
  686. sql += $@" AND a.UserId={request.UserId}";
  687. }
  688. else
  689. {
  690. sql += $@" AND b.Id IN (SELECT
  691. GroupId
  692. FROM
  693. tede_group_user
  694. WHERE
  695. UserId = {request.UserId})";
  696. }
  697. if (!string.IsNullOrWhiteSpace(request.Keyword))
  698. {
  699. sql += $@" AND (c.Name LIKE '%{request.Keyword}%'
  700. OR a.Title LIKE '%{request.Keyword}%'
  701. OR a.HtmlContent LIKE '%{request.Keyword}%')";
  702. }
  703. if (!string.IsNullOrEmpty(request.Sort))
  704. sql += $@" ORDER BY a.ReadCount DESC";
  705. else
  706. sql += $@" ORDER BY a.CreatedDate DESC";
  707. sql += $@" LIMIT {(request.Page - 1) * request.PerPage} , {request.PerPage}";
  708. var result = new PagedList<TopicListPageResult>();
  709. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  710. var database = new Database(databaseType, _connectionString);
  711. var connection = database.GetConnection();
  712. result.Items = await connection.QueryAsync<TopicListPageResult, Entity.Group, User, TopicListPageResult>(sql, (topicListPageResult, group, user) =>
  713. {
  714. topicListPageResult.AvatarUrl = user != null ? StringUtils.AddDomainMin(user.AvatarUrl) : "";
  715. topicListPageResult.GroupName = group != null ? group.Name : "";
  716. topicListPageResult.UserName = user != null ? user.Name : "";
  717. return topicListPageResult;
  718. }, splitOn: "Id,GroupId,Name");
  719. sql = $@"SELECT
  720. count(1)
  721. FROM
  722. tede_topic a
  723. INNER JOIN
  724. tede_group b ON a.GroupId = b.id
  725. INNER JOIN
  726. tede_user c ON a.UserId = c.Id
  727. WHERE 1=1 ";
  728. if (request.TopicGroupIds.Count > 0)
  729. {
  730. var groupStr = "(";
  731. foreach (var item in request.TopicGroupIds)
  732. {
  733. groupStr += $"{item},";
  734. }
  735. groupStr = groupStr.Remove(groupStr.Length - 1, 1);
  736. groupStr += ")";
  737. sql += $@" and a.GroupId IN {groupStr}";
  738. }
  739. else
  740. sql += $@" and a.GroupId > 0";
  741. if (request.IsAdmin)
  742. {
  743. sql += $@" AND a.UserId={request.UserId}";
  744. }
  745. else
  746. {
  747. sql += $@" AND b.Id IN (SELECT
  748. GroupId
  749. FROM
  750. tede_group_user
  751. WHERE
  752. UserId = {request.UserId})";
  753. }
  754. if (!string.IsNullOrWhiteSpace(request.Keyword))
  755. {
  756. sql += $@" AND (c.Name LIKE '%{request.Keyword}%'
  757. OR a.Title LIKE '%{request.Keyword}%'
  758. OR a.HtmlContent LIKE '%{request.Keyword}%')";
  759. }
  760. result.Total = await connection.ExecuteScalarAsync<int>(sql);
  761. return result;
  762. }
  763. /// <summary>
  764. /// 删除草稿箱
  765. /// </summary>
  766. /// <param name="userId"></param>
  767. /// <param name="groupId"></param>
  768. /// <returns></returns>
  769. public async Task<bool> ClearDraftAsync(int userId, int groupId)
  770. {
  771. var query = Q.NewQuery();
  772. query.Where(nameof(Entity.Topic.Topic.UserId), userId);
  773. query.Where(nameof(Entity.Topic.Topic.IsDraft), true);
  774. query.Where(nameof(Entity.Topic.Topic.GroupId), groupId);
  775. return await _repository.DeleteAsync(query) > 0;
  776. }
  777. }
  778. }