DepartmentRepository.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. private readonly Repository<Entity.DepartmentUser> departmentUserRepository;
  23. public DepartmentRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  24. {
  25. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  26. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  27. _repository = new Repository<Department>(database);
  28. _userRepository = new Repository<User>(database);
  29. departmentUserRepository = new Repository<Entity.DepartmentUser>(database);
  30. _mapper = mapper;
  31. }
  32. public IDatabase Database => _repository.Database;
  33. public string TableName => _repository.TableName;
  34. public List<TableColumn> TableColumns => _repository.TableColumns;
  35. /// <summary>
  36. /// 是否存在下级部门
  37. /// </summary>
  38. /// <param name="parentId"></param>
  39. /// <returns></returns>
  40. public async Task<bool> ExistsChildrenAsync(int parentId)
  41. {
  42. return await _repository.ExistsAsync(Q.Where(nameof(Department.ParentId), parentId));
  43. }
  44. public async Task<Department> GetAsync(int id)
  45. {
  46. var department = await _repository.GetAsync(id);
  47. return department;
  48. }
  49. /// <summary>
  50. /// 根据部门名称查询对应的数据
  51. /// </summary>
  52. /// <param name="name"></param>
  53. /// <returns></returns>
  54. public async Task<Department> GetAsync(string name)
  55. {
  56. return await _repository.GetAsync(Q.Where(nameof(Department.Name), name));
  57. }
  58. public async Task<string> GetNameAsync(int id)
  59. {
  60. if (id <= 0) return string.Empty;
  61. return await _repository.GetAsync<string>(Q
  62. .Select(nameof(Department.Name))
  63. .Where(nameof(Department.Id), id)
  64. );
  65. }
  66. public async Task<int> InsertAsync(Department department)
  67. {
  68. if (department.Sort > 0)
  69. {
  70. var exists = await _repository.ExistsAsync(Q
  71. .Where(nameof(Department.Sort), department.Sort)
  72. );
  73. if (exists)
  74. {
  75. throw new Exception("部门顺序已存在,请更换");
  76. }
  77. }
  78. return await _repository.InsertAsync(department);
  79. }
  80. public async Task<bool> UpdateAsync(Department department)
  81. {
  82. if (department.Sort > 0)
  83. {
  84. var exists = await _repository.ExistsAsync(Q
  85. .WhereNot(nameof(Department.Id), department.Id)
  86. .Where(nameof(Department.Sort), department.Sort)
  87. );
  88. if (exists)
  89. {
  90. throw new Exception("部门顺序已存在,请更换");
  91. }
  92. }
  93. return await _repository.UpdateAsync(department);
  94. }
  95. public async Task<IEnumerable<Department>> GetListAsync(int parentId)
  96. {
  97. return await _repository.GetAllAsync(Q.Where(nameof(Department.ParentId), parentId)
  98. .OrderBy(nameof(Department.Sort)));
  99. }
  100. /// <summary>
  101. /// 排除
  102. /// </summary>
  103. /// <param name="request"></param>
  104. /// <returns></returns>
  105. public async Task<IEnumerable<Department>> GetListAsync(DepartmentUserRequest request)
  106. {
  107. return await _repository.GetAllAsync(Q.Where(nameof(Department.ParentId), request.DepartmentId)
  108. .WhereNot(nameof(Department.Id), request.SourceDepartmentId));
  109. }
  110. /// <summary>
  111. /// 添加
  112. /// </summary>
  113. /// <param name="request"></param>
  114. /// <returns></returns>
  115. public async Task<Department> AddAsync(Department request)
  116. {
  117. if (request.ParentId > 0)
  118. {
  119. var parent = await _repository.GetAsync(request.ParentId);
  120. if (parent == null) throw new BusinessException("上级部门不存在");
  121. }
  122. if (request.LeaderId > 0)
  123. {
  124. var leader = await _repository.GetAsync(request.LeaderId);
  125. if (leader == null) throw new BusinessException("该主管不存在");
  126. }
  127. var department = _mapper.Map<Department>(request);
  128. department.Id = await InsertAsync(department);
  129. return department;
  130. }
  131. /// <summary>
  132. /// 删除
  133. /// </summary>
  134. /// <param name="id"></param>
  135. /// <returns></returns>
  136. public async Task<bool> DeleteAsync(int id)
  137. {
  138. var dep = await _repository.GetAsync(id);
  139. if (dep == null) throw new BusinessException("该部门不存在");
  140. if (await ExistsChildrenAsync(id))
  141. throw new BusinessException("请先删除下级部门");
  142. //TODO 是否解散部门群
  143. return await _repository.DeleteAsync(id);
  144. }
  145. /// <summary>
  146. /// 删除
  147. /// </summary>
  148. /// <param name="query"></param>
  149. /// <returns></returns>
  150. public async Task<bool> DeleteAsync(SqlKata.Query query)
  151. {
  152. return await _repository.DeleteAsync(query) > 0;
  153. }
  154. /// <summary>
  155. /// 更新
  156. /// </summary>
  157. /// <param name="id"></param>
  158. /// <param name="request"></param>
  159. /// <returns></returns>
  160. public async Task<bool> UpdateAsync(int id, Department request)
  161. {
  162. var dep = await GetAsync(id);
  163. if (dep == null) throw new BusinessException("该部门不存在");
  164. dep.LeaderId = request.LeaderId;
  165. dep.Name = request.Name;
  166. dep.Sort = request.Sort;
  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. //获取用户ID
  195. var departmentUsers = await departmentUserRepository.GetAllAsync<int>(Q.WhereIn(nameof(Entity.DepartmentUser.DepartmentId), departments.Select(n => n.Id)).Select(nameof(Entity.DepartmentUser.UserId)));
  196. var users = await _userRepository.GetAllAsync(Q.WhereIn(nameof(User.Id), departmentUsers));
  197. result.Users = users.ToList();
  198. for (int i = 0; i < result.Users.Count(); i++)
  199. {
  200. if (!request.IsShow)
  201. if (request.UserIds.Contains(result.Users[i].Id))
  202. result.Users.Remove(result.Users[i]);
  203. if (request.IsShow)
  204. if (request.UserIds.Contains(result.Users[i].Id))
  205. result.Users[i].IsDisable = true;
  206. }
  207. foreach (var user in result.Users)
  208. user.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  209. return result;
  210. }
  211. /// <summary>
  212. /// 获取所用下级部门
  213. /// </summary>
  214. /// <param name="id"></param>
  215. /// <param name="departments"></param>
  216. /// <returns></returns>
  217. public async Task<List<Department>> GetDepartmentById(int id, List<Department> departments)
  218. {
  219. var department = await _repository.GetAsync(id);
  220. if (department != null)
  221. {
  222. departments.Add(department);
  223. var list = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), department.Id));
  224. foreach (var item in list)
  225. {
  226. departments.Add(item);
  227. await GetDepartmentById(item.Id, departments);
  228. }
  229. }
  230. return departments;
  231. }
  232. /// <summary>
  233. /// 获取全部部门
  234. /// </summary>
  235. /// <returns></returns>
  236. public async Task<List<DepartmentTreeResult>> GetAllDepartmentAsync()
  237. {
  238. var department = await _repository.GetAllAsync<DepartmentTreeResult>(Q.Where(nameof(Department.ParentId), 0));
  239. foreach (var item in department)
  240. {
  241. return await GetDepartmentById(item.Id, department.ToList());
  242. }
  243. return department.ToList();
  244. }
  245. /// <summary>
  246. /// 获取所用下级部门
  247. /// </summary>
  248. /// <param name="id"></param>
  249. /// <param name="departments"></param>
  250. /// <returns></returns>
  251. public async Task<List<DepartmentTreeResult>> GetDepartmentById(int id, List<DepartmentTreeResult> departments)
  252. {
  253. var list = await _repository.GetAllAsync<DepartmentTreeResult>(Q.Where(nameof(Entity.Department.ParentId), id));
  254. if (list.Any())
  255. {
  256. departments.FindLast(n => n.Id == id).HasChildren = true;
  257. departments.FindLast(n => n.Id == id).Children = list.ToList();
  258. var result = new List<DepartmentTreeResult>();
  259. foreach (var item in list)
  260. {
  261. // result.Add(item);
  262. await GetDepartmentById(item.Id, departments);
  263. }
  264. }
  265. return departments;
  266. }
  267. /// <summary>
  268. /// 获取所用下级部门
  269. /// </summary>
  270. /// <param name="id"></param>
  271. /// <param name="departments"></param>
  272. /// <returns></returns>
  273. public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments)
  274. {
  275. var departmentList = await _repository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), Pid));
  276. if (departmentList.Count == 0)
  277. return departments;
  278. foreach (var item in departmentList)
  279. {
  280. departments.Add(item);
  281. await GetDepartmentByPid(item.Id, departments);
  282. }
  283. return departments;
  284. }
  285. /// <summary>
  286. /// 根据部门ID获取全名称
  287. /// </summary>
  288. /// <param name="departmentId"></param>
  289. /// <param name="departmentName"></param>
  290. /// <returns></returns>
  291. public async Task<string> GetDepartmentFullPathAsync(int departmentId, string departmentName)
  292. {
  293. var department = await _repository.GetAsync(departmentId);
  294. if (department == null)
  295. {
  296. return departmentName;
  297. }
  298. if (string.IsNullOrEmpty(departmentName))
  299. departmentName = department.Name;
  300. else
  301. departmentName = department.Name + "/" + departmentName;
  302. return await GetDepartmentFullPathAsync(department.ParentId, departmentName);
  303. }
  304. public async Task<IEnumerable<Department>> GetAllAsync(SqlKata.Query query)
  305. {
  306. return await _repository.GetAllAsync(query);
  307. }
  308. }
  309. }