ReplyService.Praise.cs 24 KB

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