DepartmentRepository.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. using GxPress.Result.User;
  16. namespace GxPress.Repository.Implement
  17. {
  18. public class DepartmentRepository : IDepartmentRepository
  19. {
  20. private readonly IMapper _mapper;
  21. private readonly Repository<Department> _repository;
  22. private readonly Repository<User> _userRepository;
  23. private readonly Repository<Entity.DepartmentUser> departmentUserRepository;
  24. public DepartmentRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  25. {
  26. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  27. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  28. _repository = new Repository<Department>(database);
  29. _userRepository = new Repository<User>(database);
  30. departmentUserRepository = new Repository<Entity.DepartmentUser>(database);
  31. _mapper = mapper;
  32. }
  33. public IDatabase Database => _repository.Database;
  34. public string TableName => _repository.TableName;
  35. public List<TableColumn> TableColumns => _repository.TableColumns;
  36. /// <summary>
  37. /// 是否存在下级部门
  38. /// </summary>
  39. /// <param name="parentId"></param>
  40. /// <returns></returns>
  41. public async Task<bool> ExistsChildrenAsync(int parentId)
  42. {
  43. return await _repository.ExistsAsync(Q.Where(nameof(Department.ParentId), parentId));
  44. }
  45. public async Task<Department> GetAsync(int id)
  46. {
  47. var department = await _repository.GetAsync(id);
  48. return department;
  49. }
  50. /// <summary>
  51. /// 根据部门名称查询对应的数据
  52. /// </summary>
  53. /// <param name="name"></param>
  54. /// <returns></returns>
  55. public async Task<Department> GetAsync(string name)
  56. {
  57. return await _repository.GetAsync(Q.Where(nameof(Department.Name), name));
  58. }
  59. public async Task<string> GetNameAsync(int id)
  60. {
  61. if (id <= 0) return string.Empty;
  62. return await _repository.GetAsync<string>(Q
  63. .Select(nameof(Department.Name))
  64. .Where(nameof(Department.Id), id)
  65. );
  66. }
  67. public async Task<int> InsertAsync(Department department)
  68. {
  69. if (department.Sort > 0)
  70. {
  71. var exists = await _repository.ExistsAsync(Q
  72. .Where(nameof(Department.Sort), department.Sort)
  73. );
  74. if (exists)
  75. {
  76. throw new Exception("部门顺序已存在,请更换");
  77. }
  78. }
  79. return await _repository.InsertAsync(department);
  80. }
  81. public async Task<bool> UpdateAsync(Department department)
  82. {
  83. if (department.Sort > 0)
  84. {
  85. var exists = await _repository.ExistsAsync(Q
  86. .WhereNot(nameof(Department.Id), department.Id)
  87. .Where(nameof(Department.Sort), department.Sort)
  88. );
  89. if (exists)
  90. {
  91. throw new Exception("部门顺序已存在,请更换");
  92. }
  93. }
  94. return await _repository.UpdateAsync(department);
  95. }
  96. public async Task<IEnumerable<DepartmentResult>> GetListAsync(int parentId)
  97. {
  98. return await _repository.GetAllAsync<DepartmentResult>(Q.Where(nameof(Department.ParentId), parentId)
  99. .OrderBy(nameof(Department.Sort)));
  100. }
  101. /// <summary>
  102. /// 排除
  103. /// </summary>
  104. /// <param name="request"></param>
  105. /// <returns></returns>
  106. public async Task<IEnumerable<DepartmentResult>> GetListAsync(DepartmentUserRequest request)
  107. {
  108. return await _repository.GetAllAsync<DepartmentResult>(Q.Where(nameof(Department.ParentId), request.DepartmentId)
  109. .WhereNot(nameof(Department.Id), request.SourceDepartmentId));
  110. }
  111. /// <summary>
  112. /// 添加
  113. /// </summary>
  114. /// <param name="request"></param>
  115. /// <returns></returns>
  116. public async Task<Department> AddAsync(Department request)
  117. {
  118. if (request.ParentId > 0)
  119. {
  120. var parent = await _repository.GetAsync(request.ParentId);
  121. if (parent == null) throw new BusinessException("上级部门不存在");
  122. }
  123. if (request.LeaderId > 0)
  124. {
  125. var leader = await _repository.GetAsync(request.LeaderId);
  126. if (leader == null) throw new BusinessException("该主管不存在");
  127. }
  128. var department = _mapper.Map<Department>(request);
  129. department.Id = await InsertAsync(department);
  130. return department;
  131. }
  132. /// <summary>
  133. /// 删除
  134. /// </summary>
  135. /// <param name="id"></param>
  136. /// <returns></returns>
  137. public async Task<bool> DeleteAsync(int id)
  138. {
  139. var dep = await _repository.GetAsync(id);
  140. if (dep == null) throw new BusinessException("该部门不存在");
  141. if (await ExistsChildrenAsync(id))
  142. throw new BusinessException("请先删除下级部门");
  143. //TODO 是否解散部门群
  144. return await _repository.DeleteAsync(id);
  145. }
  146. /// <summary>
  147. /// 删除
  148. /// </summary>
  149. /// <param name="query"></param>
  150. /// <returns></returns>
  151. public async Task<bool> DeleteAsync(SqlKata.Query query)
  152. {
  153. return await _repository.DeleteAsync(query) > 0;
  154. }
  155. /// <summary>
  156. /// 更新
  157. /// </summary>
  158. /// <param name="id"></param>
  159. /// <param name="request"></param>
  160. /// <returns></returns>
  161. public async Task<bool> UpdateAsync(int id, Department request)
  162. {
  163. var dep = await GetAsync(id);
  164. if (dep == null) throw new BusinessException("该部门不存在");
  165. dep.LeaderId = request.LeaderId;
  166. dep.Name = request.Name;
  167. dep.Sort = request.Sort;
  168. return await UpdateAsync(dep);
  169. }
  170. /// <summary>
  171. /// 获取树列表
  172. /// </summary>
  173. /// <param name="id"></param>
  174. /// <returns></returns>
  175. public async Task<List<DepartmentTreeResult>> GetTreeAsync(int id)
  176. {
  177. var departments = await GetListAsync(id);
  178. var list = _mapper.Map<List<DepartmentTreeResult>>(departments);
  179. foreach (var item in list)
  180. {
  181. item.HasChildren = await ExistsChildrenAsync(item.Id);
  182. }
  183. return list;
  184. }
  185. /// <summary>
  186. /// 根据部门ID获取成员以及下级部门
  187. /// </summary>
  188. /// <param name="request"></param>
  189. /// <returns></returns>
  190. public async Task<DepartmentUserResult> GetDepartmentUserResultAsync(DepartmentUserRequest request)
  191. {
  192. var result = new DepartmentUserResult();
  193. var departments = await GetListAsync(request);
  194. var departmentResults = await GetParentDepartmentResults(request.DepartmentIds.ToList());
  195. var departmentList = new List<DepartmentResult>();
  196. foreach (var item in departments)
  197. {
  198. if (departmentResults.Any(n => n.Id.Equals(item.Id)))
  199. departmentList.Add(item);
  200. }
  201. // if (departmentList.Count == 0)
  202. // return result;
  203. result.Departments = departmentList;
  204. //获取用户ID
  205. var departmentUsers = await departmentUserRepository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), request.DepartmentId).Select(nameof(Entity.DepartmentUser.UserId)));
  206. //判断是否包含该用户
  207. if (!departmentUsers.Contains(request.UserId))
  208. return result;
  209. var users = await _userRepository.GetAllAsync<UserInfoResult>(Q.WhereIn(nameof(User.Id), departmentUsers));
  210. result.Users = users.ToList();
  211. foreach (var item in result.Users)
  212. {
  213. item.TypeId = 0;
  214. item.TypeValue = 0;
  215. }
  216. for (int i = 0; i < result.Users.Count(); i++)
  217. {
  218. if (!request.IsShow)
  219. if (request.UserIds.Contains(result.Users[i].Id))
  220. result.Users.Remove(result.Users[i]);
  221. if (request.IsShow)
  222. if (request.UserIds.Contains(result.Users[i].Id))
  223. result.Users[i].IsDisable = true;
  224. }
  225. foreach (var user in result.Users)
  226. user.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  227. return result;
  228. }
  229. /// <summary>
  230. /// 获取所用下级部门
  231. /// </summary>
  232. /// <param name="id"></param>
  233. /// <param name="departments"></param>
  234. /// <returns></returns>
  235. public async Task<List<DepartmentResult>> GetDepartmentById(int id, List<DepartmentResult> departments)
  236. {
  237. var department = await _repository.GetAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.Id), id));
  238. if (department != null)
  239. {
  240. departments.Add(department);
  241. var list = await _repository.GetAllAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.ParentId), department.Id));
  242. foreach (var item in list)
  243. {
  244. departments.Add(item);
  245. await GetDepartmentById(item.Id, departments);
  246. }
  247. }
  248. return departments;
  249. }
  250. /// <summary>
  251. /// 获取全部部门
  252. /// </summary>
  253. /// <returns></returns>
  254. public async Task<List<DepartmentTreeResult>> GetAllDepartmentAsync()
  255. {
  256. var DepartmentTreeResults = new List<DepartmentTreeResult>();
  257. var department = await _repository.GetAllAsync<DepartmentTreeResult>(Q.Where(nameof(Department.Id), ">", 0));
  258. DepartmentTreeResults.AddRange(department.Where(n => n.ParentId == 0));
  259. foreach (var item in DepartmentTreeResults)
  260. {
  261. await GetDepartmentById(item.Id, DepartmentTreeResults, department.ToList());
  262. }
  263. return DepartmentTreeResults;
  264. }
  265. /// <summary>
  266. /// 获取所用下级部门
  267. /// </summary>
  268. /// <param name="id"></param>
  269. /// <param name="departments"></param>
  270. /// <returns></returns>
  271. public async Task<List<DepartmentTreeResult>> GetDepartmentById(int id, List<DepartmentTreeResult> departments, List<DepartmentTreeResult> allDepartments)
  272. {
  273. GetDepartmentTreeResult(id, departments, allDepartments);
  274. var list = allDepartments.Where(n => n.ParentId == id);
  275. if (list.Any())
  276. {
  277. foreach (var item in list)
  278. {
  279. GetDepartmentTreeResult(item.Id, departments, allDepartments);
  280. }
  281. }
  282. return departments;
  283. }
  284. public List<DepartmentTreeResult> GetDepartmentTreeResult(int id, List<DepartmentTreeResult> data, List<DepartmentTreeResult> allDepartments)
  285. {
  286. var list = allDepartments.Where(n => n.ParentId == id).ToList();
  287. if (list == null || list.Count == 0)
  288. return data;
  289. foreach (var item in data)
  290. {
  291. if (data.Any(n => n.Id == id))
  292. {
  293. data.FirstOrDefault(n => n.Id == id).Children = list;
  294. data.FirstOrDefault(n => n.Id == id).HasChildren = true;
  295. break;
  296. }
  297. else
  298. {
  299. if (data.Any(n => n.HasChildren == true))
  300. {
  301. foreach (var children in data.Where(n => n.HasChildren == true))
  302. {
  303. if (children.Children.Any(n => n.Id == id))
  304. {
  305. foreach (var dd in children.Children)
  306. {
  307. if (dd.Id == id)
  308. {
  309. dd.Children = list;
  310. dd.HasChildren = true;
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. return data;
  320. }
  321. /// <summary>
  322. /// 获取所用下级部门
  323. /// </summary>
  324. /// <param name="id"></param>
  325. /// <param name="departments"></param>
  326. /// <returns></returns>
  327. public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments)
  328. {
  329. var departmentList = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), Pid));
  330. if (departmentList.Count == 0)
  331. return departments;
  332. foreach (var item in departmentList)
  333. {
  334. departments.Add(item);
  335. await GetDepartmentByPid(item.Id, departments);
  336. }
  337. return departments;
  338. }
  339. /// <summary>
  340. /// 获取所用下级部门
  341. /// </summary>
  342. /// <param name="id"></param>
  343. /// <param name="departments"></param>
  344. /// <returns></returns>
  345. public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments, List<Department> allDepartments)
  346. {
  347. var departmentList = allDepartments.Where(n => n.ParentId == Pid);
  348. if (departmentList.Count() == 0)
  349. return departments;
  350. foreach (var item in departmentList)
  351. {
  352. departments.Add(item);
  353. await GetDepartmentByPid(item.Id, departments, allDepartments);
  354. }
  355. return departments;
  356. }
  357. /// <summary>
  358. /// 根据部门ID获取全名称
  359. /// </summary>
  360. /// <param name="departmentId"></param>
  361. /// <param name="departmentName"></param>
  362. /// <returns></returns>
  363. public async Task<string> GetDepartmentFullPathAsync(int departmentId, string departmentName)
  364. {
  365. var department = await _repository.GetAsync(departmentId);
  366. if (department == null)
  367. {
  368. return departmentName;
  369. }
  370. if (string.IsNullOrEmpty(departmentName))
  371. departmentName = department.Name;
  372. else
  373. departmentName = department.Name + "/" + departmentName;
  374. return await GetDepartmentFullPathAsync(department.ParentId, departmentName);
  375. }
  376. public async Task<IEnumerable<Department>> GetAllAsync(SqlKata.Query query)
  377. {
  378. return await _repository.GetAllAsync(query);
  379. }
  380. public async Task<IEnumerable<Entity.Department>> GetUserInDepartmentAsync(List<int> departmentIds)
  381. {
  382. //
  383. var departmentIdsList = new List<int>();
  384. var result = new DepartmentUserInsResult();
  385. result.Result = new List<DepartmentUserInResult>();
  386. var allDepartment = await GetAllAsync(Q.Where(nameof(Entity.Department.Id), ">", 0));
  387. foreach (var departmentId in departmentIds)
  388. {
  389. result.Result.Add(new DepartmentUserInResult
  390. {
  391. DepartmentId = departmentId,
  392. Item = await GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
  393. });
  394. }
  395. if (departmentIds.Count() == 1)
  396. departmentIdsList.AddRange(departmentIds);
  397. for (int i = 0; i < result.Result.Count; i++)
  398. {
  399. for (int k = i + 1; k < result.Result.Count; k++)
  400. {
  401. if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
  402. {
  403. departmentIdsList.Add(result.Result[i].DepartmentId);
  404. }
  405. else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
  406. {
  407. departmentIdsList.Add(result.Result[k].DepartmentId);
  408. result.Result[i].DepartmentId = result.Result[k].DepartmentId;
  409. result.Result[i].Item = result.Result[k].Item;
  410. }
  411. else
  412. {
  413. //departmentIdsList.Add(result.Result[i].DepartmentId);
  414. departmentIdsList.Add(result.Result[k].DepartmentId);
  415. }
  416. }
  417. }
  418. departmentIdsList = await GetDepartmentDistinct(allDepartment.ToList(), departmentIdsList.Distinct().ToList());
  419. return await GetAllAsync(Q.WhereIn(nameof(Entity.Department.Id), departmentIdsList));
  420. //
  421. }
  422. public async Task<List<int>> GetDepartmentDistinct(List<Entity.Department> allDepartment, List<int> departmentIds)
  423. {
  424. //
  425. var departmentIdsList = new List<int>();
  426. var result = new DepartmentUserInsResult();
  427. result.Result = new List<DepartmentUserInResult>();
  428. foreach (var departmentId in departmentIds)
  429. {
  430. result.Result.Add(new DepartmentUserInResult
  431. {
  432. DepartmentId = departmentId,
  433. Item = await GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
  434. });
  435. }
  436. if (departmentIds.Count() <= 2)
  437. departmentIdsList.AddRange(departmentIds);
  438. for (int i = 0; i < result.Result.Count; i++)
  439. {
  440. for (int k = i + 1; k < result.Result.Count; k++)
  441. {
  442. if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
  443. {
  444. departmentIdsList.Add(result.Result[i].DepartmentId);
  445. }
  446. else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
  447. {
  448. departmentIdsList.Add(result.Result[k].DepartmentId);
  449. }
  450. else
  451. {
  452. departmentIdsList.Add(result.Result[i].DepartmentId);
  453. }
  454. }
  455. }
  456. return departmentIdsList;
  457. }
  458. public async Task<IEnumerable<DepartmentResult>> GetParentDepartmentResults(List<int> departmentIds)
  459. {
  460. var departmentResults = new List<DepartmentResult>();
  461. foreach (var item in departmentIds)
  462. {
  463. await GetParentDepartmentResults(item, departmentResults);
  464. }
  465. return departmentResults;
  466. }
  467. public async Task<IEnumerable<DepartmentResult>> GetParentDepartmentResults(int departmentId, List<DepartmentResult> departmentResults)
  468. {
  469. var department = await _repository.GetAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.Id), departmentId));
  470. if (department == null)
  471. return departmentResults;
  472. departmentResults.Add(department);
  473. var parentDepartment = await _repository.GetAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.Id), department.ParentId));
  474. if (parentDepartment == null)
  475. return departmentResults;
  476. departmentResults.Add(parentDepartment);
  477. await GetParentDepartmentResults(parentDepartment.Id, departmentResults);
  478. return departmentResults;
  479. }
  480. }
  481. }