<?php

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

class ChangeColumnOrderIdToOrderIssues extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $this->cleanOrderIssueRepeat();
        Schema::table('order_issues', function (Blueprint $table) {
            $table->integer('order_id')->unique('order_issues_order_id_unique')->change();
        });
    }

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

    public function cleanOrderIssueRepeat()
    {
        $orderIssue_unique = \App\OrderIssue::withTrashed()
            ->selectRaw('count(1) as count,id,order_id')
            ->whereNotNull('order_id')
            ->orderByDesc('created_at')
            ->groupBy('order_id')
            ->having('count', '>', 1)
            ->limit(100)
            ->get();
        if ($orderIssue_unique->count() == 0) return;
        foreach ($orderIssue_unique as $item) {
            $orderIssue_delete = OrderIssue::withTrashed()->where('order_id', $item->order_id)->get();
            /** @var OrderIssue $order_issue_unique */
            $order_issue_unique = $orderIssue_delete->whereNull('deleted_at')->first();
            if (!$order_issue_unique) $order_issue_unique = $orderIssue_delete->first();
            $ids = [];
            foreach ($orderIssue_delete as $key => $order_issue) {
                if ($order_issue->id == $order_issue_unique->id) continue;
                $ids[] = $order_issue->id;
                if($order_issue['logistic_number_return'])$order_issue_unique['logistic_number_return'] =$order_issue['logistic_number_return'];
                if($order_issue['rejecting_status']!='未退回')$order_issue_unique['rejecting_status'] =$order_issue['rejecting_status'];
                if($order_issue['is_new_rejecting']!='无')$order_issue_unique['is_new_rejecting'] =$order_issue['is_new_rejecting'];
                if($order_issue['user_workgroup_id'])$order_issue_unique['user_workgroup_id'] =$order_issue['user_workgroup_id'];
                if($order_issue['imported_status']!='正常')$order_issue_unique['imported_status'] =$order_issue['imported_status'];
                if($order_issue['custom_code'])$order_issue_unique['custom_code'] =$order_issue['custom_code'];
            }
            $order_issue_unique->save();
            app('LogService')->log(__METHOD__, __FUNCTION__, '修改OrderIssue:' . json_encode($order_issue_unique));
            $orderIssues = OrderIssue::query()->whereIn('id',$ids)->get();
            $bool = OrderIssue::query()->whereIn('id',$ids)->forceDelete();
            if($bool)app('LogService')->log(__METHOD__, __FUNCTION__, '删除OrderIssue:' . json_encode($orderIssues));
        }
    }
}
