using System; using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using GxPress.Common.AppOptions; using GxPress.Common.Exceptions; using GxPress.Common.Tools; using GxPress.Entity; using GxPress.Repository.Interface; using GxPress.Request.GroupChatUser; using GxPress.Result.GroupChatUser; using Microsoft.Extensions.Options; using Datory; using Dapper; namespace GxPress.Repository.Implement { public class GroupChatUserRepository : IGroupChatUserRepository { private readonly Repository _repository; private readonly Repository _groupChatRepository; private readonly Repository _userRepository; private readonly Repository _oftenContactRepository; private readonly IMapper _mapper; private readonly Repository _middleRepository; private readonly string _connectionString; private readonly string _databaseTypeStr; public GroupChatUserRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType; _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString; var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _groupChatRepository = new Repository(database); _userRepository = new Repository(database); _oftenContactRepository = new Repository(database); _middleRepository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; /// /// 添加 /// /// /// public async Task InsertAsync(GroupChatUserInRequest request) { var user = await _userRepository.GetAsync(request.UserId); if (user == null) throw new BusinessException("用户不存在"); var groupChat = await _groupChatRepository.GetAsync(request.GroupChatId); if (groupChat == null) throw new BusinessException("群不存在"); //查询用户是否存在 var groupChatUser = await _repository.GetAsync(Q .Where(nameof(GroupChatUser.GroupChatId), request.GroupChatId) .Where(nameof(GroupChatUser.UserId), request.UserId)); if (groupChatUser == null) throw new BusinessException("用户已经添加过了"); var groupChatUserEntity = new GroupChatUser { GroupChatId = request.GroupChatId, UserId = request.UserId, ImId = request.GroupChartImId }; //添加用户 var oftenContact = new OftenContact { ChatType = 2, ContactUserId = request.GroupChartImId, IndividualGroupId = 0, UserId = request.UserId }; await _oftenContactRepository.InsertAsync(oftenContact); return await _repository.InsertAsync(groupChatUserEntity) > 0; } /// /// 添加多个 /// /// /// public async Task InsertAsync(List groupChatUsers) { if (groupChatUsers.Count == 0 || groupChatUsers == null) return false; string sql = $@"INSERT INTO `tede_group_chat_user`(`Guid`,`CreatedDate`,`LastModifiedDate`,`GroupChatId`,`UserId`,`IsDisturb`,`IsTop`,`ImId`,`IsAdmin`,`GroupChatImId`)VALUES"; var guId = Guid.NewGuid().ToString(); var nowTime = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); foreach (var item in groupChatUsers) { var IsDisturb = item.IsDisturb ? 1 : 0; var IsTop = item.IsTop ? 1 : 0; var IsAdmin = item.IsAdmin ? 1 : 0; sql += $@"('{guId}','{nowTime}','{nowTime}','{item.GroupChatId}',{item.UserId}, {IsDisturb},{IsTop},'{item.ImId}',{IsAdmin},'{item.GroupChatImId}'),"; } sql = sql.Remove(sql.Length - 1, 1); var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); return await connection.ExecuteAsync(sql) > 0; } /// /// 修改数据 /// /// /// public async Task UpdateAsync(GroupChatUserUpRequest request) { throw new NotImplementedException(); } /// /// 删除用户 /// /// /// public async Task DeleteAsync(int id) { return await _repository.DeleteAsync(id); } /// /// 根据群ID获取群用户 /// /// /// public async Task> GetGroupChatUserByGroupChatIdAsync(GroupChatUserSearchRequest request) { var sql = $"SELECT a.*,b.Name,b.AvatarUrl FROM tede_group_chat_user a inner join tede_user b on a.UserId=b.Id where a.GroupChatImId='{request.GroupChatImId}'"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var items = await connection .QueryAsync(sql, (groupChatUserByGroupChatIdResult, user) => { groupChatUserByGroupChatIdResult.UserName = user.Name; groupChatUserByGroupChatIdResult.AvatarUrl = user != null ? StringUtils.AddDomainMin(user.AvatarUrl) : ""; return groupChatUserByGroupChatIdResult; }, splitOn: "Name"); return items; } /// /// 是否免打扰 /// /// /// public async Task UpIsDisturbAsync(GroupChatUserIsDisturbRequest request) { //获取群 var groupChat = await _groupChatRepository.GetAsync(request.GroupChatId); if (groupChat == null) throw new BusinessException("群已经解散"); var groupChatUser = await _repository.GetAsync(Q.Where(nameof(GroupChatUser.GroupChatId), request.GroupChatId).Where(nameof(GroupChatUser.UserId), request.UserId)); if (groupChatUser == null) throw new BusinessException("用户不在群里面"); groupChatUser.IsDisturb = !groupChatUser.IsDisturb; return await _repository.UpdateAsync(groupChatUser); } /// /// 是否置顶 /// /// /// public async Task UpIsTopAsync(GroupChatUserIsTopRequest request) { //获取群 var groupChat = await _groupChatRepository.GetAsync(request.GroupChatId); if (groupChat == null) throw new BusinessException("群已经解散"); var oftenContact = await _oftenContactRepository.GetAsync(Q.Where(nameof(Entity.OftenContact.UserId), request.UserId).Where(nameof(Entity.OftenContact.ContactUserId), groupChat.GroupChatImId)); if (oftenContact == null) return false; return await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsTop), request.IsTop).Where(nameof(Entity.Middle.Middle.UserId), request.UserId).Where(nameof(Entity.Middle.Middle.MiddleId), oftenContact.Id).Where(nameof(Entity.Middle.Middle.FolderType), 10)) > 0; } /// /// 获取 /// /// /// public async Task FindGroupChatUserByIdAsync(int id) { return await _repository.GetAsync(id); } public async Task GetAsync(SqlKata.Query query) { return await _repository.GetAsync(query); } /// /// 根据群IMID获取用户 /// /// /// public async Task> FindGroupChatUserByImId(string imId) { var groupChat = await _groupChatRepository.GetAsync(Q.Where(nameof(GroupChat.GroupChatImId), imId)); return await _repository.GetAllAsync(Q.Where(nameof(GroupChatUser.GroupChatId), groupChat.Id).Where(nameof(GroupChatUser.IsAdmin), false)); } /// /// 是否免打扰 /// /// /// public async Task IsDisturbAsync(GroupChatUserIsDisturbRequest request) { //获取群 var groupChat = await _groupChatRepository.GetAsync(request.GroupChatId); if (groupChat == null) throw new BusinessException("群已经解散"); var groupChatUser = await _repository.GetAsync(Q.Where(nameof(GroupChatUser.GroupChatId), request.GroupChatId).Where(nameof(GroupChatUser.UserId), request.UserId)); if (groupChatUser == null) return false; return groupChatUser.IsDisturb; } public async Task> GetAllAsync(SqlKata.Query query) { return await _repository.GetAllAsync(query); } public async Task InsertAsync(GroupChatUser groupChatUser) { return await _repository.InsertAsync(groupChatUser); } public async Task> GetUserIdsAsync(int userId) { string sql = $"SELECT UserId FROM tede_group_chat_user where GroupChatId in(SELECT GroupChatId FROM tede_group_chat_user where UserId={userId})"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var items = await connection .QueryAsync(sql); return items; } /// /// 获取用户加入的群聊用户人数 /// /// /// public async Task GetUserIntoGroupChatUserCountAsync(int userId) { string sql = $@"SELECT COUNT(1) FROM tede_user WHERE Id IN (SELECT UserId FROM tede_group_chat_user WHERE GroupChatId IN (SELECT GroupChatId FROM tede_group_chat_user WHERE UserId ={userId}))"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); var result = await connection .ExecuteScalarAsync(sql); return result; } } }