<?php
namespace JinDouYun\Controller\Cart;

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Cart\MTestCart;
use JinDouYun\Model\System\MEnterpriseBindPayment;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

class TestCart extends BaseController
{
    private $objMTestCart;

    /**
     * TestCart constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @throws \Exception
     */
    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $thisChoiceCustomerId = $this->request->param('request_id');//当前选择的客户id
        $this->objMTestCart = new MTestCart($this->onlineUserId, $this->onlineEnterpriseId, false, $thisChoiceCustomerId);
    }

    /**
     * 添加购物车,公共字段
     *
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (!isset($params['goodsData'])) {
            parent::sendOutput('goodsData参数错误', ErrorCode::$paramError);
        }
        $goodsData = [];
        foreach ($params['goodsData'] as $key => $val) {
            $goodsData[$key] = [
                'goodsBasicId' => isset($val['goodsBasicId']) ? $val['goodsBasicId'] : '',
                'goodsId'      => isset($val['goodsId']) ? $val['goodsId'] : '',
                'skuId'        => isset($val['skuId']) ? $val['skuId'] : '',
                'buyNum'       => isset($val['buyNum']) ? $val['buyNum'] : '',
                'shopId'       => isset($val['shopId']) ? $val['shopId'] : '',
                'source'       => isset($val['source']) ? $val['source'] : '',
            ];

            foreach ($goodsData[$key] as $k => $v) {
                if (empty($v)) {
                    parent::sendOutput($k . '参数错误', ErrorCode::$paramError);
                }
            }
            $goodsData[$key]['goodsCode'] = createCode(StatusCode::$code['goodsBasic']['prefix'], $goodsData[$key]['goodsId'], StatusCode::$code['goodsBasic']['length']);
        }
        $cartData['goodsData'] = $goodsData;//过滤后数据
        return $cartData;
    }


    /**
     * 加入购物车
     * @throws \Exception
     */
    public function addCart()
    {
        $cartData = $this->commonFieldFilter();
        $result = $this->objMTestCart->addCart($cartData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }
}