This commit is contained in:
Your Name
2026-09-09 12:18:17 +08:00
parent 0cea43b027
commit 40319eea41
76 changed files with 4821 additions and 268 deletions
@@ -748,16 +748,16 @@ class PrescriptionLogic
$bizPo = PrescriptionOrder::where('prescription_id', $id)
->where('prescription_audit_status', 2)
->whereNull('delete_time')
->where('fulfillment_status', '<>', 4)
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
->order('id', 'desc')
->find();
$arr['business_prescription_audit_rejected'] = $bizPo ? 1 : 0;
$arr['business_prescription_audit_remark'] = $bizPo ? (string) ($bizPo->prescription_audit_remark ?? '') : '';
// 与 PrescriptionLists「业务订单」角标一致:未删除且非已取消(4) 即视为存在有效业务订单
// 与列表和创建校验一致:已取消 / 已退款的订单保留历史关联,但不再占用处方。
$hasBizOrder = PrescriptionOrder::where('prescription_id', $id)
->whereNull('delete_time')
->where('fulfillment_status', '<>', 4)
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
->count() > 0;
$arr['has_prescription_order'] = $hasBizOrder ? 1 : 0;
@@ -1216,7 +1216,7 @@ class PrescriptionLogic
$rxId = (int) ($row->id ?? 0);
$bizCount = (int) PrescriptionOrder::where('prescription_id', $rxId)
->whereNull('delete_time')
->where('fulfillment_status', '<>', 4)
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
->count();
if ($bizCount > 0) {
self::setError('该处方已存在业务订单,无法作废');
@@ -963,6 +963,31 @@ class PrescriptionOrderLogic
* @return array<string,mixed>|false
*/
public static function create(array $params, int $adminId, array $adminInfo)
{
self::$error = '';
try {
return Db::transaction(static function () use ($params, $adminId, $adminInfo) {
// 先锁始终存在的处方行;空订单集合也能串行化两次创建。
// 首个一致性读发生在获得该锁后,等待者可见前一次创建提交的订单。
$rx = Prescription::where('id', (int) ($params['prescription_id'] ?? 0))
->whereNull('delete_time')->lock(true)->find();
if (!$rx) {
throw new \DomainException('处方不存在');
}
$result = self::createLocked($params, $adminId, $adminInfo);
if ($result === false) {
throw new \DomainException(self::$error ?: '创建业务订单失败');
}
return $result;
});
} catch (\Throwable $e) {
self::$error = $e->getMessage();
return false;
}
}
private static function createLocked(array $params, int $adminId, array $adminInfo)
{
self::$error = '';
$rxId = (int) $params['prescription_id'];
@@ -989,8 +1014,9 @@ class PrescriptionOrderLogic
return false;
}
if (PrescriptionOrder::where('prescription_id', $rxId)->whereNull('delete_time')->where('fulfillment_status', '<>', 4)->count() > 0) {
self::$error = '该处方已存在有效业务订单(未撤回前不可重复创建)';
if (PrescriptionOrder::where('prescription_id', $rxId)->whereNull('delete_time')
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)->count() > 0) {
self::$error = '该处方已存在有效业务订单(撤回或全额退款后可重新创建)';
return false;
}
@@ -1087,7 +1113,7 @@ class PrescriptionOrderLogic
return false;
}
self::writeLog((int) $order->id, $adminId, $adminInfo, 'create', '创建业务订单');
self::writeLog((int) $order->id, $adminId, $adminInfo, 'create', '创建业务订单', true);
if ($payOrderIds !== []) {
self::replacePayOrderLinks((int) $order->id, $payOrderIds);
@@ -1103,6 +1129,52 @@ class PrescriptionOrderLogic
return $out;
}
/** 只修正业务订单创建时间,不改变关联支付、处方、药房快照或履约状态。 */
public static function editTime(array $params, int $adminId, array $adminInfo)
{
self::$error = '';
// 菜单尚未部署时,中间件可能放行未知权限,因此必须显式校验。
if ((int) ($adminInfo['root'] ?? 0) !== 1
&& !in_array('tcm.prescriptionOrder/editTime', AuthLogic::getAuthByAdminId($adminId), true)) {
self::$error = '无权限修改业务订单创建时间';
return false;
}
$validator = new \app\adminapi\validate\tcm\PrescriptionOrderValidate();
if (!$validator->scene('editTime')->check($params)) {
self::$error = (string) $validator->getError();
return false;
}
try {
return Db::transaction(static function () use ($params, $adminId, $adminInfo) {
$order = PrescriptionOrder::where('id', (int) $params['id'])
->whereNull('delete_time')->lock(true)->find();
if (!$order) {
throw new \DomainException('订单不存在');
}
if (!self::canAccessOrder($order, $adminId, $adminInfo)) {
throw new \DomainException('无权限操作此订单');
}
$oldValue = $order->getData('create_time');
$dateTime = (string) $params['create_time'];
$isTimestamp = is_int($oldValue) || (is_string($oldValue) && ctype_digit($oldValue));
$newValue = $isTimestamp ? (int) strtotime($dateTime) : $dateTime;
if ((string) $oldValue !== (string) $newValue) {
$order->create_time = $newValue;
$order->save();
$oldLabel = $isTimestamp ? date('Y-m-d H:i:s', (int) $oldValue) : (string) $oldValue;
self::writeLog((int) $order->id, $adminId, $adminInfo, 'edit_time',
'修改业务订单创建时间:' . $oldLabel . ' → ' . $dateTime, true);
}
return ['id' => (int) $order->id, 'create_time' => $newValue];
});
} catch (\Throwable $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function detail(int $id, int $adminId, array $adminInfo): ?array
{
self::$error = '';