2026_10_28_140000_create_recharge_bonuses.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Schema;
  5. use Illuminate\Support\Facades\DB;
  6. return new class extends Migration {
  7. public function up(): void
  8. {
  9. Schema::create('recharge_bonuses', function (Blueprint $t) {
  10. $t->string('channel_type', 100)->primary(); $t->decimal('bonus_percent', 5, 2)->default(0);
  11. $t->json('translations'); $t->boolean('enabled')->default(false); $t->timestamps();
  12. });
  13. Schema::create('recharge_bonus_rewards', function (Blueprint $t) {
  14. $t->id(); $t->string('source_type', 24); $t->unsignedBigInteger('source_id'); $t->string('dedup_key', 64)->unique();
  15. $t->string('member_id', 100); $t->string('channel_type', 100); $t->unsignedBigInteger('principal_log_id');
  16. $t->decimal('principal_amount', 24, 10); $t->decimal('bonus_percent', 5, 2); $t->decimal('amount', 24, 2);
  17. $t->unsignedBigInteger('credited_log_id')->nullable(); $t->json('rule_snapshot'); $t->timestamps();
  18. $t->unique(['source_type', 'source_id'], 'recharge_bonus_source');
  19. });
  20. Schema::create('recharge_bonus_chain_receipts', function (Blueprint $t) {
  21. $t->string('dedup_key', 64)->primary(); $t->unsignedBigInteger('recharge_id')->nullable();
  22. $t->unsignedBigInteger('credited_log_id')->nullable(); $t->timestamps();
  23. });
  24. foreach (['payment_orders', 'recharges'] as $table) {
  25. Schema::table($table, function (Blueprint $t) { $t->json('recharge_bonus_snapshot')->nullable(); });
  26. }
  27. }
  28. public function down(): void
  29. {
  30. if (DB::table('recharge_bonus_rewards')->exists() || DB::table('recharge_bonus_chain_receipts')->exists()
  31. || DB::table('payment_orders')->whereNotNull('recharge_bonus_snapshot')->exists()
  32. || DB::table('recharges')->whereNotNull('recharge_bonus_snapshot')->exists()) {
  33. throw new \RuntimeException('已有充值赠送快照或记录,不能回滚此迁移');
  34. }
  35. foreach (['payment_orders', 'recharges'] as $table) Schema::table($table, function (Blueprint $t) { $t->dropColumn('recharge_bonus_snapshot'); });
  36. foreach (['recharge_bonus_rewards', 'recharge_bonus_chain_receipts', 'recharge_bonuses'] as $table) Schema::dropIfExists($table);
  37. }
  38. };