DepartmentRepository.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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>> GetParentDepartmentById(int id, List<Department> departments)
  346. {
  347. var department = await _repository.GetAsync(id);
  348. departments.Add(department);
  349. if (department.ParentId == 0)
  350. return departments;
  351. department = await _repository.GetAsync(Q.Where(nameof(Entity.Department.ParentId), department.ParentId));
  352. if (department == null)
  353. return departments;
  354. //departments.Add(item);
  355. await GetParentDepartmentById(department.ParentId, departments);
  356. return departments;
  357. }
  358. /// <summary>
  359. /// 获取所用下级部门
  360. /// </summary>
  361. /// <param name="id"></param>
  362. /// <param name="departments"></param>
  363. /// <returns></returns>
  364. public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments, List<Department> allDepartments)
  365. {
  366. var departmentList = allDepartments.Where(n => n.ParentId == Pid);
  367. if (departmentList.Count() == 0)
  368. return departments;
  369. foreach (var item in departmentList)
  370. {
  371. departments.Add(item);
  372. await GetDepartmentByPid(item.Id, departments, allDepartments);
  373. }
  374. return departments;
  375. }
  376. /// <summary>
  377. /// 根据部门ID获取全名称
  378. /// </summary>
  379. /// <param name="departmentId"></param>
  380. /// <param name="departmentName"></param>
  381. /// <returns></returns>
  382. public async Task<string> GetDepartmentFullPathAsync(int departmentId, string departmentName)
  383. {
  384. var department = await _repository.GetAsync(departmentId);
  385. if (department == null)
  386. {
  387. return departmentName;
  388. }
  389. if (string.IsNullOrEmpty(departmentName))
  390. departmentName = department.Name;
  391. else
  392. departmentName = department.Name + "/" + departmentName;
  393. return await GetDepartmentFullPathAsync(department.ParentId, departmentName);
  394. }
  395. public async Task<IEnumerable<Department>> GetAllAsync(SqlKata.Query query)
  396. {
  397. return await _repository.GetAllAsync(query);
  398. }
  399. public async Task<IEnumerable<Entity.Department>> GetUserInDepartmentAsync(List<int> departmentIds)
  400. {
  401. //
  402. var departmentIdsList = new List<int>();
  403. var result = new DepartmentUserInsResult();
  404. result.Result = new List<DepartmentUserInResult>();
  405. var allDepartment = await GetAllAsync(Q.Where(nameof(Entity.Department.Id), ">", 0));
  406. foreach (var departmentId in departmentIds)
  407. {
  408. result.Result.Add(new DepartmentUserInResult
  409. {
  410. DepartmentId = departmentId,
  411. Item = await GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
  412. });
  413. }
  414. if (departmentIds.Count() == 1)
  415. departmentIdsList.AddRange(departmentIds);
  416. for (int i = 0; i < result.Result.Count; i++)
  417. {
  418. for (int k = 0; k < result.Result.Count; k++)
  419. {
  420. if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
  421. {
  422. departmentIdsList.Add(result.Result[i].DepartmentId);
  423. }
  424. else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
  425. {
  426. departmentIdsList.Add(result.Result[k].DepartmentId);
  427. // result.Result[i].DepartmentId = result.Result[k].DepartmentId;
  428. // result.Result[i].Item = result.Result[k].Item;
  429. }
  430. else
  431. {
  432. //departmentIdsList.Add(result.Result[i].DepartmentId);
  433. departmentIdsList.Add(result.Result[k].DepartmentId);
  434. }
  435. }
  436. }
  437. departmentIdsList = await GetDepartmentDistinct(allDepartment.ToList(), departmentIdsList.Distinct().ToList());
  438. departmentIdsList = departmentIdsList.Distinct().ToList();
  439. var departments = new List<Entity.Department>();
  440. foreach (var departmentId in departmentIdsList)
  441. {
  442. var item = await GetParentDepartmentById(departmentId, new List<Entity.Department>());
  443. departments.Add(item.Where(n=>n.ParentId==0).FirstOrDefault());
  444. }
  445. return departments;
  446. //
  447. }
  448. public async Task<List<int>> GetDepartmentDistinct(List<Entity.Department> allDepartment, List<int> departmentIds)
  449. {
  450. //
  451. var departmentIdsList = new List<int>();
  452. var result = new DepartmentUserInsResult();
  453. result.Result = new List<DepartmentUserInResult>();
  454. foreach (var departmentId in departmentIds)
  455. {
  456. result.Result.Add(new DepartmentUserInResult
  457. {
  458. DepartmentId = departmentId,
  459. Item = await GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
  460. });
  461. }
  462. if (departmentIds.Count() <= 2)
  463. departmentIdsList.AddRange(departmentIds);
  464. for (int i = 0; i < result.Result.Count; i++)
  465. {
  466. for (int k = i + 1; k < result.Result.Count; k++)
  467. {
  468. if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
  469. {
  470. departmentIdsList.Add(result.Result[i].DepartmentId);
  471. }
  472. else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
  473. {
  474. departmentIdsList.Add(result.Result[k].DepartmentId);
  475. }
  476. else
  477. {
  478. departmentIdsList.Add(result.Result[i].DepartmentId);
  479. }
  480. }
  481. }
  482. return departmentIdsList;
  483. }
  484. public async Task<IEnumerable<DepartmentResult>> GetParentDepartmentResults(List<int> departmentIds)
  485. {
  486. var departmentResults = new List<DepartmentResult>();
  487. foreach (var item in departmentIds)
  488. {
  489. await GetParentDepartmentResults(item, departmentResults);
  490. }
  491. return departmentResults;
  492. }
  493. public async Task<IEnumerable<DepartmentResult>> GetParentDepartmentResults(int departmentId, List<DepartmentResult> departmentResults)
  494. {
  495. var department = await _repository.GetAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.Id), departmentId));
  496. if (department == null)
  497. return departmentResults;
  498. departmentResults.Add(department);
  499. var parentDepartment = await _repository.GetAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.Id), department.ParentId));
  500. if (parentDepartment == null)
  501. return departmentResults;
  502. departmentResults.Add(parentDepartment);
  503. await GetParentDepartmentResults(parentDepartment.Id, departmentResults);
  504. return departmentResults;
  505. }
  506. }
  507. }