<?php

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

class ChangePriceModelTable extends Migration
{

    protected $tables = ["owner_storage_price_models","owner_price_operations","owner_price_logistics","owner_price_expresses","owner_price_direct_logistics"];
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        foreach(\App\Authority::query()->where("name","like","客户管理%")->get() as $authority){
            $name =  mb_substr($authority->name,2);
            $authority->update(["name"=>"项目".$name,"alias_name"=>"项目".$name]);
        };
        foreach ($this->tables as $table){
            Schema::table($table,function (Blueprint $table){
                $table->char("operation",1)->index()->nullable()->comment("操作类型");
                $table->bigInteger("target_id")->index()->nullable()->comment("目标ID");
            });
        }
        \App\Authority::query()->firstOrCreate(["name"=>"项目管理-项目-计费模型-审核"],["name"=>"项目管理-项目-计费模型-审核","alias_name"=>"项目管理-项目-计费模型-审核"]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        foreach(\App\Authority::query()->where("name","like","项目管理%")->get() as $authority){
            $name =  mb_substr($authority->name,2);
            $authority->update(["name"=>"客户".$name,"alias_name"=>"客户".$name]);
        };
        foreach ($this->tables as $table){
            Schema::table($table,function (Blueprint $table){
                $table->dropColumn("operation",1);
                $table->dropColumn("target_id");
            });
        }
        \App\Authority::query()->where("name","项目管理-项目-计费模型-审核")->delete();
    }

    /*TODO 可回滚数据功能:1.增加两个字段 operation(哪种类型操作) target_id(映射目标)
                        2.新增操作：未审核时增加operation为C
                        修改前提，对应的映射目标不可被查看修改
                        3.修改操作：当前修改对象是否为被标记过对象，是->直接修改当前对象 否->生成标记对象
                        3.删除操作：当前删除对象是否为被标记过对象，是->目标是否存在(是->删除当前对象,将目标对象标记为删除 否->将当前对象标记为删除) 否->将当前对象标记为删除
        审核操作：被标记为C的删除标记信息 被标记为U的删除目标对象与当前对象的标记信息 被标记为D的直接删除
        恢复操作：被标记为C的直接删除 被标记为U的删除当前对象 被标记为D的删除当前对象的标记信息
    */
}
