This commit is contained in:
Your Name
2026-11-20 10:30:04 +08:00
parent 378bf32069
commit 0dc41a1507
96 changed files with 13497 additions and 791 deletions
@@ -10,6 +10,7 @@ use app\common\model\doctor\Medicine as DoctorMedicine;
use app\common\model\tcm\Prescription;
use app\common\model\tcm\PrescriptionOrder;
use app\common\model\tcm\Diagnosis;
use app\common\service\DataScope\DataScopeService;
use app\common\service\wechat\WechatWorkAppMessageService;
use app\common\service\pharmacy\PharmacyHerbIdentityResolver;
use app\common\service\pharmacy\LockedPharmacySnapshotMutation;
@@ -24,6 +25,12 @@ use app\common\service\esign\PrescriptionEsignPolicy;
class PrescriptionLogic
{
public const PRESCRIBE_PERMISSIONS = [
'tcm.prescription/add', 'cf.prescription/add', 'tcm.diagnosis/chufang', 'tcm.diagnosis/kaifang',
];
public const VOID_MANAGE_PERMISSIONS = ['tcm.prescription/void', 'cf.prescription/del'];
public const AUDIT_PERMISSIONS = ['tcm.prescription/audit', 'cf.prescription/audit'];
private static $error = '';
/** @var array{ok: bool, message?: string, errcode?: int}|null 最近一次审核成功后的企微通知结果 */
@@ -140,6 +147,70 @@ class PrescriptionLogic
return false;
}
/** @return array{own: bool, manage: bool, owner_ids: ?array} */
public static function getVoidAccess(int $adminId, array $adminInfo): array
{
if ((int) ($adminInfo['root'] ?? 0) === 1) {
return ['own' => true, 'manage' => true, 'owner_ids' => null];
}
$uris = array_map('strtolower', AuthLogic::getAuthByAdminId($adminId));
$hasVoidGrant = array_intersect(self::VOID_MANAGE_PERMISSIONS, $uris) !== [];
$manage = ($hasVoidGrant || array_intersect(self::AUDIT_PERMISSIONS, $uris) !== [])
&& self::canAuditPrescription($adminId, $adminInfo);
$own = $hasVoidGrant || array_intersect(self::PRESCRIBE_PERMISSIONS, $uris) !== [];
return [
'own' => $own,
'manage' => $manage,
'owner_ids' => ($own || $manage) ? DataScopeService::getVisibleAdminIds($adminId, $adminInfo) : [],
];
}
public static function hasVoidAccess(array $row, int $adminId, array $adminInfo, array $access): bool
{
$creator = (int) ($row['creator_id'] ?? 0);
$assistant = (int) ($row['assistant_id'] ?? 0);
$ownerIds = $access['owner_ids'];
if ($adminId <= 0 || !empty($row['delete_time']) || ($ownerIds !== null
&& !in_array($creator, $ownerIds, true) && !in_array($assistant, $ownerIds, true))) {
return false;
}
return ($access['own'] && $creator === $adminId)
|| ($access['manage'] && self::canViewPrescription($row, $adminId, $adminInfo));
}
public static function canRestoreRejected(array $row): bool
{
return (int) ($row['audit_status'] ?? 0) === 2 && (int) ($row['void_status'] ?? 0) === 1
&& (int) ($row['audit_time'] ?? 0) > 0 && (int) $row['audit_time'] === (int) ($row['void_time'] ?? 0)
&& (int) ($row['audit_by'] ?? 0) > 0 && (int) $row['audit_by'] === (int) ($row['void_by'] ?? 0)
&& (empty($row['esign_required']) || ($row['esign_status'] ?? 'pending') === 'pending');
}
private static function withActionAvailability(array $row, int $viewerAdminId, array $viewerAdminInfo): array
{
$id = (int) $row['id'];
// 与列表和创建校验一致:已取消 / 已退款的订单保留历史关联,但不再占用处方。
$hasBizOrder = PrescriptionOrder::where('prescription_id', $id)
->whereNull('delete_time')
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
->count() > 0;
$row['has_prescription_order'] = $hasBizOrder ? 1 : 0;
$row['can_restore_rejected'] = self::canRestoreRejected($row);
$row['can_void'] = !$hasBizOrder && empty($row['void_status'])
&& self::hasVoidAccess(
$row,
$viewerAdminId,
$viewerAdminInfo,
self::getVoidAccess($viewerAdminId, $viewerAdminInfo)
)
&& PrescriptionOrderLogic::remoteSnapshotLockErrorForPrescription($id) === null;
return $row;
}
private static function generateSn(): string
{
return 'RX' . date('Ymd') . str_pad((string)mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
@@ -463,7 +534,12 @@ class PrescriptionLogic
}
// 已通过且未作废:不可编辑
PrescriptionEsignPolicy::assertMutable($prescription->toArray());
$current = $prescription->toArray();
PrescriptionEsignPolicy::assertMutable($current);
if (!empty($prescription->void_status) && !self::canRestoreRejected($current)) {
self::setError('该处方已作废,请重新开方');
return false;
}
if ((int) ($prescription->audit_status ?? 1) === 1 && (int) ($prescription->void_status ?? 0) === 0) {
self::setError('该处方已通过审核,不可编辑');
return false;
@@ -807,7 +883,6 @@ class PrescriptionLogic
$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'] ?? '');
$bizPo = PrescriptionOrder::where('prescription_id', $id)
->where('prescription_audit_status', 2)
@@ -818,12 +893,8 @@ class PrescriptionLogic
$arr['business_prescription_audit_rejected'] = $bizPo ? 1 : 0;
$arr['business_prescription_audit_remark'] = $bizPo ? (string) ($bizPo->prescription_audit_remark ?? '') : '';
// 与列表和创建校验一致:已取消 / 已退款的订单保留历史关联,但不再占用处方。
$hasBizOrder = PrescriptionOrder::where('prescription_id', $id)
->whereNull('delete_time')
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
->count() > 0;
$arr['has_prescription_order'] = $hasBizOrder ? 1 : 0;
$arr = self::withActionAvailability($arr, $viewerAdminId, $viewerAdminInfo);
$arr['visible_role_ids'] = self::visibleRoleIdsToArray($arr['visible_role_ids'] ?? '');
/**
* 处方笺展示所需的"收件信息/订单号/合计"等字段,
@@ -871,7 +942,7 @@ class PrescriptionLogic
if (!$row || (int) ($row->void_status ?? 0) === 1 || (int) ($row->audit_status ?? 1) !== 0) {
return null;
}
PrescriptionEsignService::assertSignedById($prescriptionId);
PrescriptionEsignService::assertSignedById($prescriptionId, false, $adminId);
$row->audit_status = 1;
$row->audit_time = time();
$row->audit_by = $adminId;
@@ -879,7 +950,7 @@ class PrescriptionLogic
$row->audit_remark = $finalRemark;
return $row->save() ? $row : null;
});
if ($row) {
if ($row && (empty($row->esign_required) || !PrescriptionEsignService::isSandboxTest($prescriptionId))) {
self::$lastAuditWecomNotify = self::notifyCreatorAuditResult($row, 'approve', $finalRemark, $adminInfo);
}
} catch (\Throwable $e) {
@@ -928,7 +999,9 @@ class PrescriptionLogic
if (!$row) {
return false;
}
self::$lastAuditWecomNotify = self::notifyCreatorAuditResult($row, $action, $remark, $adminInfo);
if (empty($row->esign_required) || !PrescriptionEsignService::isSandboxTest($id)) {
self::$lastAuditWecomNotify = self::notifyCreatorAuditResult($row, $action, $remark, $adminInfo);
}
return true;
}
@@ -958,7 +1031,7 @@ class PrescriptionLogic
if ($action === 'approve') {
try {
PrescriptionEsignService::assertSignedById($id);
PrescriptionEsignService::assertSignedById($id, false, $adminId);
} catch (\DomainException $e) {
self::setError($e->getMessage());
return false;
@@ -1135,7 +1208,7 @@ class PrescriptionLogic
$result = $row->toArray();
$result['esign'] = PrescriptionEsignService::describe($result, $viewerAdminId);
return $result;
return self::withActionAvailability($result, $viewerAdminId, $viewerAdminInfo);
}
/**
@@ -1275,13 +1348,21 @@ class PrescriptionLogic
/**
* 作废处方
*/
public static function void(int $id, int $adminId, string $adminName): bool
public static function void(int $id, int $adminId, array $adminInfo): bool
{
self::setError('');
try {
// Check visibility before taking pharmacy locks or reporting order state.
$access = self::getVoidAccess($adminId, $adminInfo);
$row = Prescription::where('id', $id)->whereNull('delete_time')->find();
if (!$row || !self::hasVoidAccess($row->toArray(), $adminId, $adminInfo, $access)) {
self::setError('处方不存在或无权作废');
return false;
}
return (bool) LockedPharmacySnapshotMutation::executeForPrescription(
$id,
static fn (): bool => self::voidLocked($id, $adminId, $adminName)
static fn (): bool => self::voidLocked($id, $adminId, $adminInfo, $access)
);
} catch (\Throwable $exception) {
if (self::getError() === '') {
@@ -1291,11 +1372,11 @@ class PrescriptionLogic
}
}
private static function voidLocked(int $id, int $adminId, string $adminName): bool
private static function voidLocked(int $id, int $adminId, array $adminInfo, array $access): bool
{
$row = Prescription::where('id', $id)->lock(true)->find();
if (!$row) {
self::setError('处方不存在');
$row = Prescription::where('id', $id)->whereNull('delete_time')->lock(true)->find();
if (!$row || !self::hasVoidAccess($row->toArray(), $adminId, $adminInfo, $access)) {
self::setError('处方不存在或无权作废');
return false;
}
if ((int)($row->void_status ?? 0) === 1) {
@@ -1315,7 +1396,7 @@ class PrescriptionLogic
$row->void_status = 1;
$row->void_time = time();
$row->void_by = $adminId;
$row->void_by_name = $adminName;
$row->void_by_name = (string) ($adminInfo['name'] ?? '');
$saved = (bool) $row->save();
if ($saved) {
PrescriptionAiStore::invalidate($id, 'voided');
@@ -60,10 +60,10 @@ class PrescriptionOrderLogic
return self::$error;
}
private static function assertEsignApproved(PrescriptionOrder $order, bool $forDispensing = false): bool
private static function assertEsignApproved(PrescriptionOrder $order, bool $forDispensing = false, ?int $reviewer = null): bool
{
try {
PrescriptionEsignService::assertSignedById((int) $order->prescription_id, $forDispensing);
PrescriptionEsignService::assertSignedById((int) $order->prescription_id, $forDispensing, $reviewer);
return true;
} catch (\DomainException $e) {
self::$error = $e->getMessage();
@@ -2397,7 +2397,7 @@ class PrescriptionOrderLogic
return false;
}
if (!self::canAuditPrescriptionOrder($adminInfo)) {
if (!self::canAuditPrescriptionOrder($adminInfo) || !self::canAccessOrder($order, $adminId, $adminInfo)) {
self::$error = '无处方审核权限';
return false;
@@ -2424,7 +2424,7 @@ class PrescriptionOrderLogic
return false;
}
if ($action === 'approve' && !self::assertEsignApproved($order)) {
if ($action === 'approve' && !self::assertEsignApproved($order, false, $adminId)) {
return false;
}
@@ -2870,10 +2870,10 @@ class PrescriptionOrderLogic
return false;
}
if ($newRx === 1 && !self::assertEsignApproved($order)) {
$oldRx = (int) $order->prescription_audit_status;
if ($newRx === 1 && $newRx !== $oldRx && !self::assertEsignApproved($order, false, $adminId)) {
return false;
}
$oldRx = (int) $order->prescription_audit_status;
if ($newRx !== $oldRx) {
$order->prescription_audit_status = $newRx;
$changeParts[] = '处方审核:' . self::auditStatusLabelForLog($oldRx)