<?php

use App\Station;
use App\StationType;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('stations', function (Blueprint $table) {
            $table->id();
            $table->integer('parent_id')->index()->nullable();
            $table->string('name')->index()->nullable();
            $table->string('code')->unique()->nullable()->comment('机器编码');
            $table->integer('station_type_id')->index();
            $table->integer('sequence')->index()->nullable();
            $table->timestamps();
        });
        $stationType= StationType::query()->firstOrCreate(['name'=>'料箱出货口']);
        $station料箱监视器01= StationType::query()->firstOrCreate(['name'=>'料箱监视器01']);
        $station= Station::query()->firstOrCreate(['name'=>'U型线入货口01'
            ,'code'=>'BIN-OUT1'
            ,'station_type_id'=>$stationType['id']
            ,'parent_id'=>$station料箱监视器01['id']
        ]);
        $station['sequence']=1;
        $station['station_type_id']=$stationType['id'];
        $station->save();
    }

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