<?php

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

class CreateDeliveryAppointmentsTable extends Migration
{
    public $authorities = [
        "入库管理-盘收一体-客户预约",
    ];
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('delivery_appointments', function (Blueprint $table) {
            $table->id();
            $table->bigInteger("user_id")->nullable()->comment("操作用户");
            $table->bigInteger("owner_id")->index()->comment("外键货主");
            $table->string("procurement_number")->nullable()->comment("采购单号");
            $table->string("asn_number")->index()->nullable()->comment("ASN单号");
            $table->bigInteger("warehouse_id")->index()->comment("外键仓库");
            $table->decimal("tonne",8,2)->nullable()->comment("吨");
            $table->decimal("cubic_meter",8,2)->nullable()->comment("立方米");
            $table->integer("box_amount")->nullable()->comment("箱数");
            $table->decimal("capacity",10,2)->default(0)->comment("产能");
            $table->date("appointment_date")->index()->comment("预约时间");
            $table->tinyInteger("date_period")->default(0)->comment("时间段");
            $table->tinyInteger("status")->default(0)->comment("状态");
            $table->timestamps();
        });
        Schema::create('delivery_appointment_cars', function (Blueprint $table) {
            $table->id();
            $table->bigInteger("delivery_appointment_id")->index()->comment("外键预约");
            $table->string("license_plate_number")->nullable()->comment("车牌号");
            $table->string("car_id")->index()->nullable()->comment("外键车型");
            $table->string("driver_name",20)->nullable()->comment("驾驶员姓名");
            $table->string("driver_phone",30)->nullable()->comment("驾驶员电话");
            $table->string("appointment_number")->comment("预约号码");
            $table->tinyInteger("status")->default(0)->comment("状态");
        });
        Schema::create('delivery_appointment_details', function (Blueprint $table) {
            $table->id();
            $table->bigInteger("delivery_appointment_id")->index()->comment("外键预约");
            $table->bigInteger("commodity_id")->index()->nullable()->comment("外键商品");
            $table->string("bar_code")->nullable()->comment("条码");
            $table->string("name")->nullable()->comment("名称");
            $table->integer("amount")->comment("数量");
        });
        foreach ($this->authorities as $authority){
            \App\Authority::query()->firstOrCreate([
                "name"=>$authority
            ],[
                "name"=>$authority,
                "alias_name"=>$authority
            ]);
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('delivery_appointments');
        Schema::dropIfExists('delivery_appointment_cars');
        Schema::dropIfExists('delivery_appointment_details');
        foreach ($this->authorities as $authority){
            \App\Authority::query()->where("name",$authority)->delete();
        }
    }
}
