<?php

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

class CreateTaxRatesTable extends Migration
{
    protected $values = [
        0,3,6,11
    ];
    protected $authorities = [
        "税率",
        "税率-查询",
        "税率-编辑",
        "税率-删除",
        "入库管理-客户预约-预约",
        "入库管理-客户预约"
    ];
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tax_rates', function (Blueprint $table) {
            $table->id();
            $table->decimal("value",5,2)->unique()->comment("值");
            $table->timestamps();
        });
        Schema::table('owners', function (Blueprint $table) {
            $table->dropColumn("tax_rate");
            $table->bigInteger("tax_rate_id")->nullable()->comment("外键税率");
            $table->string("relevance")->nullable()->comment("关联模型的JSON数组");
        });
        foreach ($this->values as $value) \App\TaxRate::query()->firstOrCreate(["value"=>$value]);
        foreach ($this->authorities as $authority)\App\Authority::query()->firstOrCreate(["name"=>$authority],["name"=>$authority,"alias_name"=>$authority]);
        \App\Authority::query()->where("name","入库管理-盘收一体-客户预约")->delete();
        Schema::table('warehouses', function (Blueprint $table) {
            $table->decimal("production_capacity")->default(0)->comment("产能");
            $table->integer("reduced_production_capacity_coefficient")->default(0)->comment("SKU减产系数");
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tax_rates');
        Schema::table('owners', function (Blueprint $table) {
            $table->dropColumn("tax_rate_id");
            $table->decimal("tax_rate")->nullable()->comment("税率");
            $table->dropColumn("relevance");
        });
        foreach ($this->authorities as $authority)\App\Authority::query()->where("name",$authority)->delete();
        \App\Authority::query()->firstOrCreate(["name"=>"入库管理-盘收一体-客户预约"],["name"=>"入库管理-盘收一体-客户预约","alias_name"=>"入库管理-盘收一体-客户预约"]);
        Schema::table('warehouses', function (Blueprint $table) {
            $table->dropColumn("production_capacity");
            $table->dropColumn("reduced_production_capacity_coefficient");
        });
    }
}
