ReplyService.Praise.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. AND a.IsRead = 0
  318. ";
  319. count += await connection.ExecuteScalarAsync<int>(sql);
  320. sql = $@" SELECT
  321. count(1)
  322. FROM
  323. tede_analyze a
  324. INNER JOIN
  325. tede_topic b ON a.SourceId = b.Id
  326. INNER JOIN
  327. tede_user c ON c.Id = a.UserId
  328. INNER JOIN
  329. tede_user d ON d.Id = b.UserId
  330. INNER JOIN
  331. tede_middle e ON e.MiddleId = b.Id
  332. INNER JOIN
  333. tede_group g ON g.Id = b.GroupId
  334. WHERE
  335. a.TypeValue = {AllTypeConst.Topic.GetHashCode()} AND e.IsDelete = 0
  336. AND a.CommentId = 0
  337. AND e.UserId = b.UserId
  338. AND a.AnalyzeType = 1
  339. AND b.UserId ={userId}
  340. AND a.IsRead = 0
  341. ";
  342. count += await connection.ExecuteScalarAsync<int>(sql);
  343. return count;
  344. }
  345. /// <summary>
  346. /// 修改未读点赞数量
  347. /// </summary>
  348. /// <param name="userId"></param>
  349. /// <returns></returns>
  350. public async Task<bool> UpdateUReadPraiseAsync(int userId)
  351. {
  352. try
  353. {
  354. using (var tran = new TransactionScope())
  355. {
  356. var intList = new List<int>();
  357. var sql = $@"SELECT
  358. a.Id
  359. FROM
  360. tede_analyze a
  361. INNER JOIN
  362. tede_note b ON a.SourceId = b.Id
  363. INNER JOIN
  364. tede_user c ON c.Id = a.UserId
  365. INNER JOIN
  366. tede_user d ON d.Id = b.UserId
  367. INNER JOIN
  368. tede_middle e ON e.MiddleId = b.Id
  369. WHERE
  370. a.TypeValue = 4 AND e.IsDelete = 0
  371. AND a.CommentId = 0
  372. AND e.UserId = b.UserId
  373. AND a.AnalyzeType = 1
  374. AND b.UserId = {userId}
  375. AND a.IsRead = 0";
  376. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  377. var connection = database.GetConnection();
  378. intList.AddRange(await connection.QueryAsync<int>(sql));
  379. sql = $@"
  380. SELECT
  381. b.Id
  382. FROM
  383. tede_comment a
  384. INNER JOIN
  385. tede_analyze b ON a.Id = b.CommentId
  386. INNER JOIN
  387. tede_user c ON c.Id = b.UserId
  388. INNER JOIN
  389. tede_user d ON d.Id = a.UserId
  390. WHERE
  391. a.TypeValue IN ({AllTypeConst.Topic.GetHashCode()},{AllTypeConst.Notice.GetHashCode()},{AllTypeConst.Note.GetHashCode()})
  392. AND a.UserId ={userId}
  393. AND b.AnalyzeType = 2
  394. AND b.IsRead = 0";
  395. intList.AddRange(await connection.QueryAsync<int>(sql));
  396. sql = $@"
  397. SELECT
  398. a.Id
  399. FROM
  400. tede_analyze a
  401. INNER JOIN
  402. tede_notice b ON a.SourceId = b.Id
  403. INNER JOIN
  404. tede_user c ON c.Id = a.UserId
  405. INNER JOIN
  406. tede_user d ON d.Id = b.UserId
  407. INNER JOIN
  408. tede_middle e ON e.MiddleId = b.Id
  409. WHERE
  410. b.UserId ={userId} AND e.UserId = b.UserId
  411. AND a.TypeValue ={AllTypeConst.Notice.GetHashCode()}
  412. AND e.IsDelete = 0
  413. AND a.CommentId = 0
  414. AND a.AnalyzeType = 1";
  415. intList.AddRange(await connection.QueryAsync<int>(sql));
  416. sql = $@"
  417. SELECT
  418. a.Id
  419. FROM
  420. tede_analyze a
  421. INNER JOIN
  422. tede_topic b ON a.SourceId = b.Id
  423. INNER JOIN
  424. tede_user c ON c.Id = a.UserId
  425. INNER JOIN
  426. tede_user d ON d.Id = b.UserId
  427. INNER JOIN
  428. tede_middle e ON e.MiddleId = b.Id
  429. INNER JOIN
  430. tede_group g ON g.Id = b.GroupId
  431. WHERE
  432. a.TypeValue = {AllTypeConst.Topic.GetHashCode()} AND e.IsDelete = 0
  433. AND a.CommentId = 0
  434. AND e.UserId = b.UserId
  435. AND a.AnalyzeType = 1
  436. AND b.UserId ={userId}";
  437. intList.AddRange(await connection.QueryAsync<int>(sql));
  438. if (intList.Count() > 0)
  439. {
  440. var query = Q.NewQuery();
  441. query.Set(nameof(Entity.Analyze.Analyze.IsRead), true);
  442. query.WhereIn(nameof(Entity.Analyze.Analyze.Id), intList);
  443. var result = await analyzeRepository.UpdateAsync(query);
  444. }
  445. tran.Complete();
  446. }
  447. }
  448. catch
  449. {
  450. return false;
  451. }
  452. return true;
  453. }
  454. }
  455. }