Files
zyt/server/app/adminapi/controller/tcm/PrescriptionAiController.php
T
2026-09-10 15:19:17 +08:00

114 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace app\adminapi\controller\tcm;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\logic\tcm\PrescriptionAiLogic;
use DomainException;
final class PrescriptionAiController extends BaseAdminController
{
public function statuses()
{
return $this->handle(false, ['ids'], function (array $p): array {
$raw = $p['ids'] ?? '';
if (!is_array($raw) && !is_string($raw) && !is_int($raw)) {
throw new DomainException('处方标识列表无效');
}
$ids = is_array($raw) ? $raw : explode(',', (string) $raw);
$ids = array_map(fn ($id): int => $this->positive(['id' => $id], 'id'), array_filter($ids, static fn ($id): bool => $id !== ''));
return PrescriptionAiLogic::statuses($ids, $this->adminId, $this->adminInfo);
});
}
public function reports()
{
return $this->handle(false, ['prescription_id', 'diagnosis_id', 'page_no', 'page_size'],
fn (array $p): array => PrescriptionAiLogic::reports($p, $this->adminId, $this->adminInfo));
}
public function detail()
{
return $this->handle(false, ['batch_id'], fn (array $p): array => PrescriptionAiLogic::detail(
$this->positive($p, 'batch_id'), $this->adminId, $this->adminInfo));
}
public function regenerate()
{
return $this->handle(true, ['prescription_id', 'reason'], fn (array $p): array => PrescriptionAiLogic::regenerate(
$this->positive($p, 'prescription_id'), $this->textValue($p, 'reason', 500), $this->adminId, $this->adminInfo));
}
public function retry()
{
return $this->handle(true, ['batch_id', 'model_key'], fn (array $p): array => PrescriptionAiLogic::retry(
$this->positive($p, 'batch_id'), $this->textValue($p, 'model_key', 16), $this->adminId, $this->adminInfo));
}
public function review()
{
return $this->handle(true, ['batch_id', 'model_key', 'status', 'comment'], fn (array $p): array => PrescriptionAiLogic::review(
$this->positive($p, 'batch_id'), $this->textValue($p, 'model_key', 16), $this->textValue($p, 'status', 32),
$this->textValue($p, 'comment', 2000), $this->adminId, $this->adminInfo));
}
public function statistics()
{
return $this->handle(false, ['date_from', 'date_to', 'doctor_id'],
fn (array $p): array => PrescriptionAiLogic::statistics($p, $this->adminId, $this->adminInfo));
}
private function handle(bool $post, array $allowed, callable $handler)
{
if ($post ? !$this->request->isPost() : !$this->request->isGet()) {
return $this->fail('请求方式错误');
}
$params = $post ? $this->request->post() : $this->request->get();
if (array_diff(array_keys($params), $allowed) !== []) {
return $this->fail('请求包含不支持的字段');
}
try {
foreach (['prescription_id', 'diagnosis_id', 'batch_id', 'page_no', 'page_size', 'doctor_id'] as $field) {
if (array_key_exists($field, $params)) {
$params[$field] = $this->positive($params, $field);
}
}
foreach (['date_from', 'date_to'] as $field) {
if (array_key_exists($field, $params)) {
$params[$field] = $this->textValue($params, $field, 10);
}
}
$actor = \app\common\service\prescriptionai\PrescriptionAiAccess::actor($this->adminId);
if ($actor === null) {
throw new DomainException('账号已停用或无权访问');
}
$this->adminInfo = $actor;
return $this->data($handler($params));
} catch (DomainException $e) {
return $this->fail($e->getMessage());
} catch (\Throwable $e) {
return $this->fail('处方AI服务暂不可用,请联系管理员检查部署');
}
}
private function positive(array $p, string $key): int
{
$raw = $p[$key] ?? null;
if (!(is_int($raw) || is_string($raw)) || !preg_match('/^[1-9]\d{0,17}$/', (string) $raw)) {
throw new DomainException('记录标识无效');
}
return (int) $raw;
}
private function textValue(array $p, string $key, int $max): string
{
$raw = $p[$key] ?? '';
if (!is_string($raw) || mb_strlen($raw) > $max) {
throw new DomainException('文本参数无效');
}
return trim($raw);
}
}