<?php

use App\Authority;
use App\Unit;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class ChangeAuthHrVerifies extends Migration
{
    protected $addNames=[
        '人事管理-组长审核',
    ];
    protected $changeNames=[
        ['人事管理-审核','人事管理-门卫审核'],
    ];
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Authority::where('name','人事管理-门卫审核')->first();
        foreach ($this->addNames as $namePack){
            if(!Authority::where('name',$namePack)->first())(new Authority(['name'=>$namePack,'alias_name'=>$namePack]))->save();
        }
        //修改或没有则添加
        foreach ($this->changeNames as $namePack){
            $authority=Authority::where('name',$namePack[0])->orWhere('name',$namePack[1])->first();
            if(!$authority){
                (new Authority(['name'=>$namePack[1],'alias_name'=>$namePack[1]]))->save();
            }elseif($authority['name']==$namePack[0]){
                $authority['name']=$namePack[1];
                $authority['alias_name']=$namePack[1];
                $authority->save();
            }
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        foreach ($this->addNames as $name){
            Authority::where('name',$name)->delete();
        }
        //改回旧的
        foreach ($this->changeNames as $namePack){
            $authority=Authority::where('name',$namePack[1])->first();
            $authority['name']=$namePack[0];
            $authority->save();
        }
    }
}
