2026_10_26_140000_create_reporting_tables.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. return new class extends Migration {
  7. public function up(): void
  8. {
  9. Schema::create('reporting_settings', function (Blueprint $t) { $t->unsignedInteger('id')->primary(); $t->decimal('valid_deposit_min', 24, 10)->default(20); $t->dateTime('activity_started_at'); $t->timestamps(); });
  10. $now = now('Asia/Shanghai')->format('Y-m-d H:i:s');
  11. DB::table('reporting_settings')->insert(['id' => 1, 'valid_deposit_min' => '20', 'activity_started_at' => $now, 'created_at' => $now, 'updated_at' => $now]);
  12. Schema::create('reporting_contexts', function (Blueprint $t) {
  13. $t->id(); $t->string('source', 40); $t->string('source_id', 80); $t->unique(['source', 'source_id']);
  14. $t->unsignedBigInteger('user_id')->nullable(); $t->unsignedBigInteger('agent_id')->nullable(); $t->unsignedBigInteger('inviter_id')->nullable();
  15. $t->string('client_platform', 8)->default('unknown'); $t->string('attribution_quality', 32)->default('event_snapshot'); $t->boolean('settlement_verified')->default(false); $t->dateTime('occurred_at'); $t->dateTime('succeeded_at')->nullable();
  16. });
  17. Schema::create('reporting_orders', function (Blueprint $t) {
  18. $t->id(); $t->string('source', 40); $t->string('source_id', 80); $t->unique(['source', 'source_id']);
  19. $t->string('parent_source', 40); $t->string('parent_id', 80); $t->unsignedBigInteger('user_id')->index();
  20. $t->unsignedBigInteger('agent_id')->nullable()->index(); $t->unsignedBigInteger('inviter_id')->nullable()->index();
  21. $t->string('attribution_quality', 32); $t->string('category', 32); $t->string('client_platform', 8)->default('unknown'); $t->string('currency', 8);
  22. $t->dateTime('bet_at')->index(); $t->dateTime('settled_at')->nullable()->index(); $t->dateTime('settlement_candidate_at')->nullable();
  23. foreach (['bet_amount', 'payout_amount', 'net_profit', 'refund_amount'] as $f) $t->decimal($f, 24, 10)->default(0);
  24. $t->boolean('settled'); $t->boolean('voided'); $t->string('time_quality', 24); $t->timestamps();
  25. });
  26. Schema::create('reporting_events', function (Blueprint $t) {
  27. $t->id(); $t->string('source', 40); $t->string('source_id', 80); $t->string('kind', 32); $t->unique(['source', 'source_id', 'kind']);
  28. $t->unsignedBigInteger('user_id')->nullable()->index(); $t->unsignedBigInteger('agent_id')->nullable()->index(); $t->unsignedBigInteger('inviter_id')->nullable()->index();
  29. $t->string('attribution_quality', 32); $t->string('category', 32)->nullable(); $t->string('client_platform', 8)->default('unknown');
  30. $t->string('currency', 8); $t->decimal('amount', 24, 10)->nullable(); $t->decimal('customer_fee', 24, 10)->default(0); $t->decimal('configured_fee', 24, 10)->nullable();
  31. $t->dateTime('occurred_at')->nullable()->index(); $t->dateTime('candidate_at')->index(); $t->timestamps();
  32. });
  33. Schema::create('reporting_reconciliations', function (Blueprint $t) {
  34. $t->id(); $t->unsignedBigInteger('event_id')->unique(); $t->decimal('actual_amount', 24, 10); $t->decimal('external_fee', 24, 10);
  35. $t->dateTime('occurred_at'); $t->string('currency', 8); $t->string('evidence', 1000); $t->unsignedBigInteger('operator_id'); $t->timestamps();
  36. });
  37. Schema::create('reporting_activity', function (Blueprint $t) {
  38. $t->id(); $t->unsignedBigInteger('user_id'); $t->unsignedBigInteger('agent_id')->nullable(); $t->unsignedBigInteger('inviter_id')->nullable();
  39. $t->string('client_platform', 8); $t->string('session_hash', 64); $t->dateTime('observed_at')->index();
  40. $t->unique(['user_id', 'session_hash', 'observed_at'], 'reporting_activity_dedup');
  41. });
  42. Schema::create('reporting_sync_state', function (Blueprint $t) { $t->string('source', 40)->primary(); $t->text('state'); $t->dateTime('updated_at'); });
  43. Schema::create('reporting_errors', function (Blueprint $t) {
  44. $t->id(); $t->string('source', 40); $t->string('source_id', 80); $t->unique(['source', 'source_id']);
  45. $t->dateTime('candidate_at')->nullable(); $t->string('reason', 500); $t->timestamps();
  46. });
  47. }
  48. public function down(): void
  49. {
  50. foreach (['reporting_errors', 'reporting_sync_state', 'reporting_activity', 'reporting_reconciliations', 'reporting_events', 'reporting_orders', 'reporting_contexts', 'reporting_settings'] as $table) Schema::dropIfExists($table);
  51. }
  52. };