DepartmentRepository.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. result.Departments = departments.ToList();
  195. //获取用户ID
  196. var departmentUsers = await departmentUserRepository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), request.DepartmentId).Select(nameof(Entity.DepartmentUser.UserId)));
  197. //判断是否包含该用户
  198. if (!departmentUsers.Contains(request.UserId))
  199. return result;
  200. var users = await _userRepository.GetAllAsync<UserInfoResult>(Q.WhereIn(nameof(User.Id), departmentUsers));
  201. result.Users = users.ToList();
  202. foreach (var item in result.Users)
  203. {
  204. item.TypeId = 0;
  205. item.TypeValue = 0;
  206. }
  207. for (int i = 0; i < result.Users.Count(); i++)
  208. {
  209. if (!request.IsShow)
  210. if (request.UserIds.Contains(result.Users[i].Id))
  211. result.Users.Remove(result.Users[i]);
  212. if (request.IsShow)
  213. if (request.UserIds.Contains(result.Users[i].Id))
  214. result.Users[i].IsDisable = true;
  215. }
  216. foreach (var user in result.Users)
  217. user.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  218. return result;
  219. }
  220. /// <summary>
  221. /// 获取所用下级部门
  222. /// </summary>
  223. /// <param name="id"></param>
  224. /// <param name="departments"></param>
  225. /// <returns></returns>
  226. public async Task<List<DepartmentResult>> GetDepartmentById(int id, List<DepartmentResult> departments)
  227. {
  228. var department = await _repository.GetAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.Id), id));
  229. if (department != null)
  230. {
  231. departments.Add(department);
  232. var list = await _repository.GetAllAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.ParentId), department.Id));
  233. foreach (var item in list)
  234. {
  235. departments.Add(item);
  236. await GetDepartmentById(item.Id, departments);
  237. }
  238. }
  239. return departments;
  240. }
  241. /// <summary>
  242. /// 获取全部部门
  243. /// </summary>
  244. /// <returns></returns>
  245. public async Task<List<DepartmentTreeResult>> GetAllDepartmentAsync()
  246. {
  247. var DepartmentTreeResults = new List<DepartmentTreeResult>();
  248. var department = await _repository.GetAllAsync<DepartmentTreeResult>(Q.Where(nameof(Department.Id), ">", 0));
  249. DepartmentTreeResults.AddRange(department.Where(n => n.ParentId == 0));
  250. foreach (var item in DepartmentTreeResults)
  251. {
  252. await GetDepartmentById(item.Id, DepartmentTreeResults, department.ToList());
  253. }
  254. return DepartmentTreeResults;
  255. }
  256. /// <summary>
  257. /// 获取所用下级部门
  258. /// </summary>
  259. /// <param name="id"></param>
  260. /// <param name="departments"></param>
  261. /// <returns></returns>
  262. public async Task<List<DepartmentTreeResult>> GetDepartmentById(int id, List<DepartmentTreeResult> departments, List<DepartmentTreeResult> allDepartments)
  263. {
  264. GetDepartmentTreeResult(id, departments, allDepartments);
  265. var list = allDepartments.Where(n => n.ParentId == id);
  266. if (list.Any())
  267. {
  268. foreach (var item in list)
  269. {
  270. GetDepartmentTreeResult(item.Id, departments, allDepartments);
  271. }
  272. }
  273. return departments;
  274. }
  275. public List<DepartmentTreeResult> GetDepartmentTreeResult(int id, List<DepartmentTreeResult> data, List<DepartmentTreeResult> allDepartments)
  276. {
  277. var list = allDepartments.Where(n => n.ParentId == id).ToList();
  278. if (list == null || list.Count == 0)
  279. return data;
  280. foreach (var item in data)
  281. {
  282. if (data.Any(n => n.Id == id))
  283. {
  284. data.FirstOrDefault(n => n.Id == id).Children = list;
  285. data.FirstOrDefault(n => n.Id == id).HasChildren = true;
  286. break;
  287. }
  288. else
  289. {
  290. if (data.Any(n => n.HasChildren == true))
  291. {
  292. foreach (var children in data.Where(n => n.HasChildren == true))
  293. {
  294. if (children.Children.Any(n => n.Id == id))
  295. {
  296. foreach (var dd in children.Children)
  297. {
  298. if (dd.Id == id)
  299. {
  300. dd.Children = list;
  301. dd.HasChildren = true;
  302. break;
  303. }
  304. }
  305. }
  306. }
  307. }
  308. }
  309. }
  310. return data;
  311. }
  312. /// <summary>
  313. /// 获取所用下级部门
  314. /// </summary>
  315. /// <param name="id"></param>
  316. /// <param name="departments"></param>
  317. /// <returns></returns>
  318. public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments)
  319. {
  320. var departmentList = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), Pid));
  321. if (departmentList.Count == 0)
  322. return departments;
  323. foreach (var item in departmentList)
  324. {
  325. departments.Add(item);
  326. await GetDepartmentByPid(item.Id, departments);
  327. }
  328. return departments;
  329. }
  330. /// <summary>
  331. /// 获取所用下级部门
  332. /// </summary>
  333. /// <param name="id"></param>
  334. /// <param name="departments"></param>
  335. /// <returns></returns>
  336. public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments, List<Department> allDepartments)
  337. {
  338. var departmentList = allDepartments.Where(n => n.ParentId == Pid);
  339. if (departmentList.Count() == 0)
  340. return departments;
  341. foreach (var item in departmentList)
  342. {
  343. departments.Add(item);
  344. await GetDepartmentByPid(item.Id, departments, allDepartments);
  345. }
  346. return departments;
  347. }
  348. /// <summary>
  349. /// 根据部门ID获取全名称
  350. /// </summary>
  351. /// <param name="departmentId"></param>
  352. /// <param name="departmentName"></param>
  353. /// <returns></returns>
  354. public async Task<string> GetDepartmentFullPathAsync(int departmentId, string departmentName)
  355. {
  356. var department = await _repository.GetAsync(departmentId);
  357. if (department == null)
  358. {
  359. return departmentName;
  360. }
  361. if (string.IsNullOrEmpty(departmentName))
  362. departmentName = department.Name;
  363. else
  364. departmentName = department.Name + "/" + departmentName;
  365. return await GetDepartmentFullPathAsync(department.ParentId, departmentName);
  366. }
  367. public async Task<IEnumerable<Department>> GetAllAsync(SqlKata.Query query)
  368. {
  369. return await _repository.GetAllAsync(query);
  370. }
  371. public async Task<IEnumerable<Entity.Department>> GetUserInDepartmentAsync(List<int> departmentIds)
  372. {
  373. //
  374. var departmentIdsList = new List<int>();
  375. var result = new DepartmentUserInsResult();
  376. result.Result = new List<DepartmentUserInResult>();
  377. var allDepartment = await GetAllAsync(Q.Where(nameof(Entity.Department.Id), ">", 0));
  378. foreach (var departmentId in departmentIds)
  379. {
  380. result.Result.Add(new DepartmentUserInResult
  381. {
  382. DepartmentId = departmentId,
  383. Item = await GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
  384. });
  385. }
  386. if (departmentIds.Count() == 1)
  387. departmentIdsList.AddRange(departmentIds);
  388. for (int i = 0; i < result.Result.Count; i++)
  389. {
  390. for (int k = i + 1; k < result.Result.Count; k++)
  391. {
  392. if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
  393. {
  394. departmentIdsList.Add(result.Result[i].DepartmentId);
  395. }
  396. else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
  397. {
  398. departmentIdsList.Add(result.Result[k].DepartmentId);
  399. result.Result[i].DepartmentId = result.Result[k].DepartmentId;
  400. result.Result[i].Item = result.Result[k].Item;
  401. }
  402. else
  403. {
  404. //departmentIdsList.Add(result.Result[i].DepartmentId);
  405. departmentIdsList.Add(result.Result[k].DepartmentId);
  406. }
  407. }
  408. }
  409. departmentIdsList = await GetDepartmentDistinct(allDepartment.ToList(), departmentIdsList.Distinct().ToList());
  410. return await GetAllAsync(Q.WhereIn(nameof(Entity.Department.Id), departmentIdsList));
  411. //
  412. }
  413. public async Task<List<int>> GetDepartmentDistinct(List<Entity.Department> allDepartment, List<int> departmentIds)
  414. {
  415. //
  416. var departmentIdsList = new List<int>();
  417. var result = new DepartmentUserInsResult();
  418. result.Result = new List<DepartmentUserInResult>();
  419. foreach (var departmentId in departmentIds)
  420. {
  421. result.Result.Add(new DepartmentUserInResult
  422. {
  423. DepartmentId = departmentId,
  424. Item = await GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
  425. });
  426. }
  427. if (departmentIds.Count() <= 2)
  428. departmentIdsList.AddRange(departmentIds);
  429. for (int i = 0; i < result.Result.Count; i++)
  430. {
  431. for (int k = i + 1; k < result.Result.Count; k++)
  432. {
  433. if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
  434. {
  435. departmentIdsList.Add(result.Result[i].DepartmentId);
  436. }
  437. else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
  438. {
  439. departmentIdsList.Add(result.Result[k].DepartmentId);
  440. }
  441. else
  442. {
  443. departmentIdsList.Add(result.Result[i].DepartmentId);
  444. }
  445. }
  446. }
  447. return departmentIdsList;
  448. }
  449. }
  450. }