<?php
/**
 * Created by PhpStorm.
 * User: Gss
 * Date: 2021/4/15 0015
 * Time: 14:48
 */

namespace JinDouYun\Controller\Goods;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Goods\MGoodsGroups;

class GoodsGroups extends BaseController
{
    private $objMGoodsGroups;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMGoodsGroups = new MGoodsGroups($this->onlineEnterpriseId, $this->onlineUserId);
    }

    /**
     * 获取参数
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $data = [
            "name" => isset($params['name']) ? $params['name'] : '',
        ];

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

        return $data;
    }

    /**
     * 商品分组添加
     */
    public function addGoodsGroups()
    {
        $data = $this->commonFieldFilter();
        $data['createTime'] = time();
        $result = $this->objMGoodsGroups->addGoodsGroups($data);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }

    /**
     * 商品分组删除
     */
    public function deleteGoodsGroups()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $update = [
            'deleteStatus' => StatusCode::$delete
        ];
        $result = $this->objMGoodsGroups->updateGoodsGroups($update, ['id' => $id]);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }

    /**
     * 商品分组修改
     */
    public function updateGoodsGroups()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $update = $this->commonFieldFilter();
        $update['updateTime'] = time();
        $result = $this->objMGoodsGroups->updateGoodsGroups($update, ['id' => $id]);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }

    /**
     * 商品分组启用/禁用
     */
    public function enableGoodsGroups()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $params = $this->request->getRawJson();
        $data = [
            'status' => isset($params['status']) ? $params['status'] : '',
        ];
        foreach ($data as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMGoodsGroups->updateGoodsGroups($data, ['id' => $id]);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }

    /**
     * 商品分组列表
     */
    public function getAllGoodsGroups()
    {
        $params = $this->request->getRawJson();

        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $selectParams['name'] = getArrayItem($params,'name',"");

        $result = $this->objMGoodsGroups->getAllGoodsGroups($selectParams);
        if (!$result->isSuccess()) {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
        $returnData = $result->getData();
        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize' => $params['pageSize'],
            'pageTotal' => $returnData['total'],
        ];
        parent::sendOutput($returnData['data'], 0, $pageData);
    }

    /**
     * 商品分组详情
     */
    public function getReservoirInfo()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

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