<?php

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

class CreateOperatorLogsTableAndAddAuthorityAndChangeProcessesColumnStatus extends Migration
{
    protected $authNames=[
        '教程',
        '二次加工管理-审核',
        '二次加工管理-质量验收',
        '二次加工管理-交接完成',
        '二次加工管理-修改价格',
        '二次加工管理-组长确认',
        '二次加工管理-财务确认',
    ];
    protected $authNamesDel=[
        '二次加工管理-验收完成'
    ];
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('operator_logs', function (Blueprint $table) {
            $table->id();
            $table->string('operator_logable_type')->index()->comment('类型(表名)');
            $table->bigInteger('operator_logable_id')->index()->comment('ID');
            $table->bigInteger('user_id')->index()->comment('用户');
            $table->string('operation')->nullable()->comment('操作');
            $table->timestamp('created_at');
        });
        foreach ($this->authNames as $name){
            \App\Authority::create(['name'=>$name,'alias_name'=>$name]);
        }
        foreach ($this->authNamesDel as $name){
            \App\Authority::where(['name'=>$name,'alias_name'=>$name])->delete();
        }
        Schema::table('processes',function (Blueprint $table){
            $table->dropColumn('status');
        });
        Schema::table('processes',function (Blueprint $table){
            $table->enum('status',['待审核','已驳回','待接单','待加工','加工中','待验收','待交接','交接完成'])->default('待审核')->comment('状态');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('operator_logs');
        foreach ($this->authNames as $name){
            \App\Authority::where(['name'=>$name,'alias_name'=>$name])->delete();
        }
        foreach ($this->authNamesDel as $name){
            \App\Authority::create(['name'=>$name,'alias_name'=>$name]);
        }
        Schema::table('processes',function (Blueprint $table){
            $table->dropColumn('status');
        });
        Schema::table('processes',function (Blueprint $table){
            $table->enum('status',['待接单','待加工','驳回','加工中','待验收','已完成'])->comment('状态');
        });
    }
}
