<?php
/**
 * 商品品牌
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/11/5
 * Time: 11:25
 */

namespace JinDouYun\Controller\GoodsManage;

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

use JinDouYun\Model\GoodsManage\MGoodsBrand;

class GoodsBrand extends BaseController
{
    private $objMGoodsBrand;

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

    /**
     * 添加和编辑商品品牌
     *
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

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

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

        $goodsBrandData['supplier'] = isset($params['supplier']) ? $params['supplier'] : '';
        $goodsBrandData['images'] = isset($params['images']) ? $params['images'] : null;
        $goodsBrandData['sort'] =  isset($params['sort']) ? $params['sort'] : 0;
        $goodsBrandData['enableStatus'] = isset($params['enableStatus']) ? $params['enableStatus'] : StatusCode::$standard;
        $goodsBrandData['createTime'] = time();
        $goodsBrandData['updateTime'] = time();
        return $goodsBrandData;
    }

    /**
     * 添加商品品牌
     * @throws \Exception
     */
    public function addBrand()
    {
        $brandData = $this->commonFieldFilter();
        $result = $this->objMGoodsBrand->addBrand($brandData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 编辑品牌信息
     * @throws \Exception
     */
    public function editBrand()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $brandData = $this->commonFieldFilter();
        $brandData['id'] = $id;
        unset($brandData['createTime']);
        $result = $this->objMGoodsBrand->editBrand($brandData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 获取指定id的品牌详情
     * @throws \Exception
     */
    public function getBrandInfoById()
    {
        $id = $this->request->param('request_id');

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

        $result = $this->objMGoodsBrand->getBrandInfoById($id);

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

    /**
     * 品牌的显示和隐藏
     * @throws \Exception
     */
    public function updateBrandStatus()
    {
        $params['id'] = $this->request->param('request_id');
        $params['enableStatus'] = $this->request->param('enableStatus');//4禁用 5启用

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

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

    /**
     * 获取所有品牌
     * @throws \Exception
     */
    public function getAllBrand()
    {
        $page = $this->request->param('page') ? $this->request->param('page') : 1;
        $pageSize = $this->request->param('pageSize') ? $this->request->param('pageSize') : 10;
        $keyword = $this->request->param('keyword') ? $this->request->param('keyword') : '';

        $offset = ($page - 1) * $pageSize;

        $selectParams = [
            'limit'  => $pageSize,
            'offset' => $offset,
        ];

        if (!empty($keyword)) {
            $selectParams['keyword'] = $keyword;
        }
        $reportData = $this->objMGoodsBrand->getAllBrand($selectParams);

        if ($reportData->isSuccess()) {
            $returnData = $reportData->getData();
            $pageData = [
                'pageIndex' => $page,
                'pageSize'  => $pageSize,
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($reportData->getData(), ErrorCode::$dberror);
    }

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

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

        $result = $this->objMGoodsBrand->delBrand($brandId);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

}