isAjax()) { $unit_name = get_login_admin('unit_name'); $admin_permission = get_login_admin('permission'); $where = array(); $where[] = ["d.status","=",1]; $where[] = ["d.delete_time","=",0]; if($admin_permission == 0){ $where[] = ['d.unit_name', '=',$unit_name]; } $list = Db::name('Department') ->where($where) // ->where('d.unit_name', $unit_name) ->field('d.*,a.nickname as leader') ->alias('d') ->join('Admin a', 'a.id = d.leader_id', 'LEFT') ->order('d.id asc') ->select(); // halt($list); return to_assign(0, '', $list); } else { return view(); } } //添加部门 public function add() { $param = get_params(); if (request()->isAjax()) { // halt($param); if ($param['id'] > 0) { //编辑已有部门 try { validate(DepartmentCheck::class)->scene('edit')->check($param); } catch (ValidateException $e) { // 验证失败 输出错误信息 return to_assign(1, $e->getError()); } $param['update_time'] = time(); $department_array = get_department_son($param['id']); if (in_array($param['pid'], $department_array)) { return to_assign(1, '上级部门不能是该部门本身或其下属部门'); } else { Db::name('Department')->strict(false)->field(true)->update($param); add_log('edit', $param['id'], $param); return to_assign(0, "编辑成功!"); } } else { //新增部门 try { validate(DepartmentCheck::class)->scene('add')->check($param); } catch (ValidateException $e) { // 验证失败 输出错误信息 return to_assign(1, $e->getError()); } if(get_login_admin('permission') != 1){ if($param['pid'] == 0){ return to_assign(1, '不可创建顶级部门'); } } $did = Db::name('Department')->strict(false)->field(true)->insertGetId($param); $pid = $did; while($pid != 0){ $value = Db::name('department')->where('id', $pid)->column('id,pid,title')[0]; $unit_name = $value['id']; $pid = $value['pid']; $title = $value['title']; } $unit_name = [ 'unit_name' => $unit_name, ]; Db::name('Department')->where('id', $did)->update($unit_name); add_log('add', $did, $param); return to_assign(0,'操作成功'); } } else { $id = isset($param['id']) ? $param['id'] : 0; $pid = isset($param['pid']) ? $param['pid'] : 0; $permission = get_login_admin('permission'); if ($id > 0) { //编辑 $detail = Db::name('Department')->where(['id' => $id])->find(); $users = Db::name('Admin')->where(['did' => $id, 'status' => 1])->select(); // $d_type = Db::name('Department')->where('id', $id)->value('type'); // View::assign('d_type', $d_type); View::assign('users', $users); View::assign('detail', $detail); } // else{ // $admin = get_login_admin(); // View::assign('admin', $admin); // } if($permission == 1){ $d_type = Db::name('Department')->where('id', $id)->value('type'); $department = set_recursion(get_department()); View::assign('d_type', $d_type); }else{ $department = Db::name('Department')->where(['status' => 1])->where('unit_name', '=', get_login_admin('unit_name'))->select()->toArray(); $department = set_recursion($department); } View::assign('permission', $permission); View::assign('department', $department); View::assign('pid', $pid); View::assign('id', $id); return view(); } } //删除 public function delete() { $id = get_params("id"); $count = Db::name('Department')->where([['pid', '=', $id], ['status', '>=', 0]])->count(); if ($count > 0) { return to_assign(1, "该部门下还有子部门,无法删除"); } $users = Db::name('Admin')->where([['did', '=', $id], ['status', '>=', 0]])->count(); if ($users > 0) { return to_assign(1, "该部门下还有员工,无法删除"); } if (Db::name('Department')->delete($id) !== false) { add_log('delete', $id); return to_assign(0, "删除部门成功"); } else { return to_assign(1, "删除失败"); } } }