<?php

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

class ChangeAuthoritiesOrderAssign extends Migration
{

    protected $changeNames=[

        ["指定分配","订单管理-指定分配"],
        ["指定分配-查询","订单管理-指定分配-查询"],
        ["指定分配-编辑","订单管理-指定分配-编辑"],
        ["商品配置","订单管理-指定分配"],
        ["商品配置-查询","订单管理-指定分配-查询"],
        ["商品配置-编辑","订单管理-指定分配-编辑"],
    ];
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //修改或没有则添加
        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->changeNames as $namePack){
            $authority=Authority::where('name',$namePack[1])->first();
            $authority['name']=$namePack[0];
            $authority->save();
        }
    }
}
