| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- use App\Services\LotteryOdds;
- use Illuminate\Database\Migrations\Migration;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- return new class extends Migration
- {
- public function up(): void
- {
- if (!Schema::hasTable('lottery_odds_templates')) {
- Schema::create('lottery_odds_templates', function (Blueprint $table) {
- $table->id();
- $table->string('game_key', 16);
- $table->json('names');
- $table->boolean('enabled')->default(false);
- $table->boolean('is_default')->nullable();
- $table->unsignedInteger('revision')->default(1);
- $table->unique(['game_key', 'is_default']);
- });
- }
- if (!Schema::hasTable('lottery_odds_items')) {
- Schema::create('lottery_odds_items', function (Blueprint $table) {
- $table->unsignedBigInteger('template_id');
- $table->unsignedBigInteger('item_id');
- $table->string('odds', 13)->nullable();
- $table->bigInteger('mininum')->nullable();
- $table->bigInteger('maxinum')->nullable();
- $table->primary(['template_id', 'item_id']);
- });
- }
- DB::transaction(function () {
- foreach (LotteryOdds::gameKeys() as $key) {
- if (DB::table('lottery_odds_templates')->where('game_key', $key)->where('is_default', 1)->exists()) {
- continue;
- }
- $id = DB::table('lottery_odds_templates')->insertGetId([
- 'game_key' => $key,
- 'names' => json_encode(['zh' => '默认', 'en' => 'Default', 'vi' => 'Mặc định'], JSON_UNESCAPED_UNICODE),
- 'enabled' => true,
- 'is_default' => true,
- ]);
- foreach (LotteryOdds::catalog($key)->get(['id', 'odds', 'mininum', 'maxinum'])->chunk(200) as $rows) {
- DB::table('lottery_odds_items')->insert($rows->map(fn ($row) => [
- 'template_id' => $id, 'item_id' => $row->id, 'odds' => $row->odds,
- 'mininum' => $row->mininum, 'maxinum' => $row->maxinum,
- ])->all());
- }
- }
- });
- if (!Schema::hasColumn('lhc_order', 'odds_template_id')) {
- Schema::table('lhc_order', function (Blueprint $table) {
- $table->unsignedBigInteger('odds_template_id')->nullable();
- $table->unsignedInteger('odds_template_revision')->nullable();
- });
- }
- }
- public function down(): void
- {
- Schema::table('lhc_order', function (Blueprint $table) {
- $table->dropColumn(['odds_template_id', 'odds_template_revision']);
- });
- Schema::dropIfExists('lottery_odds_items');
- Schema::dropIfExists('lottery_odds_templates');
- }
- };
|