RepositoryExtension.Comment.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Aliyun.OSS;
  2. using Elasticsearch.Net;
  3. using JiaZhiQuan.Common.AliOSS;
  4. using JiaZhiQuan.Common.ElasticSearch;
  5. using JiaZhiQuan.Common.ElasticSearch.Models;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using Senparc.Weixin.MP.Containers;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. using System.Diagnostics;
  13. using System.Dynamic;
  14. using System.Linq;
  15. using System.Net.Http;
  16. using System.Reflection;
  17. using System.Security.Cryptography;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using Wicture.DbRESTFul;
  21. using Wicture.DbRESTFul.Cache;
  22. using Wicture.DbRESTFul.Infrastructure.Repository;
  23. namespace JiaZhiQuan.Common
  24. {
  25. public static partial class RepositoryExtension
  26. {
  27. public static async Task<PostCommentState> GetPostCommentState(this DbRESTFulRepository repository, long postId, long authorId = 0, IDbConnection conn = null)
  28. {
  29. var userId = long.Parse(repository.Context.Identity?.Id ?? "0");
  30. var model = await repository.QuerySingleOrDefaultAsync<PostCommentState>($"select notFocused, notFans, notSelf, `all` from n_post_comment_setting where postId={postId}", null, conn, null, true);
  31. if (model == null)
  32. {
  33. model = new PostCommentState();
  34. }
  35. if (userId > 0)
  36. {
  37. if (model.all == 0)
  38. {
  39. if (model.notFocused == 0 && model.notFans == 0 && model.notSelf == 0)
  40. {
  41. model.canComment = 1;
  42. return model;
  43. }
  44. // 检查是否是作者
  45. if (authorId == 0)
  46. {
  47. authorId = await repository.QuerySingleOrDefaultAsync<long>($"select userId from n_post where id={postId}", null, conn, null, false);
  48. }
  49. var isAuthor = authorId == userId;
  50. if (isAuthor) model.canComment = 1;
  51. else if (model.notSelf == 0)
  52. {
  53. // 查询关注状态
  54. var conditions = new List<string>();
  55. if (model.notFans > 0)
  56. {
  57. conditions.Add($" (userId={authorId} and fanId={userId}) ");
  58. }
  59. if (model.notFocused > 0)
  60. {
  61. conditions.Add($" (userId={userId} and fanId={authorId}) ");
  62. }
  63. if (conditions.Count > 0)
  64. {
  65. var condition = string.Join("or", conditions);
  66. var records = await repository.QueryAsync<dynamic>($"select userId, fanId from n_user_fan where {condition}", null, conn, null, false);
  67. if ((model.notFans > 0 && records.Any(e => e.userId == authorId && e.fanId == userId)) || (model.notFocused > 0 && records.Any(e => e.userId == userId && e.fanId == authorId)))
  68. {
  69. model.canComment = 1;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. return model;
  76. }
  77. }
  78. }