133 lines
3.9 KiB
PHP
133 lines
3.9 KiB
PHP
<?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' => '变更创建人(指派医助)',
|
|
'split' => '拆分订单',
|
|
'split_child' => '拆分生成子单',
|
|
];
|
|
|
|
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;
|
|
}
|
|
}
|