93 lines
5.2 KiB
PHP
93 lines
5.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
// Read local configuration without booting ThinkPHP, writing SQL, or emitting credentials/content.
|
|
$pdo = null;
|
|
$stage = 'configuration';
|
|
try {
|
|
$sections = @parse_ini_file(dirname(__DIR__) . '/server/.env', true, INI_SCANNER_RAW);
|
|
if (!is_array($sections)) throw new RuntimeException('configuration_unreadable');
|
|
$cfg = [];
|
|
foreach ($sections as $key => $value) {
|
|
if (strtoupper((string) $key) === 'DATABASE' && is_array($value)) {
|
|
$cfg = array_change_key_case($value, CASE_UPPER);
|
|
}
|
|
}
|
|
$host = trim((string) ($cfg['HOSTNAME'] ?? ''));
|
|
$database = trim((string) ($cfg['DATABASE'] ?? ''));
|
|
$port = (int) ($cfg['HOSTPORT'] ?? 0);
|
|
if ($host !== '127.0.0.1' || $database !== 'zyt' || $port !== 3306) {
|
|
throw new RuntimeException('local_target_mismatch');
|
|
}
|
|
$prefix = (string) ($cfg['PREFIX'] ?? 'zyt_');
|
|
if (!preg_match('/^[A-Za-z0-9_]+$/D', $prefix)) throw new RuntimeException('invalid_prefix');
|
|
$stage = 'connection';
|
|
$pdo = new PDO("mysql:host=127.0.0.1;port=3306;dbname=zyt;charset=utf8mb4",
|
|
(string) ($cfg['USERNAME'] ?? ''), (string) ($cfg['PASSWORD'] ?? ''), [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
PDO::ATTR_TIMEOUT => 5,
|
|
]);
|
|
unset($cfg, $sections);
|
|
$pdo->exec('SET TRANSACTION READ ONLY');
|
|
$pdo->beginTransaction();
|
|
$stage = 'metadata';
|
|
$orderTable = $prefix . 'tcm_prescription_order';
|
|
$logTable = $prefix . 'tcm_prescription_order_log';
|
|
$metadata = $pdo->prepare('SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME IN (?, ?)');
|
|
$metadata->execute([$orderTable, $logTable]);
|
|
$columns = [];
|
|
foreach ($metadata->fetchAll(PDO::FETCH_ASSOC) as $column) {
|
|
$columns[$column['TABLE_NAME']][$column['COLUMN_NAME']] = $column['DATA_TYPE'];
|
|
}
|
|
$required = [$orderTable => ['prescription_audit_signature', 'prescription_audit_signed_by',
|
|
'prescription_audit_signed_name', 'prescription_audit_signed_at'], $logTable => ['audit_signature']];
|
|
$result = ['checked_at' => date('c'), 'connection' => '127.0.0.1:3306 / zyt',
|
|
'transaction' => 'READ ONLY; rollback after SELECTs', 'columns' => []];
|
|
foreach ($required as $table => $names) {
|
|
foreach ($names as $name) {
|
|
$result['columns'][] = ['table' => $table, 'column' => $name,
|
|
'exists' => isset($columns[$table][$name]), 'type' => $columns[$table][$name] ?? null];
|
|
}
|
|
}
|
|
$orderNo = 'PO20261017141255370242';
|
|
$projection = ['id', 'prescription_id', 'prescription_audit_status', 'payment_slip_audit_status', 'fulfillment_status'];
|
|
foreach ([
|
|
'prescription_audit_signature' => "COALESCE(OCTET_LENGTH(`prescription_audit_signature`),0) AS signature_bytes",
|
|
'prescription_audit_signed_by' => 'COALESCE(`prescription_audit_signed_by`,0)>0 AS signed_by_present',
|
|
'prescription_audit_signed_name' => "CHAR_LENGTH(TRIM(COALESCE(`prescription_audit_signed_name`,'')))>0 AS signed_name_present",
|
|
'prescription_audit_signed_at' => '`prescription_audit_signed_at` AS signed_at',
|
|
] as $column => $expression) {
|
|
if (isset($columns[$orderTable][$column])) $projection[] = $expression;
|
|
}
|
|
$stage = 'order';
|
|
$query = $pdo->prepare('SELECT ' . implode(',', $projection) . " FROM `{$orderTable}` WHERE order_no=? AND delete_time IS NULL LIMIT 1");
|
|
$query->execute([$orderNo]);
|
|
$order = $query->fetch(PDO::FETCH_ASSOC);
|
|
$result['order_no'] = $orderNo;
|
|
$result['order'] = $order ?: null;
|
|
$result['history'] = null;
|
|
if ($order && isset($columns[$logTable])) {
|
|
$stage = 'history';
|
|
$hasLogSignature = isset($columns[$logTable]['audit_signature']);
|
|
$countExpression = $hasLogSignature ? ", SUM(COALESCE(OCTET_LENGTH(audit_signature),0)>0) AS signed_log_count" : '';
|
|
$query = $pdo->prepare("SELECT COUNT(*) AS total_order_logs{$countExpression} FROM `{$logTable}` WHERE prescription_order_id=?");
|
|
$query->execute([(int) $order['id']]);
|
|
$result['history'] = $query->fetch(PDO::FETCH_ASSOC);
|
|
$result['history']['signature_column_available'] = $hasLogSignature;
|
|
$sizeExpression = $hasLogSignature ? ', COALESCE(OCTET_LENGTH(audit_signature),0) AS signature_bytes' : '';
|
|
$query = $pdo->prepare("SELECT id,action,admin_id,create_time{$sizeExpression} FROM `{$logTable}` WHERE prescription_order_id=? AND action IN ('audit_rx_approve','audit_rx_reject','revoke_rx_audit','manual_log') ORDER BY id DESC LIMIT 100");
|
|
$query->execute([(int) $order['id']]);
|
|
$result['history']['review_events'] = $query->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
$pdo->rollBack();
|
|
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR) . "\n";
|
|
} catch (Throwable $error) {
|
|
if ($pdo instanceof PDO && $pdo->inTransaction()) $pdo->rollBack();
|
|
// Never emit raw exception messages: database connection errors can contain account data.
|
|
echo json_encode(['success' => false, 'stage' => $stage, 'exception' => get_class($error),
|
|
'code' => (string) $error->getCode()], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR) . "\n";
|
|
exit(1);
|
|
}
|