<?php


namespace App\Http\Controllers\api\thirdPart\haiq;


use App\Http\Requests\ForeignHaiRobotic_taskUpdateRequest;
use App\Services\ForeignHaiRoboticsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class StorageController
{
    protected $post;

    /** @var ForeignHaiRoboticsService $service */
    private $service;
    public function __construct()
    {
        $this->service=app('ForeignHaiRoboticsService');
        $stockInfo = [
            "skuCode" => "TEST-BSSKU",//商品编码 sku
            "qty" => 9,//数量
        ];
        $bin = [
            "taskCode" => "TEST-BS2010100001",//任务编号 全局唯一
            "binCode" => "TEST-BIN01",//料箱编码
            "fromLocCode" => "HAIB1-02-01",//源库位编码
            "toLocCode" => "HAIB1-02-01",//目标库位编码 出库填多个，表示这些库位都可以支持
            "sequence" => -1,//出库顺序 -1表示没有顺序，只有移库出库时需要指定顺序，其他可为-1
            "stockInfo" => [$stockInfo],//商品信息
        ];
        /*$this->request = [[
            "groupCode" => 1,//组号/波次号 一组任务需要一起完成，再开始下一组任务；没有组任务的限制默认传-1或空
            "taskMode" => 3,//任务模式 值 1 (输送线入库)值 2 (输送线出库)值 3（货架到缓存货架）值4（货架到流利货架）
            "priority" => 99,//优先级 1-2147483647 1最低
            "sequenceFlag" => -1,//是否需要有序 1：需要有序 0：不需要有序
            "bins" => [$bin],//可执行货箱任务
        ]];*/
        $this->post = [[
            "taskMode"      => 1,
            "bins"=>[[
                "taskCode"  =>"TESTx1za3X14",
                "binCode"   => "IDE0000130",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "BIN-OUT1",
            ],[
                "taskCode"  =>"TESTx2za3X43",
                "binCode"   => "IDE0000131",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "BIN-OUT1",
            ]],
            "groupCode"     => 'WMxz3acX14',
            "priority"      => 20,
            "sequenceFlag"  => 1,
        ]];
        $this->post2 = [[
            "taskMode"      => 2,
            "bins"=>[[
                "taskCode"  =>"TESTxx2zbaa1",
                "binCode"   => "IDE0000034",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "BIN-OUT1",
            ],[
                "taskCode"  =>"TESTxx3zbaa1",
                "binCode"   => "IDE0000093",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "BIN-OUT1",
            ],[
                "taskCode"  =>"TESTxx4zbaa1",
                "binCode"   => "IDE0000128",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "BIN-OUT1",
            ],[
                "taskCode"  =>"TESTxx5zbaa1",
                "binCode"   => "IDE0000129",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "BIN-OUT1",
            ],[
                "taskCode"  =>"TESTxx6zbaa1",
                "binCode"   => "IDE0000127",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "BIN-OUT1",
            ]],
            "groupCode"     => 'WMxz64b2a1',
            "priority"      => 20,
            "sequenceFlag"  => 1,
        ]];;
        $this->backIn = [[
            "taskMode"      => 1,
            "bins"=>[[
                "taskCode"  =>"testIn1ax",
                "binCode"   => "IDE0000007",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "",
            ],[
                "taskCode"  =>"testIn2ax",
                "binCode"   => "IDE0000129",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "",
            ],],
            "groupCode"     => 'WMx135ax',
            "priority"      => 20,
            "sequenceFlag"  => 1,
        ]];
        $this->backIn2 = [[
            "taskMode"      => 6,
            "bins"=>[[
                "taskCode"  =>"testIn123",
                "binCode"   => "IDE0000130",
                "fromLocCode" => "HAIB2-01-03",
                "toLocCode" => "",
            ],[
                "taskCode"  =>"testIn223",
                "binCode"   => "IDE0000034",
                "fromLocCode" => "HAIB2-02-03",
                "toLocCode" => "",
            ],[
                "taskCode"  =>"testIn323",
                "binCode"   => "IDE0000093",
                "fromLocCode" => "HAIB2-03-03",
                "toLocCode" => "",
            ]],
            "groupCode"     => 'WMx14',
            "priority"      => 20,
            "sequenceFlag"  => 1,
        ]];
        $this->move = [[
            "taskMode"      => 1,
            "bins"=>[[
                "taskCode"  =>"testm1z6",
                "binCode"   => "IDE0000034",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "",
            ],[
                "taskCode"  =>"testm2z6",
                "binCode"   => "IDE0000093",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "",
            ],[
                "taskCode"  =>"testm3z6",
                "binCode"   => "IDE0000130",
                "fromLocCode" => "BIN-IN1",
                "toLocCode" => "",
            ],],
            "groupCode"     => 'WMm1z6',
            "priority"      => 20,
            "sequenceFlag"  => 1,
        ]];//128,129.127
        $this->move2 = [[
            "taskMode"      => 6,
            "bins"=>[[
                "taskCode"  =>"testm11",
                "binCode"   => "IDE0000128",
                "fromLocCode" => "HAIB2-01-01",
                "toLocCode" => "",
            ],[
                "taskCode"  =>"testm21",
                "binCode"   => "IDE000129",
                "fromLocCode" => "HAIB2-02-01",
                "toLocCode" => "",
            ],[
                "taskCode"  =>"testm31",
                "binCode"   => "IDE0000127",
                "fromLocCode" => "HAIB2-03-01",
                "toLocCode" => "",
            ],],
            "groupCode"     => 'WMm2',
            "priority"      => 20,
            "sequenceFlag"  => 1,
        ]];
        $this->lightOn = [
            "areaCode"=> "1004",
            "PTLAction"=> 1,
            "PTLSettings"=> [
                "color"=> 1,
                "frequency"=> 1
            ],
            "displayInfo"=> [
                "detail01"=> "detail01",
                "detail02"=> "detail02",
                "detail03"=> "detail03",
                "qrCode"=> "qrCode",
                "qty00"=> "11",
                "qty01"=> 1,
                "qty02"=> 2,
                "title"=> "title",
                "uomDesc01"=> "uo",
                "uomDesc02"=> "uo"
            ],
            "locCode"=> "HAIB2-02-03"
        ];
        $this->lightOff = [
            "PTLAction"=> 0,
            "locCode"=> "HAIB2-02-03"
        ];
    }

    public function relocate(Request $request,$post){
        $response = Http::post(config('api.haiq.storage.moveBin'),$post);
        if (!$response->ok()){
            app('LogService')->log(__METHOD__,"haiq-请求失败,路径异常","REQUEST：".json_encode($this->post)." | RESPONSE：".$response);
            return ['success'=>false,"data"=>$response->body()];
        }
        if (($response["code"] ?? false) && $response["code"] != 200){
            app('LogService')->log(__METHOD__,"haiq-料箱出库失败","REQUEST：".json_encode($this->post)." | RESPONSE：".$response);
            return ['success'=>false,"data"=>$response["errMsg"]];
        }
       if (($response["status"] ?? false) && $response["status"] == 500){
           app('LogService')->log(__METHOD__,"haiq-料箱出库失败","REQUEST：".json_encode($this->post)." | RESPONSE：".$response);
           return ['success'=>false,"data"=>$response["msg"]];
        }
        app('LogService')->log(__METHOD__,"haiq-料箱出库成功","REQUEST：".json_encode($this->post)." | RESPONSE：".$response);
        return ["success"=>true];
    }
    public function light(Request $request,$post){
        $response = Http::post(config('api.haiq.storage.light'),$post);
        return $response->body();
    }
    public function moveBinOut(Request $request){
        return $this->relocate($request,$this->post);
    }
    public function moveBin2(Request $request){
        return $this->relocate($request,$this->post2);
    }
    public function moveBinIn(Request $request){
        return $this->relocate($request,$this->backIn);
    }
    public function moveBinIn2(Request $request){
        return $this->relocate($request,$this->backIn2);
    }

    public function moveBinM(Request $request){
        return $this->relocate($request,$this->move);
    }
    public function moveBinM2(Request $request){
        return $this->relocate($request,$this->move2);
    }
    public function lightOn(Request $request){
        return $this->light($request,$this->lightOn);
    }
    public function lightOff(Request $request){
        return $this->light($request,$this->lightOff);
    }
    public function taskUpdate(ForeignHaiRobotic_taskUpdateRequest $request){
        return $request->method();
        try{
            $this->service->taskUpdate(
                $request['groupCode'],
                $request['taskCode'],
                $request['updateEventType'],
                $request['status'],
                $request['binCode'],
                $request['kubotId'],
                $request['description']
            );
            return ['code'=>200,'errMsg'=>'','data'=>$request->all()];
        }catch (\Exception $e){
            return ['code'=>$e->getCode(),'errMsg'=>$e->getMessage(),'data'=>$request->all()];
        }
    }
    public function exception(Request $request){
        $this->service->excepted();
        app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->all()));
        return ['code'=>200,'errMsg'=>'','data'=>$request->all()];
    }
}
