<?php

namespace App\Console\Commands;

use App\Modules\User\Models\QappUser;
use Illuminate\Console\Command;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class NewVersionPrepare extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'new_viersion_prepare';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '新版本上线准备';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        if (!Schema::hasColumn('qapp_users', 'channel_id')) {
            Schema::table('qapp_users', function (Blueprint $table) {
                $table->unsignedInteger('channel_id');
                $table->dropIndex('device_no');
                $table->index(['device_no', 'channel_id'], 'idx_device_no');
            });
            DB::table('qapp_users')->update(['channel_id' => env('QUICKAPP_SITE')]);
        }
        if (!Schema::hasTable('qapp_package_info')) {
            Schema::create('qapp_package_info', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('channel_id');
                $table->string('package', 255);
                $table->string('company', 255);
                $table->integer('app_pay_merchat_id');
                $table->integer('h5_pay_merchat_id');
                $table->integer('ali_pay_merchat_id');
                $table->timestamps();
            });
        }
    }
}
