Department.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\user\controller;
  15. use app\base\BaseController;
  16. use app\user\model\Department as DepartmentModel;
  17. use app\user\validate\DepartmentCheck;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\facade\View;
  21. class Department extends BaseController
  22. {
  23. public function index()
  24. {
  25. if (request()->isAjax()) {
  26. $list = Db::name('Department')->order('sort desc,id asc')->select()->toArray();
  27. foreach ($list as $key => &$v) {
  28. $admin_array = Db::name('Admin')->where([['id','in',$v['leader_ids']]])->column('name');
  29. $v['leader'] = split_array_field($admin_array);
  30. }
  31. $res = generateTree($list);
  32. return to_assign(0, '', $res);
  33. } else {
  34. return view();
  35. }
  36. }
  37. //添加部门
  38. public function add()
  39. {
  40. $param = get_params();
  41. if (request()->isAjax()) {
  42. if ($param['id'] > 0) {
  43. try {
  44. validate(DepartmentCheck::class)->scene('edit')->check($param);
  45. } catch (ValidateException $e) {
  46. // 验证失败 输出错误信息
  47. return to_assign(1, $e->getError());
  48. }
  49. $param['update_time'] = time();
  50. $department_array = get_department_son($param['id']);
  51. if (in_array($param['pid'], $department_array)) {
  52. return to_assign(1, '上级部门不能是该部门本身或其下属部门');
  53. } else {
  54. Db::name('Department')->strict(false)->field(true)->update($param);
  55. $model = new DepartmentModel();
  56. $model->update_auth_dids_son_dids();
  57. add_log('edit', $param['id'], $param);
  58. return to_assign();
  59. }
  60. } else {
  61. try {
  62. validate(DepartmentCheck::class)->scene('add')->check($param);
  63. } catch (ValidateException $e) {
  64. // 验证失败 输出错误信息
  65. return to_assign(1, $e->getError());
  66. }
  67. $did = Db::name('Department')->strict(false)->field(true)->insertGetId($param);
  68. $model = new DepartmentModel();
  69. $model->update_auth_dids_son_dids();
  70. add_log('add', $did, $param);
  71. return to_assign();
  72. }
  73. } else {
  74. $id = isset($param['id']) ? $param['id'] : 0;
  75. $pid = isset($param['pid']) ? $param['pid'] : 0;
  76. $department = set_recursion(get_department());
  77. if ($id > 0) {
  78. $detail = Db::name('Department')->where(['id' => $id])->find();
  79. //获取子部门
  80. $department_array = get_department_son($id);
  81. $users = get_department_employee($id);
  82. View::assign('users', $users);
  83. View::assign('detail', $detail);
  84. }
  85. View::assign('department', $department);
  86. View::assign('pid', $pid);
  87. View::assign('id', $id);
  88. return view();
  89. }
  90. }
  91. //删除
  92. public function delete()
  93. {
  94. $id = get_params("id");
  95. $count = Db::name('Department')->where([['pid', '=', $id], ['status', '>=', 0]])->count();
  96. if ($count > 0) {
  97. return to_assign(1, "该部门下还有子部门,无法删除");
  98. }
  99. $users = Db::name('Admin')->where([['did', '=', $id], ['status', '>=', 0]])->count();
  100. if ($users > 0) {
  101. return to_assign(1, "该部门下还有员工,无法删除");
  102. }
  103. if (Db::name('Department')->delete($id) !== false) {
  104. add_log('delete', $id);
  105. return to_assign(0, "删除部门成功");
  106. } else {
  107. return to_assign(1, "删除失败");
  108. }
  109. }
  110. }