You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Admin;
use App\Models\Department;
use App\Models\OperateLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DepartmentController extends CommonController
{
/**
* @OA\Get(
* path="/api/admin/department",
* tags={"后台管理"},
* summary="获取部门",
* description="",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="获取部门"
* )
* )
*/
public function index()
{
$data = (new Department())->with(["manager", "leader"])->orderBy("sortnumber")->get()->toArray();
$data = array2tree($data);
return $this->success($data);
}
/**
* @OA\Get(
* path="/api/admin/department/show",
* tags={"后台管理"},
* summary="获取部门详情",
* description="",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="获取部门"
* )
* )
*/
public function show()
{
$data = (new Department())->with(["manager", "leader"])->find(request()->id);
return $this->success($data);
}
/**
* @OA\Post(
* path="/api/admin/department/save",
* tags={"后台管理"},
* summary="保存部门",
* description="",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), description="部门ID为空表示新增不为空表示更新"),
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=true, description="名称"),
* @OA\Parameter(name="pid", in="query", @OA\Schema(type="integer"), description="父id默认为0"),
* @OA\Parameter(name="manager_id", in="query", @OA\Schema(type="integer"), description="部门负责人ID"),
* @OA\Parameter(name="leader_id", in="query", @OA\Schema(type="integer"), description="部门分管人ID"),
* @OA\Parameter(name="sortnumber", in="query", @OA\Schema(type="integer"), description="同级排序默认为0"),
* @OA\Response(
* response="200",
* description="保存部门"
* )
* )
*/
public function save()
{
if (request()->id) {
return $this->update();
} else {
return $this->store();
}
}
public function store()
{
DB::beginTransaction();
try {
$data = (new Department())->filterRequestColumns(request(), ["id"]);
$model = Department::create($data);
DB::commit();
// 加日志
OperateLog::addLogs($this->getUser(), "新建部门[$model->name]成功");
return $this->success($model);
} catch (\Exception $exception) {
DB::rollBack();
// 加日志
OperateLog::addLogs($this->getUser(), "新建部门失败", $exception->getMessage());
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
}
public function update()
{
DB::beginTransaction();
try {
$model = Department::find(request()->id);
$data = $model->filterRequestColumns(request(), ["id"]);
$model->update($data);
DB::commit();
// 加日志
OperateLog::addLogs($this->getUser(), "更新部门[$model->name]信息成功");
return $this->success($model);
} catch (\Exception $exception) {
DB::rollBack();
// 加日志
OperateLog::addLogs($this->getUser(), '更新部门信息失败', $exception->getMessage());
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
}
/**
* @OA\Post(
* path="/api/admin/department/delete",
* tags={"后台管理"},
* summary="删除部门",
* description="",
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), description="ID"),
* @OA\Response(
* response="200",
* description="删除部门"
* )
* )
*/
public function delete(Request $request)
{
$department = Department::find($request->id);
try {
$department->delete();
// 加日志
OperateLog::addLogs($this->getUser(), "删除部门信息[$department->name]成功");
return $this->success("删除成功");
} catch (\Exception $exception) {
// 加日志
OperateLog::addLogs($this->getUser(), "删除部门信息[$department->name]失败", $exception->getMessage());
return $this->fail([$exception->getCode(), $exception->getMessage()]);
}
}
}