功能性

This commit is contained in:
Your Name
2026-11-27 17:25:53 +08:00
parent 954e6f00d0
commit 82a91858f9
7 changed files with 217 additions and 29 deletions
@@ -1053,12 +1053,12 @@ class PrescriptionLogic
}
try {
// Keep the signature check and audit write under the same row lock.
// Reject also shares pharmacy claim locks with void/edit operations.
$mutate = static fn () => self::auditLocked($id, $action, $remark, $adminId, $adminInfo);
$row = $action === 'reject'
? LockedPharmacySnapshotMutation::executeForPrescription($id, $mutate)
: Db::transaction($mutate);
$row = match ($action) {
'approve' => PrescriptionOrderLogic::approvePrescriptionAndLinkedOrders($id, $mutate),
'reject' => LockedPharmacySnapshotMutation::executeForPrescription($id, $mutate),
default => Db::transaction($mutate),
};
} catch (\Throwable $exception) {
if (self::getError() === '') {
self::setError($exception->getMessage());
@@ -40,6 +40,7 @@ use app\common\service\tcm\RevisitDueHook;
use think\db\Query;
use app\common\service\esign\PrescriptionEsignService;
use app\common\service\esign\PrescriptionEsignPolicy;
use app\common\service\esign\PrescriptionReviewEsignService;
use think\facade\Config;
use think\facade\Db;
use think\facade\Log;
@@ -774,6 +775,87 @@ class PrescriptionOrderLogic
}
}
/** @param callable(): (Prescription|false) $approve */
public static function approvePrescriptionAndLinkedOrders(int $prescriptionId, callable $approve): Prescription|false
{
return Db::transaction(static function () use ($prescriptionId, $approve): Prescription|false {
// Follow the order-before-prescription lock order used by order review and revocation.
$orders = PrescriptionOrder::where('prescription_id', $prescriptionId)
->whereNull('delete_time')
->where('prescription_audit_status', 0)
->whereIn('fulfillment_status', [1, 2])
->order('id', 'asc')
->lock(true)
->select();
$rx = $approve();
if ($rx) {
foreach ($orders as $order) {
self::recordPrescriptionApproval($order, $rx->toArray());
}
}
return $rx;
});
}
/** Reconcile a pending order using the existing approval and its archived signatures. */
public static function syncRecordedPrescriptionApproval(int $orderId, bool $apply = false): bool
{
return Db::transaction(static function () use ($orderId, $apply): bool {
$order = PrescriptionOrder::where('id', $orderId)->whereNull('delete_time')->lock(true)->find();
if (!$order || (int) $order->prescription_audit_status !== 0
|| !in_array((int) $order->fulfillment_status, [1, 2], true)) {
return false;
}
$rx = Prescription::where('id', (int) $order->prescription_id)->lock(true)->find();
if (!$rx || !self::hasRecordedPrescriptionApproval($rx->toArray())) {
return false;
}
if ($apply) {
self::recordPrescriptionApproval($order, $rx->toArray());
}
return true;
});
}
private static function hasRecordedPrescriptionApproval(array $rx): bool
{
if ((int) ($rx['audit_status'] ?? 0) !== 1 || !empty($rx['void_status']) || !empty($rx['delete_time'])
|| (int) ($rx['audit_by'] ?? 0) <= 0 || (int) ($rx['audit_time'] ?? 0) <= 0
|| !PrescriptionEsignPolicy::hasPrescriptionContent($rx)) {
return false;
}
$id = (int) $rx['id'];
try {
PrescriptionEsignService::assertSignedById($id);
if (!empty($rx['esign_required'])) {
$doctor = Db::name('prescription_esign')->where('prescription_id', $id)->find();
if (!$doctor) {
return false;
}
PrescriptionReviewEsignService::assertVerified($rx, $doctor, null);
}
} catch (\DomainException) {
return false;
}
return true;
}
private static function recordPrescriptionApproval(PrescriptionOrder $order, array $rx): void
{
$remark = mb_substr(trim((string) $rx['audit_remark']), 0, 500);
$order->prescription_audit_status = 1;
$order->prescription_audit_remark = $remark;
self::syncFulfillmentStatus($order);
if (!$order->save()) {
throw new \RuntimeException('业务订单处方审核状态同步失败');
}
self::writeLog(
(int) $order->id, (int) $rx['audit_by'], ['name' => (string) $rx['audit_by_name']],
'audit_rx_approve', '同步已开处方审核通过' . ($remark !== '' ? ',意见:' . $remark : ''),
strict: true, occurredAt: (int) $rx['audit_time']
);
}
private static function syncFulfillmentStatus(PrescriptionOrder $order): void
{
$fs = (int) $order->fulfillment_status;
@@ -1046,7 +1128,7 @@ class PrescriptionOrderLogic
private static function createLocked(array $params, int $adminId, array $adminInfo)
{
self::$error = '';
// 新建业务订单的两项审核均为待审核,不能在创建时预填单号。
// 新建时支付单仍须审核,不能预填单号。
if (trim((string) ($params['tracking_number'] ?? '')) !== '') {
self::$error = self::TRACKING_AUDIT_REQUIRED;
@@ -1176,6 +1258,9 @@ class PrescriptionOrderLogic
}
self::writeLog((int) $order->id, $adminId, $adminInfo, 'create', '创建业务订单', true);
if (self::hasRecordedPrescriptionApproval($rx)) {
self::recordPrescriptionApproval($order, $rx);
}
if ($payOrderIds !== []) {
self::replacePayOrderLinks((int) $order->id, $payOrderIds);
@@ -5807,7 +5892,8 @@ class PrescriptionOrderLogic
array $adminInfo,
string $action,
string $summary,
bool $strict = false
bool $strict = false,
?int $occurredAt = null
): void
{
$adminName = $adminInfo['name'] ?? '';
@@ -5821,10 +5907,12 @@ class PrescriptionOrderLogic
$log->admin_name = mb_substr($adminName, 0, 64);
$log->action = mb_substr($action, 0, 32);
$log->summary = mb_substr($summary, 0, 500);
$log->create_time = time();
$log->create_time = $occurredAt ?? time();
try {
$log->save();
if (!$log->save() && $strict) {
throw new \RuntimeException('操作日志写入失败,变更未保存');
}
} catch (\Throwable $e) {
if ($strict) {
throw new \RuntimeException('操作日志写入失败,变更未保存', 0, $e);