123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 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<Entity.Friends> _repository;
- private readonly IMapper _mapper;
- private readonly IDistributedCache _cache;
- public FriendsRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
- {
- var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _mapper = mapper;
- _cache = cache;
- _repository = new Repository<Entity.Friends>(database);
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="model"></param>
- /// <returns></returns>
- public async Task<int> AddAsync(Entity.Friends model)
- {
- return await _repository.InsertAsync(model);
- }
- /// <summary>
- /// 删除我的好友
- /// </summary>
- /// <param name="friendsIds"></param>
- /// <param name="userId"></param>
- /// <returns></returns>
- public async Task<bool> DeleteAsync(IEnumerable<int> friendsIds, int userId)
- {
- return await _repository.DeleteAsync(Q.Where(nameof(Entity.Friends.UserId), userId).WhereIn(nameof(Entity.Friends.FriendsUserId), friendsIds)) > 0;
- }
- /// <summary>
- /// 获取好友数据
- /// </summary>
- /// <param name="userId"></param>
- /// <param name="addressBookGroupId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<int>> 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<int>(query);
- }
- /// <summary>
- /// 好友移动到自己分组
- /// </summary>
- /// <param name="friendsUserId"></param>
- /// <param name="userId"></param>
- /// <param name="addressBookGroupId"></param>
- /// <returns></returns>
- public async Task<bool> UpdateAddressBookGroupId(IEnumerable<int> 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;
- }
- /// <summary>
- /// 是否是好友
- /// </summary>
- /// <param name="friendsUserId"></param>
- /// <param name="userId"></param>
- /// <returns></returns>
- public async Task<bool> 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);
- }
- }
- }
|