<?php
/**
 * 区块管理后台
 * Created by PhpStorm.
 * User: QianNiao-C
 * Date: 2019/10/25
 * Time: 11:32
 */

namespace JinDouYun\Controller\BlockManage;

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

use JinDouYun\Model\BlockManage\MZone;

class Zone extends BaseController
{
    public $objMZone;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMZone = new MZone();
    }

    /**
     * 添加,编辑区块接收公共字段
     *
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $zoneFieldData = [
            'title'    => isset($params['title']) ? $params['title'] : '',
            'zoneData' => isset($params['data']) ? $params['data'] : '',
        ];
        foreach ($zoneFieldData as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $zoneFieldData['zoneData'] = json_encode($zoneFieldData['zoneData']);
        return $zoneFieldData;
    }

    /**
     * 添加区块
     */
    public function addZone()
    {
        $zoneData = $this->commonFieldFilter();
        $result = $this->objMZone->addZone($zoneData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 区块启用和禁用
     */
    public function updateEnableStatus()
    {
        $params['id'] = $this->request->param('request_id');
        $params['enableStatus'] = $this->request->param('enableStatus');//5启用 4停用

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

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

    /**
     * 获取指定区块信息
     */
    public function getZoneInfoById()
    {
        $id = $this->request->param('request_id');

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

        $result = $this->objMZone->getZoneInfoById($id);
        if ($result->isSuccess()) {
            $resultData = $result->getData();
            $resultData['zoneData'] = json_decode($resultData['zoneData'], true);
            parent::sendOutput($resultData);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 编辑区块信息
     */
    public function editZone()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $zoneData = $this->commonFieldFilter();
        $zoneData['id'] = $id;
        unset($zoneData['createTime']);
        $result = $this->objMZone->editZone($zoneData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * 获取所有区块
     */
    public function getAllZone()
    {
        $page = $this->request->param('page') ?: 1;
        $pageSize = $this->request->param('pageSize') ?: 10;

        $offset = ($page - 1) * $pageSize;
        $selectParams = [
            'limit'  => $pageSize,
            'offset' => $offset,
        ];

        $result = $this->objMZone->getAllZone($selectParams);

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

}