RepositoryExtension.Blacklist.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Elasticsearch.Net;
  2. using JiaZhiQuan.Common.ElasticSearch;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using Senparc.Weixin.MP.Containers;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Diagnostics;
  10. using System.Dynamic;
  11. using System.Linq;
  12. using System.Net.Http;
  13. using System.Reflection;
  14. using System.Security.Cryptography;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using Wicture.DbRESTFul;
  18. using Wicture.DbRESTFul.Cache;
  19. using Wicture.DbRESTFul.Infrastructure.Repository;
  20. namespace JiaZhiQuan.Common
  21. {
  22. public static partial class RepositoryExtension
  23. {
  24. /// <summary>
  25. /// 检查拉黑情况
  26. /// 如果用户主动拉黑,则 返回 您已拉黑此用户,无法进行此操作
  27. /// 如果被其他用户拉黑,则 返回 由于用户隐私设置,无法进行此操作
  28. /// 如果正常,则返回 null
  29. /// </summary>
  30. public async static Task<string> CheckInBlacklist(this DbRESTFulRepository repository, long userId, IList<long> blackUserIds, IDbConnection conn = null, string errorMessage = "无法进行此操作")
  31. {
  32. var query = new { userId, blackUserIds };
  33. var id = await repository.QuerySingleOrDefaultAsync<int>("select id from n_user_blacklist where userId=@userId and blackUserId in @blackUserIds limit 1", query, conn);
  34. if (id > 0)
  35. {
  36. throw new LogicalException($"您已拉黑此用户,{errorMessage}");
  37. }
  38. id = await repository.QuerySingleOrDefaultAsync<int>("select id from n_user_blacklist where blackUserId=@userId and userId in @blackUserIds limit 1", query, conn);
  39. if (id > 0)
  40. {
  41. throw new LogicalException($"由于对方隐私设置,{errorMessage}");
  42. }
  43. return null;
  44. }
  45. /// <summary>
  46. /// 获取当前用户的黑名单
  47. /// </summary>
  48. public async static Task<List<long>> GetBlackUserIdList(this DbRESTFulRepository repository, long userId = 0, IDbConnection conn = null)
  49. {
  50. if (userId == 0)
  51. {
  52. userId = long.Parse(repository.Context?.Identity?.Id ?? "0");
  53. }
  54. if (userId > 0)
  55. {
  56. var list = (await repository.QueryAsync<dynamic>("select userId, blackUserId from n_user_blacklist where userId=@userId or blackUserId=@userId", new { userId }, conn)).ToList();
  57. var set = new HashSet<long>();
  58. list.ForEach(e =>
  59. {
  60. if (e.userId != userId) set.Add(e.userId);
  61. if (e.blackUserId != userId) set.Add(e.blackUserId);
  62. });
  63. return set.ToList();
  64. }
  65. return new List<long>();
  66. }
  67. }
  68. }