<?php

namespace App\Listeners;

use App\Events\InformWMSReceivedEvent;
use App\Http\Controllers\api\thirdPart\flux\ReceiveController;
use App\Http\Controllers\Controller;
use App\RejectedBill;
use App\RejectedBillItem;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Auth;

class InformWMSReceivedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param InformWMSReceivedEvent $event`
     * @return string`
     */
    public function handle(InformWMSReceivedEvent $event)
    {
        $rejectedBill = RejectedBill::find($event->rejectedBill['id']);
        if(config('api.jianshang_rejecteds_log_switch'))Controller::logS(__METHOD__,__FUNCTION__,'推送WMS入库事件进入，退单号：'.$rejectedBill['logistic_number_return'],Auth::user()['id']);
        if($rejectedBill['is_loaded']==2){
            if(config('api.jianshang_rejecteds_log_switch'))Controller::logS(__METHOD__,__FUNCTION__,"推送WMS入库，仅将待推单修改为待确认! 字段状态：{$rejectedBill['is_loaded']}，退单号：".$rejectedBill['logistic_number_return'],Auth::user()['id']);
            $rejectedBill->setIsLoaded_toWaitConfirm();
            return 'toBeConfirm';
        }
        if($rejectedBill['is_loaded']==4){
            if(config('api.jianshang_rejecteds_log_switch'))Controller::logS(__METHOD__,__FUNCTION__,'推送WMS入库，重复提交，状态已经待确认! 退单号：'.$rejectedBill['logistic_number_return'],Auth::user()['id']);
            return 'alreadyBeWaitConfirm';
        }
        if( $rejectedBill['is_loaded']==1){
            if(config('api.jianshang_rejecteds_log_switch'))Controller::logS(__METHOD__,__FUNCTION__,'推送WMS入库，重复提交，状态已经是入库! 退单号：'.$rejectedBill['logistic_number_return'],Auth::user()['id']);
            return 'alreadyStored';
        }
        $result_TransferToWMS=ReceiveController::accomplishToWMS($rejectedBill);
        if ($result_TransferToWMS=='fail'){
            if(config('api.jianshang_rejecteds_log_switch'))Controller::logS(__METHOD__,'error_'.__FUNCTION__,'推送WMS入库失败！！退单号：'.$rejectedBill['logistic_number_return'],Auth::user()['id']);
            return $result_TransferToWMS;
        }
        if ($result_TransferToWMS=='exception'){
//            $rejectedBill->is_loaded=3;//奇怪，出现过要重保存才成功的情况，未知原因的保险
//            $rejectedBill->save();
            $rejectedBill['is_loaded']=3;
            $rejectedBill->update();
            if(config('api.jianshang_rejecteds_log_switch'))Controller::logS(__METHOD__,'error_'.__FUNCTION__,"推送WMS入库异常！！字段状态：{$rejectedBill['is_loaded']}，退单号：".$rejectedBill['logistic_number_return'],Auth::user()['id']);
            return $result_TransferToWMS;
        }
//        $rejectedBill->is_loaded=1;//奇怪，出现过要重保存才成功的情况，未知原因的保险
//        $rejectedBill->save();
        $rejectedBill['is_loaded']=1;
        $rejectedBill->update();
        if(config('api.jianshang_rejecteds_log_switch'))Controller::logS(__METHOD__,__FUNCTION__,"推送WMS后退单改为已入库, 真实状态：{$rejectedBill['is_loaded']}，退单号：".$rejectedBill['logistic_number_return'],Auth::user()['id']);
        $rejectedBill->items->each(function(RejectedBillItem $item){
            $item['is_loaded']='已入库';
            $item->save();
        });
        if(config('api.jianshang_rejecteds_log_switch'))Controller::logS(__METHOD__,__FUNCTION__,"推送WMS后商品标记完为入库状态。退单号：".$rejectedBill['logistic_number_return'],Auth::user()['id']);
        return $result_TransferToWMS;
    }
}
