更新
This commit is contained in:
@@ -7,7 +7,9 @@ namespace app\adminapi\controller\tcm;
|
||||
use app\adminapi\controller\BaseAdminController;
|
||||
use app\adminapi\logic\tcm\PrescriptionLogic;
|
||||
use app\adminapi\validate\tcm\PrescriptionValidate;
|
||||
use app\common\service\esign\DoctorEsignIdentityService;
|
||||
use app\common\service\esign\PrescriptionEsignService;
|
||||
use app\common\service\esign\PrescriptionReviewEsignService;
|
||||
|
||||
/**
|
||||
* 中医处方单控制器
|
||||
@@ -171,12 +173,14 @@ class PrescriptionController extends BaseAdminController
|
||||
*/
|
||||
public function void()
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
return $this->fail('请使用 POST 作废处方');
|
||||
}
|
||||
$id = (int)($this->request->post('id') ?? 0);
|
||||
if (!$id) {
|
||||
if ($id <= 0) {
|
||||
return $this->fail('处方ID不能为空');
|
||||
}
|
||||
$admin = $this->adminInfo;
|
||||
$ok = PrescriptionLogic::void($id, (int)$this->adminId, $admin['name'] ?? '');
|
||||
$ok = PrescriptionLogic::void($id, $this->adminId, $this->adminInfo);
|
||||
if (!$ok) {
|
||||
return $this->fail(PrescriptionLogic::getError());
|
||||
}
|
||||
@@ -193,6 +197,11 @@ class PrescriptionController extends BaseAdminController
|
||||
return $this->esignOperation('status', false);
|
||||
}
|
||||
|
||||
public function esignIdentityStatus()
|
||||
{
|
||||
return $this->esignOperation('identityStatus', false);
|
||||
}
|
||||
|
||||
public function esignStart()
|
||||
{
|
||||
return $this->esignOperation('start', true);
|
||||
@@ -203,11 +212,102 @@ class PrescriptionController extends BaseAdminController
|
||||
return $this->esignOperation('complete', true);
|
||||
}
|
||||
|
||||
/** @notes 验证处方签署短信 */
|
||||
public function esignVerifyWillingness()
|
||||
{
|
||||
return $this->esignOperation('verifyWillingness', true);
|
||||
}
|
||||
|
||||
public function esignDownload()
|
||||
{
|
||||
return $this->esignOperation('download', false);
|
||||
}
|
||||
|
||||
public function esignReviewStatus()
|
||||
{
|
||||
return $this->esignReviewOperation(false);
|
||||
}
|
||||
|
||||
public function esignReviewConfirm()
|
||||
{
|
||||
return $this->esignReviewOperation(true);
|
||||
}
|
||||
|
||||
private function esignReviewOperation(bool $confirm)
|
||||
{
|
||||
if ($confirm ? !$this->request->isPost() : !$this->request->isGet()) {
|
||||
return $this->fail($confirm ? '请使用 POST 请求' : '请使用 GET 请求');
|
||||
}
|
||||
$params = $confirm ? $this->request->post() : $this->request->get();
|
||||
$id = (int) ($params['id'] ?? 0);
|
||||
$orderId = (int) ($params['order_id'] ?? 0);
|
||||
$actor = (int) $this->adminId;
|
||||
$verifyOnly = $params['verify_only'] ?? false;
|
||||
if ($id <= 0 || $orderId < 0 || !is_bool($verifyOnly)) {
|
||||
return $this->fail('审核签署参数无效');
|
||||
}
|
||||
if ($confirm && (int) ($params['actor_id'] ?? 0) !== $actor) {
|
||||
return $this->fail('登录账号已切换,请刷新并由当前审核人确认');
|
||||
}
|
||||
try {
|
||||
if ($confirm) {
|
||||
ignore_user_abort(true);
|
||||
set_time_limit(360);
|
||||
}
|
||||
$data = $confirm
|
||||
? PrescriptionReviewEsignService::confirm($id, $orderId, $actor, $this->adminInfo, $verifyOnly)
|
||||
: PrescriptionReviewEsignService::status($id, $orderId, $actor, $this->adminInfo);
|
||||
return $this->data($data)->header(['Cache-Control' => 'private, no-store']);
|
||||
} catch (\app\common\service\esign\DoctorEsignAuthorizationRequired) {
|
||||
return $this->fail('审核人快捷签授权需要开通或续期,请在「审核签署身份」中处理');
|
||||
} catch (\Throwable $error) {
|
||||
return $this->fail($error instanceof \DomainException || $error instanceof \app\common\service\esign\EsignProviderException
|
||||
? $error->getMessage() : '审核签署暂不可用,请刷新状态后重试');
|
||||
}
|
||||
}
|
||||
|
||||
/** @notes 查询医师电子签署认证 */
|
||||
public function esignDoctorStatus()
|
||||
{
|
||||
return $this->esignDoctorOperation('status', false);
|
||||
}
|
||||
|
||||
/** @notes 发起医师电子签署认证 */
|
||||
public function esignDoctorStart()
|
||||
{
|
||||
return $this->esignDoctorOperation('start', true, 'mobile');
|
||||
}
|
||||
|
||||
/** @notes 完成医师电子签署认证 */
|
||||
public function esignDoctorComplete()
|
||||
{
|
||||
return $this->esignDoctorOperation('complete', true, 'identity_number');
|
||||
}
|
||||
|
||||
private function esignDoctorOperation(string $operation, bool $post, string $field = '')
|
||||
{
|
||||
if ($post ? !$this->request->isPost() : !$this->request->isGet()) {
|
||||
return $this->fail($post ? '请使用 POST 请求' : '请使用 GET 请求');
|
||||
}
|
||||
try {
|
||||
if (!$post) {
|
||||
return $this->data(DoctorEsignIdentityService::status((int) $this->adminId))
|
||||
->header(['Cache-Control' => 'private, no-store']);
|
||||
}
|
||||
$input = $this->request->post($field, '');
|
||||
if (!is_string($input)) {
|
||||
return $this->fail($field === 'mobile' ? '手机号格式无效' : '身份证号码格式无效');
|
||||
}
|
||||
if ($operation === 'complete') {
|
||||
ignore_user_abort(true);
|
||||
}
|
||||
return $this->data(DoctorEsignIdentityService::{$operation}((int) $this->adminId, $input))
|
||||
->header(['Cache-Control' => 'private, no-store']);
|
||||
} catch (\Throwable $error) {
|
||||
return $this->fail($error instanceof \DomainException ? $error->getMessage() : '医师认证暂不可用,请稍后重试');
|
||||
}
|
||||
}
|
||||
|
||||
private function esignOperation(string $operation, bool $post)
|
||||
{
|
||||
if ($post && !$this->request->isPost()) {
|
||||
@@ -223,6 +323,35 @@ class PrescriptionController extends BaseAdminController
|
||||
set_time_limit(300);
|
||||
}
|
||||
try {
|
||||
$body = $post ? $this->request->post(false) : [];
|
||||
if ($operation === 'verifyWillingness') {
|
||||
$code = $this->request->post('auth_code', '');
|
||||
if (!is_string($code)) {
|
||||
return $this->fail('短信验证码格式无效');
|
||||
}
|
||||
return $this->data(PrescriptionEsignService::verifyWillingness($id, (int) $this->adminId,
|
||||
$this->adminInfo, $code));
|
||||
}
|
||||
if ($operation === 'start') {
|
||||
$restartIdentity = array_key_exists('restart_identity', $body) ? $body['restart_identity'] : false;
|
||||
if (!is_bool($restartIdentity)) {
|
||||
return $this->fail('重新核身标记格式无效');
|
||||
}
|
||||
return $this->data(PrescriptionEsignService::start($id, (int) $this->adminId,
|
||||
$this->adminInfo, $restartIdentity));
|
||||
}
|
||||
if ($operation === 'complete') {
|
||||
$verifyOnly = array_key_exists('verify_only', $body) ? $body['verify_only'] : false;
|
||||
if (!is_bool($verifyOnly)) {
|
||||
return $this->fail('验签标记格式无效');
|
||||
}
|
||||
$identityNumber = $this->request->post('identity_number', '');
|
||||
if (!is_string($identityNumber)) {
|
||||
return $this->fail('身份证号码格式无效');
|
||||
}
|
||||
return $this->data(PrescriptionEsignService::complete($id, (int) $this->adminId,
|
||||
$this->adminInfo, $identityNumber, $verifyOnly));
|
||||
}
|
||||
return $this->data(PrescriptionEsignService::{$operation}($id, (int) $this->adminId, $this->adminInfo));
|
||||
} catch (\Throwable $e) {
|
||||
return $this->fail($e instanceof \DomainException ? $e->getMessage() : '电子签署暂不可用,请联系管理员');
|
||||
|
||||
@@ -17,6 +17,7 @@ declare (strict_types=1);
|
||||
namespace app\adminapi\http\middleware;
|
||||
|
||||
use app\adminapi\logic\LoginLogic;
|
||||
use app\adminapi\logic\tcm\PrescriptionLogic;
|
||||
use app\common\service\pharmacy\PharmacyUploadPermissionAlias;
|
||||
use app\common\service\qywx\QywxPromotionOperatorAccess;
|
||||
use app\common\{
|
||||
@@ -94,9 +95,18 @@ class AuthMiddleware
|
||||
$reads = array_merge($writes, ['tcm.prescription/detail', 'tcm.prescription/lists', 'cf.prescription/lists',
|
||||
'cf.prescription/detail', 'cf.prescription/audit', 'tcm.prescription/audit',
|
||||
'tcm.prescriptionorder/lists', 'tcm.prescriptionorder/detail', 'tcm.prescriptionorder/auditprescription']);
|
||||
$grants = in_array($action, ['esignstart', 'esigncomplete'], true) ? $writes : $reads;
|
||||
return in_array($action, ['esignstart', 'esigncomplete', 'esignstatus', 'esigndownload'], true)
|
||||
&& array_intersect($grants, $uris) !== []
|
||||
$audit = ['tcm.prescription/audit', 'cf.prescription/audit', 'tcm.prescriptionorder/auditprescription'];
|
||||
$writeActions = ['esignstart', 'esigncomplete', 'esignidentitystatus', 'esignverifywillingness'];
|
||||
$identityActions = ['esigndoctorstatus', 'esigndoctorstart', 'esigndoctorcomplete'];
|
||||
$grants = match (true) {
|
||||
in_array($action, $writeActions, true) => $writes,
|
||||
in_array($action, $identityActions, true) => array_merge($writes, $audit),
|
||||
$action === 'esignreviewconfirm' => $audit,
|
||||
default => $reads,
|
||||
};
|
||||
return in_array($action, array_merge($writeActions, $identityActions,
|
||||
['esignstatus', 'esigndownload', 'esignreviewstatus', 'esignreviewconfirm']), true)
|
||||
&& array_intersect($grants, $uris) !== []
|
||||
? $next($request) : JsonService::fail('权限不足,无法访问电子签署');
|
||||
}
|
||||
|
||||
@@ -264,17 +274,18 @@ class AuthMiddleware
|
||||
}
|
||||
|
||||
$prescriptionWriteAliases = [
|
||||
'tcm.prescription/add' => [
|
||||
'tcm.prescription/add', 'cf.prescription/add',
|
||||
'tcm.diagnosis/chufang', 'tcm.diagnosis/kaifang',
|
||||
],
|
||||
'tcm.prescription/add' => PrescriptionLogic::PRESCRIBE_PERMISSIONS,
|
||||
'tcm.prescription/edit' => [
|
||||
'tcm.prescription/edit', 'cf.prescription/edit',
|
||||
'tcm.diagnosis/chufang', 'tcm.diagnosis/kaifang',
|
||||
],
|
||||
'tcm.prescription/delete' => ['tcm.prescription/delete', 'cf.prescription/del'],
|
||||
'tcm.prescription/void' => ['tcm.prescription/void', 'cf.prescription/del', 'cf.prescription/audit'],
|
||||
'tcm.prescription/audit' => ['tcm.prescription/audit', 'cf.prescription/audit'],
|
||||
'tcm.prescription/void' => array_merge(
|
||||
PrescriptionLogic::PRESCRIBE_PERMISSIONS,
|
||||
PrescriptionLogic::VOID_MANAGE_PERMISSIONS,
|
||||
PrescriptionLogic::AUDIT_PERMISSIONS
|
||||
),
|
||||
'tcm.prescription/audit' => PrescriptionLogic::AUDIT_PERMISSIONS,
|
||||
];
|
||||
if (isset($prescriptionWriteAliases[$accessUri])
|
||||
&& count(array_intersect($prescriptionWriteAliases[$accessUri], $adminUris)) > 0) {
|
||||
|
||||
@@ -20,10 +20,11 @@ class OperationLog
|
||||
public function handle($response)
|
||||
{
|
||||
$request = request();
|
||||
$accessUri = strtolower($request->controller() . '/' . $request->action());
|
||||
|
||||
// Temporary visual observations and rejected upload parameters must never
|
||||
// enter the generic request/response audit log if this listener is enabled.
|
||||
if (strtolower($request->controller() . '/' . $request->action()) === 'tcm.diagnosis/aitonguescreenshot') {
|
||||
if ($accessUri === 'tcm.diagnosis/aitonguescreenshot') {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -53,6 +54,16 @@ class OperationLog
|
||||
|
||||
$params = $request->param();
|
||||
|
||||
// Signing requests can carry identity inputs, OTPs and private provider URLs.
|
||||
$esign = str_starts_with($accessUri, 'tcm.prescription/esign');
|
||||
if ($esign) {
|
||||
$recordId = in_array($accessUri, ['tcm.prescription/esignstart', 'tcm.prescription/esigncomplete',
|
||||
'tcm.prescription/esignverifywillingness', 'tcm.prescription/esignstatus',
|
||||
'tcm.prescription/esignidentitystatus', 'tcm.prescription/esigndownload'], true);
|
||||
$id = $request->isGet() ? $request->get('id', 0) : $request->post('id', 0);
|
||||
$params = $recordId ? ['id' => is_scalar($id) ? (int) $id : 0] : [];
|
||||
}
|
||||
|
||||
//过滤密码参数
|
||||
if (isset($params['password'])) {
|
||||
$params['password'] = "******";
|
||||
@@ -73,14 +84,29 @@ class OperationLog
|
||||
$systemLog->admin_name = $request->adminInfo['name'] ?? '';
|
||||
$systemLog->action = $notes;
|
||||
$systemLog->account = $request->adminInfo['account'] ?? '';
|
||||
$systemLog->url = $request->url(true);
|
||||
$systemLog->url = $esign ? strtok($request->url(true), '?') : $request->url(true);
|
||||
$systemLog->type = $request->isGet() ? 'GET' : 'POST';
|
||||
$systemLog->params = json_encode($params, true);
|
||||
$systemLog->ip = $request->ip();
|
||||
$systemLog->result = $this->shortenOperationLogResult((string) $response->getContent());
|
||||
$content = (string) $response->getContent();
|
||||
$systemLog->result = $esign ? $this->esignOperationLogResult($content) : $this->shortenOperationLogResult($content);
|
||||
return $systemLog->save();
|
||||
}
|
||||
|
||||
private function esignOperationLogResult(string $content): string
|
||||
{
|
||||
$decoded = json_decode($content, true);
|
||||
$result = [];
|
||||
if (is_int($decoded['code'] ?? null)) {
|
||||
$result['code'] = $decoded['code'];
|
||||
}
|
||||
$status = $decoded['data']['status'] ?? null;
|
||||
if (is_string($status) && preg_match('/^[a-z_]{1,32}$/D', $status)) {
|
||||
$result['status'] = $status;
|
||||
}
|
||||
return json_encode($result);
|
||||
}
|
||||
|
||||
/** MySQL TEXT 上限约 64KB,列表接口常含 base64 签名导致超长 */
|
||||
private function shortenOperationLogResult(string $content): string
|
||||
{
|
||||
@@ -100,6 +126,10 @@ class OperationLog
|
||||
private function stripLargeLogFields(array &$data): void
|
||||
{
|
||||
foreach ($data as $key => &$value) {
|
||||
if ($key === 'auth_url') {
|
||||
unset($data[$key]);
|
||||
continue;
|
||||
}
|
||||
if (is_array($value)) {
|
||||
$this->stripLargeLogFields($value);
|
||||
continue;
|
||||
@@ -112,4 +142,4 @@ class OperationLog
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use app\adminapi\lists\BaseAdminDataLists;
|
||||
use app\common\lists\ListsSearchInterface;
|
||||
use app\common\lists\Traits\HasDataScopeFilter;
|
||||
use app\adminapi\logic\tcm\PrescriptionLogic;
|
||||
use app\adminapi\logic\tcm\PrescriptionOrderLogic;
|
||||
use app\common\model\tcm\Prescription;
|
||||
use app\common\model\tcm\PrescriptionOrder;
|
||||
|
||||
@@ -256,6 +257,7 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
|
||||
$rejectedRx = [];
|
||||
$bizRejectRemark = [];
|
||||
$hasBizOrderRx = [];
|
||||
$hasReleasedOrderRx = [];
|
||||
if ($rxIds !== []) {
|
||||
$bizRejectRows = PrescriptionOrder::whereIn('prescription_id', $rxIds)
|
||||
->where('prescription_audit_status', 2)
|
||||
@@ -276,14 +278,18 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
|
||||
}
|
||||
}
|
||||
|
||||
$orderRxIds = array_unique(array_map(
|
||||
'intval',
|
||||
PrescriptionOrder::whereIn('prescription_id', $rxIds)
|
||||
->whereNull('delete_time')
|
||||
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
|
||||
->column('prescription_id')
|
||||
));
|
||||
$hasBizOrderRx = array_fill_keys($orderRxIds, true);
|
||||
$orders = PrescriptionOrder::whereIn('prescription_id', $rxIds)
|
||||
->whereNull('delete_time')
|
||||
->field(['prescription_id', 'fulfillment_status'])
|
||||
->select();
|
||||
foreach ($orders as $order) {
|
||||
$prescriptionId = (int) $order->prescription_id;
|
||||
if (in_array((int) $order->fulfillment_status, PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES, true)) {
|
||||
$hasReleasedOrderRx[$prescriptionId] = true;
|
||||
} else {
|
||||
$hasBizOrderRx[$prescriptionId] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取医助姓名
|
||||
@@ -295,7 +301,13 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
|
||||
}
|
||||
|
||||
// 处理字段格式
|
||||
$voidAccess = $lists === [] ? [] : PrescriptionLogic::getVoidAccess($this->adminId, $this->adminInfo);
|
||||
foreach ($lists as &$item) {
|
||||
$rid = (int) $item['id'];
|
||||
$item['can_void'] = empty($item['void_status']) && empty($hasBizOrderRx[$rid])
|
||||
&& PrescriptionLogic::hasVoidAccess($item, $this->adminId, $this->adminInfo, $voidAccess)
|
||||
&& (empty($hasReleasedOrderRx[$rid]) || PrescriptionOrderLogic::remoteSnapshotLockErrorForPrescription($rid) === null);
|
||||
$item['can_restore_rejected'] = PrescriptionLogic::canRestoreRejected($item);
|
||||
$item['esign'] = \app\common\service\esign\PrescriptionEsignService::describe($item, (int) $this->adminId);
|
||||
// 设置默认值
|
||||
$item['usage_time'] = $item['usage_time'] ?? '饭前';
|
||||
@@ -307,7 +319,6 @@ class PrescriptionLists extends BaseAdminDataLists implements ListsSearchInterfa
|
||||
$item['audit_status'] = (int) ($item['audit_status'] ?? 1);
|
||||
$item['void_status'] = (int) ($item['void_status'] ?? 0);
|
||||
$item['visible_role_ids'] = PrescriptionLogic::visibleRoleIdsToArray((string) ($item['visible_role_ids'] ?? ''));
|
||||
$rid = (int) ($item['id'] ?? 0);
|
||||
$item['business_prescription_audit_rejected'] = !empty($rejectedRx[$rid]) ? 1 : 0;
|
||||
$item['business_prescription_audit_remark'] = (string) ($bizRejectRemark[$rid] ?? '');
|
||||
$item['has_prescription_order'] = !empty($hasBizOrderRx[$rid]) ? 1 : 0;
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user