2026_11_03_160000_create_reward_rules.php 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /** Per-category rebate/backflow tiers replace the three global config values. */
  7. return new class extends Migration {
  8. private const FIELDS = ['rebate', 'huishui_percentage', 'benefits_bibi_return'];
  9. public function up(): void
  10. {
  11. Schema::create('rebate_rules', function (Blueprint $t) {
  12. $t->engine = 'InnoDB';
  13. $t->id();
  14. $t->string('category', 32)->comment('游戏分类,与分享返佣分类标识一致');
  15. $t->integer('level')->comment('会员等级 level.level');
  16. $t->decimal('min_effective_amount', 20, 2)->default(0)->comment('有效投注门槛');
  17. $t->decimal('percentage', 8, 4)->default(0)->comment('返还百分比,0.8 表示 0.8%');
  18. $t->decimal('turnover_multiple', 8, 2)->default(0)->comment('打码量倍数,0 表示无');
  19. $t->timestamps();
  20. $t->unique(['category', 'level', 'min_effective_amount'], 'rebate_rules_tier_unique');
  21. });
  22. Schema::create('backflow_rules', function (Blueprint $t) {
  23. $t->engine = 'InnoDB';
  24. $t->id();
  25. $t->string('category', 32)->comment('游戏分类,与分享返佣分类标识一致');
  26. $t->decimal('min_loss_amount', 20, 2)->default(0)->comment('亏损金额门槛');
  27. $t->decimal('reward_amount', 20, 2)->default(0)->comment('奖励金额');
  28. $t->decimal('turnover_multiple', 8, 2)->default(0)->comment('打码量倍数,0 表示无');
  29. $t->timestamps();
  30. $t->unique(['category', 'min_loss_amount'], 'backflow_rules_tier_unique');
  31. });
  32. Schema::table('rebates', function (Blueprint $t) {
  33. $t->json('details')->nullable()->comment('分类明细');
  34. $t->decimal('need_flow', 20, 2)->default(0)->comment('需要打码量');
  35. });
  36. Schema::table('rebates', function (Blueprint $t) {
  37. $t->decimal('rebate_ratio', 12, 6)->nullable()->comment('综合返还比例')->change();
  38. });
  39. Schema::table('backflow', function (Blueprint $t) {
  40. $t->decimal('loss_amount', 20, 2)->default(0)->comment('分类净亏损合计');
  41. $t->json('details')->nullable()->comment('分类明细');
  42. $t->decimal('need_flow', 20, 2)->default(0)->comment('需要打码量');
  43. });
  44. if (Schema::hasTable('share_bet_facts')) {
  45. Schema::table('share_bet_facts', function (Blueprint $t) {
  46. $t->index('updated_at', 'share_bet_facts_updated_at_index');
  47. });
  48. }
  49. // Keep the old values in the down() path, so rollback restores what operators had.
  50. $old = DB::table('config')->whereIn('field', self::FIELDS)->get()->map(fn ($row) => (array) $row)->all();
  51. Schema::create('reward_rule_config_backup', function (Blueprint $t) {
  52. $t->string('field', 64)->primary(); $t->json('row');
  53. });
  54. foreach ($old as $row) {
  55. DB::table('reward_rule_config_backup')->insert(['field' => $row['field'], 'row' => json_encode($row, JSON_UNESCAPED_UNICODE)]);
  56. }
  57. DB::table('config')->whereIn('field', self::FIELDS)->delete();
  58. }
  59. public function down(): void
  60. {
  61. if (Schema::hasTable('reward_rule_config_backup')) {
  62. foreach (DB::table('reward_rule_config_backup')->get() as $backup) {
  63. $row = json_decode($backup->row, true);
  64. if (!DB::table('config')->where('field', $row['field'])->exists()) DB::table('config')->insert($row);
  65. }
  66. Schema::drop('reward_rule_config_backup');
  67. }
  68. if (Schema::hasTable('share_bet_facts')) {
  69. Schema::table('share_bet_facts', function (Blueprint $t) {
  70. $t->dropIndex('share_bet_facts_updated_at_index');
  71. });
  72. }
  73. Schema::table('backflow', function (Blueprint $t) {
  74. $t->dropColumn(['loss_amount', 'details', 'need_flow']);
  75. });
  76. Schema::table('rebates', function (Blueprint $t) {
  77. $t->dropColumn(['details', 'need_flow']);
  78. });
  79. Schema::dropIfExists('backflow_rules');
  80. Schema::dropIfExists('rebate_rules');
  81. }
  82. };