This commit is contained in:
Your Name
2026-04-22 12:14:39 +08:00
parent a1a0b74596
commit 4872e4d2e6
21 changed files with 1131 additions and 235 deletions
@@ -0,0 +1,130 @@
<?php
declare(strict_types=1);
namespace app\adminapi\logic\order;
use app\common\model\auth\Admin;
use app\common\model\OrderActionLog;
/**
* 支付单操作日志(写主库、失败忽略)
*/
class OrderActionLogLogic
{
/** @var array<string,string> 动作码 => 中文说明 */
public const ACTION_LABELS = [
'view_detail' => '查看详情',
'edit' => '编辑订单',
'wx_qrcode' => '小程序码',
'pay' => '确认支付',
'refund' => '退款',
'cancel' => '取消订单',
'delete' => '删除订单',
'create' => '创建订单',
'create_wechat_work' => '创建订单(企微对外收款)',
'assign_assistant' => '变更创建人(指派医助)',
];
public static function record(
int $orderId,
int $adminId,
array $adminInfo,
string $action,
string $summary = ''
): void {
if ($orderId <= 0) {
return;
}
$adminName = (string)($adminInfo['name'] ?? '');
if ($adminName === '' && $adminId > 0) {
$adminName = (string) Admin::where('id', $adminId)->value('name');
}
$log = new OrderActionLog();
$log->order_id = $orderId;
$log->admin_id = $adminId;
$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();
try {
$log->save();
} catch (\Throwable $e) {
// 忽略日志表未创建等错误,不影响主业务
}
}
/**
* 单条支付单操作记录列表(新在前)
*
* @return array{lists: array<int, array<string,mixed>>, count: int}
*/
public static function listByOrderId(int $orderId, int $pageNo, int $pageSize): array
{
if ($orderId <= 0) {
return ['lists' => [], 'count' => 0];
}
$pageSize = max(1, min(100, $pageSize));
$pageNo = max(1, $pageNo);
$offset = ($pageNo - 1) * $pageSize;
$q = OrderActionLog::where('order_id', $orderId);
$count = (int) $q->count();
$rows = OrderActionLog::where('order_id', $orderId)
->order('id', 'desc')
->limit($offset, $pageSize)
->select()
->toArray();
foreach ($rows as &$r) {
$code = (string)($r['action'] ?? '');
$r['action_label'] = self::ACTION_LABELS[$code] ?? $code;
$r['create_time_text'] = !empty($r['create_time'])
? date('Y-m-d H:i:s', (int) $r['create_time'])
: '';
}
unset($r);
return ['lists' => $rows, 'count' => $count];
}
/**
* 按时间范围统计每人操作次数(仅统计有日志表的数据)
*
* @return array<int, array{admin_id: int, admin_name: string, cnt: int}>
*/
public static function statsByAdmin(int $startTime, int $endTime, int $limit = 50): array
{
if ($endTime < $startTime) {
return [];
}
$limit = max(1, min(200, $limit));
try {
$rows = OrderActionLog::field('admin_id, admin_name, COUNT(*) AS cnt')
->whereBetween('create_time', [$startTime, $endTime])
->group('admin_id, admin_name')
->order('cnt', 'desc')
->limit($limit)
->select()
->toArray();
} catch (\Throwable $e) {
return [];
}
$out = [];
foreach ($rows as $r) {
$out[] = [
'admin_id' => (int)($r['admin_id'] ?? 0),
'admin_name' => (string)($r['admin_name'] ?? ''),
'cnt' => (int)($r['cnt'] ?? 0),
];
}
return $out;
}
}
+104 -4
View File
@@ -7,6 +7,7 @@ namespace app\adminapi\logic\order;
use app\common\model\Order;
use app\common\model\OrderDetail;
use app\common\model\auth\Admin;
use app\common\model\auth\AdminRole;
use app\common\model\tcm\Diagnosis;
use app\common\model\tcm\PrescriptionOrder;
use app\common\model\tcm\PrescriptionOrderPayOrder;
@@ -38,13 +39,25 @@ class OrderLogic
public static function create(array $params)
{
try {
$channel = (string)($params['payment_channel'] ?? 'normal');
if (!in_array($channel, ['normal', 'fubei'], true)) {
$channel = 'normal';
}
$requirePaymentSlipAudit = (int)($params['require_payment_slip_audit'] ?? 0) === 1;
$order = new Order();
$order->order_no = self::generateOrderNo();
$order->patient_id = $params['patient_id'];
$order->creator_id = $params['creator_id'];
$order->order_type = $params['order_type'];
$order->amount = $params['amount'];
$order->status = 1; // 待支付
// 付呗且申请审核支付单:待审核(5);否则待支付(1)
if ($channel === 'fubei') {
$order->payment_method = 'fubei';
$order->status = $requirePaymentSlipAudit ? 5 : 1;
} else {
$order->status = 1; // 待支付
}
$order->remark = $params['remark'] ?? '';
$order->save();
@@ -331,7 +344,13 @@ class OrderLogic
return false;
}
if ($order->status != 1) {
$st = (int)$order->status;
// 1=待支付;5=待审核(付呗+申请支付单审核) 时允许录入手动到账
if ($st === 1) {
// 正常待支付
} elseif ($st === 5 && (string)($order->payment_method ?? '') === 'fubei') {
// 待审核的付呗单,通过人工确认后标记已支付
} else {
self::setError('订单状态不允许支付');
return false;
}
@@ -362,8 +381,9 @@ class OrderLogic
return false;
}
if ($order->status != 1) {
self::setError('只有待支付的订单才能取消');
$st = (int)$order->status;
if ($st !== 1 && !($st === 5 && (string)($order->payment_method ?? '') === 'fubei')) {
self::setError('只有待支付或待审核(付呗)的订单才能取消');
return false;
}
@@ -1097,4 +1117,84 @@ class OrderLogic
return null;
}
/**
* 批量指派给医助:将「创建人」变更为目标医助(须为医助 role_id=2),用于列表筛选项为创建人=该医助
*
* @param int[] $orderIds
* @return array{success: int, per_log: list<array{order_id: int, summary: string}>, fail: int, errors: list<string>}|false
*/
public static function batchAssignAssistant(array $orderIds, int $assistantAdminId)
{
$assistantAdminId = (int) $assistantAdminId;
if ($assistantAdminId <= 0) {
self::setError('请选择医助');
return false;
}
if (
AdminRole::where('admin_id', $assistantAdminId)
->where('role_id', 2)
->count() === 0
) {
self::setError('目标账号不是医助角色');
return false;
}
$ids = array_values(array_unique(array_filter(array_map('intval', $orderIds), static fn (int $id) => $id > 0)));
if ($ids === []) {
self::setError('请选择订单');
return false;
}
if (count($ids) > 200) {
self::setError('单次最多200单');
return false;
}
$targetName = (string) Admin::where('id', $assistantAdminId)->whereNull('delete_time')->value('name');
if ($targetName === '') {
$targetName = (string) $assistantAdminId;
}
$perLog = [];
$errors = [];
foreach ($ids as $oid) {
$order = Order::where('id', $oid)->whereNull('delete_time')->find();
if (! $order) {
$errors[] = "订单{$oid}不存在或已删除";
continue;
}
$oldCreatorId = (int) $order->creator_id;
if ($oldCreatorId === $assistantAdminId) {
$errors[] = "订单{$oid}创建人已是该医助,已跳过";
continue;
}
$oldName = $oldCreatorId > 0
? (string) Admin::where('id', $oldCreatorId)->whereNull('delete_time')->value('name')
: '';
if ($oldName === '' && $oldCreatorId > 0) {
$oldName = (string) $oldCreatorId;
} elseif ($oldName === '') {
$oldName = '—';
}
$order->creator_id = $assistantAdminId;
if (! $order->save()) {
$errors[] = "订单{$oid}保存失败";
continue;
}
$perLog[] = [
'order_id' => (int) $order->id,
'summary' => '创建人由「' . $oldName . '」变更为医助「' . $targetName . '」',
];
}
if ($perLog === []) {
self::setError($errors !== [] ? implode('', $errors) : '未能变更任何订单');
return false;
}
return [
'success' => count($perLog),
'per_log' => $perLog,
'fail' => count($errors),
'errors' => $errors,
];
}
}
@@ -1513,14 +1513,25 @@ class PrescriptionOrderLogic
return $out;
}
/** 完成订单时可选的履约终态:3=已完成,7-12=业务结案类状态 */
private const COMPLETE_FULFILLMENT_TARGETS = [3, 7, 8, 9, 10, 11, 12];
/** 完成时是否按「实付=关联支付单金额」重算并清空诊单医助(与 clearDiagnosisAssistantOnComplete 的终态判断一致) */
private const COMPLETE_FULFILLMENT_TERMINAL = [3, 8, 9, 10, 11, 12];
/**
* 将「已发货」(fulfillment_status=5) 且支付审核已通过的订单标记为「已完成」(3)
* 将「已发货/已签收」且支付审核已通过的订单结案:可选「已完成」或 7-12 业务状态
*
* @return array<string,mixed>|false
*/
public static function complete(int $id, int $adminId, array $adminInfo)
public static function complete(int $id, int $adminId, array $adminInfo, int $targetFulfillmentStatus = 3)
{
self::$error = '';
if (!in_array($targetFulfillmentStatus, self::COMPLETE_FULFILLMENT_TARGETS, true)) {
self::$error = '无效的完成状态';
return false;
}
$order = PrescriptionOrder::where('id', $id)->whereNull('delete_time')->find();
if (!$order) {
self::$error = '订单不存在';
@@ -1530,8 +1541,9 @@ class PrescriptionOrderLogic
self::$error = '无权限操作';
return false;
}
if ((int) $order->fulfillment_status !== 5) {
self::$error = '仅「已发货」状态可标记为已完成';
$curFs = (int) $order->fulfillment_status;
if ($curFs !== 5 && $curFs !== 6) {
self::$error = '仅「已发货」或「已签收」状态可完成订单';
return false;
}
if ((int) $order->payment_slip_audit_status !== 1) {
@@ -1539,17 +1551,22 @@ class PrescriptionOrderLogic
return false;
}
// 计算关联订单总额
$linkedPayOrderIds = self::linkedPayOrderIdList($id);
$linkedPayOrders = self::linkedPayOrdersPayload($linkedPayOrderIds);
$label = self::fulfillmentStatusLabel($targetFulfillmentStatus);
$linkedPayOrderIds = self::linkedPayOrderIdList($id);
$linkedPayOrders = self::linkedPayOrdersPayload($linkedPayOrderIds);
$linkedPayPaidTotal = 0.0;
foreach ($linkedPayOrders as $o) {
$linkedPayPaidTotal += (float) ($o['amount'] ?? 0);
}
$linkedPayPaidTotal = round($linkedPayPaidTotal, 2);
$order->fulfillment_status = 3;
$order->paid = $linkedPayPaidTotal;
if ($targetFulfillmentStatus === 3) {
$order->fulfillment_status = 3;
$order->paid = $linkedPayPaidTotal;
} else {
$order->fulfillment_status = $targetFulfillmentStatus;
}
try {
$order->save();
@@ -1558,14 +1575,22 @@ class PrescriptionOrderLogic
return false;
}
$unassignSummary = self::clearDiagnosisAssistantOnComplete((int) $order->diagnosis_id);
$unassignSummary = '';
if (in_array($targetFulfillmentStatus, self::COMPLETE_FULFILLMENT_TERMINAL, true)) {
$unassignSummary = self::clearDiagnosisAssistantOnComplete((int) $order->diagnosis_id);
}
if ($targetFulfillmentStatus === 3) {
$logLine = '订单完成,状态变更为「' . $label . '」,实付金额更新为 ¥' . $linkedPayPaidTotal . $unassignSummary;
} else {
$logLine = '订单处理完成,状态变更为「' . $label . '」' . $unassignSummary;
}
self::writeLog(
$id,
$adminId,
$adminInfo,
'complete',
'订单完成,状态变更为「已完成」,实付金额更新为 ¥' . $linkedPayPaidTotal . $unassignSummary
$logLine
);
$out = $order->toArray();
@@ -1575,6 +1600,26 @@ class PrescriptionOrderLogic
return $out;
}
private static function fulfillmentStatusLabel(int $fs): string
{
$m = [
1 => '待双审通过',
2 => '待发货',
3 => '已完成',
4 => '已取消',
5 => '已发货',
6 => '已签收',
7 => '进行中',
8 => '暂不制药',
9 => '拒收',
10 => '退款',
11 => '保留药方',
12 => '制药缓发',
];
return $m[$fs] ?? ('状态' . (string) $fs);
}
/**
* 完成订单时把关联诊单的 assistant_id 清空,让患者回到「待分配」状态
* 仅当该患者没有其它进行中的处方订单时才清空,避免误覆盖
@@ -1600,7 +1645,7 @@ class PrescriptionOrderLogic
// 同一诊单下若仍有未完成/未取消的处方订单,则不清空
$pendingCount = PrescriptionOrder::where('diagnosis_id', $diagnosisId)
->whereNull('delete_time')
->whereNotIn('fulfillment_status', [3, 4])
->whereNotIn('fulfillment_status', [3, 4, 8, 9, 10, 11, 12])
->count();
if ($pendingCount > 0) {
return '';
@@ -1635,7 +1680,7 @@ class PrescriptionOrderLogic
if ($pa !== 1) {
return false;
}
if ($fs === 3 || $fs === 4) {
if (in_array($fs, [3, 4, 8, 9, 10, 11, 12], true)) {
return false;
}
if (trim((string) ($item['gancao_reciperl_order_no'] ?? '')) !== '') {