| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace App\Console\Commands;
- use App\Models\BalanceLog;
- use App\Services\ThirdGameBalanceService;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\File;
- class ThirdGameBalanceUsers extends Command
- {
- protected $signature = 'third-game:balance-users
- {--from= : 开始时间,例如 2026-07-14 10:00:00}
- {--to= : 结束时间,例如 2026-07-14 23:59:59}
- {--member_id=* : 只查询指定 member_id,可重复传入}
- {--csv= : CSV 输出路径,默认保存到 storage/app}
- {--show=100 : 控制台最多展示条数}';
- protected $description = '导出有三方游戏转入、失败退回或转出流水的玩家(只读,不回收余额)';
- public function handle(ThirdGameBalanceService $service): int
- {
- try {
- $from = $this->parseTimeOption('from');
- $to = $this->parseTimeOption('to');
- } catch (\InvalidArgumentException $e) {
- $this->error($e->getMessage());
- return self::FAILURE;
- }
- if ($from !== null && $to !== null && $from->greaterThan($to)) {
- $this->error('--from 不能晚于 --to');
- return self::FAILURE;
- }
- $transferInType = '三方游戏转入';
- $transferOutType = '三方游戏转出';
- $query = BalanceLog::query()
- ->whereNotNull('member_id')
- ->where('member_id', '<>', '')
- ->whereIn('change_type', [$transferInType, $transferOutType])
- ->select('member_id')
- ->selectRaw(
- 'SUM(CASE WHEN change_type = ? AND amount < 0 THEN 1 ELSE 0 END) AS transfer_in_count',
- [$transferInType]
- )
- ->selectRaw(
- 'SUM(CASE WHEN change_type = ? AND amount < 0 THEN -amount ELSE 0 END) AS transfer_in_total',
- [$transferInType]
- )
- ->selectRaw(
- 'SUM(CASE WHEN change_type = ? AND amount > 0 THEN 1 ELSE 0 END) AS refund_count',
- [$transferInType]
- )
- ->selectRaw(
- 'SUM(CASE WHEN change_type = ? AND amount > 0 THEN amount ELSE 0 END) AS refund_total',
- [$transferInType]
- )
- ->selectRaw(
- 'SUM(CASE WHEN change_type = ? AND amount > 0 THEN 1 ELSE 0 END) AS transfer_out_count',
- [$transferOutType]
- )
- ->selectRaw(
- 'SUM(CASE WHEN change_type = ? AND amount > 0 THEN amount ELSE 0 END) AS transfer_out_total',
- [$transferOutType]
- )
- ->selectRaw('MIN(created_at) AS first_at, MAX(created_at) AS last_at');
- if ($from !== null) {
- $query->where('created_at', '>=', $from->format('Y-m-d H:i:s'));
- }
- if ($to !== null) {
- $query->where('created_at', '<=', $to->format('Y-m-d H:i:s'));
- }
- $memberIds = array_values(array_filter(array_map('strval', (array) $this->option('member_id'))));
- if ($memberIds !== []) {
- $query->whereIn('member_id', $memberIds);
- }
- $rows = $query->groupBy('member_id')->orderBy('member_id')->get();
- $report = [];
- foreach ($rows as $row) {
- $transferInCount = (int) $row->transfer_in_count;
- $refundCount = (int) $row->refund_count;
- $transferOutCount = (int) $row->transfer_out_count;
- $flowTypes = [];
- if ($transferInCount > 0) {
- $flowTypes[] = '转入';
- }
- if ($refundCount > 0) {
- $flowTypes[] = '失败退款';
- }
- if ($transferOutCount > 0) {
- $flowTypes[] = '转出';
- }
- $report[] = [
- 'member_id' => (string) $row->member_id,
- 'player_id' => $service->playerId((string) $row->member_id),
- 'flow_type' => implode('+', $flowTypes),
- 'transfer_in_count' => $transferInCount,
- 'transfer_in_total' => $this->decimal($row->transfer_in_total),
- 'refund_count' => $refundCount,
- 'refund_total' => $this->decimal($row->refund_total),
- 'transfer_out_count' => $transferOutCount,
- 'transfer_out_total' => $this->decimal($row->transfer_out_total),
- 'first_at' => (string) $row->first_at,
- 'last_at' => (string) $row->last_at,
- ];
- }
- $csvPath = (string) $this->option('csv');
- if ($csvPath === '') {
- $csvPath = storage_path('app/third_game_balance_users_' . now()->format('Ymd_His') . '.csv');
- }
- if (!$this->writeCsv($csvPath, $report)) {
- return self::FAILURE;
- }
- $show = max(0, (int) $this->option('show'));
- if ($show > 0 && $report !== []) {
- $this->table(array_keys($report[0]), array_slice($report, 0, $show));
- }
- $this->info('共找到 ' . count($report) . ' 个玩家');
- $this->info('CSV:' . $csvPath);
- $this->comment('本命令只读:未请求三方接口,未修改钱包或流水。');
- return self::SUCCESS;
- }
- private function parseTimeOption(string $name): ?Carbon
- {
- $value = trim((string) $this->option($name));
- if ($value === '') {
- return null;
- }
- try {
- return Carbon::createFromFormat('Y-m-d H:i:s', $value);
- } catch (\Throwable $e) {
- throw new \InvalidArgumentException('--' . $name . ' 格式必须为 YYYY-MM-DD HH:MM:SS');
- }
- }
- private function decimal($value): string
- {
- return bcadd(is_numeric($value) ? (string) $value : '0', '0', 10);
- }
- private function writeCsv(string $path, array $rows): bool
- {
- try {
- File::ensureDirectoryExists(dirname($path));
- $handle = fopen($path, 'wb');
- if ($handle === false) {
- throw new \RuntimeException('无法创建 CSV');
- }
- fwrite($handle, "\xEF\xBB\xBF");
- $headers = $rows !== [] ? array_keys($rows[0]) : [
- 'member_id', 'player_id', 'flow_type', 'transfer_in_count', 'transfer_in_total',
- 'refund_count', 'refund_total', 'transfer_out_count', 'transfer_out_total',
- 'first_at', 'last_at',
- ];
- fputcsv($handle, $headers);
- foreach ($rows as $row) {
- fputcsv($handle, $row);
- }
- fclose($handle);
- return true;
- } catch (\Throwable $e) {
- if (isset($handle) && is_resource($handle)) {
- fclose($handle);
- }
- $this->error('写入 CSV 失败:' . $e->getMessage());
- return false;
- }
- }
- }
|