<?php
/**
 * 购物车
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/11/6
 * Time: 16:10
 */

namespace JinDouYun\Controller\Cart;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;
use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Cart\MCart;

class Cart extends BaseController
{
    private $objMCart;

    /**
     * ApiCart constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @throws \Exception
     */
    public function __construct($isCheckAcl = false, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $userCenterId = $this->request->param('request_id');
        $this->onlineUserId = $userCenterId;
        $this->objMCart = new MCart($this->onlineUserId, $this->onlineEnterpriseId);
    }

    /**
     * 添加购物车,公共字段
     *
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $cartData = [
            'goodsData' => isset($params['goodsData']) ? $params['goodsData'] : [],//商品数据
        ];

        foreach ($cartData as $key => $value) {
            if (empty($value) && $value !== 0) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        $goodsData = [];
        foreach ($cartData['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->objMCart->manageAddCart($cartData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 更新商品数量
     * @throws \Exception
     */
    public function updateBuyNum()
    {
        $paramsData = $this->request->getRawJson();
        $params = [
            'cartId' => isset($paramsData['cartId']) ? $paramsData['cartId'] : '',
            'buyNum' => isset($paramsData['buyNum']) ? $paramsData['buyNum'] : '',
        ];

        foreach ($params as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        $result = $this->objMCart->updateBuyNum($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * inventory字段废弃 inventoryNum库存数量
     * 获取用户购物车数据
     * @throws \Exception
     */
    public function getCartByUserCenterId()
    {
        $result = $this->objMCart->getManageCartByUserCenterId();
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => 0,
                'pageSize'  => 0,
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($result->getData(), ErrorCode::$dberror);
    }

    /**
     * 更新选中状态
     * @throws \Exception
     */
    public function updateSelection()
    {
        $paramsData = $this->request->getRawJson();

        $params = [
            'selection' => isset($paramsData['selection']) ? $paramsData['selection'] : '',//4=>未选中 5=>已选中
            'id'        => isset($paramsData['cartId']) ? $paramsData['cartId'] : 0,
        ];

        foreach ($params as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

        $result = $this->objMCart->updateSelection($params);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 删除购物车中商品(可批量)
     */
    public function delCart()
    {
        $params = $this->request->getRawJson();
        $paramsData = [
            'id' => isset($params['cartId']) ? $params['cartId'] : 0,
        ];

        foreach ($paramsData as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMCart->delCart($paramsData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 清空指定用户下购物车数据
     */
    public function clearCart()
    {
        $result = $this->objMCart->clearCart();
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 批量删除购物车商品
     */
    public function clearCartByGoodsId()
    {
        $params = $this->request->getRawJson();
        $paramsData = [
            'goodsId' => isset($params['goodsId']) ? $params['goodsId'] : '',
        ];
        foreach ($paramsData as $key => $value) {
            if (empty($value)) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMCart->clearCartByGoodsId($paramsData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
}