123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- 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;
- namespace GxPress.Repository.Implement
- {
- public class AddressBookGroupRepository : IAddressBookGroupRepository
- {
- private readonly Repository<AddressBookGroup> _repository;
- private readonly Repository<AddressBookGroupUser> _addressBookGroupUserRepository;
- private readonly Repository<Department> _departmentRepository;
- private readonly Repository<User> _userRepository;
- private readonly IMapper _mapper;
- public AddressBookGroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
- {
- var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<AddressBookGroup>(database);
- _addressBookGroupUserRepository = new Repository<AddressBookGroupUser>(database);
- _departmentRepository = new Repository<Department>(database);
- _userRepository = new Repository<User>(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)
- {
- //删除组下面的
- await _addressBookGroupUserRepository.DeleteAsync(Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId),
- id));
- return await _repository.DeleteAsync(id);
- }
- /// <summary>
- /// 修改
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> UpdateAsync(AddressBookGroupUpRequest request)
- {
- var addressBookGroup = await _repository.GetAsync(request.AddressBookGroupId);
- addressBookGroup.GroupName = request.GroupName;
- return await _repository.UpdateAsync(addressBookGroup);
- }
- /// <summary>
- /// 获取用户通讯录组
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<IEnumerable<AddressBookGroupResult>> GetAddressBookGroupListAsync(
- AddressBookGroupSearchRequest request)
- {
- var result = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId).Where(nameof(AddressBookGroup.ParentId), request.ParendId));
- var addressBookGroupResults = result.Select(n => _mapper.Map<AddressBookGroupResult>(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));
- }
- }
- return addressBookGroupResults;
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> InsertAsync(AddressBookGroupInRequest request)
- {
- var addressBookGroup = new AddressBookGroup
- {
- GroupName = request.AddressBookGroupName,
- UserId = request.UserId,
- ParentId = request.ParentId
- };
- return await _repository.InsertAsync(addressBookGroup) > 0;
- }
- /// <summary>
- /// 获取通讯录用户组
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<AddressBookListResult> GetAddressBookListAsync(AddressBookListRequest request)
- {
- var result = new AddressBookListResult();
- //获取系统分配组
- var departments = await _departmentRepository.GetAllAsync(Q.Where(nameof(Department.ParentId), 0).OrderBy(nameof(Department.Sort)));
- result.Departments = departments.ToList();
- var addressBookGroups =
- await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId));
- var list = addressBookGroups.ToList();
- result.AddressBookGroups = list;
- return result;
- }
- /// <summary>
- /// 添加通讯录用户
- /// </summary>
- /// <param name="addressBookGroupId"></param>
- /// <param name="addressBookGroupUser"></param>
- /// <returns></returns>
- public async Task<bool> 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;
- }
- /// <summary>
- /// 添加联系人
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> 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<User> users = new List<User>();
- 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<AddressBookGroupUser>();
- 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;
- }
- /// <summary>
- /// 递归查询部门下级用户
- /// </summary>
- /// <param name="users"></param>
- /// <param name="departmentId"></param>
- /// <returns></returns>
- public async Task<List<User>> GetUserByDepartmentIdAsync(List<User> 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;
- }
- /// <summary>
- /// 根据通讯录PID获取用户
- /// </summary>
- /// <param name="users"></param>
- /// <param name="pid"></param>
- /// <param name="userId"></param>
- /// <returns></returns>
- public async Task<List<User>> GetUserByAddressBookGroupPidAsync(List<User> 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(Q.Where(nameof(User.Id), item.UserId).Where(nameof(User.IsDelete), false));
- users.Add(user);
- }
- await GetUserByAddressBookGroupPidAsync(users, addressBookGroup.Id, userId);
- }
- return users;
- }
- /// <summary>
- /// 根据组ID获取用户
- /// </summary>
- /// <param name="addressBookGroupId"></param>
- /// <returns></returns>
- public async Task<List<User>> GetUserByAddressBookGroupIdAsync(int addressBookGroupId)
- {
- var users = new List<User>();
- 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<List<AddressBookGroup>> GetAllAsync(SqlKata.Query query)
- {
- return await _repository.GetAllAsync(query);
- }
- }
- }
|