feat: interept

This commit is contained in:
hashbro
2026-10-13 02:44:36 +08:00
parent bb695c12c4
commit 7291a84951
26 changed files with 1023 additions and 114 deletions
@@ -5,9 +5,12 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Concerns\PortalAware;
use App\Http\Controllers\Controller;
use App\Models\TransferRecord;
use App\Models\WalletAddress;
use App\Support\AgentScope;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class TransferRecordController extends Controller
{
@@ -67,10 +70,75 @@ class TransferRecordController extends Controller
'msg' => '',
'count' => $paginator->total(),
'data' => $data,
'summary' => $this->successSummary($request),
]);
}
private function baseQuery(Request $request): Builder
/**
* Successful totals under the same top filters. Sweep vs fee-topup are split
* by operator (`fee-topup…`); status is always success.
*
* @return array{totals: list<array{asset: string, amount: string}>, fee_totals: list<array{asset: string, amount: string}>}
*/
private function successSummary(Request $request): array
{
$base = $this->baseQuery($request, ignoreStatus: true)
->where('transfer_records.status', TransferRecord::STATUS_SUCCESS);
return [
'totals' => $this->sumByAsset(
(clone $base)->where(function (Builder $q) {
$q->whereNull('transfer_records.operator')
->orWhere('transfer_records.operator', 'not like', 'fee-topup%');
})
),
'fee_totals' => $this->sumByAsset(
(clone $base)->where('transfer_records.operator', 'like', 'fee-topup%')
),
];
}
/**
* @return list<array{asset: string, amount: string}>
*/
private function sumByAsset(Builder $q): array
{
$amountExpr = DB::connection()->getDriverName() === 'sqlite'
? 'CAST(transfer_records.amount AS REAL)'
: 'CAST(transfer_records.amount AS DECIMAL(32, 18))';
$rows = $q->selectRaw('UPPER(transfer_records.asset) as asset, COALESCE(SUM('.$amountExpr.'), 0) as total')
->groupByRaw('UPPER(transfer_records.asset)')
->get();
$byAsset = [];
foreach ($rows as $row) {
$asset = strtoupper(trim((string) $row->asset));
if ($asset === '') {
continue;
}
$formatted = WalletAddress::formatAmount(strtolower($asset), $row->total);
$byAsset[$asset] = $formatted === '' ? '0' : $formatted;
}
$order = ['USDT', 'ETH', 'TRX', 'BNB', 'BTC'];
$totals = [];
foreach ($order as $asset) {
$totals[] = [
'asset' => $asset,
'amount' => $byAsset[$asset] ?? '0',
];
unset($byAsset[$asset]);
}
ksort($byAsset);
foreach ($byAsset as $asset => $amount) {
$totals[] = ['asset' => $asset, 'amount' => $amount];
}
return $totals;
}
private function baseQuery(Request $request, bool $ignoreStatus = false): Builder
{
$q = TransferRecord::query();
$agent = $this->agent();
@@ -95,8 +163,9 @@ class TransferRecordController extends Controller
$txHash = trim((string) $request->query('tx_hash', ''));
$chain = trim((string) $request->query('chain', ''));
$asset = trim((string) $request->query('asset', ''));
$status = trim((string) $request->query('status', ''));
$status = $ignoreStatus ? '' : trim((string) $request->query('status', ''));
$operator = trim((string) $request->query('operator', ''));
[$dateFrom, $dateTo] = $this->dateRange($request);
if ($from !== '') {
$q->where('transfer_records.from_address', 'like', '%'.$from.'%');
@@ -119,7 +188,44 @@ class TransferRecordController extends Controller
if ($operator !== '') {
$q->where('transfer_records.operator', 'like', '%'.$operator.'%');
}
if ($dateFrom !== null) {
$q->where('transfer_records.created_at', '>=', $dateFrom);
}
if ($dateTo !== null) {
$q->where('transfer_records.created_at', '<=', $dateTo);
}
return $q;
}
/**
* @return array{0: ?Carbon, 1: ?Carbon}
*/
private function dateRange(Request $request): array
{
$from = trim((string) $request->query('date_from', ''));
$to = trim((string) $request->query('date_to', ''));
$range = trim((string) $request->query('date_range', ''));
if (($from === '' || $to === '') && $range !== '') {
if (preg_match('/^(\d{4}-\d{2}-\d{2})\s*-\s*(\d{4}-\d{2}-\d{2})$/', $range, $m) === 1) {
$from = $m[1];
$to = $m[2];
}
}
$start = null;
$end = null;
try {
if ($from !== '') {
$start = Carbon::parse($from)->startOfDay();
}
if ($to !== '') {
$end = Carbon::parse($to)->endOfDay();
}
} catch (\Throwable) {
return [null, null];
}
return [$start, $end];
}
}