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 _repository; private readonly Repository _userRepository; private readonly Repository departmentUserRepository; public DepartmentRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _userRepository = new Repository(database); departmentUserRepository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; /// /// 是否存在下级部门 /// /// /// public async Task ExistsChildrenAsync(int parentId) { return await _repository.ExistsAsync(Q.Where(nameof(Department.ParentId), parentId)); } public async Task GetAsync(int id) { var department = await _repository.GetAsync(id); return department; } /// /// 根据部门名称查询对应的数据 /// /// /// public async Task GetAsync(string name) { return await _repository.GetAsync(Q.Where(nameof(Department.Name), name)); } public async Task GetNameAsync(int id) { if (id <= 0) return string.Empty; return await _repository.GetAsync(Q .Select(nameof(Department.Name)) .Where(nameof(Department.Id), id) ); } public async Task 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 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> GetListAsync(int parentId) { return await _repository.GetAllAsync(Q.Where(nameof(Department.ParentId), parentId) .OrderBy(nameof(Department.Sort))); } /// /// 排除 /// /// /// public async Task> GetListAsync(DepartmentUserRequest request) { return await _repository.GetAllAsync(Q.Where(nameof(Department.ParentId), request.DepartmentId) .WhereNot(nameof(Department.Id), request.SourceDepartmentId)); } /// /// 添加 /// /// /// public async Task 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(request); department.Id = await InsertAsync(department); return department; } /// /// 删除 /// /// /// public async Task 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); } /// /// 删除 /// /// /// public async Task DeleteAsync(SqlKata.Query query) { return await _repository.DeleteAsync(query) > 0; } /// /// 更新 /// /// /// /// public async Task 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); } /// /// 获取树列表 /// /// /// public async Task> GetTreeAsync(int id) { var departments = await GetListAsync(id); var list = _mapper.Map>(departments); foreach (var item in list) { item.HasChildren = await ExistsChildrenAsync(item.Id); } return list; } /// /// 根据部门ID获取成员以及下级部门 /// /// /// public async Task GetDepartmentUserResultAsync(DepartmentUserRequest request) { var result = new DepartmentUserResult(); var departments = await GetListAsync(request); var departmentResults = await GetParentDepartmentResults(request.DepartmentIds.ToList()); var departmentList = new List(); 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(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(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; } /// /// 获取所用下级部门 /// /// /// /// public async Task> GetDepartmentById(int id, List departments) { var department = await _repository.GetAsync(Q.Where(nameof(Entity.Department.Id), id)); if (department != null) { departments.Add(department); var list = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), department.Id)); foreach (var item in list) { departments.Add(item); await GetDepartmentById(item.Id, departments); } } return departments; } /// /// 获取全部部门 /// /// public async Task> GetAllDepartmentAsync() { var DepartmentTreeResults = new List(); var department = await _repository.GetAllAsync(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; } /// /// 获取所用下级部门 /// /// /// /// public async Task> GetDepartmentById(int id, List departments, List 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 GetDepartmentTreeResult(int id, List data, List 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; } /// /// 获取所用下级部门 /// /// /// /// public async Task> GetDepartmentByPid(int Pid, List 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; } /// /// 获取所用下级部门 /// /// /// /// public async Task> GetDepartmentByPid(int Pid, List departments, List 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; } /// /// 根据部门ID获取全名称 /// /// /// /// public async Task 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> GetAllAsync(SqlKata.Query query) { return await _repository.GetAllAsync(query); } public async Task> GetUserInDepartmentAsync(List departmentIds) { // var departmentIdsList = new List(); var result = new DepartmentUserInsResult(); result.Result = new List(); 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(), allDepartment.ToList()) }); } if (departmentIds.Count() == 1) 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); 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()); return await GetAllAsync(Q.WhereIn(nameof(Entity.Department.Id), departmentIdsList)); // } public async Task> GetDepartmentDistinct(List allDepartment, List departmentIds) { // var departmentIdsList = new List(); var result = new DepartmentUserInsResult(); result.Result = new List(); foreach (var departmentId in departmentIds) { result.Result.Add(new DepartmentUserInResult { DepartmentId = departmentId, Item = await GetDepartmentByPid(departmentId, new List(), 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> GetParentDepartmentResults(List departmentIds) { var departmentResults = new List(); foreach (var item in departmentIds) { await GetParentDepartmentResults(item, departmentResults); } return departmentResults; } public async Task> GetParentDepartmentResults(int departmentId, List departmentResults) { var department = await _repository.GetAsync(Q.Where(nameof(Entity.Department.Id), departmentId)); if (department == null) return departmentResults; departmentResults.Add(department); var parentDepartment = await _repository.GetAsync(Q.Where(nameof(Entity.Department.Id), department.ParentId)); if (parentDepartment == null) return departmentResults; departmentResults.Add(parentDepartment); await GetParentDepartmentResults(parentDepartment.Id, departmentResults); return departmentResults; } } }