TopicRepository.cs 28 KB

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