DepartmentService.Delete.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using GxPress.Entity;
  5. using SqlKata;
  6. namespace GxPress.Service.Implement.Department
  7. {
  8. public partial class DepartmentService
  9. {
  10. /// <summary>
  11. /// 删除部门
  12. /// </summary>
  13. /// <param name="id"></param>
  14. /// <returns></returns>
  15. public async Task<bool> DeleteAsync(int id)
  16. {
  17. var departments = new List<Entity.Department>();
  18. var department = await _departmentRepository.GetAsync(id);
  19. departments.Add(department);
  20. //获取下级部门
  21. departments = await _departmentRepository.GetDepartmentById(id, departments);
  22. foreach (var item in departments)
  23. {
  24. var users = await _userRepository.GetUserByDepartmentIdAsync(item.Id);
  25. var enumerable = users as User[] ?? users.ToArray();
  26. if (enumerable.Any())
  27. {
  28. await _userService.DeleteUsersAsync(enumerable.Select(n => n.Id));
  29. }
  30. }
  31. //删除部门
  32. Query q = new Query();
  33. q.WhereIn(nameof(department.Id), departments.Select(n => n.Id));
  34. return await _departmentRepository.DeleteAsync(q);
  35. //return true;
  36. }
  37. }
  38. }