<?php

namespace App\Console\Commands;

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

class CreateSplitAccessLog extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'table:access_logs'; //quarter季度

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '按周期生成日志数据库';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $month = date('m');
        $year = date("Y");
//        $month = 7;
        if ($month >= 10) {//下一年
            $year = $year + 1;
        }
        $quarter = CommonService::getQuarter($month);
        if ($quarter == 4) {//下一个周期季度
            $quarter = 1;
        }else {
            $quarter = $quarter + 1;
        }
        $table_name = "access_logs_{$year}{$quarter}";
        if (!Schema::hasTable($table_name)) {
            Schema::create($table_name, function (Blueprint $table) {
                $table->id();
                $table->integer('user_id')->nullable();
                $table->string('method')->nullable();
                $table->string('route')->nullable();
                $table->enum('channel', ['其他', 'H5', 'Admin', '小程序', 'APP'])->nullable()->comment("渠道");
                $table->string('time')->nullable();
                $table->string('client_ip')->nullable();
                $table->longText('params')->nullable();
                $table->text('user_agent')->nullable();
                $table->text("headers")->nullable();
                $table->timestamps();
                $table->index('user_id');
                $table->index('created_at');
            });
        }


        return Command::SUCCESS;
    }



}
