TopicRepository.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. ccpph.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. ccpph.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. ccpph.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. ccpph.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. ccpph.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. ccpph.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. ccpph.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 ccpph.tede_topic a inner join ccpph.tede_user b on a.UserId=b.Id inner join ccpph.tede_Department c on c.Id=b.DepartmentId where a.GroupId={request.GroupId}";
  172. var sql =
  173. $"{showKey} ccpph.tede_topic a inner join ccpph.tede_user b on a.UserId=b.Id inner join ccpph.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 ccpph.tede_topic a inner join ccpph.tede_topic_addressee b on a.Id=b.TopicId inner join ccpph.tede_user c on c.Id=b.UserId inner join ccpph.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 string AssembleSql(TopicPageSearchRequest request)
  280. {
  281. var sql = $@"SELECT
  282. a.*,
  283. (SELECT
  284. FolderName
  285. FROM
  286. tede_middle
  287. WHERE
  288. id = a.FolderId) AS FolderName,
  289. (SELECT
  290. COUNT(1)
  291. FROM
  292. ccpph.tede_analyze
  293. WHERE
  294. TypeValue = 3
  295. AND SourceId = a.Id
  296. AND AnalyzeType = 1) AS PraiseCount,
  297. (SELECT
  298. COUNT(1)
  299. FROM
  300. ccpph.tede_analyze
  301. WHERE
  302. UserId = {request.UserId} AND TypeValue = 3
  303. AND SourceId = a.Id
  304. AND AnalyzeType = 1
  305. LIMIT 0 , 1) AS IsPraise,
  306. (SELECT
  307. COUNT(1)
  308. FROM
  309. ccpph.tede_comment
  310. WHERE
  311. ArticleId = a.Id AND TypeValue = 3
  312. AND Pid = 0) AS CommentCount,
  313. (SELECT
  314. COUNT(1)
  315. FROM
  316. ccpph.tede_analyze
  317. WHERE
  318. UserId = {request.UserId} AND TypeValue = 3
  319. AND SourceId = a.Id
  320. AND AnalyzeType = 4) AS RetransmissionCount,
  321. (SELECT
  322. COUNT(1)
  323. FROM
  324. ccpph.tede_analyze
  325. WHERE
  326. UserId = {request.UserId} AND TypeValue = 3
  327. AND SourceId = a.Id
  328. AND AnalyzeType = 4
  329. LIMIT 0 , 1) AS IsRetransmission,
  330. (SELECT
  331. COUNT(1)
  332. FROM
  333. ccpph.tede_analyze
  334. WHERE
  335. UserId = {request.UserId} AND TypeValue = 3
  336. AND SourceId = a.Id
  337. AND AnalyzeType = 3
  338. LIMIT 0 , 1) AS IsCollect,
  339. b.Name, b.AvatarUrl
  340. FROM
  341. ccpph.tede_note a
  342. INNER JOIN
  343. ccpph.tede_user b ON a.UserId = b.Id
  344. INNER JOIN
  345. ccpph.tede_middle c ON c.MiddleId = a.Id
  346. WHERE
  347. c.FolderType = 4 AND a.IsTopic = 1
  348. AND (a.FolderId IN (SELECT
  349. MiddleId
  350. FROM
  351. tede_folder_user
  352. WHERE
  353. MiddleId = a.FolderId AND UserId = {request.UserId})
  354. OR a.UserId IN (SELECT
  355. Id
  356. FROM
  357. tede_user
  358. WHERE
  359. 1 = (SELECT
  360. RoleId
  361. FROM
  362. tede_middle
  363. WHERE
  364. id = a.FolderId)
  365. AND id = {request.UserId}))";
  366. if (request.TopicGroupIds.Count > 0)
  367. {
  368. var topicGroupId = "";
  369. foreach (var item in request.TopicGroupIds)
  370. {
  371. topicGroupId += $"{item},";
  372. }
  373. topicGroupId = topicGroupId.Remove(topicGroupId.Length - 1, 1);
  374. sql += $@" AND a.UserId IN (SELECT
  375. UserId
  376. FROM
  377. tede_topic_group_user
  378. WHERE
  379. TopicGroupId IN ({topicGroupId}))";
  380. }
  381. if (!string.IsNullOrWhiteSpace(request.Keyword))
  382. {
  383. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  384. OR a.Title LIKE '%{request.Keyword}%'
  385. OR a.HtmlContent LIKE '%{request.Keyword}%')";
  386. }
  387. sql += $@" ORDER BY a.CreatedDate DESC
  388. LIMIT {(request.Page - 1) * request.PerPage} , {request.PerPage}";
  389. return sql;
  390. }
  391. public string AssembleSqlCount(TopicPageSearchRequest request)
  392. {
  393. var sql = $@"SELECT count(1) FROM
  394. ccpph.tede_note a
  395. INNER JOIN
  396. ccpph.tede_user b ON a.UserId = b.Id
  397. INNER JOIN
  398. ccpph.tede_middle c ON c.MiddleId = a.Id
  399. WHERE
  400. c.FolderType = 4 AND a.IsTopic = 1
  401. AND (a.FolderId IN (SELECT
  402. MiddleId
  403. FROM
  404. tede_folder_user
  405. WHERE
  406. MiddleId = a.FolderId AND UserId = {request.UserId})
  407. OR a.UserId IN (SELECT
  408. Id
  409. FROM
  410. tede_user
  411. WHERE
  412. 1 = (SELECT
  413. RoleId
  414. FROM
  415. tede_middle
  416. WHERE
  417. id = a.FolderId)
  418. AND id = {request.UserId}))";
  419. if (request.TopicGroupIds.Count > 0)
  420. {
  421. var topicGroupId = "";
  422. foreach (var item in request.TopicGroupIds)
  423. {
  424. topicGroupId += $"{item},";
  425. }
  426. topicGroupId = topicGroupId.Remove(topicGroupId.Length - 1, 1);
  427. sql += $@" AND a.UserId IN (SELECT
  428. UserId
  429. FROM
  430. tede_topic_group_user
  431. WHERE
  432. TopicGroupId IN ({topicGroupId}))";
  433. }
  434. if (!string.IsNullOrWhiteSpace(request.Keyword))
  435. {
  436. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  437. OR a.Title LIKE '%{request.Keyword}%'
  438. OR a.HtmlContent LIKE '%{request.Keyword}%')";
  439. }
  440. return sql;
  441. }
  442. }
  443. }