<?php
/**
 * Created by PhpStorm.
 * User: LZP
 * Date: 2022/4/15
 * Time: 11:23
 */
namespace App\HttpController;

use EasySwoole\Http\AbstractInterface\Controller;
use App\Com\JdSdk;

class ProductService extends Controller
{
    private $JdSdk;

    public function __construct()
    {
        $this->JdSdk = new JdSdk();
    }

    /**
     * 3.5  商品上下架状态接口
     *
     * @param $skuArr
     *
     * @return array|bool
     */
    public function skuState()
    {
        // 获取 `POST` 或者 `GET` 提交的所有参数
        $data = $this->request()->getRequestParam();
var_dump($data);

        // 返回给客户端
        $this->response()->write($data . PHP_EOL);

        $skuArr = $data['skuArr'];

        $sku = implode(',', $skuArr);


        $response = $this->JdSdk->api_product_sku_state($sku);
        //$response = json_decode($response, true);
//        =====$response=====
//        { "success": true, "resultMessage": "操作成功", "resultCode": "0000", "result": [ { "state": 0, "sku": 5729524 } ], "code": 200 }

        return $response;

    }

    /**
     * 6.2  批量获取库存接口（建议订单详情页、下单使用）
     *
     * @param $skuNums
     * @param $area
     * $skuNums商品和数量 此是个String 再重构吧  [{skuId: 569172,num:101}], $area格式：1_0_0 (分别代表1、2、3级地址)
     * @return mixed
     */
    public function getNewStockById()
    {
        // 获取 `POST` 或者 `GET` 提交的所有参数
        $data = $this->request()->getRequestParam();

        $skuNums = $data['skuArr'];

        $area = $data['area'];


        $response = $this->JdSdk->api_stock_get_new_stock_by_id($skuNums, $area);

        return $response;
    }

    /**
     * TODO 待测 5.2  批量查询协议价价格
     * HTTPS请求方式：POST
     *
     * @return string
     */
    function api_price_get_price()
    {
        // 获取 `POST` 或者 `GET` 提交的所有参数
        $data = $this->request()->getRequestParam();

        $sku = $data['sku'];

        $response = $this->JdSdk->api_price_get_price($sku);

        return $response;
    }


    /**
     * 当请求方法未找到时，自动调用该方法，可自行覆盖该方法实现自己的逻辑
     *
     */
    protected function actionNotFound(?string $action)
    {
        $this->response()->withStatus(404);
        $file = EASYSWOOLE_ROOT.'/vendor/easyswoole/easyswoole/src/Resource/Http/404.html';
        if(!is_file($file)){
            $file = EASYSWOOLE_ROOT.'/src/Resource/Http/404.html';
        }
        $this->response()->write(file_get_contents($file));
    }


    /**
     * 此控制器抛异常时会执行此方法
     *
     * @param \Throwable $throwable
     * @throws \Throwable
     * CreateTime: 2020/8/19 12:48 上午
     */
    public function onException(\Throwable $throwable): void
    {
        parent::onException($throwable); // TODO: Change the autogenerated stub
    }

}