ReplyService.Praise.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using Dapper;
  4. using Datory;
  5. using GxPress.Common.Tools;
  6. using GxPress.EnumConst;
  7. using GxPress.Request.Reply;
  8. using GxPress.Result.Reply;
  9. using System.Transactions;
  10. using System.Linq;
  11. namespace GxPress.Service.Implement.Reply
  12. {
  13. /// <summary>
  14. /// 点赞
  15. /// </summary>
  16. public partial class ReplyService
  17. {
  18. /// <summary>
  19. /// 获取话题本赞
  20. /// </summary>
  21. /// <param name="request"></param>
  22. /// <returns></returns>
  23. public async Task<IEnumerable<ReplyResult>> GetNotePraiseAsync(ReplyRequest request)
  24. {
  25. var topicNoteConstValue = AllTypeConst.TopicNote.GetHashCode();
  26. string sqlStr = string.Empty;
  27. if (!string.IsNullOrEmpty(request.KeyWord))
  28. {
  29. sqlStr = $@" AND (
  30. b.Title LIKE '%{request.KeyWord}%'
  31. OR b.Content LIKE '%{request.KeyWord}%' or c.Name like '%{request.KeyWord}%')";
  32. }
  33. string sql = $@"
  34. SELECT
  35. a.Id,
  36. a.SourceId,
  37. a.TypeValue,
  38. a.IsRead,
  39. b.Title,
  40. b.Content,
  41. c.Name as UserName,
  42. c.AvatarUrl,
  43. b.CreatedDate,
  44. a.UserId,
  45. b.IsTopic,
  46. d.Name
  47. FROM
  48. tede_analyze a
  49. INNER JOIN
  50. tede_note b ON a.SourceId = b.Id
  51. INNER JOIN
  52. tede_user c ON c.Id = a.UserId
  53. INNER JOIN
  54. tede_user d ON d.Id = b.UserId
  55. INNER JOIN
  56. tede_middle e ON e.MiddleId = b.Id
  57. WHERE
  58. a.TypeValue ={topicNoteConstValue} AND e.IsDelete = 0
  59. AND e.FolderType ={topicNoteConstValue}
  60. AND a.CommentId = 0
  61. AND e.UserId = b.UserId
  62. AND a.AnalyzeType = 1
  63. AND b.UserId ={request.UserId}
  64. And b.IsTopic=1
  65. {sqlStr}
  66. ORDER BY a.CreatedDate DESC
  67. ";
  68. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  69. var connection = database.GetConnection();
  70. var result = await connection.QueryAsync<ReplyResult>(sql);
  71. foreach (var item in result)
  72. {
  73. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  74. item.CommentContent = "点赞了你的" + ((AllTypeConst)item.TypeValue).GetDescriptionOriginal();
  75. item.Title = string.IsNullOrEmpty(item.Title) ? GetTitleText(item.Content) : item.Title;
  76. item.Remark = ((AllTypeConst)item.TypeValue).GetDescriptionOriginal();
  77. item.Content = string.Empty;
  78. }
  79. return result;
  80. }
  81. /// <summary>
  82. /// 获取评论点赞
  83. /// </summary>
  84. /// <param name="request"></param>
  85. /// <returns></returns>
  86. public async Task<IEnumerable<ReplyResult>> GetCommentPraiseAsync(ReplyRequest request)
  87. {
  88. var sqlStr = string.Empty;
  89. if (!string.IsNullOrEmpty(request.KeyWord))
  90. {
  91. sqlStr = $@" AND (a.Content LIKE '%{request.KeyWord}%'
  92. OR c.Name LIKE '%{request.KeyWord}%')";
  93. }
  94. string sql = $@"
  95. SELECT
  96. a.Id,
  97. a.Content,
  98. a.TypeValue,
  99. b.IsRead,
  100. b.UserId,
  101. c.Name AS UserName,
  102. c.AvatarUrl,
  103. c.Id AS UserId,
  104. d.Name,
  105. b.CreatedDate
  106. FROM
  107. tede_comment a
  108. INNER JOIN
  109. tede_analyze b ON a.Id = b.CommentId
  110. INNER JOIN
  111. tede_user c ON c.Id = b.UserId
  112. INNER JOIN
  113. tede_user d ON d.Id = a.UserId
  114. WHERE
  115. a.TypeValue IN ({AllTypeConst.Topic.GetHashCode()},{AllTypeConst.Notice.GetHashCode()},{AllTypeConst.Note.GetHashCode()})
  116. AND a.UserId = {request.UserId}
  117. {sqlStr}
  118. AND b.AnalyzeType = 2
  119. ORDER BY a.CreatedDate DESC";
  120. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  121. var connection = database.GetConnection();
  122. var result = await connection.QueryAsync<ReplyResult>(sql);
  123. foreach (var item in result)
  124. {
  125. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  126. item.CommentContent = "点赞了你的" + ((AllTypeConst)item.TypeValue).GetDescriptionOriginal();
  127. item.Title = item.Content;
  128. item.IsComment = true;
  129. item.Remark = ((AllTypeConst)item.TypeValue).GetDescriptionOriginal();
  130. }
  131. return result;
  132. }
  133. /// <summary>
  134. /// 获取通知点赞
  135. /// </summary>
  136. /// <param name="request"></param>
  137. /// <returns></returns>
  138. public async Task<IEnumerable<ReplyResult>> GetNoticePraiseAsync(ReplyRequest request)
  139. {
  140. var sqlStr = string.Empty;
  141. if (!string.IsNullOrEmpty(request.KeyWord))
  142. {
  143. sqlStr = $@" AND (b.Title LIKE '%{request.KeyWord}%'
  144. OR b.Content LIKE '%{request.KeyWord}%'
  145. OR c.Name LIKE '%{request.KeyWord}%')";
  146. }
  147. string sql = $@"
  148. SELECT
  149. a.Id,
  150. a.SourceId,
  151. a.TypeValue,
  152. a.IsRead,
  153. b.Title,
  154. b.Content,
  155. c.Name,
  156. c.AvatarUrl,
  157. b.CreatedDate,
  158. a.UserId,
  159. d.Name AS UserName
  160. FROM
  161. tede_analyze a
  162. INNER JOIN
  163. tede_notice b ON a.SourceId = b.Id
  164. INNER JOIN
  165. tede_user c ON c.Id = a.UserId
  166. INNER JOIN
  167. tede_user d ON d.Id = b.UserId
  168. INNER JOIN
  169. tede_middle e ON e.MiddleId = b.Id
  170. WHERE
  171. b.UserId = {request.UserId} AND e.UserId = b.UserId
  172. AND a.TypeValue ={AllTypeConst.Notice.GetHashCode()}
  173. AND e.IsDelete = 0
  174. AND a.CommentId = 0
  175. AND a.AnalyzeType = 1
  176. {sqlStr}
  177. ORDER BY a.CreatedDate DESC";
  178. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  179. var connection = database.GetConnection();
  180. var result = await connection.QueryAsync<ReplyResult>(sql);
  181. foreach (var item in result)
  182. {
  183. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  184. item.CommentContent = "点赞了你的" + ((AllTypeConst)item.TypeValue).GetDescriptionOriginal();
  185. item.Title = string.IsNullOrEmpty(item.Title) ? GetTitleText(item.Content) : item.Title;
  186. item.Remark = ((AllTypeConst)item.TypeValue).GetDescriptionOriginal();
  187. item.Content = string.Empty;
  188. }
  189. return result;
  190. }
  191. /// <summary>
  192. /// 获取话题点赞
  193. /// </summary>
  194. /// <param name="request"></param>
  195. /// <returns></returns>
  196. public async Task<IEnumerable<ReplyResult>> GetTopicPraiseAsync(ReplyRequest request)
  197. {
  198. var sqlStr = string.Empty;
  199. if (!string.IsNullOrEmpty(request.KeyWord))
  200. {
  201. sqlStr = $@" AND ( b.Title LIKE '%{request.KeyWord}%'
  202. OR b.Content LIKE '%{request.KeyWord}%'
  203. OR c.Name LIKE '%{request.KeyWord}%')";
  204. }
  205. string sql = $@"
  206. SELECT
  207. a.Id,
  208. a.SourceId,
  209. a.TypeValue,
  210. a.IsRead,
  211. b.Title,
  212. b.Content,
  213. c.Name as UserName,
  214. c.AvatarUrl,
  215. b.CreatedDate,
  216. a.UserId,
  217. d.Name,
  218. g.Id as GroupId,
  219. g.Name as GroupName
  220. FROM
  221. tede_analyze a
  222. INNER JOIN
  223. tede_topic b ON a.SourceId = b.Id
  224. INNER JOIN
  225. tede_user c ON c.Id = a.UserId
  226. INNER JOIN
  227. tede_user d ON d.Id = b.UserId
  228. INNER JOIN
  229. tede_middle e ON e.MiddleId = b.Id
  230. INNER JOIN
  231. tede_group g ON g.Id = b.GroupId
  232. WHERE
  233. a.TypeValue ={AllTypeConst.Topic.GetHashCode()} AND e.IsDelete = 0
  234. AND a.CommentId = 0
  235. AND e.UserId = b.UserId
  236. AND a.AnalyzeType = 1
  237. AND b.UserId = {request.UserId}
  238. {sqlStr}
  239. ORDER BY a.CreatedDate DESC";
  240. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  241. var connection = database.GetConnection();
  242. var result = await connection.QueryAsync<ReplyResult>(sql);
  243. foreach (var item in result)
  244. {
  245. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  246. item.CommentContent = "点赞了你的" + ((AllTypeConst)item.TypeValue).GetDescriptionOriginal();
  247. item.Title = string.IsNullOrEmpty(item.Title) ? GetTitleText(item.Content) : item.Title;
  248. item.Remark = ((AllTypeConst)item.TypeValue).GetDescriptionOriginal();
  249. item.IsGroup = true;
  250. item.Content = string.Empty;
  251. }
  252. return result;
  253. }
  254. /// <summary>
  255. /// 未读点赞数量
  256. /// </summary>
  257. /// <param name="userId"></param>
  258. /// <returns></returns>
  259. public async Task<int> GetUReadPraiseCountAsync(int userId)
  260. {
  261. var sql = $@" SELECT
  262. COUNT(1)
  263. FROM
  264. tede_analyze a
  265. INNER JOIN
  266. tede_note b ON a.SourceId = b.Id
  267. INNER JOIN
  268. tede_user c ON c.Id = a.UserId
  269. INNER JOIN
  270. tede_user d ON d.Id = b.UserId
  271. INNER JOIN
  272. tede_middle e ON e.MiddleId = b.Id
  273. WHERE
  274. a.TypeValue = 4 AND e.IsDelete = 0
  275. AND a.CommentId = 0
  276. AND e.UserId = b.UserId
  277. AND a.AnalyzeType = 1
  278. AND b.UserId = {userId}
  279. AND a.IsRead = 0";
  280. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  281. var connection = database.GetConnection();
  282. var count = await connection.ExecuteScalarAsync<int>(sql);
  283. sql = $@" SELECT
  284. count(1)
  285. FROM
  286. tede_comment a
  287. INNER JOIN
  288. tede_analyze b ON a.Id = b.CommentId
  289. INNER JOIN
  290. tede_user c ON c.Id = b.UserId
  291. INNER JOIN
  292. tede_user d ON d.Id = a.UserId
  293. WHERE
  294. a.TypeValue IN ({AllTypeConst.Topic.GetHashCode()},{AllTypeConst.Notice.GetHashCode()},{AllTypeConst.Note.GetHashCode()})
  295. AND a.UserId ={userId}
  296. AND b.AnalyzeType = 2
  297. AND b.IsRead = 0";
  298. count += await connection.ExecuteScalarAsync<int>(sql);
  299. sql = $@" SELECT
  300. COUNT(1)
  301. FROM
  302. tede_analyze a
  303. INNER JOIN
  304. tede_notice b ON a.SourceId = b.Id
  305. INNER JOIN
  306. tede_user c ON c.Id = a.UserId
  307. INNER JOIN
  308. tede_user d ON d.Id = b.UserId
  309. INNER JOIN
  310. tede_middle e ON e.MiddleId = b.Id
  311. WHERE
  312. b.UserId ={userId} AND e.UserId = b.UserId
  313. AND a.TypeValue ={AllTypeConst.Notice.GetHashCode()}
  314. AND e.IsDelete = 0
  315. AND a.CommentId = 0
  316. AND a.AnalyzeType = 1";
  317. count += await connection.ExecuteScalarAsync<int>(sql);
  318. sql = $@" SELECT
  319. count(1)
  320. FROM
  321. tede_analyze a
  322. INNER JOIN
  323. tede_topic b ON a.SourceId = b.Id
  324. INNER JOIN
  325. tede_user c ON c.Id = a.UserId
  326. INNER JOIN
  327. tede_user d ON d.Id = b.UserId
  328. INNER JOIN
  329. tede_middle e ON e.MiddleId = b.Id
  330. INNER JOIN
  331. tede_group g ON g.Id = b.GroupId
  332. WHERE
  333. a.TypeValue = {AllTypeConst.Topic.GetHashCode()} AND e.IsDelete = 0
  334. AND a.CommentId = 0
  335. AND e.UserId = b.UserId
  336. AND a.AnalyzeType = 1
  337. AND b.UserId ={userId}";
  338. count += await connection.ExecuteScalarAsync<int>(sql);
  339. return count;
  340. }
  341. /// <summary>
  342. /// 修改未读点赞数量
  343. /// </summary>
  344. /// <param name="userId"></param>
  345. /// <returns></returns>
  346. public async Task<bool> UpdateUReadPraiseAsync(int userId)
  347. {
  348. try
  349. {
  350. using (var tran = new TransactionScope())
  351. {
  352. var intList = new List<int>();
  353. var sql = $@"SELECT
  354. a.Id
  355. FROM
  356. tede_analyze a
  357. INNER JOIN
  358. tede_note b ON a.SourceId = b.Id
  359. INNER JOIN
  360. tede_user c ON c.Id = a.UserId
  361. INNER JOIN
  362. tede_user d ON d.Id = b.UserId
  363. INNER JOIN
  364. tede_middle e ON e.MiddleId = b.Id
  365. WHERE
  366. a.TypeValue = 4 AND e.IsDelete = 0
  367. AND a.CommentId = 0
  368. AND e.UserId = b.UserId
  369. AND a.AnalyzeType = 1
  370. AND b.UserId = {userId}
  371. AND a.IsRead = 0";
  372. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  373. var connection = database.GetConnection();
  374. intList.AddRange(await connection.QueryAsync<int>(sql));
  375. sql = $@"
  376. SELECT
  377. b.Id
  378. FROM
  379. tede_comment a
  380. INNER JOIN
  381. tede_analyze b ON a.Id = b.CommentId
  382. INNER JOIN
  383. tede_user c ON c.Id = b.UserId
  384. INNER JOIN
  385. tede_user d ON d.Id = a.UserId
  386. WHERE
  387. a.TypeValue IN ({AllTypeConst.Topic.GetHashCode()},{AllTypeConst.Notice.GetHashCode()},{AllTypeConst.Note.GetHashCode()})
  388. AND a.UserId ={userId}
  389. AND b.AnalyzeType = 2
  390. AND b.IsRead = 0";
  391. intList.AddRange(await connection.QueryAsync<int>(sql));
  392. sql = $@"
  393. SELECT
  394. a.Id
  395. FROM
  396. tede_analyze a
  397. INNER JOIN
  398. tede_notice b ON a.SourceId = b.Id
  399. INNER JOIN
  400. tede_user c ON c.Id = a.UserId
  401. INNER JOIN
  402. tede_user d ON d.Id = b.UserId
  403. INNER JOIN
  404. tede_middle e ON e.MiddleId = b.Id
  405. WHERE
  406. b.UserId ={userId} AND e.UserId = b.UserId
  407. AND a.TypeValue ={AllTypeConst.Notice.GetHashCode()}
  408. AND e.IsDelete = 0
  409. AND a.CommentId = 0
  410. AND a.AnalyzeType = 1";
  411. intList.AddRange(await connection.QueryAsync<int>(sql));
  412. sql = $@"
  413. SELECT
  414. a.Id
  415. FROM
  416. tede_analyze a
  417. INNER JOIN
  418. tede_topic b ON a.SourceId = b.Id
  419. INNER JOIN
  420. tede_user c ON c.Id = a.UserId
  421. INNER JOIN
  422. tede_user d ON d.Id = b.UserId
  423. INNER JOIN
  424. tede_middle e ON e.MiddleId = b.Id
  425. INNER JOIN
  426. tede_group g ON g.Id = b.GroupId
  427. WHERE
  428. a.TypeValue = {AllTypeConst.Topic.GetHashCode()} AND e.IsDelete = 0
  429. AND a.CommentId = 0
  430. AND e.UserId = b.UserId
  431. AND a.AnalyzeType = 1
  432. AND b.UserId ={userId}";
  433. intList.AddRange(await connection.QueryAsync<int>(sql));
  434. if (intList.Count() > 0)
  435. {
  436. var query = Q.NewQuery();
  437. query.Set(nameof(Entity.Analyze.Analyze.IsRead), true);
  438. query.WhereIn(nameof(Entity.Analyze.Analyze.Id), intList);
  439. var result = await analyzeRepository.UpdateAsync(query);
  440. }
  441. tran.Complete();
  442. }
  443. }
  444. catch
  445. {
  446. return false;
  447. }
  448. return true;
  449. }
  450. }
  451. }