This commit is contained in:
Your Name
2026-11-26 15:46:20 +08:00
parent db6d327e91
commit be79e09c83
170 changed files with 21159 additions and 13217 deletions
File diff suppressed because it is too large Load Diff
@@ -51,7 +51,7 @@ class PatientAiReportLogic extends BaseLogic
private const PROMPT_VERSION = 'patient-longitudinal-report-tcm-four-diagnoses-v7';
/** @var array<int,string> */
private const MODEL_KEYS = ['qwen', 'openai'];
private const MODEL_KEYS = ['qwen', 'openai', 'doubao'];
private const MAX_RESPONSE_BYTES = 65536;
@@ -174,7 +174,7 @@ class PatientAiReportLogic extends BaseLogic
array $adminInfo
): ?array {
if (!in_array($modelKey, self::MODEL_KEYS, true)) {
self::setError('AI模型仅支持qwen或openai');
self::setError('AI模型仅支持qwen、openai或doubao');
return null;
}
if (!self::hasPermission($adminId, $adminInfo, self::PERMISSION_READ)) {
@@ -1664,7 +1664,7 @@ class PatientAiReportLogic extends BaseLogic
$authorized = array_fill_keys(array_map('intval', $diagnosisIds), true);
$versionById = [];
$modelVersions = ['qwen' => 0, 'openai' => 0];
$modelVersions = array_fill_keys(self::MODEL_KEYS, 0);
foreach (array_reverse($rows) as $row) {
$model = strtolower(trim((string) ($row['model_key'] ?? '')));
if (!array_key_exists($model, $modelVersions)) {
@@ -1675,7 +1675,7 @@ class PatientAiReportLogic extends BaseLogic
}
$reports = [];
$latestByModel = ['qwen' => null, 'openai' => null];
$latestByModel = array_fill_keys(self::MODEL_KEYS, null);
$generatedReport = null;
foreach ($rows as $row) {
$sourceDiagnosisIds = array_values(array_filter(array_map(
@@ -29,7 +29,7 @@ class PrescriptionLibraryAiLogic extends BaseLogic
private const PERMISSION_EDIT = 'tcm.prescriptionlibrary/editaireport';
/** @var array<int,string> */
private const MODEL_KEYS = ['qwen', 'openai'];
private const MODEL_KEYS = ['qwen', 'openai', 'doubao'];
/** @var array<string,string> */
private const TEXT_REPORT_SECTIONS = [
@@ -28,6 +28,7 @@ class PrescriptionLogic
public const PRESCRIBE_PERMISSIONS = [
'tcm.prescription/add', 'cf.prescription/add', 'tcm.diagnosis/chufang', 'tcm.diagnosis/kaifang',
];
public const REVIEW_SUBMIT_PERMISSIONS = [...self::PRESCRIBE_PERMISSIONS, 'tcm.prescription/edit', 'cf.prescription/edit'];
public const VOID_MANAGE_PERMISSIONS = ['tcm.prescription/void', 'cf.prescription/del'];
public const AUDIT_PERMISSIONS = ['tcm.prescription/audit', 'cf.prescription/audit'];
@@ -188,18 +189,67 @@ class PrescriptionLogic
&& (int) ($row['audit_by'] ?? 0) > 0 && (int) $row['audit_by'] === (int) ($row['void_by'] ?? 0)
&& (empty($row['esign_required']) || ($row['esign_status'] ?? 'pending') === 'pending');
}
public static function canSubmitForReview(int $adminId, array $adminInfo): bool
{
return $adminId > 0 && ((int) ($adminInfo['root'] ?? 0) === 1
|| array_intersect(self::REVIEW_SUBMIT_PERMISSIONS, array_map('strtolower', AuthLogic::getAuthByAdminId($adminId))) !== []);
}
public static function withSigningStatus(array $row, int $adminId, bool $maySubmit, bool $details = false): array
{
$row['esign'] = PrescriptionEsignService::describe($row, $adminId, $details, $maySubmit);
$row['review_submitted_at'] = $row['esign']['review_submitted_at'];
$row['can_submit_for_review'] = $row['esign']['can_submit_for_review'];
$row['can_review_prescription'] = $row['esign']['can_review_prescription'];
return $row;
}
public static function submitForReview(int $id, int $adminId, array $adminInfo): array
{
if (!self::canSubmitForReview($adminId, $adminInfo)) {
throw new \DomainException('无权限上传处方');
}
return Db::transaction(static function () use ($id, $adminId): array {
$prescription = Prescription::where('id', $id)->whereNull('delete_time')->lock(true)->find();
if (!$prescription || (int) $prescription->creator_id !== $adminId) {
throw new \DomainException('处方不存在或非本人开具');
}
$row = $prescription->toArray();
$required = PrescriptionEsignService::requiresSignature($row, PrescriptionEsignService::enabled());
if (!empty($row['void_status']) || !PrescriptionEsignService::hasUploadContent($row, $required)) {
throw new \DomainException('请确认处方未作废并已补全内容');
}
$submittedAt = (int) ($row['review_submitted_at'] ?? 0);
if ($submittedAt <= 0) {
if (!PrescriptionEsignPolicy::isPendingReview($row)) {
throw new \DomainException('当前处方状态不可上传');
}
$submittedAt = time();
if (!$prescription->save(['review_submitted_at' => $submittedAt])) {
throw new \DomainException('处方上传未完成,请重试');
}
}
return ['review_submitted_at' => $submittedAt, 'can_submit_for_review' => false];
});
}
private static function withActionAvailability(array $row, int $viewerAdminId, array $viewerAdminInfo): array
{
$id = (int) $row['id'];
// 与列表和创建校验一致:已取消 / 已退款的订单保留历史关联,但不再占用处方。
$hasBizOrder = PrescriptionOrder::where('prescription_id', $id)
$orders = PrescriptionOrder::where('prescription_id', $id)
->whereNull('delete_time')
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
->count() > 0;
->field(['fulfillment_status', 'prescription_audit_status', 'payment_slip_audit_status'])
->select()->toArray();
$hasBizOrder = array_filter($orders, static fn (array $order): bool =>
!in_array((int) $order['fulfillment_status'], PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES, true)) !== [];
$row['has_prescription_order'] = $hasBizOrder ? 1 : 0;
$row['can_restore_rejected'] = self::canRestoreRejected($row);
$row['can_void'] = !$hasBizOrder && empty($row['void_status'])
$row['can_void'] = empty($row['void_status']) && !self::hasBlockingVoidOrder($row, $orders)
&& self::hasVoidAccess(
$row,
$viewerAdminId,
@@ -211,6 +261,24 @@ class PrescriptionLogic
return $row;
}
/** Pending prescriptions can be voided while any active order is still waiting for both reviews. */
public static function hasBlockingVoidOrder(array $prescription, array $orders): bool
{
foreach ($orders as $order) {
$fulfillmentStatus = (int) ($order['fulfillment_status'] ?? -1);
if (in_array($fulfillmentStatus, PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES, true)) {
continue;
}
if ($fulfillmentStatus !== 1
|| !in_array($order['prescription_audit_status'] ?? null, [0, '0'], true)
|| !in_array($order['payment_slip_audit_status'] ?? null, [0, '0'], true)
|| !PrescriptionEsignPolicy::isPendingReview($prescription)) {
return true;
}
}
return false;
}
private static function generateSn(): string
{
return 'RX' . date('Ymd') . str_pad((string)mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
@@ -648,6 +716,7 @@ class PrescriptionLogic
'is_system_auto' => 0,
// 开方医生修改后重新进入待审核
'audit_status' => 0,
'review_submitted_at' => 0,
'audit_time' => null,
'audit_by' => null,
'audit_by_name' => '',
@@ -790,7 +859,7 @@ class PrescriptionLogic
'patient_name' => $patientName,
'phone' => $phone,
'gender' => $gender,
]);
] + (PrescriptionEsignPolicy::isPendingReview($prescription->toArray()) ? ['review_submitted_at' => 0] : []));
$latestPo = PrescriptionOrder::where('prescription_id', $rxId)
->whereNull('delete_time')
@@ -881,7 +950,8 @@ class PrescriptionLogic
return null;
}
$arr = $row->toArray();
$arr['esign'] = PrescriptionEsignService::describe($arr, $viewerAdminId);
$arr = self::withSigningStatus($arr, $viewerAdminId,
(int) $row->creator_id === $viewerAdminId && self::canSubmitForReview($viewerAdminId, $viewerAdminInfo), true);
$arr['gender_desc'] = $arr['gender'] == 1 ? '男' : '女';
$bizPo = PrescriptionOrder::where('prescription_id', $id)
@@ -942,7 +1012,7 @@ class PrescriptionLogic
if (!$row || (int) ($row->void_status ?? 0) === 1 || (int) ($row->audit_status ?? 1) !== 0) {
return null;
}
PrescriptionEsignService::assertSignedById($prescriptionId, false, $adminId);
PrescriptionEsignService::assertSignedById($prescriptionId, false, $adminId, orderApproval: true);
$row->audit_status = 1;
$row->audit_time = time();
$row->audit_by = $adminId;
@@ -1046,6 +1116,7 @@ class PrescriptionLogic
}
if ($action === 'reject') {
PrescriptionEsignService::assertReviewReady($row->toArray());
if (trim($remark) === '') {
self::setError('驳回时请填写审核意见');
@@ -1166,10 +1237,9 @@ class PrescriptionLogic
$rows,
static fn (array $row): bool => self::canViewPrescription($row, $viewerAdminId, $viewerAdminInfo)
));
return array_map(static function (array $row) use ($viewerAdminId): array {
$row['esign'] = PrescriptionEsignService::describe($row, $viewerAdminId);
return $row;
}, $visible);
$maySubmit = in_array($viewerAdminId, array_map('intval', array_column($visible, 'creator_id')), true)
&& self::canSubmitForReview($viewerAdminId, $viewerAdminInfo);
return array_map(static fn (array $row): array => self::withSigningStatus($row, $viewerAdminId, $maySubmit), $visible);
}
/**
@@ -1206,8 +1276,8 @@ class PrescriptionLogic
return null;
}
$result = $row->toArray();
$result['esign'] = PrescriptionEsignService::describe($result, $viewerAdminId);
$result = self::withSigningStatus($row->toArray(), $viewerAdminId,
(int) $row->creator_id === $viewerAdminId && self::canSubmitForReview($viewerAdminId, $viewerAdminInfo));
return self::withActionAvailability($result, $viewerAdminId, $viewerAdminInfo);
}
@@ -1348,7 +1418,7 @@ class PrescriptionLogic
/**
* 作废处方
*/
public static function void(int $id, int $adminId, array $adminInfo): bool
public static function void(int $id, int $adminId, array $adminInfo, bool $pendingOnly = false): bool
{
self::setError('');
try {
@@ -1362,7 +1432,7 @@ class PrescriptionLogic
return (bool) LockedPharmacySnapshotMutation::executeForPrescription(
$id,
static fn (): bool => self::voidLocked($id, $adminId, $adminInfo, $access)
static fn (array $orders): bool => self::voidLocked($id, $adminId, $adminInfo, $access, $pendingOnly, $orders)
);
} catch (\Throwable $exception) {
if (self::getError() === '') {
@@ -1372,7 +1442,7 @@ class PrescriptionLogic
}
}
private static function voidLocked(int $id, int $adminId, array $adminInfo, array $access): bool
private static function voidLocked(int $id, int $adminId, array $adminInfo, array $access, bool $pendingOnly, array $orders): bool
{
$row = Prescription::where('id', $id)->whereNull('delete_time')->lock(true)->find();
if (!$row || !self::hasVoidAccess($row->toArray(), $adminId, $adminInfo, $access)) {
@@ -1383,13 +1453,12 @@ class PrescriptionLogic
self::setError('该处方已作废');
return false;
}
$rxId = (int) ($row->id ?? 0);
$bizCount = (int) PrescriptionOrder::where('prescription_id', $rxId)
->whereNull('delete_time')
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
->count();
if ($bizCount > 0) {
self::setError('该处方已存在业务订单,无法作废');
if ($pendingOnly && !PrescriptionEsignPolicy::isPendingReview($row->toArray())) {
self::setError('处方审核状态已变化,请刷新列表后查看');
return false;
}
if (self::hasBlockingVoidOrder($row->toArray(), $orders)) {
self::setError('关联业务订单的审核或履约状态不允许作废此处方');
return false;
}
@@ -60,16 +60,33 @@ class PrescriptionOrderLogic
return self::$error;
}
private static function assertEsignApproved(PrescriptionOrder $order, bool $forDispensing = false, ?int $reviewer = null): bool
private static function assertEsignApproved(PrescriptionOrder $order, bool $forDispensing = false, ?int $orderAuditor = null): bool
{
try {
PrescriptionEsignService::assertSignedById((int) $order->prescription_id, $forDispensing, $reviewer);
PrescriptionEsignService::assertSignedById(
(int) $order->prescription_id, $forDispensing, $orderAuditor, orderApproval: $orderAuditor !== null
);
return true;
} catch (\DomainException $e) {
self::$error = $e->getMessage();
return false;
}
}
private static function assertPrescriptionReviewReady(PrescriptionOrder $order): void
{
$rx = Db::name('tcm_prescription')->where('id', (int) $order->prescription_id)->lock(true)->find();
PrescriptionEsignService::assertReviewReady($rx ?: []);
}
public static function withReviewAvailability(array $order, array $rx): array
{
$order['review_submitted_at'] = (int) ($rx['review_submitted_at'] ?? 0);
$order['can_review_prescription'] = (int) ($order['prescription_audit_status'] ?? -1) === 0
&& !in_array((int) ($order['fulfillment_status'] ?? 0), [3, 4], true)
&& PrescriptionEsignService::canReview($rx);
return $order;
}
private static function assertTrackingAuditApproved(PrescriptionOrder $order): bool
{
@@ -1284,6 +1301,8 @@ class PrescriptionOrderLogic
}
}
$arr = self::withReviewAvailability($arr, $arr['prescription'] ?? []);
$canHerbs = self::canViewPrescriptionHerbsInOrderDetail($adminInfo);
$arr['prescription_detail_herbs_visible'] = $canHerbs;
if (! $canHerbs && isset($arr['prescription']) && is_array($arr['prescription'])) {
@@ -2329,21 +2348,7 @@ class PrescriptionOrderLogic
try {
$order->save();
// 撤回订单后,将关联的处方审核状态改回"待审核"
$prescriptionId = (int) $order->prescription_id;
if ($prescriptionId > 0) {
Prescription::where('id', $prescriptionId)
->whereNull('delete_time')
->update([
'audit_status' => 0, // 0=待审核
'audit_time' => null,
'audit_by' => null,
'audit_by_name' => '',
'audit_remark' => '',
'update_time' => time(),
]);
}
self::resetPrescriptionReview((int) $order->prescription_id);
} catch (\Throwable $e) {
self::$error = $e->getMessage();
@@ -2427,6 +2432,9 @@ class PrescriptionOrderLogic
if ($action === 'approve' && !self::assertEsignApproved($order, false, $adminId)) {
return false;
}
if ($action === 'reject') {
self::assertPrescriptionReviewReady($order);
}
if ($action === 'approve') {
$order->prescription_audit_status = 1;
@@ -2673,26 +2681,7 @@ class PrescriptionOrderLogic
}
// 同步撤回处方表的审核状态
$prescriptionId = (int) $order->prescription_id;
if ($prescriptionId > 0) {
try {
$prescription = Prescription::where('id', $prescriptionId)->whereNull('delete_time')->find();
if ($prescription) {
$prescription->audit_status = 0;
$prescription->audit_remark = '';
$prescription->audit_time = null;
$prescription->audit_by = null;
$prescription->audit_by_name = '';
$prescription->save();
}
} catch (\Throwable $e) {
// 记录日志但不影响主流程
Log::error('撤回处方审核时同步处方表失败: ' . $e->getMessage(), [
'prescription_id' => $prescriptionId,
'order_id' => $id
]);
}
}
self::resetPrescriptionReview((int) $order->prescription_id);
self::writeLog((int) $order->id, $adminId, $adminInfo, 'revoke_rx_audit', '撤回处方审核,状态重置为待审核');
@@ -2704,6 +2693,21 @@ class PrescriptionOrderLogic
return $out;
}
private static function resetPrescriptionReview(int $prescriptionId): void
{
if ($prescriptionId > 0) {
Prescription::where('id', $prescriptionId)->whereNull('delete_time')->update([
'audit_status' => 0,
'review_submitted_at' => 0,
'audit_time' => null,
'audit_by' => null,
'audit_by_name' => '',
'audit_remark' => '',
'update_time' => time(),
]);
}
}
/**
* 撤回支付单审核:将已通过/已驳回的支付单审核重置为待审核
*
@@ -2871,10 +2875,16 @@ class PrescriptionOrderLogic
return false;
}
$oldRx = (int) $order->prescription_audit_status;
if ($newRx === 1 && $newRx !== $oldRx && !self::assertEsignApproved($order, false, $adminId)) {
return false;
}
if ($newRx !== $oldRx) {
if ($newRx === 1 && !self::assertEsignApproved($order, false, $adminId)) {
return false;
}
if ($newRx === 2) {
self::assertPrescriptionReviewReady($order);
}
if ($newRx === 0) {
self::resetPrescriptionReview((int) $order->prescription_id);
}
$order->prescription_audit_status = $newRx;
$changeParts[] = '处方审核:' . self::auditStatusLabelForLog($oldRx)
. ' → ' . self::auditStatusLabelForLog($newRx);
@@ -5982,13 +5992,16 @@ class PrescriptionOrderLogic
$changes['辅方袋数'] = [$oldAuxDosageBagCount, $auxDosageBagCount, '袋'];
}
}
$changes['订单服用天数'] = [$oldMedDays, $medDays, '天'];
$parts = [];
foreach ($changes as $label => [$oldValue, $newValue, $unit]) {
if ($oldValue !== $newValue) {
$parts[] = sprintf('%s %d%s → %d%s', $label, $oldValue, $unit, $newValue, $unit);
}
}
$prescriptionChanged = $parts !== [];
if ($oldMedDays !== $medDays) {
$parts[] = sprintf('订单服用天数:%d → %d', $oldMedDays, $medDays);
}
if ($parts === []) {
return true;
}
@@ -6010,6 +6023,9 @@ class PrescriptionOrderLogic
}
$rxUpdates['aux_usage'] = $auxUsage;
}
if ($prescriptionChanged && PrescriptionEsignPolicy::isPendingReview($rx->toArray())) {
$rxUpdates['review_submitted_at'] = 0;
}
$rx->save($rxUpdates);
$order->medication_days = $medDays;
@@ -21,7 +21,7 @@ final class TongueScreenshotLogic extends BaseLogic
throw new \DomainException('截图分析请求包含不支持的字段');
}
$model = array_key_exists('model', $params) ? $params['model'] : 'qwen';
if (!is_string($model) || !in_array($model, ['qwen', 'openai'], true)) {
if (!is_string($model) || !in_array($model, ['qwen', 'openai', 'doubao'], true)) {
throw new \DomainException('截图分析模型无效');
}
$rawId = $params['id'] ?? null;