<?php
/**
 * 前台分类相关接口
 * Created by PhpStorm.
 * User: wxj
 * Date: 2019/11/15
 * Time: 17:09
 */

namespace JinDouYun\Controller\GoodsCategory;

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

use JinDouYun\Model\GoodsCategory\MGoodsCategory;

class ApiGoodsCategory extends BaseController
{
    private $objMGoodsCategory;

    /**
     * GoodsCategory constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @throws \Exception
     */
    public function __construct($isCheckAcl = false, $isMustLogin = false)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMGoodsCategory = new MGoodsCategory($this->onlineUserId, $this->onlineEnterpriseId);
        $authorization = $this->request->getServerParam('HTTP_AUTHORIZATION');
        if (!empty($authorization)) {
            self::getUserIdByAuthorization();
        }
    }

    /**
     * 商品分类
     */
    public function getAllCategory()
    {
        $params = $this->request->getRawJson();

        if($this->onlineUserId){
            $data['userCenterId'] = $this->onlineUserId;
        }
        $data['enableStatus'] = StatusCode::$standard;
        $result = $this->objMGoodsCategory->apiGetAllCategory($data);

        if ($result->isSuccess()) {
            $returnData = $result->getData();

            parent::sendOutput($returnData['data'], 0, ['pageTotal'=>count($returnData['data'])]);
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 获取所有一级分类或一级分类下的二级分类
     */
    public function getAllCategoryByPid()
    {
        $params['id'] = $this->request->param('request_id');
        $selectParams['pid'] = 0;
        if (isset($params['id']) && (int)$params['id']) {
            $selectParams['pid'] = (int)$params['id'];
        }
        $selectParams['deleteStatus'] = StatusCode::$standard;
        $result = $this->objMGoodsCategory->getCategoryTitleByIds($selectParams);

        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => 0,
                'pageSize'  => 10,
                'pageTotal' => count($returnData),
            ];
            parent::sendOutput($returnData, 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
}