123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- 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<AddressBookGroupUser> _repository;
- private readonly Repository<AddressBookGroup> _addressBookGroupRepository;
- private readonly Repository<User> _userRepository;
- private readonly IMapper _mapper;
- private readonly string _connectionString;
- private readonly string _databaseTypeStr;
- public AddressBookGroupUserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
- {
- _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
- _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
- var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<AddressBookGroupUser>(database);
- _userRepository = new Repository<User>(database);
- _addressBookGroupRepository = new Repository<AddressBookGroup>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<bool> DeleteAsync(int id)
- {
- return await _repository.DeleteAsync(id);
- }
- /// <summary>
- /// 批量删除通讯录自建分组用户
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- public async Task<bool> DeletesAsync(List<int> ids)
- {
- return await _repository.DeleteAsync(Q.WhereIn(nameof(Entity.AddressBookGroupUser.Id), ids)) > 0;
- }
- /// <summary>
- /// 查询通讯录好友
- /// </summary>
- /// <param name="request"></param>
- /// <param name="userId"></param>
- /// <returns></returns>
- public async Task<AddressBookGroupUserListResult> 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 tede_user a
- inner join tede_address_book_group_user b on a.Id=b.UserId
- where b. AddressBookGroupId={request.AddressBookGroupId}";
- var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- result.AddressBookGroupUsers = await connection.QueryAsync<AddressBookGroupUserModel>(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).OrderByDesc(nameof(AddressBookGroup.Sort)));
- return result;
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> InsertAsync(AddressBookGroupUserInRequest request)
- {
- //查询组是否存在
- var addressBookGroup = await _addressBookGroupRepository.GetAsync(request.AddressBookGroupId);
- if (addressBookGroup == null)
- throw new BusinessException("通讯录组不存在!");
- try
- {
- using (var trans = new TransactionScope())
- {
- foreach (var item in request.UserId)
- {
- var addressBookGroupUser = new AddressBookGroupUser
- {
- AddressBookGroupId = request.AddressBookGroupId,
- UserId = item
- };
- //查询用户是否是好友
- await _repository.InsertAsync(addressBookGroupUser);
- }
- trans.Complete();
- }
-
- }
- catch
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 移动分组成员到分组下面
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> 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<IEnumerable<AddressBookGroupUser>> GetAllAsync(Query query)
- {
- return await _repository.GetAllAsync(query);
- }
- }
- }
|