新增编辑删除部门架构

This commit is contained in:
yaooo 2023-10-28 15:58:35 +08:00
parent c16a8cc7b8
commit 112ff1f367
1 changed files with 38 additions and 50 deletions

View File

@ -22,7 +22,8 @@ class UserDepartment extends ApiController
protected $middleware = [
Auth::class => ['except' => []]
];
//获取部门架构
public function index()
{
$cate = Db::name('Department')
@ -35,78 +36,65 @@ class UserDepartment extends ApiController
$this->apiSuccess('获取成功', $list);
}
//获取部门树形架构
public function tree()
{
$department = set_recursion(get_department());
$this->apiSuccess('获取成功', $department);
}
//添加部门
public function add()
public function operate()
{
$param = get_params();
if (request()->isAjax()) {
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();
}
if (!empty($param['id']) && $param['id'] > 0) {
try {
validate(DepartmentCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
$this->apiError($e->getError());
}
$param['update_time'] = time();
$department_array = get_department_son($param['id']);
if (in_array($param['pid'], $department_array)) {
$this->apiError('上级部门不能是该部门本身或其下属部门');
} else {
try {
validate(DepartmentCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$did = Db::name('Department')->strict(false)->field(true)->insertGetId($param);
add_log('add', $did, $param);
return to_assign();
Db::name('Department')->strict(false)->field(true)->update($param);
add_log('edit', $param['id'], $param);
$this->apiSuccess('操作成功');
}
} else {
$id = isset($param['id']) ? $param['id'] : 0;
$pid = isset($param['pid']) ? $param['pid'] : 0;
$department = set_recursion(get_department());
if ($id > 0) {
$detail = Db::name('Department')->where(['id' => $id])->find();
//获取子部门
$department = get_department();
$department_list = get_data_node($department, $id);
$department_array = array_column($department_list, 'id');
//包括自己部门在内
$department_array[] = $id;
$users = Db::name('Admin')->where([['did','in',$department_array], ['status','=',1]])->select();
View::assign('users', $users);
View::assign('detail', $detail);
try {
validate(DepartmentCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
$this->apiError($e->getError());
}
View::assign('department', $department);
View::assign('pid', $pid);
View::assign('id', $id);
return view();
$did = Db::name('Department')->strict(false)->field(true)->insertGetId($param);
add_log('add', $did, $param);
$this->apiSuccess('操作成功');
}
}
//删除
public function delete()
{
$id = get_params("id");
if (empty($id)) {
$this->apiError('部门id不能为空');
}
$count = Db::name('Department')->where([['pid', '=', $id], ['status', '>=', 0]])->count();
if ($count > 0) {
return to_assign(1, "该部门下还有子部门,无法删除");
$this->apiError('该部门下还有子部门,无法删除');
}
$users = Db::name('Admin')->where([['did', '=', $id], ['status', '>=', 0]])->count();
if ($users > 0) {
return to_assign(1, "该部门下还有员工,无法删除");
$this->apiError('该部门下还有员工,无法删除');
}
if (Db::name('Department')->delete($id) !== false) {
add_log('delete', $id);
return to_assign(0, "删除部门成功");
$this->apiSuccess('删除部门成功');
} else {
return to_assign(1, "删除失败");
$this->apiError('删除失败');
}
}
}