<?php
/**
 * 角色权限管理Controller
 * Created by PhpStorm.
 * User: 小威
 * Date: 2019/11/08
 * Time: 16:00
 */

namespace JinDouYun\Controller\Department;

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

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Department\MRoleAcl;

class RoleAcl extends BaseController
{
    private $objMRoleAcl;

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

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

        $returnData = [
            "roleId" => isset($params['roleId']) ? $params['roleId'] : '',
            "isAdministrator" => isset($params['isAdministrator']) ? $params['isAdministrator'] : '',
        ];

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

        //选填项
        $returnData['userCenterId'] = isset($params['userCenterId']) ? $params['userCenterId'] : '';
        $returnData['acl'] = !empty($params['acl']) ? json_encode($params['acl']) : NULL;
        $returnData['updateTime'] = time();
        $returnData['createTime'] = time();

        return $returnData;
    }

    /**
     * 角色权限添加
     */
    public function addRoleAcl()
    {
        $addRoleAclData = $this->commonFieldFilter();

        $result = $this->objMRoleAcl->addRoleAcl($addRoleAclData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 角色/用户 权限详情
     * 角色id
     */
    public function getRoleAclInfo()
    {
        $params = $this->request->getRawJson();
        $where = [];
        if(isset($params['roleId'])) {
            $where['roleId'] = $params['roleId'];
        }

        if(isset($params['userCenterId'])) {
            $where['userCenterId'] = $params['userCenterId'];
        }

        if (empty($where)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

        $result = $this->objMRoleAcl->getRoleAclInfo($where);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 初始化缓存
     */
    public function initCache()
    {
        $result = $this->objMRoleAcl->initCache();
        if(!$result->isSuccess()){
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
        parent::sendOutput($result->getData());
    }
}