更新
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user