using System.Collections.Generic; using System.Linq; 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.AddressBookGroupUser; using GxPress.Result.AddressBookGroupUser; using Microsoft.Extensions.Options; using Datory; using SqlKata; using System.Transactions; using Dapper; namespace GxPress.Repository.Implement { public class AddressBookGroupUserRepository : IAddressBookGroupUserRepository { private readonly Repository _repository; private readonly Repository _addressBookGroupRepository; private readonly Repository _userRepository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypeStr; public AddressBookGroupUserRepository(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); _userRepository = new Repository(database); _addressBookGroupRepository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; /// /// 删除 /// /// /// public async Task DeleteAsync(int id) { return await _repository.DeleteAsync(id); } /// /// 批量删除通讯录自建分组用户 /// /// /// public async Task DeletesAsync(List ids) { return await _repository.DeleteAsync(Q.WhereIn(nameof(Entity.AddressBookGroupUser.Id), ids)) > 0; } /// /// 查询通讯录好友 /// /// /// /// public async Task GetAddressBookGroupUserListAsync( AddressBookGroupUserSearchRequest request, int userId) { var result = new AddressBookGroupUserListResult(); //sql var sql = $"select b.Id,b.AddressBookGroupId,a.Id as UserId,a.Name as UserName,a.AvatarUrl,a.ImId from ccpph.tede_user a inner join ccpph.tede_address_book_group_user b on a.Id=b.UserId where b. AddressBookGroupId={request.AddressBookGroupId}"; var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); result.AddressBookGroupUsers = await connection.QueryAsync(sql); var userlist = result.AddressBookGroupUsers.ToList(); //排除成员 for (int i = 0; i < userlist.Count; i++) { if (!string.IsNullOrWhiteSpace(userlist[i].AvatarUrl)) userlist[i].AvatarUrl = StringUtils.AddDomainMin(userlist[i].AvatarUrl); if (!request.IsShow) if (request.UserIds.Contains(userlist[i].UserId)) userlist.Remove(userlist[i]); if (request.IsShow) if (request.UserIds.Contains(userlist[i].UserId)) userlist[i].IsDisable = true; } result.AddressBookGroupUsers = userlist; result.AddressBookGroups = await _addressBookGroupRepository.GetAllAsync(Q.Where(nameof(AddressBookGroup.ParentId), request.AddressBookGroupId).Where(nameof(AddressBookGroup.UserId), userId)); return result; } /// /// 添加 /// /// /// public async Task InsertAsync(AddressBookGroupUserInRequest request) { //查询组是否存在 var addressBookGroup = await _addressBookGroupRepository.GetAsync(request.AddressBookGroupId); if (addressBookGroup == null) throw new BusinessException("通讯录组不存在!"); var user = await _userRepository.GetAsync(request.UserId); if (user == null) throw new BusinessException("用户不存在!"); var addressBookGroupUserEntity = await _repository.GetAsync(Q .Where(nameof(AddressBookGroupUser.UserId), request.UserId) .Where(nameof(AddressBookGroupUser.AddressBookGroupId), request.AddressBookGroupId)); if (addressBookGroupUserEntity != null) throw new BusinessException("用户已经存在!"); var addressBookGroupUser = new AddressBookGroupUser { AddressBookGroupId = request.AddressBookGroupId, UserId = request.UserId }; return await _repository.InsertAsync(addressBookGroupUser) > 0; } /// /// 移动分组成员到分组下面 /// /// /// public async Task AddressBookGroupUserMoveAsync(AddressBookGroupUserMoveRequest request) { var addressBookGroup = await _addressBookGroupRepository.GetAsync(request.AddressBookGroupId); if (addressBookGroup == null) throw new BusinessException("分组ID不存在"); try { //事务 using (var transactionScope = new TransactionScope()) { //获取成员 var addressBookGroupUsers = await _repository.GetAllAsync(Q.WhereIn(nameof(AddressBookGroupUser.Id), request.AddressBookGroupUserId)); //获取小组成员 var addressBookGroupUsersDto = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), request.AddressBookGroupId)); foreach (var item in addressBookGroupUsers) { if (!addressBookGroupUsersDto.Any(n => n.UserId == item.UserId)) { item.Id = 0; item.AddressBookGroupId = request.AddressBookGroupId; //添加 await _repository.InsertAsync(item); } } await _repository.DeleteAsync(Q.WhereIn(nameof(AddressBookGroupUser.Id), request.AddressBookGroupUserId)); transactionScope.Complete(); } } catch (System.Exception ex) { throw new BusinessException(ex.Message); } return true; } public async Task> GetAllAsync(Query query) { return await _repository.GetAllAsync(query); } } }