<?php

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

class CreateOrderCommodityAssignsTable extends Migration
{

    protected $authorities = [
        "订单管理-指定分配",
        "订单管理-指定分配-查询",
        "订单管理-指定分配-编辑",
    ];
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        foreach ($this->authorities as $val) \App\Authority::query()->firstOrCreate(["name"=>$val,"alias_name"=>$val]);

        Schema::create('order_commodity_assigns', function (Blueprint $table) {
            $table->id();
            $table->bigInteger("order_id")->index()->comment("外键订单");
            $table->bigInteger("commodity_id")->comment("外键商品");
            $table->integer("amount")->default(0)->comment("数量");
            $table->date("produced_at")->nullable()->comment("生产日期");
            $table->date("valid_at")->nullable()->comment("失效日期");
            $table->string("batch_number")->index()->nullable()->comment("批次号");
            $table->string("location")->index()->nullable()->comment("库位");
            $table->string("region")->index()->nullable()->comment("库区");
            $table->bigInteger("user_id")->index()->nullable()->comment("操作人");
            $table->timestamps();
            $table->index(["commodity_id","produced_at"]);
            $table->index(["commodity_id","valid_at"]);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('order_commodity_assigns');

        foreach ($this->authorities as $val) \App\Authority::query()->where("name",$val)->where("alias_name",$val)->delete();
    }
}
