You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
272 lines
8.5 KiB
PHP
272 lines
8.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\UserService;
|
|
use Illuminate\Http\Request;
|
|
use App\Lib\Rs;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class PeriodController extends Controller
|
|
{
|
|
private $userService;
|
|
|
|
public function __construct(UserService $userService)
|
|
{
|
|
$this->userService = $userService;
|
|
}
|
|
|
|
/**
|
|
* 获取时间段列表
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function list(Request $request)
|
|
{
|
|
$payload = $request->attributes->get('payload');
|
|
if (!$payload) {
|
|
return Rs::error('用户信息无效', 401);
|
|
}
|
|
|
|
$user = $this->userService->getUserInfoFromPayload($payload);
|
|
if (!$user) {
|
|
return Rs::error('用户不存在', 401);
|
|
}
|
|
|
|
$isAdmin = $this->userService->isAdmin($payload);
|
|
$userDeptId = $user->dept_id ?? null;
|
|
|
|
$departmentId = $request->input('department_id', null);
|
|
$name = $request->input('name', '');
|
|
$status = $request->input('status', null);
|
|
$page = $request->input('page', 1);
|
|
$pageSize = $request->input('page_size', 10);
|
|
|
|
$query = DB::table('period')
|
|
->where('deleted', 0);
|
|
|
|
// 权限控制:非管理员只能查看自己科室的时间段
|
|
if (!$isAdmin) {
|
|
if (empty($userDeptId)) {
|
|
return Rs::error('请先绑定科室');
|
|
}
|
|
$query->where('department_id', $userDeptId);
|
|
// 如果用户传入了科室参数,需要验证是否属于自己科室
|
|
if ($departmentId !== null && $departmentId !== '') {
|
|
if ($departmentId != $userDeptId) {
|
|
return Rs::error('无权限访问其他科室的数据');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!empty($name)) {
|
|
$query->where('name', 'like', '%' . $name . '%');
|
|
}
|
|
|
|
if ($status !== null && $status !== '') {
|
|
$query->where('status', $status);
|
|
}
|
|
|
|
if ($departmentId !== null && $departmentId !== '' && ($isAdmin || empty($userDeptId))) {
|
|
// 管理员或无科室限制的用户可以根据科室筛选
|
|
$query->where('department_id', $departmentId);
|
|
}
|
|
|
|
$total = $query->count();
|
|
|
|
$periods = $query->orderBy('id', 'desc')
|
|
->skip(($page - 1) * $pageSize)
|
|
->take($pageSize)
|
|
->get();
|
|
|
|
foreach ($periods as $period) {
|
|
$department = DB::table('department')
|
|
->where('id', $period->department_id)
|
|
->where('deleted', 0)
|
|
->first();
|
|
$period->department_name = $department ? $department->name : '';
|
|
}
|
|
|
|
$data = [
|
|
'list' => $periods,
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'page_size' => $pageSize,
|
|
'total_pages' => ceil($total / $pageSize),
|
|
'is_admin' => $isAdmin,
|
|
'user_dept_id' => $userDeptId
|
|
];
|
|
|
|
return Rs::success($data);
|
|
}
|
|
|
|
/**
|
|
* 保存时间段
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function Save(Request $request)
|
|
{
|
|
$payload = $request->attributes->get('payload');
|
|
if (!$payload) {
|
|
return Rs::error('用户信息无效', 401);
|
|
}
|
|
|
|
$user = $this->userService->getUserInfoFromPayload($payload);
|
|
if (!$user) {
|
|
return Rs::error('用户不存在', 401);
|
|
}
|
|
|
|
$isAdmin = $this->userService->isAdmin($payload);
|
|
$userDeptId = $user->dept_id ?? null;
|
|
|
|
$data = $request->validate([
|
|
'id' => 'required|integer',
|
|
'department_id' => 'required|integer',
|
|
'name' => 'required|string|max:50',
|
|
'begin_time' => 'required|string|date_format:H:i:s',
|
|
'end_time' => 'required|string|date_format:H:i:s',
|
|
'cutoff_time' => 'nullable|string|date_format:H:i:s',
|
|
'status' => 'required|integer|in:0,1',
|
|
]);
|
|
|
|
// 权限控制:非管理员只能操作自己科室的时间段
|
|
if (!$isAdmin) {
|
|
if (empty($userDeptId)) {
|
|
return Rs::error('请先绑定科室');
|
|
}
|
|
if ($data['department_id'] != $userDeptId) {
|
|
return Rs::error('无权限操作其他科室的数据');
|
|
}
|
|
|
|
// 如果是更新操作,检查时间段是否属于该科室
|
|
if ($data['id'] > 0) {
|
|
$period = DB::table('period')
|
|
->where('id', $data['id'])
|
|
->where('deleted', 0)
|
|
->first();
|
|
|
|
if (!$period) {
|
|
return Rs::error('时间段不存在');
|
|
}
|
|
|
|
if ($period->department_id != $userDeptId) {
|
|
return Rs::error('无权限操作该时间段');
|
|
}
|
|
}
|
|
}
|
|
|
|
// 验证时间逻辑:结束时间必须晚于开始时间
|
|
$beginTime = strtotime('2000-01-01 ' . $data['begin_time']);
|
|
$endTime = strtotime('2000-01-01 ' . $data['end_time']);
|
|
|
|
if ($endTime <= $beginTime) {
|
|
return Rs::error('结束时间必须晚于开始时间');
|
|
}
|
|
|
|
// 如果没有设置截止预约时间,默认使用结束时间
|
|
if (empty($data['cutoff_time'])) {
|
|
$data['cutoff_time'] = $data['end_time'];
|
|
}
|
|
|
|
// 验证截止预约时间
|
|
$cutoffTime = strtotime('2000-01-01 ' . $data['cutoff_time']);
|
|
if ($cutoffTime > $endTime) {
|
|
return Rs::error('截止预约时间不能晚于结束时间');
|
|
}
|
|
|
|
$conditions = ['id' => $data['id']];
|
|
$values = [
|
|
'department_id' => $data['department_id'],
|
|
'name' => $data['name'],
|
|
'begin_time' => $data['begin_time'],
|
|
'end_time' => $data['end_time'],
|
|
'cutoff_time' => $data['cutoff_time'],
|
|
'status' => $data['status'],
|
|
];
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
if ($data['id'] == 0) {
|
|
// 新增操作
|
|
$values['created_at'] = now();
|
|
$values['updated_at'] = now();
|
|
$periodId = DB::table('period')->insertGetId($values);
|
|
} else {
|
|
// 更新操作
|
|
$values['updated_at'] = now();
|
|
$updated = DB::table('period')
|
|
->where('id', $data['id'])
|
|
->update($values);
|
|
if ($updated === false) {
|
|
DB::rollBack();
|
|
return Rs::error('操作失败:时间段不存在');
|
|
}
|
|
$periodId = $data['id'];
|
|
}
|
|
|
|
DB::commit();
|
|
return Rs::success(['id' => $periodId]);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return Rs::error('保存失败:' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除时间段
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function Delete(Request $request)
|
|
{
|
|
$payload = $request->attributes->get('payload');
|
|
if (!$payload) {
|
|
return Rs::error('用户信息无效', 401);
|
|
}
|
|
|
|
$user = $this->userService->getUserInfoFromPayload($payload);
|
|
if (!$user) {
|
|
return Rs::error('用户不存在', 401);
|
|
}
|
|
|
|
$isAdmin = $this->userService->isAdmin($payload);
|
|
$userDeptId = $user->dept_id ?? null;
|
|
|
|
$data = $request->validate([
|
|
'id' => 'required|integer',
|
|
]);
|
|
|
|
// 查询时间段信息
|
|
$period = DB::table('period')
|
|
->where('id', $data['id'])
|
|
->where('deleted', 0)
|
|
->first();
|
|
|
|
if (!$period) {
|
|
return Rs::error('时间段不存在');
|
|
}
|
|
|
|
// 权限控制:非管理员只能删除自己科室的时间段
|
|
if (!$isAdmin) {
|
|
if (empty($userDeptId)) {
|
|
return Rs::error('请先绑定科室');
|
|
}
|
|
if ($period->department_id != $userDeptId) {
|
|
return Rs::error('无权限删除该时间段');
|
|
}
|
|
}
|
|
|
|
$deleted = DB::table('period')->where('id', $data['id'])->update(['deleted' => 1]);
|
|
|
|
if ($deleted) {
|
|
return Rs::success();
|
|
}
|
|
|
|
return Rs::error('删除失败');
|
|
}
|
|
}
|