BlacklistUserRepository.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using AutoMapper;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Entity;
  9. using GxPress.Repository.Interface;
  10. using GxPress.Request.BlacklistUser;
  11. using Microsoft.Extensions.Options;
  12. using Datory;
  13. namespace GxPress.Repository.Implement
  14. {
  15. public class BlacklistUserRepository : IBlacklistUserRepository
  16. {
  17. private readonly Repository<BlacklistUser> _repository;
  18. private readonly Repository<User> _userRepository;
  19. private readonly IMapper _mapper;
  20. public BlacklistUserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  21. {
  22. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  23. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  24. _repository = new Repository<BlacklistUser>(database);
  25. _userRepository = new Repository<User>(database);
  26. _mapper = mapper;
  27. }
  28. public IDatabase Database => _repository.Database;
  29. public string TableName => _repository.TableName;
  30. public List<TableColumn> TableColumns => _repository.TableColumns;
  31. /// <summary>
  32. /// 获取黑名单
  33. /// </summary>
  34. /// <param name="request"></param>
  35. /// <returns></returns>
  36. public async Task<IEnumerable<BlacklistUser>> GetBlacklistUserAsync(BlacklistUserSearchRequest request)
  37. {
  38. var result= await _repository.GetAllAsync(Q.Where(nameof(BlacklistUser.UserId), request.UserId));
  39. foreach (var item in result)
  40. item.AvatarUrl=StringUtils.AddDomainMin(item.AvatarUrl);
  41. return result;
  42. }
  43. /// <summary>
  44. /// 根据ID获取
  45. /// </summary>
  46. /// <param name="id"></param>
  47. /// <returns></returns>
  48. public async Task<BlacklistUser> GetAsync(int id)
  49. {
  50. return await _repository.GetAsync(id);
  51. }
  52. /// <summary>
  53. /// 根据用户ID查询黑名单
  54. /// </summary>
  55. /// <param name="userId"></param>
  56. /// <param name="blackUserId"></param>
  57. /// <returns></returns>
  58. public async Task<BlacklistUser> GetBlackUserAsync(int userId, int blackUserId)
  59. {
  60. return await _repository.GetAsync(Q.Where(nameof(BlacklistUser.UserId), userId).Where(nameof(BlacklistUser.BlackUserId), blackUserId));
  61. }
  62. /// <summary>
  63. /// 添加
  64. /// </summary>
  65. /// <param name="userId"></param>
  66. /// <param name="request"></param>
  67. /// <returns></returns>
  68. public async Task<bool> InsertAsync(int userId, BlacklistUserInRequest request)
  69. {
  70. var user = await _userRepository.GetAsync(request.UserId);
  71. var blacklistUser = new BlacklistUser
  72. {
  73. UserId = userId,
  74. AvatarUrl = user.AvatarUrl,
  75. BlackUserName = user.Name,
  76. ImId = user.ImId,
  77. BlackUserId = request.UserId
  78. };
  79. //修改聊天界面黑名单
  80. return await _repository.InsertAsync(blacklistUser) > 0;
  81. }
  82. public async Task<bool> InsertAsync(BlacklistUser request)
  83. {
  84. return await _repository.InsertAsync(request) > 0;
  85. }
  86. /// <summary>
  87. /// 删除
  88. /// </summary>
  89. /// <param name="id"></param>
  90. /// <returns></returns>
  91. public async Task<bool> DeleteAsync(int id)
  92. {
  93. return await _repository.DeleteAsync(id);
  94. }
  95. /// <summary>
  96. /// 查询该用户是否是黑名单
  97. /// </summary>
  98. /// <param name="userId"></param>
  99. /// <param name="blackUserId"></param>
  100. /// <returns></returns>
  101. public async Task<bool> FindUserIsBlack(int userId, int blackUserId)
  102. {
  103. var count = await _repository.CountAsync(Q.Where(nameof(BlacklistUser.UserId), userId)
  104. .Where(nameof(BlacklistUser.BlackUserId), blackUserId));
  105. return count > 0;
  106. }
  107. }
  108. }