using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Transactions; using AutoMapper; using GxPress.Common.AppOptions; using GxPress.Common.Exceptions; using GxPress.Common.Tools; using GxPress.Entity; using GxPress.Repository.Interface; using GxPress.Request.AddressBookGroup; using GxPress.Result.AddressBook; using Microsoft.Extensions.Options; using Datory; using GxPress.Result.AddressBookGroup; using Dapper; namespace GxPress.Repository.Implement { public class AddressBookGroupRepository : IAddressBookGroupRepository { private readonly Repository _repository; private readonly Repository _addressBookGroupUserRepository; private readonly Repository _departmentRepository; private readonly Repository _userRepository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypeStr; public AddressBookGroupRepository(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); _addressBookGroupUserRepository = new Repository(database); _departmentRepository = new Repository(database); _userRepository = 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) { //删除组下面的 await _addressBookGroupUserRepository.DeleteAsync(Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), id)); return await _repository.DeleteAsync(id); } /// /// 修改 /// /// /// public async Task UpdateAsync(AddressBookGroupUpRequest request) { var addressBookGroup = await _repository.GetAsync(request.AddressBookGroupId); addressBookGroup.GroupName = request.GroupName; return await _repository.UpdateAsync(addressBookGroup); } /// /// 获取用户通讯录组 /// /// /// public async Task> GetAddressBookGroupListAsync( AddressBookGroupSearchRequest request) { var result = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId).Where(nameof(AddressBookGroup.ParentId), request.ParendId).OrderByDesc(nameof(AddressBookGroup.Sort))); var addressBookGroupResults = result.Select(n => _mapper.Map(n)).ToList(); for (int i = 0; i < addressBookGroupResults.Count; i++) { if (request.FolderIds.Contains(addressBookGroupResults[i].Id)) addressBookGroupResults.Remove(addressBookGroupResults[i]); else { addressBookGroupResults[i].IsChildren = await _repository.ExistsAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId).Where(nameof(AddressBookGroup.ParentId), addressBookGroupResults[i].Id)); } addressBookGroupResults[i].UserCount = await _addressBookGroupUserRepository.CountAsync(Q.Where(nameof(Entity.AddressBookGroupUser.AddressBookGroupId), addressBookGroupResults[i].Id)); } return addressBookGroupResults; } /// /// 添加 /// /// /// public async Task InsertAsync(AddressBookGroupInRequest request) { var addressBookGroup = new AddressBookGroup { GroupName = request.AddressBookGroupName, UserId = request.UserId, ParentId = request.ParentId }; var id = await _repository.InsertAsync(addressBookGroup); addressBookGroup = await _repository.GetAsync(id); addressBookGroup.Sort = id; await _repository.UpdateAsync(addressBookGroup); return addressBookGroup; } /// /// 获取通讯录用户组 /// /// /// public async Task GetAddressBookListAsync(AddressBookListRequest request) { var result = new AddressBookListResult(); //获取系统分配组 var departments = await _departmentRepository.GetAllAsync(Q.Where(nameof(Department.ParentId), 0)); result.Departments = departments.ToList(); var addressBookGroups = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId).OrderByDesc(nameof(AddressBookGroup.Sort))); var list = addressBookGroups.ToList(); result.AddressBookGroups = list; return result; } /// /// 添加通讯录用户 /// /// /// /// public async Task IsInsertAddressBookUserAsync(int addressBookGroupId, AddressBookGroupUser addressBookGroupUser) { if (await _addressBookGroupUserRepository.CountAsync(Q .Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId) .Where(nameof(AddressBookGroupUser.UserId), addressBookGroupUser.UserId)) == 0) { await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser); } return true; } /// /// 添加联系人 /// /// /// public async Task InsertAddressBookUserAsync(AddressBookInUserRequest request) { if (request.AddressBookGroupId <= 0) { throw new BusinessException("文件ID为空"); } try { using (TransactionScope transactionScope = new TransactionScope()) { //添加用户 foreach (var userId in request.UserIds) { var addressBookGroupUser = new AddressBookGroupUser { AddressBookGroupId = request.AddressBookGroupId, UserId = userId }; //await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser); await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser); } //添加部门人员 foreach (var departmentId in request.DepartmentIds) { List users = new List(); users = await GetUserByDepartmentIdAsync(users, departmentId); foreach (var user in users) { var addressBookGroupUser = new AddressBookGroupUser { AddressBookGroupId = request.AddressBookGroupId, UserId = user.Id }; // await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser); await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser); } } //添加组人员 foreach (var addressBookGroupId in request.AddressBookGroupIds) { var addressBookGroupUserList = new List(); if (addressBookGroupId == 0) { var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync(); addressBookGroupUserList.AddRange(addressBookGroupUsers); } else { var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync( Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId)); addressBookGroupUserList.AddRange(addressBookGroupUsers); } foreach (var item in addressBookGroupUserList) { var addressBookGroupUser = new AddressBookGroupUser { AddressBookGroupId = request.AddressBookGroupId, UserId = item.UserId }; //await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser); await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser); } } transactionScope.Complete(); } } catch (Exception e) { throw new BusinessException(e.Message); } return true; } /// /// 递归查询部门下级用户 /// /// /// /// public async Task> GetUserByDepartmentIdAsync(List users, int departmentId) { var userList = await _userRepository.GetAllAsync(Q.Where(nameof(User.DepartmentId), departmentId)); users.AddRange(userList); var departments = await _departmentRepository.GetAllAsync(Q.Where(nameof(Department.ParentId), departmentId)); if (departments.Any()) { foreach (var department in departments) { await GetUserByDepartmentIdAsync(users, department.Id); } } return users; } /// /// 根据通讯录PID获取用户 /// /// /// /// /// public async Task> GetUserByAddressBookGroupPidAsync(List users, int pid, int userId) { var addressBookGroups = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.ParentId), pid).Where(nameof(AddressBookGroup.UserId), userId)); if (!addressBookGroups.Any()) { return users; } foreach (var addressBookGroup in addressBookGroups) { var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync( Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroup.Id)); if (!addressBookGroupUsers.Any()) continue; foreach (var item in addressBookGroupUsers) { var user = await _userRepository.GetAsync(item.UserId); users.Add(user); } await GetUserByAddressBookGroupPidAsync(users, addressBookGroup.Id, userId); } return users; } /// /// 根据组ID获取用户 /// /// /// public async Task> GetUserByAddressBookGroupIdAsync(int addressBookGroupId) { var users = new List(); var addressBookGroupUserList = await _addressBookGroupUserRepository.GetAllAsync( Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId)); foreach (var item in addressBookGroupUserList) { var user = await _userRepository.GetAsync(item.UserId); users.Add(user); } return users; } public async Task> GetAllAsync(SqlKata.Query query) { return await _repository.GetAllAsync(query); } /// /// 排序 /// /// /// public async Task SortAsync(AddressBookGroupSortRequest request) { var frist = await _repository.GetAsync(request.FristId); var last = await _repository.GetAsync(request.LastId); if (last != null && frist != null) { var databaseType = StringUtils.ToEnum(_databaseTypeStr, DatabaseType.MySql); var database = new Database(databaseType, _connectionString); var connection = database.GetConnection(); if (frist.Sort < last.Sort) { //修改last var lessThanSql = $"update tede_address_book_group set Sort=Sort-1 where UserId={request.UserId} and Sort<={last.Sort} and Id>0"; if (await connection.ExecuteAsync(lessThanSql) > 0) { //修改sort frist.Sort = last.Sort; await _repository.UpdateAsync(frist); return true; } } else { //修改last var lessThanSql = $"update tede_address_book_group set Sort=Sort+1 where UserId={request.UserId} and Sort>={last.Sort} and Id>0"; if (await connection.ExecuteAsync(lessThanSql) > 0) { //修改sort frist.Sort = last.Sort; await _repository.UpdateAsync(frist); return true; } } } return false; } } }