605 lines
27 KiB
PHP
605 lines
27 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
|
||
use App\Http\Controllers\Concerns\ResolvesPlatformAdminContext;
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\PlatformOrder;
|
||
use App\Support\SubscriptionActivationService;
|
||
use Illuminate\Database\Eloquent\Builder;
|
||
use Illuminate\Http\RedirectResponse;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\View\View;
|
||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||
|
||
class PlatformOrderController extends Controller
|
||
{
|
||
use ResolvesPlatformAdminContext;
|
||
|
||
public function index(Request $request): View
|
||
{
|
||
$this->ensurePlatformAdmin($request);
|
||
|
||
$filters = [
|
||
'status' => trim((string) $request->query('status', '')),
|
||
'payment_status' => trim((string) $request->query('payment_status', '')),
|
||
'merchant_id' => trim((string) $request->query('merchant_id', '')),
|
||
'plan_id' => trim((string) $request->query('plan_id', '')),
|
||
'fail_only' => (string) $request->query('fail_only', ''),
|
||
'synced_only' => (string) $request->query('synced_only', ''),
|
||
'sync_status' => trim((string) $request->query('sync_status', '')),
|
||
'keyword' => trim((string) $request->query('keyword', '')),
|
||
// 只看“可同步订阅”的订单:已支付 + 已生效 + 未同步(用于运营快速处理)
|
||
'syncable_only' => (string) $request->query('syncable_only', ''),
|
||
// 只看最近 24 小时批量同步过的订单(可治理追踪)
|
||
'batch_synced_24h' => (string) $request->query('batch_synced_24h', ''),
|
||
];
|
||
|
||
$orders = $this->applyFilters(PlatformOrder::query()->with(['merchant', 'plan', 'siteSubscription']), $filters)
|
||
->latest('id')
|
||
->paginate(10)
|
||
->withQueryString();
|
||
|
||
$baseQuery = $this->applyFilters(PlatformOrder::query(), $filters);
|
||
|
||
// 同步失败原因聚合(Top 5):用于运营快速判断“常见失败原因”
|
||
// 注意:这里用 JSON_EXTRACT 做 group by,MySQL 会返回带引号的 JSON 字符串,展示时做一次 trim 处理。
|
||
$failedReasonRows = (clone $baseQuery)
|
||
->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation_error.message') IS NOT NULL")
|
||
->selectRaw("JSON_EXTRACT(meta, '$.subscription_activation_error.message') as reason, count(*) as cnt")
|
||
->groupBy('reason')
|
||
->orderByDesc('cnt')
|
||
->limit(5)
|
||
->get();
|
||
|
||
$failedReasonStats = $failedReasonRows->map(function ($row) {
|
||
$reason = (string) ($row->reason ?? '');
|
||
$reason = trim($reason, "\" ");
|
||
|
||
return [
|
||
'reason' => $reason !== '' ? $reason : '(空)',
|
||
'count' => (int) ($row->cnt ?? 0),
|
||
];
|
||
})->values()->all();
|
||
|
||
return view('admin.platform_orders.index', [
|
||
'orders' => $orders,
|
||
'filters' => $filters,
|
||
'filterOptions' => [
|
||
'statuses' => $this->statusLabels(),
|
||
'paymentStatuses' => $this->paymentStatusLabels(),
|
||
],
|
||
'merchants' => PlatformOrder::query()->with('merchant')
|
||
->select('merchant_id')
|
||
->whereNotNull('merchant_id')
|
||
->distinct()
|
||
->get()
|
||
->pluck('merchant')
|
||
->filter()
|
||
->unique('id')
|
||
->values(),
|
||
'plans' => PlatformOrder::query()->with('plan')
|
||
->select('plan_id')
|
||
->whereNotNull('plan_id')
|
||
->distinct()
|
||
->get()
|
||
->pluck('plan')
|
||
->filter()
|
||
->unique('id')
|
||
->values(),
|
||
'statusLabels' => $this->statusLabels(),
|
||
'paymentStatusLabels' => $this->paymentStatusLabels(),
|
||
'summaryStats' => [
|
||
'total_orders' => (clone $baseQuery)->count(),
|
||
'paid_orders' => (clone $baseQuery)->where('payment_status', 'paid')->count(),
|
||
'activated_orders' => (clone $baseQuery)->where('status', 'activated')->count(),
|
||
'synced_orders' => (clone $baseQuery)
|
||
->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation.subscription_id') IS NOT NULL")
|
||
->count(),
|
||
'failed_sync_orders' => (clone $baseQuery)
|
||
->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation_error.message') IS NOT NULL")
|
||
->count(),
|
||
'unsynced_orders' => (clone $baseQuery)
|
||
->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation.subscription_id') IS NULL")
|
||
->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation_error.message') IS NULL")
|
||
->count(),
|
||
'total_payable_amount' => (float) ((clone $baseQuery)->sum('payable_amount') ?: 0),
|
||
'total_paid_amount' => (float) ((clone $baseQuery)->sum('paid_amount') ?: 0),
|
||
'syncable_orders' => (clone $baseQuery)
|
||
->where('payment_status', 'paid')
|
||
->where('status', 'activated')
|
||
->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation.subscription_id') IS NULL")
|
||
->count(),
|
||
'batch_synced_24h_orders' => (function () use ($baseQuery) {
|
||
$since = now()->subHours(24)->format('Y-m-d H:i:s');
|
||
|
||
$q = (clone $baseQuery)
|
||
->whereRaw("JSON_EXTRACT(meta, '$.batch_activation.at') IS NOT NULL");
|
||
|
||
$driver = $q->getQuery()->getConnection()->getDriverName();
|
||
if ($driver === 'sqlite') {
|
||
$q->whereRaw("JSON_EXTRACT(meta, '$.batch_activation.at') >= ?", [$since]);
|
||
} else {
|
||
$q->whereRaw("JSON_UNQUOTE(JSON_EXTRACT(meta, '$.batch_activation.at')) >= ?", [$since]);
|
||
}
|
||
|
||
return $q->count();
|
||
})(),
|
||
],
|
||
'failedReasonStats' => $failedReasonStats,
|
||
]);
|
||
}
|
||
|
||
public function show(Request $request, PlatformOrder $order): View
|
||
{
|
||
$this->ensurePlatformAdmin($request);
|
||
|
||
$order->loadMissing(['merchant', 'plan', 'siteSubscription']);
|
||
|
||
return view('admin.platform_orders.show', [
|
||
'order' => $order,
|
||
'statusLabels' => $this->statusLabels(),
|
||
'paymentStatusLabels' => $this->paymentStatusLabels(),
|
||
]);
|
||
}
|
||
|
||
public function activateSubscription(Request $request, PlatformOrder $order, SubscriptionActivationService $service): RedirectResponse
|
||
{
|
||
$admin = $this->ensurePlatformAdmin($request);
|
||
|
||
try {
|
||
$subscription = $service->activateOrder($order->id, $admin->id);
|
||
|
||
// 同步成功:清理失败记录(若存在)
|
||
$meta = (array) ($order->meta ?? []);
|
||
data_forget($meta, 'subscription_activation_error');
|
||
$order->meta = $meta;
|
||
$order->save();
|
||
} catch (\Throwable $e) {
|
||
// 同步失败:写入错误信息,便于运营排查(可治理)
|
||
$meta = (array) ($order->meta ?? []);
|
||
data_set($meta, 'subscription_activation_error', [
|
||
'message' => $e->getMessage(),
|
||
'at' => now()->toDateTimeString(),
|
||
'admin_id' => $admin->id,
|
||
]);
|
||
$order->meta = $meta;
|
||
$order->save();
|
||
|
||
return redirect()->back()->with('error', '订阅同步失败:' . $e->getMessage());
|
||
}
|
||
|
||
return redirect()->back()->with('success', '订阅已同步:' . $subscription->subscription_no);
|
||
}
|
||
|
||
public function markPaidAndActivate(Request $request, PlatformOrder $order, SubscriptionActivationService $service): RedirectResponse
|
||
{
|
||
$admin = $this->ensurePlatformAdmin($request);
|
||
|
||
// 最小状态推进:将订单标记为已支付 + 已生效,并补齐时间与金额字段
|
||
$now = now();
|
||
$order->payment_status = 'paid';
|
||
$order->status = 'activated';
|
||
$order->paid_at = $order->paid_at ?: $now;
|
||
$order->activated_at = $order->activated_at ?: $now;
|
||
$order->paid_amount = $order->paid_amount > 0 ? $order->paid_amount : $order->payable_amount;
|
||
$order->save();
|
||
|
||
// 立刻同步订阅
|
||
try {
|
||
$subscription = $service->activateOrder($order->id, $admin->id);
|
||
|
||
$meta = (array) ($order->meta ?? []);
|
||
data_forget($meta, 'subscription_activation_error');
|
||
$order->meta = $meta;
|
||
$order->save();
|
||
} catch (\Throwable $e) {
|
||
$meta = (array) ($order->meta ?? []);
|
||
data_set($meta, 'subscription_activation_error', [
|
||
'message' => $e->getMessage(),
|
||
'at' => now()->toDateTimeString(),
|
||
'admin_id' => $admin->id,
|
||
]);
|
||
$order->meta = $meta;
|
||
$order->save();
|
||
|
||
return redirect()->back()->with('error', '订单已标记为已支付/已生效,但订阅同步失败:' . $e->getMessage());
|
||
}
|
||
|
||
return redirect()->back()->with('success', '订单已标记支付并生效,订阅已同步:' . $subscription->subscription_no);
|
||
}
|
||
|
||
public function export(Request $request): StreamedResponse
|
||
{
|
||
$this->ensurePlatformAdmin($request);
|
||
|
||
$filters = [
|
||
'status' => trim((string) $request->query('status', '')),
|
||
'payment_status' => trim((string) $request->query('payment_status', '')),
|
||
'merchant_id' => trim((string) $request->query('merchant_id', '')),
|
||
'plan_id' => trim((string) $request->query('plan_id', '')),
|
||
'fail_only' => (string) $request->query('fail_only', ''),
|
||
'synced_only' => (string) $request->query('synced_only', ''),
|
||
'sync_status' => trim((string) $request->query('sync_status', '')),
|
||
'keyword' => trim((string) $request->query('keyword', '')),
|
||
// 只看“可同步订阅”的订单:已支付 + 已生效 + 未同步(用于运营快速处理)
|
||
'syncable_only' => (string) $request->query('syncable_only', ''),
|
||
// 只看最近 24 小时批量同步过的订单(可治理追踪)
|
||
'batch_synced_24h' => (string) $request->query('batch_synced_24h', ''),
|
||
];
|
||
|
||
$includeMeta = (string) $request->query('include_meta', '') === '1';
|
||
|
||
$query = $this->applyFilters(
|
||
PlatformOrder::query()->with(['merchant', 'plan', 'siteSubscription']),
|
||
$filters
|
||
)->orderBy('id');
|
||
|
||
$filename = 'platform_orders_' . now()->format('Ymd_His') . '.csv';
|
||
|
||
return response()->streamDownload(function () use ($query, $includeMeta) {
|
||
$out = fopen('php://output', 'w');
|
||
|
||
// UTF-8 BOM,避免 Excel 打开中文乱码
|
||
fwrite($out, "\xEF\xBB\xBF");
|
||
|
||
$headers = [
|
||
'ID',
|
||
'订单号',
|
||
'站点',
|
||
'套餐',
|
||
'订单类型',
|
||
'订单状态',
|
||
'支付状态',
|
||
'应付金额',
|
||
'已付金额',
|
||
'下单时间',
|
||
'支付时间',
|
||
'生效时间',
|
||
'同步状态',
|
||
'订阅号',
|
||
'订阅到期',
|
||
'同步时间',
|
||
'同步失败原因',
|
||
'同步失败时间',
|
||
];
|
||
|
||
if ($includeMeta) {
|
||
$headers[] = '原始meta(JSON)';
|
||
}
|
||
|
||
fputcsv($out, $headers);
|
||
|
||
$query->chunkById(500, function ($orders) use ($out, $includeMeta) {
|
||
foreach ($orders as $order) {
|
||
$syncedId = (int) data_get($order->meta, 'subscription_activation.subscription_id', 0);
|
||
$syncErr = (string) (data_get($order->meta, 'subscription_activation_error.message') ?? '');
|
||
|
||
if ($syncedId > 0) {
|
||
$syncStatus = '已同步';
|
||
} elseif ($syncErr !== '') {
|
||
$syncStatus = '同步失败';
|
||
} else {
|
||
$syncStatus = '未同步';
|
||
}
|
||
|
||
$row = [
|
||
$order->id,
|
||
$order->order_no,
|
||
$order->merchant?->name ?? '',
|
||
$order->plan_name ?: ($order->plan?->name ?? ''),
|
||
$order->order_type,
|
||
$order->status,
|
||
$order->payment_status,
|
||
(float) $order->payable_amount,
|
||
(float) $order->paid_amount,
|
||
optional($order->placed_at)->format('Y-m-d H:i:s') ?: '',
|
||
optional($order->paid_at)->format('Y-m-d H:i:s') ?: '',
|
||
optional($order->activated_at)->format('Y-m-d H:i:s') ?: '',
|
||
$syncStatus,
|
||
$order->siteSubscription?->subscription_no ?: '',
|
||
optional($order->siteSubscription?->ends_at)->format('Y-m-d H:i:s') ?: '',
|
||
(string) (data_get($order->meta, 'subscription_activation.synced_at') ?? ''),
|
||
$syncErr,
|
||
(string) (data_get($order->meta, 'subscription_activation_error.at') ?? ''),
|
||
];
|
||
|
||
if ($includeMeta) {
|
||
$row[] = json_encode($order->meta, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||
}
|
||
|
||
fputcsv($out, $row);
|
||
}
|
||
});
|
||
|
||
fclose($out);
|
||
}, $filename, [
|
||
'Content-Type' => 'text/csv; charset=UTF-8',
|
||
]);
|
||
}
|
||
|
||
public function batchActivateSubscriptions(Request $request, SubscriptionActivationService $service): RedirectResponse
|
||
{
|
||
$admin = $this->ensurePlatformAdmin($request);
|
||
|
||
// 支持两种 scope:
|
||
// - scope=filtered:只处理当前筛选范围内的订单(更安全,默认)
|
||
// - scope=all:处理全部订单(谨慎)
|
||
$scope = (string) $request->input('scope', 'filtered');
|
||
|
||
$filters = [
|
||
'status' => trim((string) $request->input('status', '')),
|
||
'payment_status' => trim((string) $request->input('payment_status', '')),
|
||
'merchant_id' => trim((string) $request->input('merchant_id', '')),
|
||
'plan_id' => trim((string) $request->input('plan_id', '')),
|
||
'fail_only' => (string) $request->input('fail_only', ''),
|
||
'synced_only' => (string) $request->input('synced_only', ''),
|
||
'sync_status' => trim((string) $request->input('sync_status', '')),
|
||
'keyword' => trim((string) $request->input('keyword', '')),
|
||
'syncable_only' => (string) $request->input('syncable_only', ''),
|
||
'batch_synced_24h' => (string) $request->input('batch_synced_24h', ''),
|
||
];
|
||
|
||
// 防误操作:批量同步默认要求先勾选“只看可同步”,避免无意识扩大处理范围
|
||
if ($scope === 'filtered' && ($filters['syncable_only'] ?? '') !== '1') {
|
||
return redirect()->back()->with('warning', '为避免误操作,请先在筛选条件中勾选「只看可同步」,再执行批量同步订阅。');
|
||
}
|
||
|
||
// 防误操作:scope=all 需要二次确认
|
||
if ($scope === 'all' && (string) $request->input('confirm', '') !== 'YES') {
|
||
return redirect()->back()->with('warning', '为避免误操作,执行全量批量同步前请在确认框输入 YES。');
|
||
}
|
||
|
||
$query = PlatformOrder::query();
|
||
|
||
if ($scope === 'filtered') {
|
||
$query = $this->applyFilters($query, $filters);
|
||
}
|
||
|
||
// 只处理“可同步”的订单(双保险,避免误操作)
|
||
$query = $query
|
||
->where('payment_status', 'paid')
|
||
->where('status', 'activated')
|
||
->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation.subscription_id') IS NULL");
|
||
|
||
$limit = (int) $request->input('limit', 50);
|
||
$limit = max(1, min(500, $limit));
|
||
|
||
$matchedTotal = (clone $query)->count();
|
||
|
||
// 默认按最新订单优先处理:避免 seed/demo 数据干扰测试,同时也更符合“先处理新问题”的运营直觉
|
||
$orders = $query->orderByDesc('id')->limit($limit)->get(['id']);
|
||
$processed = $orders->count();
|
||
|
||
$success = 0;
|
||
$failed = 0;
|
||
$failedReasonCounts = [];
|
||
|
||
foreach ($orders as $orderRow) {
|
||
try {
|
||
$service->activateOrder($orderRow->id, $admin->id);
|
||
|
||
// 轻量审计:记录批量同步动作(方便追溯)
|
||
$order = PlatformOrder::query()->find($orderRow->id);
|
||
if ($order) {
|
||
$meta = (array) ($order->meta ?? []);
|
||
$audit = (array) (data_get($meta, 'audit', []) ?? []);
|
||
$nowStr = now()->toDateTimeString();
|
||
$audit[] = [
|
||
'action' => 'batch_activate_subscription',
|
||
'scope' => $scope,
|
||
'at' => $nowStr,
|
||
'admin_id' => $admin->id,
|
||
];
|
||
data_set($meta, 'audit', $audit);
|
||
|
||
// 便于筛选/统计:记录最近一次批量同步信息(扁平字段)
|
||
data_set($meta, 'batch_activation', [
|
||
'at' => $nowStr,
|
||
'admin_id' => $admin->id,
|
||
'scope' => $scope,
|
||
]);
|
||
|
||
$order->meta = $meta;
|
||
$order->save();
|
||
}
|
||
|
||
$success++;
|
||
} catch (\Throwable $e) {
|
||
$failed++;
|
||
|
||
$reason = trim((string) $e->getMessage());
|
||
$reason = $reason !== '' ? $reason : '未知错误';
|
||
$failedReasonCounts[$reason] = ($failedReasonCounts[$reason] ?? 0) + 1;
|
||
|
||
// 批量同步失败也需要可治理:写入失败原因到订单 meta,便于后续筛选/导出/清理
|
||
$order = PlatformOrder::query()->find($orderRow->id);
|
||
if ($order) {
|
||
$meta = (array) ($order->meta ?? []);
|
||
data_set($meta, 'subscription_activation_error', [
|
||
'message' => $reason,
|
||
'at' => now()->toDateTimeString(),
|
||
'admin_id' => $admin->id,
|
||
]);
|
||
$order->meta = $meta;
|
||
$order->save();
|
||
}
|
||
}
|
||
}
|
||
|
||
$msg = '批量同步订阅完成:成功 ' . $success . ' 条,失败 ' . $failed . ' 条(命中 ' . $matchedTotal . ' 条,本次处理 ' . $processed . ' 条,limit=' . $limit . ')';
|
||
|
||
if ($failed > 0 && count($failedReasonCounts) > 0) {
|
||
arsort($failedReasonCounts);
|
||
$top = array_slice($failedReasonCounts, 0, 3, true);
|
||
$topText = collect($top)->map(function ($cnt, $reason) {
|
||
$reason = mb_substr((string) $reason, 0, 60);
|
||
return $reason . '(' . $cnt . ')';
|
||
})->implode(';');
|
||
|
||
$msg .= ';失败原因Top:' . $topText;
|
||
}
|
||
|
||
return redirect()->back()->with('success', $msg);
|
||
}
|
||
|
||
public function clearSyncErrors(Request $request): RedirectResponse
|
||
{
|
||
$this->ensurePlatformAdmin($request);
|
||
|
||
// 支持两种模式:
|
||
// - scope=all(默认):清理所有订单的失败标记
|
||
// - scope=filtered:仅清理当前筛选结果命中的订单(更安全)
|
||
$scope = (string) $request->input('scope', 'all');
|
||
|
||
$filters = [
|
||
'status' => trim((string) $request->input('status', '')),
|
||
'payment_status' => trim((string) $request->input('payment_status', '')),
|
||
'merchant_id' => trim((string) $request->input('merchant_id', '')),
|
||
'plan_id' => trim((string) $request->input('plan_id', '')),
|
||
'fail_only' => (string) $request->input('fail_only', ''),
|
||
'synced_only' => (string) $request->input('synced_only', ''),
|
||
'sync_status' => trim((string) $request->input('sync_status', '')),
|
||
'keyword' => trim((string) $request->input('keyword', '')),
|
||
'syncable_only' => (string) $request->input('syncable_only', ''),
|
||
'batch_synced_24h' => (string) $request->input('batch_synced_24h', ''),
|
||
];
|
||
|
||
$query = PlatformOrder::query()
|
||
->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation_error.message') IS NOT NULL");
|
||
|
||
if ($scope === 'filtered') {
|
||
$query = $this->applyFilters($query, $filters);
|
||
}
|
||
|
||
$orders = $query->get(['id', 'meta']);
|
||
$matched = $orders->count();
|
||
|
||
$cleared = 0;
|
||
foreach ($orders as $order) {
|
||
$meta = (array) ($order->meta ?? []);
|
||
if (! data_get($meta, 'subscription_activation_error')) {
|
||
continue;
|
||
}
|
||
|
||
data_forget($meta, 'subscription_activation_error');
|
||
|
||
// 轻量审计:记录清理动作(不做独立表,先落 meta,便于排查)
|
||
$audit = (array) (data_get($meta, 'audit', []) ?? []);
|
||
$audit[] = [
|
||
'action' => 'clear_sync_error',
|
||
'scope' => $scope,
|
||
'at' => now()->toDateTimeString(),
|
||
'admin_id' => $this->platformAdminId($request),
|
||
];
|
||
data_set($meta, 'audit', $audit);
|
||
|
||
$order->meta = $meta;
|
||
$order->save();
|
||
$cleared++;
|
||
}
|
||
|
||
$msg = $scope === 'filtered'
|
||
? '已清除当前筛选范围内的同步失败标记:'
|
||
: '已清除全部订单的同步失败标记:';
|
||
|
||
return redirect()->back()->with('success', $msg . $cleared . ' 条(命中 ' . $matched . ' 条)');
|
||
}
|
||
|
||
protected function applyFilters(Builder $query, array $filters): Builder
|
||
{
|
||
return $query
|
||
->when($filters['status'] !== '', fn (Builder $builder) => $builder->where('status', $filters['status']))
|
||
->when($filters['payment_status'] !== '', fn (Builder $builder) => $builder->where('payment_status', $filters['payment_status']))
|
||
->when(($filters['merchant_id'] ?? '') !== '', fn (Builder $builder) => $builder->where('merchant_id', (int) $filters['merchant_id']))
|
||
->when(($filters['plan_id'] ?? '') !== '', fn (Builder $builder) => $builder->where('plan_id', (int) $filters['plan_id']))
|
||
->when(($filters['fail_only'] ?? '') !== '', function (Builder $builder) {
|
||
// 只看同步失败:meta.subscription_activation_error.message 存在即视为失败
|
||
$builder->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation_error.message') IS NOT NULL");
|
||
})
|
||
->when(($filters['synced_only'] ?? '') !== '', function (Builder $builder) {
|
||
// 只看已同步:meta.subscription_activation.subscription_id 存在即视为已同步
|
||
$builder->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation.subscription_id') IS NOT NULL");
|
||
})
|
||
->when(($filters['sync_status'] ?? '') !== '', function (Builder $builder) use ($filters) {
|
||
// 同步状态筛选:unsynced / synced / failed
|
||
if (($filters['sync_status'] ?? '') === 'synced') {
|
||
$builder->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation.subscription_id') IS NOT NULL")
|
||
->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation_error.message') IS NULL");
|
||
} elseif (($filters['sync_status'] ?? '') === 'failed') {
|
||
$builder->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation_error.message') IS NOT NULL");
|
||
} elseif (($filters['sync_status'] ?? '') === 'unsynced') {
|
||
$builder->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation.subscription_id') IS NULL")
|
||
->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation_error.message') IS NULL");
|
||
}
|
||
})
|
||
->when(($filters['keyword'] ?? '') !== '', function (Builder $builder) use ($filters) {
|
||
// 关键词搜索:订单号 / 站点名称 / 订阅号
|
||
$kw = trim((string) ($filters['keyword'] ?? ''));
|
||
if ($kw === '') {
|
||
return;
|
||
}
|
||
|
||
$builder->where(function (Builder $q) use ($kw) {
|
||
$q->where('order_no', 'like', '%' . $kw . '%')
|
||
->orWhere('order_type', 'like', '%' . $kw . '%')
|
||
->orWhere('plan_name', 'like', '%' . $kw . '%')
|
||
->orWhereHas('merchant', function (Builder $mq) use ($kw) {
|
||
$mq->where('name', 'like', '%' . $kw . '%')
|
||
->orWhere('slug', 'like', '%' . $kw . '%');
|
||
})
|
||
->orWhereHas('siteSubscription', function (Builder $sq) use ($kw) {
|
||
$sq->where('subscription_no', 'like', '%' . $kw . '%');
|
||
});
|
||
|
||
if (ctype_digit($kw)) {
|
||
$q->orWhere('id', (int) $kw);
|
||
}
|
||
});
|
||
})
|
||
->when(($filters['syncable_only'] ?? '') !== '', function (Builder $builder) {
|
||
// 只看可同步:已支付 + 已生效 + 尚未写入 subscription_activation.subscription_id
|
||
$builder->where('payment_status', 'paid')
|
||
->where('status', 'activated')
|
||
->whereRaw("JSON_EXTRACT(meta, '$.subscription_activation.subscription_id') IS NULL");
|
||
})
|
||
->when(($filters['batch_synced_24h'] ?? '') !== '', function (Builder $builder) {
|
||
// 只看最近 24 小时批量同步过的订单(基于 meta.batch_activation.at)
|
||
$since = now()->subHours(24)->format('Y-m-d H:i:s');
|
||
|
||
$builder->whereRaw("JSON_EXTRACT(meta, '$.batch_activation.at') IS NOT NULL");
|
||
|
||
// sqlite 测试库没有 JSON_UNQUOTE(),需要做兼容
|
||
$driver = $builder->getQuery()->getConnection()->getDriverName();
|
||
if ($driver === 'sqlite') {
|
||
$builder->whereRaw("JSON_EXTRACT(meta, '$.batch_activation.at') >= ?", [$since]);
|
||
} else {
|
||
$builder->whereRaw("JSON_UNQUOTE(JSON_EXTRACT(meta, '$.batch_activation.at')) >= ?", [$since]);
|
||
}
|
||
});
|
||
}
|
||
|
||
protected function statusLabels(): array
|
||
{
|
||
return [
|
||
'pending' => '待处理',
|
||
'paid' => '已支付',
|
||
'activated' => '已生效',
|
||
'cancelled' => '已取消',
|
||
'refunded' => '已退款',
|
||
];
|
||
}
|
||
|
||
protected function paymentStatusLabels(): array
|
||
{
|
||
return [
|
||
'unpaid' => '未支付',
|
||
'paid' => '已支付',
|
||
'partially_refunded' => '部分退款',
|
||
'refunded' => '已退款',
|
||
'failed' => '支付失败',
|
||
];
|
||
}
|
||
}
|