TopicRepository.cs 38 KB

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