This commit is contained in:
Your Name
2026-11-17 17:06:05 +08:00
parent 08892f4270
commit 378bf32069
51 changed files with 4656 additions and 103 deletions
@@ -19,6 +19,8 @@ use think\facade\Log;
use think\facade\Db;
use app\common\service\prescriptionai\PrescriptionAiStore;
use app\common\service\prescriptionai\PrescriptionAiRequest;
use app\common\service\esign\PrescriptionEsignService;
use app\common\service\esign\PrescriptionEsignPolicy;
class PrescriptionLogic
{
@@ -394,6 +396,10 @@ class PrescriptionLogic
'assistant_id' => $assistantIdForRx,
];
if (PrescriptionEsignService::enabled()) {
$data['esign_required'] = 1;
$data['esign_status'] = 'pending';
}
try {
return Db::transaction(static function () use ($data, $params, $adminId, $adminInfo, $diagnosisIdRule, $dateYmd): int {
$replayed = PrescriptionAiRequest::replay($params, $adminId, true);
@@ -457,6 +463,7 @@ class PrescriptionLogic
}
// 已通过且未作废:不可编辑
PrescriptionEsignPolicy::assertMutable($prescription->toArray());
if ((int) ($prescription->audit_status ?? 1) === 1 && (int) ($prescription->void_status ?? 0) === 0) {
self::setError('该处方已通过审核,不可编辑');
return false;
@@ -555,7 +562,7 @@ class PrescriptionLogic
'usage_way' => $params['usage_way'] ?? $prescription->usage_way,
'dietary_taboo' => is_array($params['dietary_taboo'] ?? null) ? implode(',', $params['dietary_taboo']) : ($params['dietary_taboo'] ?? $prescription->dietary_taboo),
'usage_notes' => $params['usage_notes'] ?? $prescription->usage_notes,
'doctor_name' => $params['doctor_name'] ?? $prescription->doctor_name,
'doctor_name' => (string) $prescription->doctor_name,
'doctor_signature' => $params['doctor_signature'] ?? $prescription->doctor_signature,
'is_shared' => (int)($params['is_shared'] ?? $prescription->is_shared),
'visible_role_ids' => array_key_exists('visible_role_ids', $params)
@@ -571,6 +578,10 @@ class PrescriptionLogic
'audit_remark' => '',
];
if (PrescriptionEsignService::enabled()) {
$data['esign_required'] = 1;
$data['esign_status'] = 'pending';
}
if ($wasVoid) {
$data['void_status'] = 0;
$data['void_time'] = null;
@@ -631,7 +642,7 @@ class PrescriptionLogic
private static function patchPatientContactLocked(int $rxId, string $patientName, string $phone, int $gender, int $adminId, array $adminInfo): bool
{
self::$error = '';
$prescription = Prescription::find($rxId);
$prescription = Prescription::where('id', $rxId)->lock(true)->find();
if (!$prescription) {
self::setError('处方不存在');
@@ -648,6 +659,7 @@ class PrescriptionLogic
return false;
}
PrescriptionEsignPolicy::assertMutable($prescription->toArray());
$patientName = trim($patientName);
$phone = trim($phone);
if ($patientName === '') {
@@ -767,6 +779,7 @@ class PrescriptionLogic
return false;
}
PrescriptionEsignPolicy::assertMutable($prescription->toArray());
$prescription->delete();
PrescriptionAiStore::invalidate($id, 'deleted');
return true;
@@ -792,6 +805,7 @@ class PrescriptionLogic
return null;
}
$arr = $row->toArray();
$arr['esign'] = PrescriptionEsignService::describe($arr, $viewerAdminId);
$arr['gender_desc'] = $arr['gender'] == 1 ? '男' : '女';
$arr['visible_role_ids'] = self::visibleRoleIdsToArray($arr['visible_role_ids'] ?? '');
@@ -848,31 +862,24 @@ class PrescriptionLogic
if ($prescriptionId <= 0) {
return;
}
$row = Prescription::find($prescriptionId);
if (!$row) {
return;
}
if ((int) ($row->void_status ?? 0) === 1) {
return;
}
if ((int) ($row->audit_status ?? 1) !== 0) {
return;
}
self::$lastAuditWecomNotify = null;
$name = (string) ($adminInfo['name'] ?? '');
$now = time();
$finalRemark = trim($remark) !== '' ? trim($remark) : '业务订单处方审核通过';
$finalRemark = mb_substr($finalRemark, 0, 500);
$row->audit_status = 1;
$row->audit_time = $now;
$row->audit_by = $adminId;
$row->audit_by_name = $name;
$row->audit_remark = $finalRemark;
try {
if ($row->save()) {
$row = Db::transaction(static function () use ($prescriptionId, $adminId, $adminInfo, $finalRemark): ?Prescription {
$row = Prescription::where('id', $prescriptionId)->lock(true)->find();
if (!$row || (int) ($row->void_status ?? 0) === 1 || (int) ($row->audit_status ?? 1) !== 0) {
return null;
}
PrescriptionEsignService::assertSignedById($prescriptionId);
$row->audit_status = 1;
$row->audit_time = time();
$row->audit_by = $adminId;
$row->audit_by_name = (string) ($adminInfo['name'] ?? '');
$row->audit_remark = $finalRemark;
return $row->save() ? $row : null;
});
if ($row) {
self::$lastAuditWecomNotify = self::notifyCreatorAuditResult($row, 'approve', $finalRemark, $adminInfo);
}
} catch (\Throwable $e) {
@@ -905,7 +912,29 @@ class PrescriptionLogic
return false;
}
$row = Prescription::find($id);
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);
} catch (\Throwable $exception) {
if (self::getError() === '') {
self::setError($exception->getMessage());
}
return false;
}
if (!$row) {
return false;
}
self::$lastAuditWecomNotify = self::notifyCreatorAuditResult($row, $action, $remark, $adminInfo);
return true;
}
private static function auditLocked(int $id, string $action, string $remark, int $adminId, array $adminInfo): Prescription|false
{
$row = Prescription::where('id', $id)->lock(true)->find();
if (!$row) {
self::setError('处方不存在');
@@ -918,7 +947,7 @@ class PrescriptionLogic
return false;
}
if ((int) ($row->audit_status ?? 1) !== 0) {
if ((int) ($row->audit_status ?? 1) !== 0 || !empty($row->void_status)) {
self::setError('当前状态不可审核');
return false;
@@ -928,18 +957,19 @@ class PrescriptionLogic
$now = time();
if ($action === 'approve') {
try {
PrescriptionEsignService::assertSignedById($id);
} catch (\DomainException $e) {
self::setError($e->getMessage());
return false;
}
$row->audit_status = 1;
$row->audit_time = $now;
$row->audit_by = $adminId;
$row->audit_by_name = $name;
$row->audit_remark = $remark;
$ok = (bool) $row->save();
if ($ok) {
self::$lastAuditWecomNotify = self::notifyCreatorAuditResult($row, 'approve', $remark, $adminInfo);
}
return $ok;
return $row->save() ? $row : false;
}
if ($action === 'reject') {
@@ -958,19 +988,11 @@ class PrescriptionLogic
$row->void_by = $adminId;
$row->void_by_name = $name;
$ok = Db::transaction(static function () use ($row, $id): bool {
Prescription::where('id', $id)->lock(true)->find();
$saved = (bool) $row->save();
if ($saved) {
PrescriptionAiStore::invalidate($id, 'voided');
}
return $saved;
});
$ok = (bool) $row->save();
if ($ok) {
self::$lastAuditWecomNotify = self::notifyCreatorAuditResult($row, 'reject', $remark, $adminInfo);
PrescriptionAiStore::invalidate($id, 'voided');
}
return $ok;
return $ok ? $row : false;
}
self::setError('无效的审核操作');
@@ -1067,10 +1089,14 @@ class PrescriptionLogic
*/
private static function filterViewablePrescriptions(array $rows, int $viewerAdminId, array $viewerAdminInfo): array
{
return array_values(array_filter(
$visible = array_values(array_filter(
$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);
}
/**
@@ -1107,7 +1133,9 @@ class PrescriptionLogic
return null;
}
return $row->toArray();
$result = $row->toArray();
$result['esign'] = PrescriptionEsignService::describe($result, $viewerAdminId);
return $result;
}
/**
@@ -1234,6 +1262,10 @@ class PrescriptionLogic
'assistant_id' => $assistantIdForRx,
];
if (PrescriptionEsignService::enabled()) {
$data['esign_required'] = 1;
$data['esign_status'] = 'pending';
}
$prescription = new Prescription();
$prescription->save($data);
@@ -38,6 +38,8 @@ use app\common\service\pharmacy\PharmacySubmissionClaimWorkflow;
use app\common\service\pharmacy\PharmacySupplyMode;
use app\common\service\tcm\RevisitDueHook;
use think\db\Query;
use app\common\service\esign\PrescriptionEsignService;
use app\common\service\esign\PrescriptionEsignPolicy;
use think\facade\Config;
use think\facade\Db;
use think\facade\Log;
@@ -58,6 +60,17 @@ class PrescriptionOrderLogic
return self::$error;
}
private static function assertEsignApproved(PrescriptionOrder $order, bool $forDispensing = false): bool
{
try {
PrescriptionEsignService::assertSignedById((int) $order->prescription_id, $forDispensing);
return true;
} catch (\DomainException $e) {
self::$error = $e->getMessage();
return false;
}
}
private static function assertTrackingAuditApproved(PrescriptionOrder $order): bool
{
if ((int) $order->prescription_audit_status !== 1 || (int) $order->payment_slip_audit_status !== 1) {
@@ -66,7 +79,7 @@ class PrescriptionOrderLogic
return false;
}
return true;
return self::assertEsignApproved($order, true);
}
private static function assertRemoteSnapshotMutable(PrescriptionOrder $order): bool
@@ -1429,12 +1442,13 @@ class PrescriptionOrderLogic
return false;
}
$rx = Prescription::where('id', $rxId)->whereNull('delete_time')->find();
$rx = Prescription::where('id', $rxId)->whereNull('delete_time')->lock(true)->find();
if (!$rx) {
self::setError('处方不存在');
return false;
}
PrescriptionEsignPolicy::assertMutable($rx->toArray());
if (!PrescriptionLogic::canViewPrescription($rx, $adminId, $adminInfo)) {
self::setError('无权限修改此处方');
@@ -2352,7 +2366,32 @@ class PrescriptionOrderLogic
public static function auditPrescription(int $id, string $action, string $remark, int $adminId, array $adminInfo)
{
self::$error = '';
$order = PrescriptionOrder::where('id', $id)->whereNull('delete_time')->find();
try {
$out = Db::transaction(static function () use ($id, $action, $remark, $adminId, $adminInfo) {
$result = self::auditPrescriptionLocked($id, $action, $remark, $adminId, $adminInfo);
if ($result === false) {
throw new \DomainException(self::$error ?: '审核未完成');
}
return $result;
});
} catch (\Throwable $exception) {
self::$error = $exception->getMessage();
return false;
}
if ($out !== false && $action === 'approve') {
// Notify only after the audit transaction releases its locks.
PrescriptionLogic::syncApproveConsumerPrescriptionIfPending(
(int) $out['prescription_id'], $adminId, $adminInfo,
trim($remark) !== '' ? trim($remark) : '业务订单处方审核通过'
);
}
return $out;
}
private static function auditPrescriptionLocked(int $id, string $action, string $remark, int $adminId, array $adminInfo)
{
self::$error = '';
$order = PrescriptionOrder::where('id', $id)->whereNull('delete_time')->lock(true)->find();
if (!$order) {
self::$error = '订单不存在';
@@ -2375,9 +2414,17 @@ class PrescriptionOrderLogic
return false;
}
if (!in_array($action, ['approve', 'reject'], true)) {
self::$error = '无效的审核操作';
return false;
}
if ($action === 'reject' && trim($remark) === '') {
self::$error = '驳回时请填写审核意见';
return false;
}
if ($action === 'approve' && !self::assertEsignApproved($order)) {
return false;
}
@@ -2403,16 +2450,6 @@ class PrescriptionOrderLogic
return false;
}
if ($action === 'approve') {
$syncRemark = trim($remark) !== '' ? trim($remark) : '业务订单处方审核通过';
PrescriptionLogic::syncApproveConsumerPrescriptionIfPending(
(int) $order->prescription_id,
$adminId,
$adminInfo,
$syncRemark
);
}
$actionName = $action === 'approve' ? 'audit_rx_approve' : 'audit_rx_reject';
$summary = $action === 'approve' ? '处方审核通过' . ($remark ? ',意见:' . $remark : '') : '处方审核驳回,意见:' . $remark;
self::writeLog((int) $order->id, $adminId, $adminInfo, $actionName, $summary);
@@ -2776,6 +2813,23 @@ class PrescriptionOrderLogic
public static function addLog(array $params, int $adminId, array $adminInfo)
{
self::$error = '';
try {
return Db::transaction(static function () use ($params, $adminId, $adminInfo) {
$result = self::addLogLocked($params, $adminId, $adminInfo);
if ($result === false) {
throw new \DomainException(self::$error ?: '日志变更未完成');
}
return $result;
});
} catch (\Throwable $exception) {
self::$error = $exception->getMessage();
return false;
}
}
private static function addLogLocked(array $params, int $adminId, array $adminInfo)
{
self::$error = '';
$id = (int) ($params['id'] ?? 0);
$summary = mb_substr(trim((string) ($params['summary'] ?? '')), 0, 500);
if ($summary === '') {
@@ -2784,7 +2838,7 @@ class PrescriptionOrderLogic
return false;
}
$order = PrescriptionOrder::where('id', $id)->whereNull('delete_time')->find();
$order = PrescriptionOrder::where('id', $id)->whereNull('delete_time')->lock(true)->find();
if (!$order) {
self::$error = '订单不存在';
@@ -2814,6 +2868,9 @@ class PrescriptionOrderLogic
if (!in_array($newRx, [0, 1, 2], true)) {
self::$error = '处方审核状态无效';
return false;
}
if ($newRx === 1 && !self::assertEsignApproved($order)) {
return false;
}
$oldRx = (int) $order->prescription_audit_status;
@@ -4947,6 +5004,9 @@ class PrescriptionOrderLogic
self::$error = '无权限操作';
return false;
}
if (!self::assertEsignApproved($order, true)) {
return false;
}
$target = self::normalizeShipMode((string) ($order->ship_mode ?? 'gancao')) === 'direct'
? 'direct'
: 'gancao';
@@ -5083,6 +5143,9 @@ class PrescriptionOrderLogic
/** @return array<string,mixed>|false */
private static function uploadEjRemote(PrescriptionOrder $order, int $adminId, array $adminInfo, array $claim)
{
if (!self::assertEsignApproved($order, true)) {
return false;
}
try {
$prescription = PrescriptionLogic::detail((int) $order->prescription_id, $adminId, $adminInfo);
if ($prescription === null) {
@@ -5203,6 +5266,9 @@ class PrescriptionOrderLogic
if (!self::canAccessOrder($order, $adminId, $adminInfo)) {
self::$error = '无权限操作';
return false;
}
if (!self::assertEsignApproved($order, true)) {
return false;
}
$item = $order->toArray();
@@ -5817,6 +5883,7 @@ class PrescriptionOrderLogic
return false;
}
PrescriptionEsignPolicy::assertMutable($rx->toArray());
if (!PrescriptionLogic::canViewPrescription($rx, $adminId, $adminInfo)) {
self::setError('无权限修改此处方');