<?php

namespace App\Services;

use App\Http\Controllers\Controller;
use App\OrderIssue;
use App\OrderIssueRejectedBill;
use App\RejectedBill;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

Class OrderIssueRejectedBillService
{
    /**
     * 添加连接
     * @param OrderIssue $orderIssue
     * @param $logistic_number_return
     * @return Builder|Model|object|null|boolean
     */
    public function joinRejectedBill($orderIssue,$logistic_number_return)
    {
        $orderIssueRejectedBill = OrderIssueRejectedBill::query()
            ->where('order_issue_id',$orderIssue->id)
            ->where('logistic_number_return',$logistic_number_return)->first();
        if($orderIssueRejectedBill)return null;
        $orderIssue->joinRejectedBill($logistic_number_return);
        $orderIssue->syncRejectingStatus();      // 问题件同步订单状态
        return OrderIssueRejectedBill::query()->with('rejectedBill.items')
                ->where('order_issue_id',$orderIssue->id)
                ->where('logistic_number_return',$logistic_number_return)->first() ?? false;
    }

    /**
     * 删除连接
     * @param OrderIssue $orderIssue
     * @param $logistic_number_return
     * @return void|null|boolean
     */
    public function unJoinRejectedBill($orderIssue,$logistic_number_return)
    {
        $orderIssueRejectedBill = OrderIssueRejectedBill::query()
            ->where('order_issue_id',$orderIssue->id)
            ->where('logistic_number_return',$logistic_number_return)->first();
        if(!$orderIssueRejectedBill)return null;

        $bool = $orderIssue->unJoinRejectedBill($logistic_number_return);
        $orderIssue->syncRejectingStatus();      // 问题件同步订单状态
        if($orderIssue->rejectedBills->count() == 0)$orderIssue->update(['is_new_rejecting' => '无']);
        return $bool;
    }

    /**
     * 修改连接
     * @param  OrderIssue $orderIssue
     * @param $logistic_number_return
     * @param $logistic_number_return_update
     * @return null|boolean|Builder|Model|object
     */
    public function reviseJoinRejectedBill($orderIssue,$logistic_number_return,$logistic_number_return_update)
    {
        $orderIssueRejectedBill = OrderIssueRejectedBill::query()->where('order_issue_id',$orderIssue->id)
            ->where('logistic_number_return',$logistic_number_return)->first();
        if($orderIssueRejectedBill) return null;
        $orderIssue->reviseJoinRejectedBill($logistic_number_return,$logistic_number_return_update);
        $orderIssue->syncRejectingStatus();      // 问题件同步订单状态
        return OrderIssueRejectedBill::query()->with('rejectedBill')
                ->where('order_issue_id',$orderIssue->id)
                ->where('logistic_number_return',$logistic_number_return)->first() ?? false;
    }

    /**
     * 退回件同步问题件
     * @param RejectedBill|Controller $rejectedBills
     * @return void
     */
    public function syncOrderIssue($rejectedBills)
    {
        if(!$rejectedBills)return;
        if(is_array($rejectedBills)){
            foreach ($rejectedBills as $rejectedBill) {
                $rejectedBill->syncOrderIssue();
            }
            return;
        }
        $rejectedBills->syncOrderIssue();
    }

    /**
     * 问题件同步退回件
     * @param OrderIssue $orderIssue
     */
    public function syncRejectedBill($orderIssue)
    {
        if(!$orderIssue)return;
        $order_packages = $orderIssue->order->packages ?? collect();
        if(!$order_packages)return;
        $logistic_number_return = RejectedBill::query()->whereIn('logistic_number_return',$order_packages->map(function($rejectedBill){
            return $rejectedBill->logistic_number_return;
        }))->get();
        $orderIssue->joinRejectedBill($logistic_number_return);
        $orderIssue->syncRejectingStatus();
    }

    /**
     * 问题件更新退单状态
     * @param OrderIssue|Controller $order_issues
     */
    public function syncOrderIssueRejectingStatus($order_issues)
    {
        /**@var OrderIssue $orderIssue */
        if(!$order_issues)return;
        if(is_array($order_issues)){
            foreach ($order_issues as $orderIssue) {
                $orderIssue->syncRejectingStatus();
            }
            return;
        }
        $order_issues->syncRejectingStatus();
    }
}
