<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFileTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('files')) {
            // 文件表
            Schema::create('files', function (Blueprint $table) {
                $table->bigInteger('id')->unsigned()->default(0)->comment('附件全局唯一ID');
                $table->string('hash', 150)->comment('文件 hash');
                $table->string('disk', 16)->default('')->comment('盘符');
                $table->string('filename')->default('')->comment('文件名');
                $table->string('origin_filename')->nullbale()->default(null)->comment('原始文件名');
                $table->string('mimetype', 32)->default('')->comment('mime type');
                $table->integer('filesize')->unsigned()->default(0)->comment('文件大小');
                $table->integer('width')->unsigned()->default(0)->comment('宽度');
                $table->integer('height')->unsigned()->default(0)->comment('高度');
                $table->decimal('duration', 10, 6)->default(0.000000)->comment('持续时间');
                $table->tinyInteger('thumb')->unsigned()->default(0)->comment('是否有缩略图 0:无 1:有');
                $table->timestamps();

                $table->primary('id');

                $table->comment('文件表');
            });
        }

        if (!Schema::hasTable('file_withs')) {
            // 文件关联表
            Schema::create('file_withs', function (Blueprint $table) {
                $table->increments('id');
                $table->bigInteger('file_id')->unsigned()->default(0)->comment('文件ID');
                $table->integer('account_id')->unsigned()->default(0)->comment('账号ID');
                $table->string('type_id', 100)->default('')->comment('类型ID');
                $table->string('type', 100)->default('')->comment('记录类型');
                $table->timestamps();
                $table->softDeletes();

                $table->index('file_id');
                $table->index('account_id');
                $table->index(['type', 'type_id']);

                $table->comment('文件关联表');
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('file_withs');
        Schema::dropIfExists('files');
    }
}
