123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using AutoMapper;
- using Datory;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Tools;
- using GxPress.Repository.Interface.DepartmentUser;
- using Microsoft.Extensions.Caching.Distributed;
- using Microsoft.Extensions.Options;
- using System.Transactions;
- using GxPress.Request.Department;
- namespace GxPress.Repository.Implement.DepartmentUser
- {
- public class DepartmentUserRepository : IDepartmentUserRepository
- {
- private readonly Repository<Entity.DepartmentUser> _repository;
- private readonly IMapper _mapper;
- private readonly IDistributedCache _cache;
- private readonly string _connectionString;
- private readonly string _databaseTypeStr;
- public DepartmentUserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
- {
- _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<Entity.DepartmentUser>(database);
- _mapper = mapper;
- _cache = cache;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- /// <summary>
- /// 根据部门ID获取用户
- /// </summary>
- /// <param name="departmentId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<int>> GetUserIdsAsync(int departmentId)
- {
- return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), departmentId).Select(nameof(Entity.DepartmentUser.UserId)));
- }
- /// <summary>
- /// 根据用户ID获取部门下面的用户
- /// </summary>
- /// <param name="departmentId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<int>> GetUserIdsByUserIdAsync(int userId)
- {
- var departmentIds = await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.UserId), userId).Select(nameof(Entity.DepartmentUser.DepartmentId)));
- return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), departmentIds).Select(nameof(Entity.DepartmentUser.UserId)));
- }
- /// <summary>
- /// 根据部门ID集合获取用户
- /// </summary>
- /// <param name="departmentId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<int>> GetUserIdsAsync(IEnumerable<int> departmentIds)
- {
- return await _repository.GetAllAsync<int>(Q.WhereIn(nameof(Entity.DepartmentUser.DepartmentId), departmentIds).Select(nameof(Entity.DepartmentUser.UserId)));
- }
- /// <summary>
- /// 获取部门下面的所有用户
- /// </summary>
- /// <param name="departmentId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<int>> GetUserIdsAsync()
- {
- return await _repository.GetAllAsync<int>(Q.Select(nameof(Entity.DepartmentUser.UserId)).GroupBy(nameof(Entity.DepartmentUser.UserId)));
- }
- /// <summary>
- /// 根据部门ID获取部门下面的所有用户
- /// </summary>
- /// <param name="departmentIds"></param>
- /// <returns></returns>
- public async Task<IEnumerable<int>> GetUserIdsByDepartmentIdsAsync(IEnumerable<int> departmentIds)
- {
- return await _repository.GetAllAsync<int>(Q.Select(nameof(Entity.DepartmentUser.UserId)).WhereIn(nameof(Entity.DepartmentUser.DepartmentId), departmentIds).GroupBy(nameof(Entity.DepartmentUser.UserId)));
- }
- /// <summary>
- /// 判断是否有部门
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public async Task<bool> IsExistsAsync(int userId)
- {
- return await _repository.ExistsAsync(Q.Where(nameof(Entity.DepartmentUser.UserId), userId));
- }
- /// <summary>
- /// 根据用户D获取部门ID
- /// </summary>
- /// <param name="departmentId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<int>> GetDepartmentIdsAsync(int userId)
- {
- return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.UserId), userId).Select(nameof(Entity.DepartmentUser.DepartmentId)));
- }
- /// <summary>
- /// 添加部门成员
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> AddDepartmentUserAsync(DepartmentUserInRequest request)
- {
- try
- {
- using (var transactionScope = new TransactionScope())
- {
- //删除
- await _repository.DeleteAsync(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), request.DepartmentId));
- foreach (var item in request.UserIds)
- {
- var entity = new Entity.DepartmentUser()
- {
- DepartmentId = request.DepartmentId,
- UserId = item
- };
- await _repository.InsertAsync(entity);
- }
- transactionScope.Complete();
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 添加部门成员
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> AddDepartmentUserAsync(IEnumerable<int> departmentIds, int userId)
- {
- try
- {
- using (var transactionScope = new TransactionScope())
- {
- //删除
- await _repository.DeleteAsync(Q.Where(nameof(Entity.DepartmentUser.UserId), userId));
- foreach (var item in departmentIds)
- {
- var entity = new Entity.DepartmentUser()
- {
- DepartmentId = item,
- UserId = userId
- };
- await _repository.InsertAsync(entity);
- }
- transactionScope.Complete();
- }
- }
- catch
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 根据用户id获取所在部门
- /// </summary>
- /// <param name="userIds"></param>
- /// <returns></returns>
- public async Task<IEnumerable<Entity.DepartmentUser>> GetDepartmentUsersByUserIdsAsync(IEnumerable<int> userIds)
- {
- return await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.DepartmentUser.UserId), userIds));
- }
- }
- }
|