TopicRepository.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. using Newtonsoft.Json;
  21. namespace GxPress.Repository.Implement
  22. {
  23. public class TopicRepository : ITopicRepository
  24. {
  25. private readonly Repository<Entity.Topic.Topic> _repository;
  26. private readonly Repository<TopicAddressee> _topicAddresseeRepository;
  27. private readonly Repository<User> _userRepository;
  28. private readonly IMapper _mapper;
  29. private readonly string _connectionString;
  30. private readonly string _databaseTypeStr;
  31. private readonly Repository<GroupUser> _groupUserRepository;
  32. private readonly Repository<Entity.Visit.Visit> _visitRepository;
  33. public TopicRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  34. {
  35. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  36. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  37. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  38. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  39. _repository = new Repository<Entity.Topic.Topic>(database);
  40. _topicAddresseeRepository = new Repository<TopicAddressee>(database);
  41. _userRepository = new Repository<User>(database);
  42. _groupUserRepository = new Repository<GroupUser>(database);
  43. _visitRepository = new Repository<Entity.Visit.Visit>(database);
  44. _mapper = mapper;
  45. }
  46. public TopicRepository()
  47. {
  48. }
  49. public IDatabase Database => _repository.Database;
  50. public string TableName => _repository.TableName;
  51. public List<TableColumn> TableColumns => _repository.TableColumns;
  52. /// <summary>
  53. /// 根据userId获取话题
  54. /// </summary>
  55. /// <param name="request"></param>
  56. /// <returns></returns>
  57. public async Task<PagedList<Entity.Topic.Topic>> GetTopicByUserIdAsync(TopicPageSearchRequest request)
  58. {
  59. var query = Q.NewQuery();
  60. if (request.UserId > 0)
  61. {
  62. query.Where(nameof(Entity.Topic.Topic.UserId), request.UserId);
  63. }
  64. if (!string.IsNullOrEmpty(request.Keyword))
  65. {
  66. var like = $"%{request.Keyword}%";
  67. query.WhereLike(nameof(Entity.Topic.Topic.Title), like);
  68. }
  69. var pagedList = new PagedList<Entity.Topic.Topic>
  70. {
  71. Total = await _repository.CountAsync(query)
  72. };
  73. var list = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  74. pagedList.Items = list;
  75. return pagedList;
  76. }
  77. /// <summary>
  78. /// APP列表显示用
  79. /// </summary>
  80. /// <param name="contentJsonDataList"></param>
  81. /// <returns></returns>
  82. public List<ContentJsonData> GetListAsync(List<ContentJsonData> contentJsonDataList)
  83. {
  84. var result = new List<ContentJsonData>();
  85. if (contentJsonDataList.Count > 0)
  86. {
  87. //查询文本
  88. var txtType = contentJsonDataList.FirstOrDefault(n => n.Type == 1);
  89. if (txtType != null)
  90. result.Add(txtType);
  91. //拼接图片
  92. var imgType = contentJsonDataList.FindAll(n => n.Type == 2);
  93. if (imgType.Any())
  94. {
  95. foreach (var item in imgType)
  96. {
  97. //判断图片数量
  98. if (result.Count(n => n.Type == 2) > 8)
  99. break;
  100. result.Add(item);
  101. }
  102. }
  103. }
  104. return result;
  105. }
  106. /// <summary>
  107. /// 根据GroupId获取通知
  108. /// </summary>
  109. /// <param name="request"></param>
  110. /// <returns></returns>
  111. public async Task<PagedList<TopicListPageResult>> FindTopicByGroupIdAsync(TopicDetailListRequest request)
  112. {
  113. var topicConts = GxPress.EnumConst.AllTypeConst.Topic.GetHashCode();
  114. //用户收件人
  115. var resultData = new PagedList<TopicListPageResult>();
  116. var showKey = $@"SELECT a.*,(SELECT
  117. COUNT(1)
  118. FROM
  119. tede_analyze
  120. WHERE
  121. TypeValue ={topicConts}
  122. AND SourceId = a.Id
  123. AND AnalyzeType = 1) AS PraiseCount,
  124. (SELECT
  125. COUNT(1)
  126. FROM
  127. tede_analyze
  128. WHERE
  129. UserId = {request.UserId} AND TypeValue = {topicConts}
  130. AND SourceId = a.Id
  131. AND AnalyzeType = 1
  132. LIMIT 0 , 1) AS IsPraise,
  133. (SELECT
  134. COUNT(1)
  135. FROM
  136. tede_comment
  137. WHERE
  138. ArticleId = a.Id and pid=0
  139. AND TypeValue = 1) AS CommentCount,
  140. (SELECT
  141. COUNT(1)
  142. FROM
  143. tede_analyze
  144. WHERE
  145. UserId = {request.UserId} AND TypeValue = {topicConts}
  146. AND SourceId = a.Id
  147. AND AnalyzeType = 4) AS RetransmissionCount,
  148. (SELECT
  149. COUNT(1)
  150. FROM
  151. tede_analyze
  152. WHERE
  153. UserId = {request.UserId} AND TypeValue = {topicConts}
  154. AND SourceId = a.Id
  155. AND AnalyzeType = 4
  156. LIMIT 0 , 1) AS IsRetransmission,
  157. (SELECT
  158. COUNT(1)
  159. FROM
  160. tede_analyze
  161. WHERE
  162. UserId = {request.UserId} AND TypeValue = {topicConts}
  163. AND SourceId = a.Id
  164. AND AnalyzeType = 4
  165. LIMIT 0 , 1) AS IsCollect,
  166. (SELECT
  167. COUNT(1)
  168. FROM
  169. tede_analyze
  170. WHERE
  171. UserId = {request.UserId} AND TypeValue = {topicConts}
  172. AND SourceId = a.Id
  173. AND AnalyzeType = 4) AS CollectCount,b.Name,b.AvatarUrl,c.Name FROM";
  174. 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}";
  175. var sql =
  176. $"{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}";
  177. var strValue = "";
  178. if (!string.IsNullOrEmpty(request.Key))
  179. strValue += $" and (a.Title like '%{request.Key}%' or a.TextContent like '%{request.Key}%' or b.Name like '%{request.Key}%')";
  180. sql += $"{strValue} order by a.CreatedDate DESC LIMIT @page , @pageSize";
  181. countSql += $"{strValue}";
  182. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  183. var database = new Database(databaseType, _connectionString);
  184. var connection = database.GetConnection();
  185. var result =
  186. connection
  187. .Query<TopicListPageResult, User, Department, TopicListPageResult>(sql,
  188. (topicListPageResult, user, department) =>
  189. {
  190. topicListPageResult.DepartmentName = department.Name;
  191. topicListPageResult.UserName = user.Name;
  192. topicListPageResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  193. return topicListPageResult;
  194. }, new { page = (request.Page - 1) * request.PerPage, pageSize = request.PerPage }, splitOn: "Name");
  195. resultData.Total = await connection.ExecuteScalarAsync<int>(countSql);
  196. var topicListPageResults = result as TopicListPageResult[] ?? result.ToArray();
  197. resultData.Items = topicListPageResults;
  198. return resultData;
  199. }
  200. /// <summary>
  201. ///最新 小组话题分页列表 匿名用户登录获取
  202. /// </summary>
  203. /// <param name="request"></param>
  204. /// <returns></returns>
  205. public async Task<PagedList<TopicListPageResult>> GetAnonymousGroupTopicPageAsync(TopicDetailListRequest request)
  206. {
  207. var topicConst = AllTypeConst.Topic.GetHashCode();
  208. var result = new PagedList<TopicListPageResult>();
  209. var sqlStr = @" AND a.GroupId IN (SELECT
  210. Id
  211. FROM
  212. tede_group
  213. WHERE
  214. IsShow = 1) ";
  215. sqlStr += $" and a.IsDraft={(request.IsDraft == true ? 1 : 0)}";
  216. if (!string.IsNullOrEmpty(request.Key))
  217. {
  218. sqlStr += $@"AND (a.Title LIKE '%{request.Key}%'
  219. OR a.TextContent LIKE '%{request.Key}%')";
  220. }
  221. string sql = $@"SELECT
  222. a.Id,
  223. a.Title,
  224. a.UserId,
  225. a.ReadCount,
  226. a.CreatedDate,
  227. a.Content,
  228. a.GroupId,
  229. (SELECT
  230. COUNT(1)
  231. FROM
  232. tede_analyze
  233. WHERE
  234. TypeValue ={topicConst} AND SourceId = a.Id
  235. AND AnalyzeType = 1) AS PraiseCount,
  236. (SELECT
  237. COUNT(1)
  238. FROM
  239. tede_analyze
  240. WHERE
  241. UserId ={request.UserId} AND TypeValue = {topicConst}
  242. AND SourceId = a.Id
  243. AND AnalyzeType = 1
  244. LIMIT 0 , 1) AS IsPraise,
  245. (SELECT
  246. COUNT(1)
  247. FROM
  248. tede_comment
  249. WHERE
  250. ArticleId = a.Id AND pid = 0
  251. AND TypeValue = 1) AS CommentCount,
  252. (SELECT
  253. COUNT(1)
  254. FROM
  255. tede_analyze
  256. WHERE
  257. UserId ={request.UserId} AND TypeValue ={topicConst}
  258. AND SourceId = a.Id
  259. AND AnalyzeType = 4) AS RetransmissionCount,
  260. (SELECT
  261. COUNT(1)
  262. FROM
  263. tede_analyze
  264. WHERE
  265. UserId ={request.UserId} AND TypeValue ={topicConst}
  266. AND SourceId = a.Id
  267. AND AnalyzeType = 4
  268. LIMIT 0 , 1) AS IsRetransmission,
  269. (SELECT
  270. COUNT(1)
  271. FROM
  272. tede_analyze
  273. WHERE
  274. UserId ={request.UserId} AND TypeValue = {topicConst}
  275. AND SourceId = a.Id
  276. AND AnalyzeType = 4
  277. LIMIT 0 , 1) AS IsCollect,
  278. (SELECT
  279. COUNT(1)
  280. FROM
  281. tede_analyze
  282. WHERE
  283. UserId ={request.UserId} AND TypeValue = {topicConst}
  284. AND SourceId = a.Id
  285. AND AnalyzeType = 4) AS CollectCount,
  286. b.Name as GroupName,
  287. c.Name as UserName,
  288. c.AvatarUrl,
  289. d.Id as DepartmentId,
  290. d.Name as DepartmentName
  291. FROM
  292. tede_topic a
  293. INNER JOIN
  294. tede_group b ON a.GroupId = b.Id
  295. INNER JOIN
  296. tede_user c ON c.Id = a.UserId
  297. INNER JOIN
  298. tede_department d ON d.Id = c.DepartmentId
  299. WHERE
  300. a.GroupId > 0 and a.IsDraft=0
  301. {sqlStr}
  302. ORDER BY a.CreatedDate DESC limit @page,@pageSize";
  303. string countSql = $@"SELECT
  304. count(1)
  305. FROM
  306. tede_topic a
  307. INNER JOIN
  308. tede_group b ON a.GroupId = b.Id
  309. INNER JOIN
  310. tede_user c ON c.Id = a.UserId
  311. WHERE
  312. a.GroupId > 0 and a.IsDraft=0
  313. {sqlStr}
  314. ORDER BY a.CreatedDate DESC";
  315. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  316. var database = new Database(databaseType, _connectionString);
  317. var connection = database.GetConnection();
  318. result.Items =
  319. await connection
  320. .QueryAsync<TopicListPageResult>(sql, new { page = (request.Page - 1) * request.PerPage, pageSize = request.PerPage });
  321. foreach (var item in result.Items)
  322. {
  323. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  324. }
  325. result.Total = await connection.ExecuteScalarAsync<int>(countSql);
  326. return result;
  327. }
  328. /// <summary>
  329. ///最新 小组话题分页列表
  330. /// </summary>
  331. /// <param name="request"></param>
  332. /// <returns></returns>
  333. public async Task<PagedList<TopicListPageResult>> GetGroupTopicPageAsync(TopicDetailListRequest request)
  334. {
  335. var topicConst = AllTypeConst.Topic.GetHashCode();
  336. var result = new PagedList<TopicListPageResult>();
  337. var sqlStr = $" and a.IsDraft={(request.IsDraft == true ? 1 : 0)}";
  338. if (request.IsDraft)
  339. sqlStr += $" and a.UserId={request.UserId}";
  340. if (request.GroupIds.Count() == 0)
  341. request.GroupIds.Add(-1);
  342. if (request.GroupIds.Count > 0)
  343. {
  344. var groupIds = new List<int>();
  345. foreach (var item in request.GroupIds)
  346. {
  347. if (item > 0)
  348. groupIds.Add(item);
  349. //全网
  350. if (item == -1)
  351. groupIds.AddRange(await GetPublicGroupAsync(request.UserId));
  352. //我的小组
  353. if (item == -2)
  354. groupIds.AddRange(await GetIntoGroupAsync(request.UserId));
  355. }
  356. var groupIdStr = string.Empty;
  357. foreach (var item in groupIds)
  358. groupIdStr += $"{item},";
  359. if (!string.IsNullOrEmpty(groupIdStr))
  360. {
  361. groupIdStr = groupIdStr.Remove(groupIdStr.Length - 1, 1);
  362. sqlStr += $@" AND a.GroupId IN ({groupIdStr})";
  363. }
  364. }
  365. if (!string.IsNullOrEmpty(request.Key))
  366. {
  367. sqlStr += $@"AND (a.Title LIKE '%{request.Key}%'
  368. OR a.TextContent LIKE '%{request.Key}%')";
  369. }
  370. string sql = $@"SELECT
  371. a.Id,
  372. a.Title,
  373. a.UserId,
  374. a.ReadCount,
  375. a.CreatedDate,
  376. a.Content,
  377. a.GroupId,
  378. (SELECT
  379. COUNT(1)
  380. FROM
  381. tede_analyze
  382. WHERE
  383. TypeValue ={topicConst} AND SourceId = a.Id
  384. AND AnalyzeType = 1) AS PraiseCount,
  385. (SELECT
  386. COUNT(1)
  387. FROM
  388. tede_analyze
  389. WHERE
  390. UserId ={request.UserId} AND TypeValue = {topicConst}
  391. AND SourceId = a.Id
  392. AND AnalyzeType = 1
  393. LIMIT 0 , 1) AS IsPraise,
  394. (SELECT
  395. COUNT(1)
  396. FROM
  397. tede_comment
  398. WHERE
  399. ArticleId = a.Id AND pid = 0
  400. AND TypeValue={topicConst}) AS CommentCount,
  401. (SELECT
  402. COUNT(1)
  403. FROM
  404. tede_analyze
  405. WHERE
  406. UserId ={request.UserId} AND TypeValue ={topicConst}
  407. AND SourceId = a.Id
  408. AND AnalyzeType = 4) AS RetransmissionCount,
  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
  417. LIMIT 0 , 1) AS IsRetransmission,
  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 IsCollect,
  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) AS CollectCount,
  435. b.Name as GroupName,
  436. c.Name as UserName,
  437. c.AvatarUrl,
  438. (SELECT
  439. Name
  440. FROM
  441. tede_department
  442. WHERE
  443. Id IN (SELECT
  444. DepartmentId
  445. FROM
  446. tede_department_user
  447. WHERE
  448. userId = a.UserId)
  449. LIMIT 1) as DepartmentName
  450. FROM
  451. tede_topic a
  452. inner JOIN
  453. tede_group b ON a.GroupId = b.Id
  454. inner JOIN
  455. tede_user c ON c.Id = a.UserId
  456. WHERE
  457. a.GroupId > 0
  458. {sqlStr}
  459. ORDER BY a.CreatedDate DESC limit {(request.Page - 1) * request.PerPage},{request.Page * request.PerPage}";
  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. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  472. var database = new Database(databaseType, _connectionString);
  473. var connection = database.GetConnection();
  474. result.Items = await connection.QueryAsync<TopicListPageResult>(sql);
  475. foreach (var item in result.Items)
  476. {
  477. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  478. }
  479. result.Total = await connection.ExecuteScalarAsync<int>(countSql);
  480. //回去小组草稿数量
  481. var query = Q.NewQuery();
  482. query.WhereIn(nameof(Entity.Topic.Topic.GroupId), request.GroupIds);
  483. query.Where(nameof(Entity.Topic.Topic.IsDraft), true);
  484. query.Where(nameof(Entity.Topic.Topic.UserId), request.UserId);
  485. result.DraftCount = await CountAsync(query);
  486. return result;
  487. }
  488. /// <summary>
  489. /// 获取共享小组
  490. /// </summary>
  491. /// <returns></returns>
  492. public async Task<IEnumerable<int>> GetPublicGroupAsync(int userId)
  493. {
  494. string sql = $@"select Id from tede_group where (IsShow=1 or id in (select groupId FROM tede_group_user where userId={userId}))";
  495. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  496. var database = new Database(databaseType, _connectionString);
  497. var connection = database.GetConnection();
  498. var result = await connection.QueryAsync<int>(sql);
  499. return result;
  500. }
  501. /// <summary>
  502. /// 我加入的小组
  503. /// </summary>
  504. /// <param name="userId"></param>
  505. /// <returns></returns>
  506. public async Task<IEnumerable<int>> GetIntoGroupAsync(int userId)
  507. {
  508. string sql = $"select a.Id from tede_group a inner join tede_group_user b on a.Id=b.GroupId where b.UserId={userId}";
  509. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  510. var database = new Database(databaseType, _connectionString);
  511. var connection = database.GetConnection();
  512. return await connection.QueryAsync<int>(sql);
  513. }
  514. /// <summary>
  515. /// 根据小组ID获取话题数量
  516. /// </summary>
  517. /// <param name="groupId"></param>
  518. /// <returns></returns>
  519. public async Task<int> GetGroupTopicCountAsync(int groupId)
  520. {
  521. return await _repository.CountAsync(Q.Where(nameof(Entity.Topic.Topic.GroupId), groupId));
  522. }
  523. /// <summary>
  524. /// 根据ID获取话题
  525. /// </summary>
  526. /// <param name="id"></param>
  527. /// <returns></returns>
  528. public async Task<Entity.Topic.Topic> GetAsync(int id)
  529. {
  530. return await _repository.GetAsync(id);
  531. }
  532. public async Task<int> CountAsync(Query query)
  533. {
  534. return await _repository.CountAsync(query);
  535. }
  536. public async Task<IEnumerable<Entity.Topic.Topic>> GetAllAsync(Query query)
  537. {
  538. return await _repository.GetAllAsync(query);
  539. }
  540. public async Task<int> InsertAsync(Entity.Topic.Topic topic)
  541. {
  542. return await _repository.InsertAsync(topic);
  543. }
  544. public async Task<bool> DeleteAsync(int id)
  545. {
  546. return await _repository.DeleteAsync(id);
  547. }
  548. public async Task<bool> UpdateAsync(Entity.Topic.Topic topic)
  549. {
  550. return await _repository.UpdateAsync(topic);
  551. }
  552. /// <summary>
  553. /// 执行话题
  554. /// </summary>
  555. /// <returns></returns>
  556. public async Task<List<JobTopicResult>> ExecuteTopic()
  557. {
  558. string sql = @"select a.*,b.UserId,b.Id,c.Name,d.AvatarUrl from tede_topic a
  559. inner join tede_topic_addressee b on a.Id=b.TopicId
  560. inner join tede_user c on c.Id=b.UserId
  561. inner join tede_user d on d.Id=a.UserId
  562. where b.IsUpload=0
  563. order by a.CreatedDate desc limit 0,100";
  564. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  565. var database = new Database(databaseType, _connectionString);
  566. var connection = database.GetConnection();
  567. var result = await connection.QueryAsync<JobTopicResult, TopicAddressee, User, User, JobTopicResult>(sql, (jobTopicResult, topicAddressee, user, userDto) =>
  568. {
  569. jobTopicResult.UserId = topicAddressee.UserId;
  570. jobTopicResult.Name = user.Name;
  571. jobTopicResult.AvatarUrl = userDto.AvatarUrl;
  572. jobTopicResult.TopicAddresseeId = topicAddressee.Id;
  573. return jobTopicResult;
  574. }, splitOn: "UserId,Name,AvatarUrl");
  575. connection.Dispose();
  576. return result.ToList();
  577. }
  578. /// <summary>
  579. /// 新版 获取笔记话题列表
  580. /// </summary>
  581. /// <param name="request"></param>
  582. /// <returns></returns>
  583. public async Task<PagedList<TopicListPageResult>> GetTopicPage(TopicPageSearchRequest request, string sql, string countSql)
  584. {
  585. var result = new PagedList<TopicListPageResult>();
  586. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  587. var database = new Database(databaseType, _connectionString);
  588. var connection = database.GetConnection();
  589. result.Items = await connection.QueryAsync<TopicListPageResult>(sql);
  590. result.Total = await connection.ExecuteScalarAsync<int>(countSql);
  591. connection.Dispose();
  592. foreach (var item in result.Items)
  593. {
  594. item.FolderResult.FolderName = item.FolderName;
  595. item.FolderResult.Id = item.ParentId;
  596. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  597. }
  598. return result;
  599. }
  600. public async Task<PagedList<TopicListPageResult>> GetTopicByGroupAsync(TopicPageSearchRequest request)
  601. {
  602. var topicConstValue = AllTypeConst.Topic.GetHashCode();
  603. var sql = $@"SELECT
  604. a.*,
  605. (SELECT
  606. COUNT(1)
  607. FROM
  608. tede_analyze
  609. WHERE
  610. TypeValue ={topicConstValue} AND SourceId = a.Id
  611. AND AnalyzeType = 1) AS PraiseCount,
  612. (SELECT
  613. COUNT(1)
  614. FROM
  615. tede_analyze
  616. WHERE
  617. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  618. AND SourceId = a.Id
  619. AND AnalyzeType = 1
  620. LIMIT 0 , 1) AS IsPraise,
  621. (SELECT
  622. COUNT(1)
  623. FROM
  624. tede_comment
  625. WHERE
  626. ArticleId = a.Id AND TypeValue = {topicConstValue}
  627. AND Pid = 0) AS CommentCount,
  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) AS RetransmissionCount,
  636. (SELECT
  637. COUNT(1)
  638. FROM
  639. tede_analyze
  640. WHERE
  641. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  642. AND SourceId = a.Id
  643. AND AnalyzeType = 4
  644. LIMIT 0 , 1) AS IsRetransmission,
  645. (SELECT
  646. COUNT(1)
  647. FROM
  648. tede_analyze
  649. WHERE
  650. UserId = {request.UserId} AND TypeValue = {topicConstValue}
  651. AND SourceId = a.Id
  652. AND AnalyzeType = 3
  653. LIMIT 0 , 1) AS IsCollect,
  654. b.Id as GroupId,
  655. b.Name,
  656. c.Name,
  657. c.AvatarUrl
  658. FROM
  659. tede_topic a
  660. INNER JOIN
  661. tede_group b ON a.GroupId = b.id
  662. INNER JOIN
  663. tede_user c ON a.UserId = c.Id
  664. WHERE 1=1 ";
  665. if (request.TopicGroupIds.Count > 0)
  666. {
  667. var groupStr = "(";
  668. foreach (var item in request.TopicGroupIds)
  669. {
  670. groupStr += $"{item},";
  671. }
  672. groupStr = groupStr.Remove(groupStr.Length - 1, 1);
  673. groupStr += ")";
  674. sql += $@" and a.GroupId IN {groupStr}";
  675. }
  676. else
  677. sql += $@" and a.GroupId > 0";
  678. if (request.IsAdmin)
  679. {
  680. sql += $@" AND a.UserId={request.UserId}";
  681. }
  682. else
  683. {
  684. sql += $@" AND b.Id IN (SELECT
  685. GroupId
  686. FROM
  687. tede_group_user
  688. WHERE
  689. UserId = {request.UserId})";
  690. }
  691. if (!string.IsNullOrWhiteSpace(request.Keyword))
  692. {
  693. sql += $@" AND (c.Name LIKE '%{request.Keyword}%'
  694. OR a.Title LIKE '%{request.Keyword}%'
  695. OR a.TextContent LIKE '%{request.Keyword}%')";
  696. }
  697. if (!string.IsNullOrEmpty(request.Sort))
  698. sql += $@" ORDER BY a.ReadCount DESC";
  699. else
  700. sql += $@" ORDER BY a.CreatedDate DESC";
  701. sql += $@" LIMIT {(request.Page - 1) * request.PerPage} , {request.PerPage}";
  702. var result = new PagedList<TopicListPageResult>();
  703. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  704. var database = new Database(databaseType, _connectionString);
  705. var connection = database.GetConnection();
  706. result.Items = await connection.QueryAsync<TopicListPageResult, Entity.Group, User, TopicListPageResult>(sql, (topicListPageResult, group, user) =>
  707. {
  708. topicListPageResult.AvatarUrl = user != null ? StringUtils.AddDomainMin(user.AvatarUrl) : "";
  709. topicListPageResult.GroupName = group != null ? group.Name : "";
  710. topicListPageResult.UserName = user != null ? user.Name : "";
  711. return topicListPageResult;
  712. }, splitOn: "Id,GroupId,Name");
  713. sql = $@"SELECT
  714. count(1)
  715. FROM
  716. tede_topic a
  717. INNER JOIN
  718. tede_group b ON a.GroupId = b.id
  719. INNER JOIN
  720. tede_user c ON a.UserId = c.Id
  721. WHERE 1=1 ";
  722. if (request.TopicGroupIds.Count > 0)
  723. {
  724. var groupStr = "(";
  725. foreach (var item in request.TopicGroupIds)
  726. {
  727. groupStr += $"{item},";
  728. }
  729. groupStr = groupStr.Remove(groupStr.Length - 1, 1);
  730. groupStr += ")";
  731. sql += $@" and a.GroupId IN {groupStr}";
  732. }
  733. else
  734. sql += $@" and a.GroupId > 0";
  735. if (request.IsAdmin)
  736. {
  737. sql += $@" AND a.UserId={request.UserId}";
  738. }
  739. else
  740. {
  741. sql += $@" AND b.Id IN (SELECT
  742. GroupId
  743. FROM
  744. tede_group_user
  745. WHERE
  746. UserId = {request.UserId})";
  747. }
  748. if (!string.IsNullOrWhiteSpace(request.Keyword))
  749. {
  750. sql += $@" AND (c.Name LIKE '%{request.Keyword}%'
  751. OR a.Title LIKE '%{request.Keyword}%'
  752. OR a.TextContent LIKE '%{request.Keyword}%')";
  753. }
  754. result.Total = await connection.ExecuteScalarAsync<int>(sql);
  755. return result;
  756. }
  757. /// <summary>
  758. /// 删除草稿箱
  759. /// </summary>
  760. /// <param name="userId"></param>
  761. /// <param name="groupId"></param>
  762. /// <returns></returns>
  763. public async Task<bool> ClearDraftAsync(int userId, int groupId)
  764. {
  765. var query = Q.NewQuery();
  766. query.Where(nameof(Entity.Topic.Topic.UserId), userId);
  767. query.Where(nameof(Entity.Topic.Topic.IsDraft), true);
  768. query.Where(nameof(Entity.Topic.Topic.GroupId), groupId);
  769. return await _repository.DeleteAsync(query) > 0;
  770. }
  771. public async Task<bool> UpdateTopicTextContentAsync()
  772. {
  773. var query = Q.NewQuery();
  774. query.Where(nameof(Entity.Topic.Topic.TextContent), "=", "");
  775. query.OrderByDesc(nameof(Entity.Topic.Topic.CreatedDate));
  776. query.Limit(100);
  777. var result = await _repository.GetAllAsync(query);
  778. foreach (var item in result)
  779. {
  780. if (!string.IsNullOrEmpty(item.Content))
  781. {
  782. var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(item.Content);
  783. foreach (var data in contentJsonData)
  784. {
  785. if (data.Type == AllTypeConst.Text.GetHashCode())
  786. item.TextContent += data.Text;
  787. }
  788. if (!string.IsNullOrEmpty(item.TextContent))
  789. await _repository.UpdateAsync(item);
  790. else
  791. {
  792. item.TextContent = "[]";
  793. await _repository.UpdateAsync(item);
  794. }
  795. }
  796. }
  797. return true;
  798. }
  799. }
  800. }