FriendsRepository.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Datory;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.Repository.Interface.Friends;
  8. using Microsoft.Extensions.Caching.Distributed;
  9. using Microsoft.Extensions.Options;
  10. namespace GxPress.Repository.Implement.Friends
  11. {
  12. public class FriendsRepository : IFriendsRepository
  13. {
  14. private readonly Repository<Entity.Friends> _repository;
  15. private readonly IMapper _mapper;
  16. private readonly IDistributedCache _cache;
  17. public FriendsRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
  18. {
  19. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  20. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  21. _mapper = mapper;
  22. _cache = cache;
  23. _repository = new Repository<Entity.Friends>(database);
  24. }
  25. public IDatabase Database => _repository.Database;
  26. public string TableName => _repository.TableName;
  27. public List<TableColumn> TableColumns => _repository.TableColumns;
  28. /// <summary>
  29. /// 添加
  30. /// </summary>
  31. /// <param name="model"></param>
  32. /// <returns></returns>
  33. public async Task<int> AddAsync(Entity.Friends model)
  34. {
  35. return await _repository.InsertAsync(model);
  36. }
  37. /// <summary>
  38. /// 删除我的好友
  39. /// </summary>
  40. /// <param name="friendsIds"></param>
  41. /// <param name="userId"></param>
  42. /// <returns></returns>
  43. public async Task<bool> DeleteAsync(IEnumerable<int> friendsIds, int userId)
  44. {
  45. return await _repository.DeleteAsync(Q.Where(nameof(Entity.Friends.UserId), userId).WhereIn(nameof(Entity.Friends.FriendsUserId), friendsIds)) > 0;
  46. }
  47. /// <summary>
  48. /// 获取好友数据
  49. /// </summary>
  50. /// <param name="userId"></param>
  51. /// <param name="addressBookGroupId"></param>
  52. /// <returns></returns>
  53. public async Task<IEnumerable<int>> GetFriendsIdsAsync(int userId, int addressBookGroupId)
  54. {
  55. var query = Q.NewQuery();
  56. query.Select(nameof(Entity.Friends.FriendsUserId));
  57. query.Where(nameof(Entity.Friends.UserId), userId);
  58. if (addressBookGroupId >= 0)
  59. query.Where(nameof(Entity.Friends.AddressBookGroupId), addressBookGroupId);
  60. return await _repository.GetAllAsync<int>(query);
  61. }
  62. /// <summary>
  63. /// 好友移动到自己分组
  64. /// </summary>
  65. /// <param name="friendsUserId"></param>
  66. /// <param name="userId"></param>
  67. /// <param name="addressBookGroupId"></param>
  68. /// <returns></returns>
  69. public async Task<bool> UpdateAddressBookGroupId(IEnumerable<int> friendsUserId, int userId, int addressBookGroupId)
  70. {
  71. var query = Q.NewQuery();
  72. query.Set(nameof(Entity.Friends.AddressBookGroupId), addressBookGroupId);
  73. query.WhereIn(nameof(Entity.Friends.FriendsUserId), friendsUserId);
  74. query.Where(nameof(Entity.Friends.UserId), userId);
  75. return await _repository.UpdateAsync(query) > 0;
  76. }
  77. /// <summary>
  78. /// 是否是好友
  79. /// </summary>
  80. /// <param name="friendsUserId"></param>
  81. /// <param name="userId"></param>
  82. /// <returns></returns>
  83. public async Task<bool> ExistsAsync(int friendsUserId, int userId)
  84. {
  85. var query = Q.NewQuery();
  86. query.Where(nameof(Entity.Friends.UserId), userId);
  87. query.Where(nameof(Entity.Friends.FriendsUserId), friendsUserId);
  88. return await _repository.ExistsAsync(query);
  89. }
  90. }
  91. }