<?php

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

class CreateRealOrderCardsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable('real_order_cards_partition')) {
            return;
        }

        Schema::create('real_order_cards_partition', function (Blueprint $table) {
            $table->increments('id')->comment('关联表ID');
            $table->tinyInteger('type')->unsigned()->default(0)->comment('订单类型（1:套餐续费 2：续费包 3:加油包 4:可选包 5:附加包）');
            $table->bigInteger('sim')->unsigned()->default(0)->comment('sim号');
            $table->integer('order_id')->unsigned()->default(0)->comment('订单ID');
            $table->integer('company_id')->unsigned()->default(0)->comment('企业ID');
            $table->integer('package_id')->unsigned()->default(0)->comment('套餐ID');
            $table->integer('counts')->unsigned()->default(0)->comment('数量');
            $table->integer('unit_price')->unsigned()->default(0)->comment('单价');
            $table->integer('virtual_order_id')->unsigned()->default(0)->comment('RD 订单ID');
            $table->timestamp('refunded_at')->nullable()->comment('退货时间');
            $table->timestamps();
            $table->softDeletes();

            $table->comment("RD订单企业套餐卡关联表");

            $table->partition('type', 'list');
            $table->addPartition('real_order_cards', 'list', [0]);
            $table->addPartition('real_order_renewal_cards', 'list', [1]);
            $table->addPartition('real_order_renewal_package_cards', 'list', [2]);
            $table->addPartition('real_order_flows_package_cards', 'list', [3]);
            $table->addPartition('real_order_optional_package_cards', 'list', [4]);
            $table->addPartition('real_order_additional_package_cards', 'list', [5]);
        });

        Schema::table('real_order_cards', function (Blueprint $table) {
            $table->unique(['sim', 'order_id']);
            $table->unique(['sim', 'deleted_at']);
            $table->comment("RD销售订单关联表");
        });

        Schema::table('real_order_renewal_cards', function (Blueprint $table) {
            $table->unique(['sim', 'order_id', 'deleted_at']);
            $table->comment("RD续费订单关联表");
        });

        Schema::table('real_order_renewal_package_cards', function (Blueprint $table) {
            $table->unique(['sim', 'order_id', 'deleted_at']);
            $table->comment("RD续费包订单关联表");
        });

        Schema::table('real_order_flows_package_cards', function (Blueprint $table) {
            $table->unique(['sim', 'order_id', 'deleted_at']);
            $table->comment("RD加油包订单关联表");
        });

        Schema::table('real_order_optional_package_cards', function (Blueprint $table) {
            $table->unique(['sim', 'order_id', 'deleted_at']);
            $table->comment("RD可选包订单关联表");
        });

        Schema::table('real_order_additional_package_cards', function (Blueprint $table) {
            $table->unique(['sim', 'order_id', 'deleted_at']);
            $table->comment("RD附加包订单关联表");
        });
    }

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