<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSplitBandSyn extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'table:band_syns';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $month = date('m');
        $year = date("Y");
        if ($month == 12) {//下一年
            $year += 1;
            $month = 1;
        }else {
            $month += 1;
        }
        $month = str_pad($month, 2, '0', STR_PAD_LEFT);
        $table_name = "band_syns_{$year}{$month}";
        if (!Schema::hasTable($table_name)) {
            Schema::create($table_name, function (Blueprint $table) {
                $table->id();
                $table->integer('user_id')->unsigned()->comment("用户id");
                $table->integer('band_id')->unsigned()->comment("手环id");
                $table->timestamp('date')->nullable()->comment("时间");
                $table->integer('step')->unsigned()->comment("步数");
                $table->string('distance',10)->comment("距离");
                $table->string('calories', 10)->comment("卡路里");
                $table->string('steps')->comment("步频");
                $table->timestamps();
                $table->unique(['user_id', 'band_id', 'date']);
            });
        }
        return Command::SUCCESS;
    }
}
