<?php

namespace App\Modules\Book\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class NovelUserPorperty extends Model
{
    use TableSuffix;

    protected  $table = 'novel_user_properties';

    protected $fillable = [
        'uid',
        'category_id',
        'category',
        'sex',
    ];


    public static function model(string $date)
    {
        self::suffix($date);
        $model = new self;
        return $model;
    }

    /**
     * 创建表
     */
    public function createTableIfNotExists()
    {
        if (!Schema::hasTable($this->table)) {
            Schema::create($this->table, function (Blueprint $table) {
                $table->increments('id');
                $table->integer('uid');
                $table->integer('category_id');
                $table->string('category');
                $table->integer('sex');
                $table->timestamps();
                $table->index('uid', 'idx_uid');
            });
        }
    }

    /**
     * 删除三天前的表
     */
    public static function dropTable()
    {
        $date = date('Ymd', strtotime('-3 days'));
        Schema::dropIfExists('novel_user_properties' . $date);
    }
}
