using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Datory; using GxPress.Common.AppOptions; using GxPress.Common.Tools; using GxPress.Repository.Interface.Friends; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Options; namespace GxPress.Repository.Implement.Friends { public class FriendsRepository : IFriendsRepository { private readonly Repository _repository; private readonly IMapper _mapper; private readonly IDistributedCache _cache; public FriendsRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper, IDistributedCache cache) { var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _mapper = mapper; _cache = cache; _repository = new Repository(database); } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; /// /// 添加 /// /// /// public async Task AddAsync(Entity.Friends model) { return await _repository.InsertAsync(model); } /// /// 删除我的好友 /// /// /// /// public async Task DeleteAsync(IEnumerable friendsIds, int userId) { return await _repository.DeleteAsync(Q.Where(nameof(Entity.Friends.UserId), userId).WhereIn(nameof(Entity.Friends.FriendsUserId), friendsIds)) > 0; } /// /// 获取好友数据 /// /// /// /// public async Task> GetFriendsIdsAsync(int userId, int addressBookGroupId) { var query = Q.NewQuery(); query.Select(nameof(Entity.Friends.FriendsUserId)); query.Where(nameof(Entity.Friends.UserId), userId); if (addressBookGroupId >= 0) query.Where(nameof(Entity.Friends.AddressBookGroupId), addressBookGroupId); return await _repository.GetAllAsync(query); } /// /// 好友移动到自己分组 /// /// /// /// /// public async Task UpdateAddressBookGroupId(IEnumerable friendsUserId, int userId, int addressBookGroupId) { var query = Q.NewQuery(); query.Set(nameof(Entity.Friends.AddressBookGroupId), addressBookGroupId); query.WhereIn(nameof(Entity.Friends.FriendsUserId), friendsUserId); query.Where(nameof(Entity.Friends.UserId), userId); return await _repository.UpdateAsync(query) > 0; } /// /// 是否是好友 /// /// /// /// public async Task ExistsAsync(int friendsUserId, int userId) { var query = Q.NewQuery(); query.Where(nameof(Entity.Friends.UserId), userId); query.Where(nameof(Entity.Friends.FriendsUserId), friendsUserId); return await _repository.ExistsAsync(query); } } }