<?php

use Illuminate\Database\Migrations\Migration;
use App\Station;
use App\StationType;
use App\Authority;

class AddStationTypeAndAddStation extends Migration
{

    public $stationTypeNames = ['缓存架'];

    public $stationNames = ['缓存架01', '缓存架02'];

    public $authorityName = ['站管理-缓存架'];

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        foreach ($this->stationTypeNames as $name) {
            if (StationType::query()->where('name', $name)->exists()) continue;
            StationType::query()->create(['name' => $name]);
        }

        $stationType = StationType::query()->where('name', '缓存架')->first();
        foreach ($this->stationNames as $stationName) {
            if (Station::query()->where('name', $stationName)->exists()) continue;
            Station::query()->create([
                'name' => $stationName,
                'code' => $stationName,
                'station_type_id' => $stationType['id'],
            ]);
        }

        foreach ($this->authorityName as $name) {
            if(Authority::query()->where('name',$name)->exists())continue;
            Authority::query()->create(['name'=>$name,
                'alias_name'=>$name]);
        }

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        foreach ($this->stationNames as $stationName) {
            if (!Station::query()->where('name', $stationName)->exists()) continue;
            Station::query()->where('name', $stationName)->delete();
        }

        foreach ($this->stationTypeNames as $name) {
            if (!Station::query()->where('name', $name)->exists()) continue;
            Station::query()->where('name', $name)->delete();
        }

        foreach ($this->authorityName as $name) {
            if(!Authority::query()->where('name',$name)->exists())continue;
            Authority::query()->where('name',$name)->delete();
        }
    }
}
