DepartmentRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. if (request.ParentId > 0)
  166. dep.ParentId = request.ParentId;
  167. return await UpdateAsync(dep);
  168. }
  169. /// <summary>
  170. /// 获取树列表
  171. /// </summary>
  172. /// <param name="id"></param>
  173. /// <returns></returns>
  174. public async Task<List<DepartmentTreeResult>> GetTreeAsync(int id)
  175. {
  176. var departments = await GetListAsync(id);
  177. var list = _mapper.Map<List<DepartmentTreeResult>>(departments);
  178. foreach (var item in list)
  179. {
  180. item.HasChildren = await ExistsChildrenAsync(item.Id);
  181. }
  182. return list;
  183. }
  184. /// <summary>
  185. /// 根据部门ID获取成员以及下级部门
  186. /// </summary>
  187. /// <param name="request"></param>
  188. /// <returns></returns>
  189. public async Task<DepartmentUserResult> GetDepartmentUserResultAsync(DepartmentUserRequest request)
  190. {
  191. var result = new DepartmentUserResult();
  192. var departments = await GetListAsync(request);
  193. result.Departments = departments;
  194. var users = await _userRepository.GetAllAsync(Q.Where(nameof(User.DepartmentId), request.DepartmentId));
  195. result.Users = users.ToList();
  196. for (int i = 0; i < result.Users.Count(); i++)
  197. {
  198. if (!request.IsShow)
  199. if (request.UserIds.Contains(result.Users[i].Id))
  200. result.Users.Remove(result.Users[i]);
  201. if (request.IsShow)
  202. if (request.UserIds.Contains(result.Users[i].Id))
  203. result.Users[i].IsDisable = true;
  204. }
  205. foreach (var user in result.Users)
  206. user.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  207. return result;
  208. }
  209. /// <summary>
  210. /// 获取所用下级部门
  211. /// </summary>
  212. /// <param name="id"></param>
  213. /// <param name="departments"></param>
  214. /// <returns></returns>
  215. public async Task<List<Department>> GetDepartmentById(int id, List<Department> departments)
  216. {
  217. var department = await _repository.GetAsync(id);
  218. if (department != null)
  219. {
  220. departments.Add(department);
  221. var list = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), department.Id));
  222. foreach (var item in list)
  223. {
  224. departments.Add(item);
  225. await GetDepartmentById(item.Id, departments);
  226. }
  227. }
  228. return departments;
  229. }
  230. /// <summary>
  231. /// 获取所用下级部门
  232. /// </summary>
  233. /// <param name="id"></param>
  234. /// <param name="departments"></param>
  235. /// <returns></returns>
  236. public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments)
  237. {
  238. var departmentList = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), Pid));
  239. if (departmentList.Count == 0)
  240. return departments;
  241. foreach (var item in departmentList)
  242. {
  243. departments.Add(item);
  244. await GetDepartmentByPid(item.Id, departments);
  245. }
  246. return departments;
  247. }
  248. /// <summary>
  249. /// 根据部门ID获取全名称
  250. /// </summary>
  251. /// <param name="departmentId"></param>
  252. /// <param name="departmentName"></param>
  253. /// <returns></returns>
  254. public async Task<string> GetDepartmentFullPathAsync(int departmentId, string departmentName)
  255. {
  256. var department = await _repository.GetAsync(departmentId);
  257. if (department == null)
  258. {
  259. return departmentName;
  260. }
  261. if (string.IsNullOrEmpty(departmentName))
  262. departmentName = department.Name;
  263. else
  264. departmentName = department.Name + "/" + departmentName;
  265. return await GetDepartmentFullPathAsync(department.ParentId, departmentName);
  266. }
  267. public async Task<IEnumerable<Department>> GetAllAsync(SqlKata.Query query)
  268. {
  269. return await _repository.GetAllAsync(query);
  270. }
  271. }
  272. }