$params 已通过 sceneAdd 验证 * @param int $adminId 当前 admin id * @param array $adminInfo request->adminInfo */ public static function add(array $params, int $adminId, array $adminInfo): bool { try { $diagnosisId = (int) ($params['diagnosis_id'] ?? 0); $diagnosis = Diagnosis::findOrEmpty($diagnosisId); if ($diagnosis->isEmpty()) { self::setError('诊单不存在'); return false; } $remindTime = (int) ($params['remind_time'] ?? 0); if ($remindTime <= time()) { self::setError('提醒时间必须晚于当前时间'); return false; } $creatorName = trim((string) ($adminInfo['name'] ?? '')); if ($creatorName === '' && $adminId > 0) { $creatorName = (string) Admin::where('id', $adminId)->value('name'); } DiagnosisTodo::create([ 'diagnosis_id' => $diagnosisId, 'patient_id' => (int) ($diagnosis->getAttr('patient_id') ?? 0), 'content' => trim((string) ($params['content'] ?? '')), 'remind_time' => $remindTime, 'status' => DiagnosisTodo::STATUS_PENDING, 'creator_id' => $adminId, 'creator_name' => $creatorName, ]); return true; } catch (\Throwable $e) { self::setError($e->getMessage()); return false; } } /** * @notes 取消待办(人工) */ public static function cancel(int $id, int $adminId): bool { try { $todo = DiagnosisTodo::findOrEmpty($id); if ($todo->isEmpty()) { self::setError('待办不存在'); return false; } $status = (int) $todo->getAttr('status'); if ($status !== DiagnosisTodo::STATUS_PENDING) { self::setError('该待办已不是「待执行」状态,无法取消'); return false; } $creatorId = (int) $todo->getAttr('creator_id'); $isSuper = self::isSuperAdmin($adminId); if ($creatorId !== $adminId && !$isSuper) { self::setError('仅创建人或超级管理员可取消'); return false; } $todo->save([ 'status' => DiagnosisTodo::STATUS_CANCELLED, 'cancelled_at' => time(), 'cancelled_by' => $adminId, ]); return true; } catch (\Throwable $e) { self::setError($e->getMessage()); return false; } } /** * @notes 详情 * * @return array */ public static function detail(int $id, int $adminId = 0): array { $todo = DiagnosisTodo::findOrEmpty($id); if ($todo->isEmpty()) { return []; } $arr = $todo->append([ 'status_text', 'remind_time_text', 'notified_at_text', 'cancelled_at_text', ])->toArray(); // 业务态:是否能被「我」取消(前端按钮显隐用) $arr['can_cancel'] = self::canCancel($todo->toArray(), $adminId); return $arr; } /** * 是否超管:role_id=1 */ public static function isSuperAdmin(int $adminId): bool { if ($adminId <= 0) { return false; } $roleIds = AdminRole::where('admin_id', $adminId)->column('role_id'); return in_array(1, array_map('intval', $roleIds), true); } /** * 业务判定:当前 admin 能否取消该待办 * * @param array $todoRow */ public static function canCancel(array $todoRow, int $adminId): bool { if ((int) ($todoRow['status'] ?? -1) !== DiagnosisTodo::STATUS_PENDING) { return false; } if ($adminId <= 0) { return false; } if ((int) ($todoRow['creator_id'] ?? 0) === $adminId) { return true; } return self::isSuperAdmin($adminId); } }