<?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\MRole;
use JinDouYun\Cache\RoleAclCache;

class Role extends BaseController
{
    private $objMRole;
    private $objRoleAclCache;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMRole = new MRole($this->onlineEnterpriseId);
        $this->objRoleAclCache = new RoleAclCache();
    }

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

        $returnData = [
            "roleName" => isset($params['roleName']) ? $params['roleName'] : '', //角色名称
        ];

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

        //选填项
        $returnData['isAdministrator'] = isset($params['isAdministrator']) ? $params['isAdministrator'] : StatusCode::$delete;
        $returnData['pid'] = isset($params['pid']) ? $params['pid'] : 0;
        $returnData['updateTime'] = time();

        //权限
        $returnData['acl'] = !empty($params['acl']) ? json_encode($params['acl']) : NULL;

        return $returnData;
    }

    /**
     * 角色添加
     */
    public function addRole()
    {
        $addRoleData = $this->commonFieldFilter();
        $addRoleData['shopId'] = $this->shopId;
        $result = $this->objMRole->addRole($addRoleData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 删
     */

    /**
     * 角色删除
     */
    public function deleteRole()
    {
        $id = $this->request->param('request_id');
        if(empty($id)){
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $result = $this->objMRole->deleteRole($id);
        if ($result->isSuccess()) {
            $this->objRoleAclCache->delRoleIdAndAcl($this->onlineEnterpriseId, $id);
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 改
     */

    /**
     * 角色修改
     */
    public function updateRole()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $params = $this->commonFieldFilter();

        $result = $this->objMRole->updateRole($params, $id);
        if ($result->isSuccess()) {
            //缓存角色和权限的关系
            $aclData = [
                'isAdministrator' => $params['isAdministrator'],
                'acl' => json_decode($params['acl']),
            ];
            $this->objRoleAclCache->cacheRoleIdAndAcl($this->onlineEnterpriseId, $id, $aclData);
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 查
     */

    /**
     * 角色列表
     */
    public function getAllRole()
    {
        $params = $this->request->getRawJson();

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

        if(isset($params['keyword']) && !empty($params['keyword'])) {
            $selectParams['roleName'] = $params['keyword'];
        }
        $selectParams['shopId'] = $this->shopId;
        //角色id
        $result = $this->objMRole->getAllRole($selectParams);

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

    /**
     * 角色详情
     */
    public function getRoleInfo()
    {
        $params['id'] = $this->request->param('request_id');
        if (empty($params['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        //自增id

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

}