146 lines
6.2 KiB
PHP
146 lines
6.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service\esign;
|
|
|
|
use DomainException;
|
|
|
|
/** Pure rules shared by API, audit and dispensing boundaries. */
|
|
final class PrescriptionEsignPolicy
|
|
{
|
|
public const CONTENT_FIELDS = [
|
|
'id', 'sn', 'creator_id', 'doctor_name', 'diagnosis_id', 'appointment_id',
|
|
'patient_id', 'patient_name', 'gender', 'age', 'phone', 'visit_no', 'prescription_date',
|
|
'prescription_name', 'prescription_type', 'clinical_diagnosis', 'tongue', 'tongue_image',
|
|
'pulse', 'pulse_condition', 'herbs', 'dose_count', 'dose_unit', 'dosage_amount',
|
|
'dosage_unit', 'dosage_bag_count', 'need_decoction', 'bags_per_dose', 'usage_days',
|
|
'times_per_day', 'aux_usage', 'usage_instruction', 'usage_time', 'usage_way',
|
|
'dietary_taboo', 'usage_notes', 'case_record', 'amount',
|
|
];
|
|
|
|
public static function snapshot(array $row): array
|
|
{
|
|
$out = [];
|
|
foreach (self::CONTENT_FIELDS as $key) {
|
|
$value = $row[$key] ?? null;
|
|
if (in_array($key, ['herbs', 'aux_usage', 'case_record'], true) && is_string($value)) {
|
|
$value = trim($value) === '' ? null : json_decode($value, true, 512, JSON_THROW_ON_ERROR);
|
|
}
|
|
if (in_array($key, ['dosage_amount', 'amount'], true) && $value !== null && is_numeric($value)) {
|
|
// ORM float casts and raw DECIMAL strings represent the same clinical quantity.
|
|
$value = self::decimal((string) $value);
|
|
}
|
|
$out[$key] = self::canonical($value);
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
private static function canonical(mixed $value): mixed
|
|
{
|
|
if (!is_array($value)) {
|
|
// DB raw rows and ORM numeric casts must fingerprint identically.
|
|
return $value === null ? null : (is_bool($value) ? (string) (int) $value : (string) $value);
|
|
}
|
|
if ($value !== [] && array_keys($value) !== range(0, count($value) - 1)) {
|
|
ksort($value);
|
|
}
|
|
return array_map([self::class, 'canonical'], $value);
|
|
}
|
|
|
|
private static function decimal(string $value): string
|
|
{
|
|
if (strpbrk($value, 'eE') !== false) {
|
|
$value = sprintf('%.12F', (float) $value);
|
|
}
|
|
if (str_contains($value, '.')) {
|
|
$value = rtrim(rtrim($value, '0'), '.');
|
|
}
|
|
return in_array($value, ['-0', '+0'], true) ? '0' : $value;
|
|
}
|
|
|
|
public static function hash(array $row): string
|
|
{
|
|
return hash('sha256', json_encode(self::snapshot($row), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR));
|
|
}
|
|
|
|
public static function assertMutable(array $row): void
|
|
{
|
|
if (!empty($row['esign_required']) && ($row['esign_status'] ?? 'pending') !== 'pending') {
|
|
throw new DomainException('处方已进入电子签署,内容已冻结;如需修改请作废后重新开方');
|
|
}
|
|
}
|
|
|
|
public static function isPendingReview(array $row): bool
|
|
{
|
|
return in_array($row['audit_status'] ?? null, [0, '0'], true)
|
|
&& empty($row['void_status']) && empty($row['delete_time']);
|
|
}
|
|
|
|
public static function isAwaitingUpload(array $row): bool
|
|
{
|
|
return self::isPendingReview($row) && empty($row['review_submitted_at']);
|
|
}
|
|
|
|
public static function hasPrescriptionContent(array $row): bool
|
|
{
|
|
$herbs = $row['herbs'] ?? null;
|
|
if (is_string($herbs)) {
|
|
$herbs = json_decode($herbs, true);
|
|
}
|
|
return empty($row['is_system_auto']) && is_array($herbs) && $herbs !== [];
|
|
}
|
|
|
|
public static function isSubmittedForReview(array $row): bool
|
|
{
|
|
if (empty($row['id']) || !empty($row['void_status']) || !empty($row['delete_time'])) {
|
|
return false;
|
|
}
|
|
// Already approved prescriptions may still enter the business-order audit flow.
|
|
return in_array($row['audit_status'] ?? null, [1, '1'], true)
|
|
|| (self::isPendingReview($row) && (int) ($row['review_submitted_at'] ?? 0) > 0);
|
|
}
|
|
|
|
public static function assertReviewReady(array $row): void
|
|
{
|
|
if (!self::isSubmittedForReview($row)) {
|
|
throw new DomainException('请开方医生先上传处方,再进行审核');
|
|
}
|
|
}
|
|
|
|
public static function assertSigner(array $row, int $actor): void
|
|
{
|
|
if ($actor <= 0 || (int) ($row['creator_id'] ?? 0) !== $actor) {
|
|
throw new DomainException('仅开方医生本人可以签署此处方');
|
|
}
|
|
if (!empty($row['void_status']) || !empty($row['delete_time'])) {
|
|
throw new DomainException('处方已作废或删除,不能签署');
|
|
}
|
|
if (!empty($row['is_system_auto'])) {
|
|
throw new DomainException('请先补全处方,再进行电子签署');
|
|
}
|
|
}
|
|
|
|
public static function assertSigned(array $row, ?array $evidence, bool $forDispensing = false): void
|
|
{
|
|
if (empty($row['esign_required'])) {
|
|
return; // Explicit migration boundary: historical handwritten prescriptions.
|
|
}
|
|
if (!empty($row['void_status']) || !empty($row['delete_time']) || ($row['esign_status'] ?? '') !== 'signed'
|
|
|| !$evidence || empty($evidence['signed_hash']) || empty($evidence['sign_service_id'])
|
|
|| empty($evidence['signed_path']) || empty($evidence['signed_at']) || empty($evidence['account_id'])
|
|
|| (int) ($evidence['prescription_id'] ?? 0) !== (int) ($row['id'] ?? 0)
|
|
|| (int) ($evidence['doctor_id'] ?? 0) !== (int) ($row['creator_id'] ?? 0)
|
|
|| !hash_equals((string) ($evidence['snapshot_hash'] ?? ''), self::hash($row))) {
|
|
throw new DomainException('处方尚未完成有效电子签署,请开方医生完成签署后重试');
|
|
}
|
|
if ($forDispensing && ($evidence['environment'] ?? '') !== 'production') {
|
|
throw new DomainException('沙箱签署处方仅供测试,不能用于正式发药');
|
|
}
|
|
$issuedAt = (int) ($evidence['issued_at'] ?? 0);
|
|
if ($issuedAt > 0 && (($evidence['signature_time_source'] ?? '') !== 'tsa'
|
|
|| (int) ($evidence['signature_time'] ?? 0) < $issuedAt)) {
|
|
throw new DomainException('处方开具和签署时间的验签证据不完整');
|
|
}
|
|
}
|
|
}
|