ReplyService.Praise.cs 24 KB

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