DepartmentRepository.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. /// 获取树列表
  186. /// </summary>
  187. /// <param name="id"></param>
  188. /// <returns></returns>
  189. public async Task<List<DepartmentTreeResult>> GetTreeAsync()
  190. {
  191. var departments = await GetAllAsync(Q.Where(nameof(Department.Id), ">", 0));
  192. var departmentParent = departments.Where(n => n.ParentId == 0);
  193. var list = _mapper.Map<List<DepartmentTreeResult>>(departmentParent);
  194. foreach (var item in list)
  195. {
  196. item.HasChildren = departments.Any(n => n.ParentId == item.Id);
  197. if (item.HasChildren)
  198. {
  199. var departmentChildren = departments.Where(n => n.ParentId == item.Id);
  200. var departmentChildrenTree = departmentChildren.Select(n => _mapper.Map<DepartmentTreeResult>(n));
  201. item.ChildrenItems = GetTreeAsync(departments.ToList(), item.Id, departmentChildrenTree.ToList());
  202. }
  203. }
  204. return list;
  205. }
  206. public List<DepartmentTreeResult> GetTreeAsync(List<Department> departments, int id, List<DepartmentTreeResult> items)
  207. {
  208. foreach (var item in items)
  209. {
  210. item.HasChildren = departments.Any(n => n.ParentId == item.Id);
  211. if (item.HasChildren)
  212. {
  213. var departmentParent = departments.Where(n => n.ParentId == item.Id);
  214. item.ChildrenItems = _mapper.Map<List<DepartmentTreeResult>>(departmentParent);
  215. foreach (var childrenItem in item.ChildrenItems)
  216. {
  217. GetTreeAsync(departments, childrenItem.Id, items);
  218. }
  219. }
  220. return items;
  221. }
  222. return items;
  223. }
  224. /// <summary>
  225. /// 根据部门ID获取成员以及下级部门
  226. /// </summary>
  227. /// <param name="request"></param>
  228. /// <returns></returns>
  229. public async Task<DepartmentUserResult> GetDepartmentUserResultAsync(DepartmentUserRequest request)
  230. {
  231. var result = new DepartmentUserResult();
  232. var departments = await GetListAsync(request);
  233. result.Departments = departments;
  234. var users = await _userRepository.GetAllAsync(Q.Where(nameof(User.DepartmentId), request.DepartmentId));
  235. result.Users = users.ToList();
  236. for (int i = 0; i < result.Users.Count(); i++)
  237. {
  238. if (!request.IsShow)
  239. if (request.UserIds.Contains(result.Users[i].Id))
  240. result.Users.Remove(result.Users[i]);
  241. if (request.IsShow)
  242. if (request.UserIds.Contains(result.Users[i].Id))
  243. result.Users[i].IsDisable = true;
  244. }
  245. foreach (var user in result.Users)
  246. user.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  247. return result;
  248. }
  249. /// <summary>
  250. /// 获取所用下级部门
  251. /// </summary>
  252. /// <param name="id"></param>
  253. /// <param name="departments"></param>
  254. /// <returns></returns>
  255. public async Task<List<Department>> GetDepartmentById(int id, List<Department> departments)
  256. {
  257. var department = await _repository.GetAsync(id);
  258. if (department != null)
  259. {
  260. departments.Add(department);
  261. var list = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), department.Id));
  262. foreach (var item in list)
  263. {
  264. departments.Add(item);
  265. await GetDepartmentById(item.Id, departments);
  266. }
  267. }
  268. return departments;
  269. }
  270. /// <summary>
  271. /// 获取所用下级部门
  272. /// </summary>
  273. /// <param name="id"></param>
  274. /// <param name="departments"></param>
  275. /// <returns></returns>
  276. public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments)
  277. {
  278. var departmentList = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), Pid));
  279. if (departmentList.Count == 0)
  280. return departments;
  281. foreach (var item in departmentList)
  282. {
  283. departments.Add(item);
  284. await GetDepartmentByPid(item.Id, departments);
  285. }
  286. return departments;
  287. }
  288. /// <summary>
  289. /// 根据部门ID获取全名称
  290. /// </summary>
  291. /// <param name="departmentId"></param>
  292. /// <param name="departmentName"></param>
  293. /// <returns></returns>
  294. public async Task<string> GetDepartmentFullPathAsync(int departmentId, string departmentName)
  295. {
  296. var department = await _repository.GetAsync(departmentId);
  297. if (department == null)
  298. {
  299. return departmentName;
  300. }
  301. if (string.IsNullOrEmpty(departmentName))
  302. departmentName = department.Name;
  303. else
  304. departmentName = department.Name + "/" + departmentName;
  305. return await GetDepartmentFullPathAsync(department.ParentId, departmentName);
  306. }
  307. public async Task<IEnumerable<Department>> GetAllAsync(SqlKata.Query query)
  308. {
  309. return await _repository.GetAllAsync(query);
  310. }
  311. }
  312. }