DepartmentRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using AutoMapper;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Exceptions;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity;
  10. using GxPress.Repository.Interface;
  11. using GxPress.Request.Department;
  12. using GxPress.Result.Department;
  13. using Microsoft.Extensions.Options;
  14. using Datory;
  15. namespace GxPress.Repository.Implement
  16. {
  17. public class DepartmentRepository : IDepartmentRepository
  18. {
  19. private readonly IMapper _mapper;
  20. private readonly Repository<Department> _repository;
  21. private readonly Repository<User> _userRepository;
  22. public DepartmentRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  23. {
  24. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  25. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  26. _repository = new Repository<Department>(database);
  27. _userRepository = new Repository<User>(database);
  28. _mapper = mapper;
  29. }
  30. public IDatabase Database => _repository.Database;
  31. public string TableName => _repository.TableName;
  32. public List<TableColumn> TableColumns => _repository.TableColumns;
  33. /// <summary>
  34. /// 是否存在下级部门
  35. /// </summary>
  36. /// <param name="parentId"></param>
  37. /// <returns></returns>
  38. public async Task<bool> ExistsChildrenAsync(int parentId)
  39. {
  40. return await _repository.ExistsAsync(Q.Where(nameof(Department.ParentId), parentId));
  41. }
  42. public async Task<Department> GetAsync(int id)
  43. {
  44. var department = await _repository.GetAsync(id);
  45. return department;
  46. }
  47. /// <summary>
  48. /// 根据部门名称查询对应的数据
  49. /// </summary>
  50. /// <param name="name"></param>
  51. /// <returns></returns>
  52. public async Task<Department> GetAsync(string name)
  53. {
  54. return await _repository.GetAsync(Q.Where(nameof(Department.Name), name));
  55. }
  56. public async Task<string> GetNameAsync(int id)
  57. {
  58. if (id <= 0) return string.Empty;
  59. return await _repository.GetAsync<string>(Q
  60. .Select(nameof(Department.Name))
  61. .Where(nameof(Department.Id), id)
  62. );
  63. }
  64. public async Task<int> InsertAsync(Department department)
  65. {
  66. if (department.Sort > 0)
  67. {
  68. var exists = await _repository.ExistsAsync(Q
  69. .Where(nameof(Department.Sort), department.Sort)
  70. );
  71. if (exists)
  72. {
  73. throw new Exception("部门顺序已存在,请更换");
  74. }
  75. }
  76. return await _repository.InsertAsync(department);
  77. }
  78. public async Task<bool> UpdateAsync(Department department)
  79. {
  80. if (department.Sort > 0)
  81. {
  82. var exists = await _repository.ExistsAsync(Q
  83. .WhereNot(nameof(Department.Id), department.Id)
  84. .Where(nameof(Department.Sort), department.Sort)
  85. );
  86. if (exists)
  87. {
  88. throw new Exception("部门顺序已存在,请更换");
  89. }
  90. }
  91. return await _repository.UpdateAsync(department);
  92. }
  93. public async Task<IEnumerable<Department>> GetListAsync(int parentId)
  94. {
  95. return await _repository.GetAllAsync(Q.Where(nameof(Department.ParentId), parentId)
  96. .OrderBy(nameof(Department.Sort)));
  97. }
  98. /// <summary>
  99. /// 排除
  100. /// </summary>
  101. /// <param name="request"></param>
  102. /// <returns></returns>
  103. public async Task<IEnumerable<Department>> GetListAsync(DepartmentUserRequest request)
  104. {
  105. return await _repository.GetAllAsync(Q.Where(nameof(Department.ParentId), request.DepartmentId)
  106. .WhereNot(nameof(Department.Id), request.SourceDepartmentId));
  107. }
  108. /// <summary>
  109. /// 添加
  110. /// </summary>
  111. /// <param name="request"></param>
  112. /// <returns></returns>
  113. public async Task<Department> AddAsync(Department request)
  114. {
  115. if (request.ParentId > 0)
  116. {
  117. var parent = await _repository.GetAsync(request.ParentId);
  118. if (parent == null) throw new BusinessException("上级部门不存在");
  119. }
  120. if (request.LeaderId > 0)
  121. {
  122. var leader = await _repository.GetAsync(request.LeaderId);
  123. if (leader == null) throw new BusinessException("该主管不存在");
  124. }
  125. var department = _mapper.Map<Department>(request);
  126. department.Id = await InsertAsync(department);
  127. return department;
  128. }
  129. /// <summary>
  130. /// 删除
  131. /// </summary>
  132. /// <param name="id"></param>
  133. /// <returns></returns>
  134. public async Task<bool> DeleteAsync(int id)
  135. {
  136. var dep = await _repository.GetAsync(id);
  137. if (dep == null) throw new BusinessException("该部门不存在");
  138. if (await ExistsChildrenAsync(id))
  139. throw new BusinessException("请先删除下级部门");
  140. //TODO 是否解散部门群
  141. return await _repository.DeleteAsync(id);
  142. }
  143. /// <summary>
  144. /// 删除
  145. /// </summary>
  146. /// <param name="query"></param>
  147. /// <returns></returns>
  148. public async Task<bool> DeleteAsync(SqlKata.Query query)
  149. {
  150. return await _repository.DeleteAsync(query) > 0;
  151. }
  152. /// <summary>
  153. /// 更新
  154. /// </summary>
  155. /// <param name="id"></param>
  156. /// <param name="request"></param>
  157. /// <returns></returns>
  158. public async Task<bool> UpdateAsync(int id, Department request)
  159. {
  160. var dep = await GetAsync(id);
  161. if (dep == null) throw new BusinessException("该部门不存在");
  162. dep.LeaderId = request.LeaderId;
  163. dep.Name = request.Name;
  164. dep.Sort = request.Sort;
  165. return await UpdateAsync(dep);
  166. }
  167. /// <summary>
  168. /// 获取树列表
  169. /// </summary>
  170. /// <param name="id"></param>
  171. /// <returns></returns>
  172. public async Task<List<DepartmentTreeResult>> GetTreeAsync(int id)
  173. {
  174. var departments = await GetListAsync(id);
  175. var list = _mapper.Map<List<DepartmentTreeResult>>(departments);
  176. foreach (var item in list)
  177. {
  178. item.HasChildren = await ExistsChildrenAsync(item.Id);
  179. }
  180. return list;
  181. }
  182. /// <summary>
  183. /// 根据部门ID获取成员以及下级部门
  184. /// </summary>
  185. /// <param name="request"></param>
  186. /// <returns></returns>
  187. public async Task<DepartmentUserResult> GetDepartmentUserResultAsync(DepartmentUserRequest request)
  188. {
  189. var result = new DepartmentUserResult();
  190. var departments = await GetListAsync(request);
  191. result.Departments = departments;
  192. var users = await _userRepository.GetAllAsync(Q.Where(nameof(User.DepartmentId), request.DepartmentId));
  193. result.Users = users.ToList();
  194. for (int i = 0; i < result.Users.Count(); i++)
  195. {
  196. if (!request.IsShow)
  197. if (request.UserIds.Contains(result.Users[i].Id))
  198. result.Users.Remove(result.Users[i]);
  199. if (request.IsShow)
  200. if (request.UserIds.Contains(result.Users[i].Id))
  201. result.Users[i].IsDisable = true;
  202. }
  203. foreach (var user in result.Users)
  204. user.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  205. return result;
  206. }
  207. /// <summary>
  208. /// 获取所用下级部门
  209. /// </summary>
  210. /// <param name="id"></param>
  211. /// <param name="departments"></param>
  212. /// <returns></returns>
  213. public async Task<List<Department>> GetDepartmentById(int id, List<Department> departments)
  214. {
  215. var department = await _repository.GetAsync(id);
  216. if (department != null)
  217. {
  218. departments.Add(department);
  219. var list = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), department.Id));
  220. foreach (var item in list)
  221. {
  222. departments.Add(item);
  223. await GetDepartmentById(item.Id, departments);
  224. }
  225. }
  226. return departments;
  227. }
  228. /// <summary>
  229. /// 获取所用下级部门
  230. /// </summary>
  231. /// <param name="id"></param>
  232. /// <param name="departments"></param>
  233. /// <returns></returns>
  234. public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments)
  235. {
  236. var departmentList = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), Pid));
  237. if (departmentList.Count == 0)
  238. return departments;
  239. foreach (var item in departmentList)
  240. {
  241. departments.Add(item);
  242. await GetDepartmentByPid(item.Id, departments);
  243. }
  244. return departments;
  245. }
  246. /// <summary>
  247. /// 根据部门ID获取全名称
  248. /// </summary>
  249. /// <param name="departmentId"></param>
  250. /// <param name="departmentName"></param>
  251. /// <returns></returns>
  252. public async Task<string> GetDepartmentFullPathAsync(int departmentId, string departmentName)
  253. {
  254. var department = await _repository.GetAsync(departmentId);
  255. if (department == null)
  256. {
  257. return departmentName;
  258. }
  259. if (string.IsNullOrEmpty(departmentName))
  260. departmentName = department.Name;
  261. else
  262. departmentName = department.Name + "/" + departmentName;
  263. return await GetDepartmentFullPathAsync(department.ParentId, departmentName);
  264. }
  265. public async Task<IEnumerable<Department>> GetAllAsync(SqlKata.Query query)
  266. {
  267. return await _repository.GetAllAsync(query);
  268. }
  269. }
  270. }