<?php

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

class AddOrderIssueSecondClientNo extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
        Schema::table('order_issues', function (Blueprint $table) {
            $table->string('second_client_no')->index()->nullable()->comment('二次客户订单号')->after('second_order_id');
        });
        $orderIssues = \App\OrderIssue::query()->withTrashed()->whereNotNull('second_order_id')->get();
        if($orderIssues->count() > 0){
            foreach ($orderIssues as $orderIssue) {
                $order = $orderIssue->hasOne('App\Order', 'id', 'second_order_id')->first();
                if($order ?? false){
                    $orderIssue->update(['second_client_no'=>$order->client_code ?? null]);
                }
            }
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
        Schema::table('order_issues',function(Blueprint $table){
            $table->dropColumn('second_client_no');
        });
    }
}
