<?php
/**
 * 商品基础资料分类
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/10/30
 * Time: 17:10
 */

namespace JinDouYun\Controller\GoodsCategory;

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

use JinDouYun\Model\GoodsCategory\MGoodsCategory;

class GoodsCategory extends BaseController
{
    private $objMGoodsCategory;

    /**
     * GoodsCategory constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @throws \Exception
     */
    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMGoodsCategory = new MGoodsCategory($this->onlineUserId,$this->onlineEnterpriseId);
    }

    /**
     * 添加和编辑分类信息的公共字段
     *
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $goodsCategoryData = [
            'title' => isset($params['title']) ? $params['title'] : '',
        ];

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

        $goodsCategoryData['sort'] = isset($params['sort']) ? $params['sort'] : 0;
        $goodsCategoryData['adImage'] = isset($params['adImage']) ? $params['adImage'] : '';
        $goodsCategoryData['images'] = isset($params['images']) ? $params['images'] : '';
        $goodsCategoryData['link'] = isset($params['link']) ? $params['link'] : '';
        $goodsCategoryData['pid'] = isset($params['pid']) ? $params['pid'] : 0;
        $goodsCategoryData['enableStatus'] = isset($params['enableStatus']) ? $params['enableStatus'] : StatusCode::$standard;
        isset($params['notCustomerType']) && $goodsCategoryData['notCustomerType'] = $params['notCustomerType'];

        return $goodsCategoryData;
    }

    /**
     * 添加分类
     * @throws \Exception
     */
    public function addCategory()
    {
        $categoryData = $this->commonFieldFilter();

        $result = $this->objMGoodsCategory->addCategory($categoryData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 获取指定id的分类详细信息
     * @throws \Exception
     */
    public function getCategoryInfoById()
    {
        $id = $this->request->param('request_id');

        if (!$id) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }

        $result = $this->objMGoodsCategory->getCategoryInfoById($id);

        if ($result->isSuccess()) {
            $this->sendOutput($result->getData());
        } else {
            $this->sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 编辑分类信息
     * @throws \Exception
     */
    public function editCategory()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $categoryData = $this->commonFieldFilter();
        $categoryData['id'] = $id;

        $result = $this->objMGoodsCategory->editCategory($categoryData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 分类的显示和隐藏
     * @throws \Exception
     */
    public function updateCategoryStatus()
    {
        $params['id'] = $this->request->param('request_id');
        $params['enableStatus'] = $this->request->param('enableStatus');
        foreach ($params as $key => $value) {
            if (empty($value)) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }

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

    /**
     * 获取所有基础商品分类的列表
     * @throws \Exception
     */
    public function getAllCategory()
    {
        $params = $this->request->getRawJson();
        $selectParams = [];
        if(isset($params['enableStatus']) && !empty($params['enableStatus'])) {
            $selectParams['enableStatus'] = $params['enableStatus'];
        }

        $result = $this->objMGoodsCategory->getAllCategory($selectParams);

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

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

    /**
     * 删除分类
     * @throws \Exception
     */
    public function delCategory()
    {
        $categoryId = $this->request->param('request_id');
        if (!$categoryId) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }

        if (!is_array($categoryId)) {
            $categoryId = [$categoryId];
        }

        $result = $this->objMGoodsCategory->delCategory($categoryId);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }
}