Department.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/GPL-3.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\validate\DepartmentCheck;
  11. use think\exception\ValidateException;
  12. use think\facade\Db;
  13. use think\facade\View;
  14. use think\facade\Session;
  15. class Department extends BaseController
  16. {
  17. public function index()
  18. {
  19. if (request()->isAjax()) {
  20. $unit_name = get_login_admin('unit_name');
  21. $admin_permission = get_login_admin('permission');
  22. $where = array();
  23. $where[] = ["d.status","=",1];
  24. $where[] = ["d.delete_time","=",0];
  25. if($admin_permission == 0){
  26. $where[] = ['d.unit_name', '=',$unit_name];
  27. }
  28. $list = Db::name('Department')
  29. ->where($where)
  30. // ->where('d.unit_name', $unit_name)
  31. ->field('d.*,a.nickname as leader')
  32. ->alias('d')
  33. ->join('Admin a', 'a.id = d.leader_id', 'LEFT')
  34. ->order('d.id asc')
  35. ->select();
  36. // halt($list);
  37. return to_assign(0, '', $list);
  38. } else {
  39. return view();
  40. }
  41. }
  42. //添加部门
  43. public function add()
  44. {
  45. $param = get_params();
  46. if (request()->isAjax()) {
  47. // halt($param);
  48. if ($param['id'] > 0) { //编辑已有部门
  49. try {
  50. validate(DepartmentCheck::class)->scene('edit')->check($param);
  51. } catch (ValidateException $e) {
  52. // 验证失败 输出错误信息
  53. return to_assign(1, $e->getError());
  54. }
  55. $param['update_time'] = time();
  56. $department_array = get_department_son($param['id']);
  57. if (in_array($param['pid'], $department_array)) {
  58. return to_assign(1, '上级部门不能是该部门本身或其下属部门');
  59. } else {
  60. Db::name('Department')->strict(false)->field(true)->update($param);
  61. add_log('edit', $param['id'], $param);
  62. return to_assign(0, "编辑成功!");
  63. }
  64. } else { //新增部门
  65. try {
  66. validate(DepartmentCheck::class)->scene('add')->check($param);
  67. } catch (ValidateException $e) {
  68. // 验证失败 输出错误信息
  69. return to_assign(1, $e->getError());
  70. }
  71. if(get_login_admin('permission') != 1){
  72. if($param['pid'] == 0){
  73. return to_assign(1, '不可创建顶级部门');
  74. }
  75. }
  76. $did = Db::name('Department')->strict(false)->field(true)->insertGetId($param);
  77. $pid = $did;
  78. while($pid != 0){
  79. $value = Db::name('department')->where('id', $pid)->column('id,pid,title')[0];
  80. $unit_name = $value['id'];
  81. $pid = $value['pid'];
  82. $title = $value['title'];
  83. }
  84. $unit_name = [
  85. 'unit_name' => $unit_name,
  86. ];
  87. Db::name('Department')->where('id', $did)->update($unit_name);
  88. add_log('add', $did, $param);
  89. return to_assign(0,'操作成功');
  90. }
  91. } else {
  92. $id = isset($param['id']) ? $param['id'] : 0;
  93. $pid = isset($param['pid']) ? $param['pid'] : 0;
  94. $permission = get_login_admin('permission');
  95. if ($id > 0) { //编辑
  96. $detail = Db::name('Department')->where(['id' => $id])->find();
  97. $users = Db::name('Admin')->where(['did' => $id, 'status' => 1])->select();
  98. // $d_type = Db::name('Department')->where('id', $id)->value('type');
  99. // View::assign('d_type', $d_type);
  100. View::assign('users', $users);
  101. View::assign('detail', $detail);
  102. }
  103. // else{
  104. // $admin = get_login_admin();
  105. // View::assign('admin', $admin);
  106. // }
  107. if($permission == 1){
  108. $d_type = Db::name('Department')->where('id', $id)->value('type');
  109. $department = set_recursion(get_department());
  110. View::assign('d_type', $d_type);
  111. }else{
  112. $department = Db::name('Department')->where(['status' => 1])->where('unit_name', '=', get_login_admin('unit_name'))->select()->toArray();
  113. $department = set_recursion($department);
  114. }
  115. View::assign('permission', $permission);
  116. View::assign('department', $department);
  117. View::assign('pid', $pid);
  118. View::assign('id', $id);
  119. return view();
  120. }
  121. }
  122. //删除
  123. public function delete()
  124. {
  125. $id = get_params("id");
  126. $count = Db::name('Department')->where([['pid', '=', $id], ['status', '>=', 0]])->count();
  127. if ($count > 0) {
  128. return to_assign(1, "该部门下还有子部门,无法删除");
  129. }
  130. $users = Db::name('Admin')->where([['did', '=', $id], ['status', '>=', 0]])->count();
  131. if ($users > 0) {
  132. return to_assign(1, "该部门下还有员工,无法删除");
  133. }
  134. if (Db::name('Department')->delete($id) !== false) {
  135. add_log('delete', $id);
  136. return to_assign(0, "删除部门成功");
  137. } else {
  138. return to_assign(1, "删除失败");
  139. }
  140. }
  141. }