2026_11_11_120000_create_lottery_odds_templates.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. use App\Services\LotteryOdds;
  3. use Illuminate\Database\Migrations\Migration;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Schema;
  7. return new class extends Migration
  8. {
  9. public function up(): void
  10. {
  11. if (!Schema::hasTable('lottery_odds_templates')) {
  12. Schema::create('lottery_odds_templates', function (Blueprint $table) {
  13. $table->id();
  14. $table->string('game_key', 16);
  15. $table->json('names');
  16. $table->boolean('enabled')->default(false);
  17. $table->boolean('is_default')->nullable();
  18. $table->unsignedInteger('revision')->default(1);
  19. $table->unique(['game_key', 'is_default']);
  20. });
  21. }
  22. if (!Schema::hasTable('lottery_odds_items')) {
  23. Schema::create('lottery_odds_items', function (Blueprint $table) {
  24. $table->unsignedBigInteger('template_id');
  25. $table->unsignedBigInteger('item_id');
  26. $table->string('odds', 13)->nullable();
  27. $table->bigInteger('mininum')->nullable();
  28. $table->bigInteger('maxinum')->nullable();
  29. $table->primary(['template_id', 'item_id']);
  30. });
  31. }
  32. DB::transaction(function () {
  33. foreach (LotteryOdds::gameKeys() as $key) {
  34. if (DB::table('lottery_odds_templates')->where('game_key', $key)->where('is_default', 1)->exists()) {
  35. continue;
  36. }
  37. $id = DB::table('lottery_odds_templates')->insertGetId([
  38. 'game_key' => $key,
  39. 'names' => json_encode(['zh' => '默认', 'en' => 'Default', 'vi' => 'Mặc định'], JSON_UNESCAPED_UNICODE),
  40. 'enabled' => true,
  41. 'is_default' => true,
  42. ]);
  43. foreach (LotteryOdds::catalog($key)->get(['id', 'odds', 'mininum', 'maxinum'])->chunk(200) as $rows) {
  44. DB::table('lottery_odds_items')->insert($rows->map(fn ($row) => [
  45. 'template_id' => $id, 'item_id' => $row->id, 'odds' => $row->odds,
  46. 'mininum' => $row->mininum, 'maxinum' => $row->maxinum,
  47. ])->all());
  48. }
  49. }
  50. });
  51. if (!Schema::hasColumn('lhc_order', 'odds_template_id')) {
  52. Schema::table('lhc_order', function (Blueprint $table) {
  53. $table->unsignedBigInteger('odds_template_id')->nullable();
  54. $table->unsignedInteger('odds_template_revision')->nullable();
  55. });
  56. }
  57. }
  58. public function down(): void
  59. {
  60. Schema::table('lhc_order', function (Blueprint $table) {
  61. $table->dropColumn(['odds_template_id', 'odds_template_revision']);
  62. });
  63. Schema::dropIfExists('lottery_odds_items');
  64. Schema::dropIfExists('lottery_odds_templates');
  65. }
  66. };