123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- using System;
- 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.Department;
- using GxPress.Result.Department;
- using Microsoft.Extensions.Options;
- using Datory;
- using GxPress.Result.User;
- namespace GxPress.Repository.Implement
- {
- public class DepartmentRepository : IDepartmentRepository
- {
- private readonly IMapper _mapper;
- private readonly Repository<Department> _repository;
- private readonly Repository<User> _userRepository;
- private readonly Repository<Entity.DepartmentUser> departmentUserRepository;
- public DepartmentRepository(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<Department>(database);
- _userRepository = new Repository<User>(database);
- departmentUserRepository = new Repository<Entity.DepartmentUser>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- /// <summary>
- /// 是否存在下级部门
- /// </summary>
- /// <param name="parentId"></param>
- /// <returns></returns>
- public async Task<bool> ExistsChildrenAsync(int parentId)
- {
- return await _repository.ExistsAsync(Q.Where(nameof(Department.ParentId), parentId));
- }
- public async Task<Department> GetAsync(int id)
- {
- var department = await _repository.GetAsync(id);
- return department;
- }
- /// <summary>
- /// 根据部门名称查询对应的数据
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public async Task<Department> GetAsync(string name)
- {
- return await _repository.GetAsync(Q.Where(nameof(Department.Name), name));
- }
- public async Task<string> GetNameAsync(int id)
- {
- if (id <= 0) return string.Empty;
- return await _repository.GetAsync<string>(Q
- .Select(nameof(Department.Name))
- .Where(nameof(Department.Id), id)
- );
- }
- public async Task<int> InsertAsync(Department department)
- {
- if (department.Sort > 0)
- {
- var exists = await _repository.ExistsAsync(Q
- .Where(nameof(Department.Sort), department.Sort)
- );
- if (exists)
- {
- throw new Exception("部门顺序已存在,请更换");
- }
- }
- return await _repository.InsertAsync(department);
- }
- public async Task<bool> UpdateAsync(Department department)
- {
- if (department.Sort > 0)
- {
- var exists = await _repository.ExistsAsync(Q
- .WhereNot(nameof(Department.Id), department.Id)
- .Where(nameof(Department.Sort), department.Sort)
- );
- if (exists)
- {
- throw new Exception("部门顺序已存在,请更换");
- }
- }
- return await _repository.UpdateAsync(department);
- }
- public async Task<IEnumerable<DepartmentResult>> GetListAsync(int parentId)
- {
- return await _repository.GetAllAsync<DepartmentResult>(Q.Where(nameof(Department.ParentId), parentId)
- .OrderBy(nameof(Department.Sort)));
- }
- /// <summary>
- /// 排除
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<IEnumerable<DepartmentResult>> GetListAsync(DepartmentUserRequest request)
- {
- return await _repository.GetAllAsync<DepartmentResult>(Q.Where(nameof(Department.ParentId), request.DepartmentId)
- .WhereNot(nameof(Department.Id), request.SourceDepartmentId));
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<Department> AddAsync(Department request)
- {
- if (request.ParentId > 0)
- {
- var parent = await _repository.GetAsync(request.ParentId);
- if (parent == null) throw new BusinessException("上级部门不存在");
- }
- if (request.LeaderId > 0)
- {
- var leader = await _repository.GetAsync(request.LeaderId);
- if (leader == null) throw new BusinessException("该主管不存在");
- }
- var department = _mapper.Map<Department>(request);
- department.Id = await InsertAsync(department);
- return department;
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<bool> DeleteAsync(int id)
- {
- var dep = await _repository.GetAsync(id);
- if (dep == null) throw new BusinessException("该部门不存在");
- if (await ExistsChildrenAsync(id))
- throw new BusinessException("请先删除下级部门");
- //TODO 是否解散部门群
- return await _repository.DeleteAsync(id);
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="query"></param>
- /// <returns></returns>
- public async Task<bool> DeleteAsync(SqlKata.Query query)
- {
- return await _repository.DeleteAsync(query) > 0;
- }
- /// <summary>
- /// 更新
- /// </summary>
- /// <param name="id"></param>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> UpdateAsync(int id, Department request)
- {
- var dep = await GetAsync(id);
- if (dep == null) throw new BusinessException("该部门不存在");
- dep.LeaderId = request.LeaderId;
- dep.Name = request.Name;
- dep.Sort = request.Sort;
- return await UpdateAsync(dep);
- }
- /// <summary>
- /// 获取树列表
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<List<DepartmentTreeResult>> GetTreeAsync(int id)
- {
- var departments = await GetListAsync(id);
- var list = _mapper.Map<List<DepartmentTreeResult>>(departments);
- foreach (var item in list)
- {
- item.HasChildren = await ExistsChildrenAsync(item.Id);
- }
- return list;
- }
- /// <summary>
- /// 根据部门ID获取成员以及下级部门
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<DepartmentUserResult> GetDepartmentUserResultAsync(DepartmentUserRequest request)
- {
- var result = new DepartmentUserResult();
- var departments = await GetListAsync(request);
- var departmentResults = await GetParentDepartmentResults(request.DepartmentIds.ToList());
- var departmentList = new List<DepartmentResult>();
- foreach (var item in departments)
- {
- if (departmentResults.Any(n => n.Id.Equals(item.Id)))
- departmentList.Add(item);
- }
- // if (departmentList.Count == 0)
- // return result;
- result.Departments = departmentList;
- //获取用户ID
- var departmentUsers = await departmentUserRepository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), request.DepartmentId).Select(nameof(Entity.DepartmentUser.UserId)));
- //判断是否包含该用户
- if (!departmentUsers.Contains(request.UserId))
- return result;
- var users = await _userRepository.GetAllAsync<UserInfoResult>(Q.WhereIn(nameof(User.Id), departmentUsers));
- result.Users = users.ToList();
- foreach (var item in result.Users)
- {
- item.TypeId = 0;
- item.TypeValue = 0;
- }
- for (int i = 0; i < result.Users.Count(); i++)
- {
- if (!request.IsShow)
- if (request.UserIds.Contains(result.Users[i].Id))
- result.Users.Remove(result.Users[i]);
- if (request.IsShow)
- if (request.UserIds.Contains(result.Users[i].Id))
- result.Users[i].IsDisable = true;
- }
- foreach (var user in result.Users)
- user.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
- return result;
- }
- /// <summary>
- /// 获取所用下级部门
- /// </summary>
- /// <param name="id"></param>
- /// <param name="departments"></param>
- /// <returns></returns>
- public async Task<List<DepartmentResult>> GetDepartmentById(int id, List<DepartmentResult> departments)
- {
- var department = await _repository.GetAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.Id), id));
- if (department != null)
- {
- departments.Add(department);
- var list = await _repository.GetAllAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.ParentId), department.Id));
- foreach (var item in list)
- {
- departments.Add(item);
- await GetDepartmentById(item.Id, departments);
- }
- }
- return departments;
- }
- /// <summary>
- /// 获取全部部门
- /// </summary>
- /// <returns></returns>
- public async Task<List<DepartmentTreeResult>> GetAllDepartmentAsync()
- {
- var DepartmentTreeResults = new List<DepartmentTreeResult>();
- var department = await _repository.GetAllAsync<DepartmentTreeResult>(Q.Where(nameof(Department.Id), ">", 0));
- DepartmentTreeResults.AddRange(department.Where(n => n.ParentId == 0));
- foreach (var item in DepartmentTreeResults)
- {
- await GetDepartmentById(item.Id, DepartmentTreeResults, department.ToList());
- }
- return DepartmentTreeResults;
- }
- /// <summary>
- /// 获取所用下级部门
- /// </summary>
- /// <param name="id"></param>
- /// <param name="departments"></param>
- /// <returns></returns>
- public async Task<List<DepartmentTreeResult>> GetDepartmentById(int id, List<DepartmentTreeResult> departments, List<DepartmentTreeResult> allDepartments)
- {
- GetDepartmentTreeResult(id, departments, allDepartments);
- var list = allDepartments.Where(n => n.ParentId == id);
- if (list.Any())
- {
- foreach (var item in list)
- {
- GetDepartmentTreeResult(item.Id, departments, allDepartments);
- }
- }
- return departments;
- }
- public List<DepartmentTreeResult> GetDepartmentTreeResult(int id, List<DepartmentTreeResult> data, List<DepartmentTreeResult> allDepartments)
- {
- var list = allDepartments.Where(n => n.ParentId == id).ToList();
- if (list == null || list.Count == 0)
- return data;
- foreach (var item in data)
- {
- if (data.Any(n => n.Id == id))
- {
- data.FirstOrDefault(n => n.Id == id).Children = list;
- data.FirstOrDefault(n => n.Id == id).HasChildren = true;
- break;
- }
- else
- {
- if (data.Any(n => n.HasChildren == true))
- {
- foreach (var children in data.Where(n => n.HasChildren == true))
- {
- if (children.Children.Any(n => n.Id == id))
- {
- foreach (var dd in children.Children)
- {
- if (dd.Id == id)
- {
- dd.Children = list;
- dd.HasChildren = true;
- break;
- }
- }
- }
- }
- }
- }
- }
- return data;
- }
- /// <summary>
- /// 获取所用下级部门
- /// </summary>
- /// <param name="id"></param>
- /// <param name="departments"></param>
- /// <returns></returns>
- public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments)
- {
- var departmentList = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), Pid));
- if (departmentList.Count == 0)
- return departments;
- foreach (var item in departmentList)
- {
- departments.Add(item);
- await GetDepartmentByPid(item.Id, departments);
- }
- return departments;
- }
- /// <summary>
- /// 获取所用上级部门
- /// </summary>
- /// <param name="id"></param>
- /// <param name="departments"></param>
- /// <returns></returns>
- public async Task<List<Department>> GetParentDepartmentById(int id, List<Department> departments)
- {
- var department = await _repository.GetAsync(id);
- departments.Add(department);
- if (department.ParentId == 0)
- return departments;
- department = await _repository.GetAsync(Q.Where(nameof(Entity.Department.ParentId), department.ParentId));
- if (department == null)
- return departments;
- //departments.Add(item);
- await GetParentDepartmentById(department.ParentId, departments);
- return departments;
- }
- /// <summary>
- /// 获取所用下级部门
- /// </summary>
- /// <param name="id"></param>
- /// <param name="departments"></param>
- /// <returns></returns>
- public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments, List<Department> allDepartments)
- {
- var departmentList = allDepartments.Where(n => n.ParentId == Pid);
- if (departmentList.Count() == 0)
- return departments;
- foreach (var item in departmentList)
- {
- departments.Add(item);
- await GetDepartmentByPid(item.Id, departments, allDepartments);
- }
- return departments;
- }
- /// <summary>
- /// 根据部门ID获取全名称
- /// </summary>
- /// <param name="departmentId"></param>
- /// <param name="departmentName"></param>
- /// <returns></returns>
- public async Task<string> GetDepartmentFullPathAsync(int departmentId, string departmentName)
- {
- var department = await _repository.GetAsync(departmentId);
- if (department == null)
- {
- return departmentName;
- }
- if (string.IsNullOrEmpty(departmentName))
- departmentName = department.Name;
- else
- departmentName = department.Name + "/" + departmentName;
- return await GetDepartmentFullPathAsync(department.ParentId, departmentName);
- }
- public async Task<IEnumerable<Department>> GetAllAsync(SqlKata.Query query)
- {
- return await _repository.GetAllAsync(query);
- }
- public async Task<IEnumerable<Entity.Department>> GetUserInDepartmentAsync(List<int> departmentIds)
- {
- //
- var departmentIdsList = new List<int>();
- var result = new DepartmentUserInsResult();
- result.Result = new List<DepartmentUserInResult>();
- var allDepartment = await GetAllAsync(Q.Where(nameof(Entity.Department.Id), ">", 0));
- foreach (var departmentId in departmentIds)
- {
- result.Result.Add(new DepartmentUserInResult
- {
- DepartmentId = departmentId,
- Item = await GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
- });
- }
- if (departmentIds.Count() == 1)
- departmentIdsList.AddRange(departmentIds);
- for (int i = 0; i < result.Result.Count; i++)
- {
- for (int k = 0; k < result.Result.Count; k++)
- {
- if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
- {
- departmentIdsList.Add(result.Result[i].DepartmentId);
- }
- else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
- {
- departmentIdsList.Add(result.Result[k].DepartmentId);
- // result.Result[i].DepartmentId = result.Result[k].DepartmentId;
- // result.Result[i].Item = result.Result[k].Item;
- }
- else
- {
- //departmentIdsList.Add(result.Result[i].DepartmentId);
- departmentIdsList.Add(result.Result[k].DepartmentId);
- }
- }
- }
- departmentIdsList = await GetDepartmentDistinct(allDepartment.ToList(), departmentIdsList.Distinct().ToList());
- departmentIdsList = departmentIdsList.Distinct().ToList();
- var departments = new List<Entity.Department>();
- foreach (var departmentId in departmentIdsList)
- {
- var item = await GetParentDepartmentById(departmentId, new List<Entity.Department>());
- departments.Add(item.Where(n=>n.ParentId==0).FirstOrDefault());
- }
- return departments;
- //
- }
- public async Task<List<int>> GetDepartmentDistinct(List<Entity.Department> allDepartment, List<int> departmentIds)
- {
- //
- var departmentIdsList = new List<int>();
- var result = new DepartmentUserInsResult();
- result.Result = new List<DepartmentUserInResult>();
- foreach (var departmentId in departmentIds)
- {
- result.Result.Add(new DepartmentUserInResult
- {
- DepartmentId = departmentId,
- Item = await GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
- });
- }
- if (departmentIds.Count() <= 2)
- departmentIdsList.AddRange(departmentIds);
- for (int i = 0; i < result.Result.Count; i++)
- {
- for (int k = i + 1; k < result.Result.Count; k++)
- {
- if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
- {
- departmentIdsList.Add(result.Result[i].DepartmentId);
- }
- else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
- {
- departmentIdsList.Add(result.Result[k].DepartmentId);
- }
- else
- {
- departmentIdsList.Add(result.Result[i].DepartmentId);
- }
- }
- }
- return departmentIdsList;
- }
- public async Task<IEnumerable<DepartmentResult>> GetParentDepartmentResults(List<int> departmentIds)
- {
- var departmentResults = new List<DepartmentResult>();
- foreach (var item in departmentIds)
- {
- await GetParentDepartmentResults(item, departmentResults);
- }
- return departmentResults;
- }
- public async Task<IEnumerable<DepartmentResult>> GetParentDepartmentResults(int departmentId, List<DepartmentResult> departmentResults)
- {
- var department = await _repository.GetAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.Id), departmentId));
- if (department == null)
- return departmentResults;
- departmentResults.Add(department);
- var parentDepartment = await _repository.GetAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.Id), department.ParentId));
- if (parentDepartment == null)
- return departmentResults;
- departmentResults.Add(parentDepartment);
- await GetParentDepartmentResults(parentDepartment.Id, departmentResults);
- return departmentResults;
- }
- }
- }
|