<?php
namespace Jindouyun\Cache;

use http\Exception;
use Mall\Framework\Factory;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\ErrorCode;

class RoleAclCache
{
    private $cache;
    protected $StaffRelationRoleKey = 'staffRelationRole';//缓存员工和角色的关系
    protected $RoleRelationAclKey = 'roleRelationAcl';//缓存角色和权限的关系 hash
    protected $staffRelationDataFieldKey = 'staffUidRelationDataField';//缓存员工和数据域的关系 hash
    protected $staffIdRelationUidKey = 'staffIdRelationUid';//缓存职工id和uid的关系 hash
    protected $uidRelationStaffIdKey = 'uidRelationStaffIdKey';//缓存uid和职工id的关系 hash
    protected $shopIdRelationWarehouseIdKey = 'shopIdRelationWarehouseId';//缓存商铺和仓库的关系 hash
    protected $authority = 'Authority';//权限缓存
    protected $Administrator = 'AdministratorRole';//超级管理员角色
    protected $authorityBindRole = 'AuthorityBindRole';//角色绑定权限

    public function __construct()
    {
        $this->cache = Factory::cache('user');
    }
    /*******************  start:缓存员工和角色的关系   value:userCenterId  score:roleId   **************/

    /**
     * 缓存员工和角色的关系
     * @param $enterpriseId
     * @param $uid
     * @param $roleId
     */
    public function cacheStaffAndRole($enterpriseId, $uid, $roleId)
    {
        $this->cache->zadd($this->StaffRelationRoleKey.'::'.$enterpriseId, $roleId, $uid);
    }

    /**
     * 获取员工的角色Id
     * @param $enterpriseId
     * @param $uid
     * @return mixed
     */
    public function getRoleIdOfStaff($enterpriseId, $uid)
    {
        $result =  $this->cache->zrange($this->StaffRelationRoleKey.'::'.$enterpriseId, 0,-1, true);
        return isset($result[$uid]) ? (int)$result[$uid] : '';
    }

    /**
     * 删除userCenterId和roleId的绑定关系
     * @param $enterpriseId
     * @param $uid
     */
    public function deleteStaffRelationRole($enterpriseId, $uid)
    {
        $this->cache->zrem($this->StaffRelationRoleKey.'::'.$enterpriseId , $uid);
    }
    /*******************  end:缓存员工和角色的关系   value:userCenterId  score:roleId     **************/



    /*******************  start:角色与权限的关系 **************/

    /**
     * 缓存 角色组id 和 权限
     * @param $enterpriseId
     * @param $roleId
     * @param $aclData
     * @return mixed
     */
    public function cacheRoleIdAndAcl($enterpriseId, $roleId, $aclData = [])
    {
        return $this->cache->hset($this->RoleRelationAclKey.'::'.$enterpriseId, $roleId, json_encode($aclData));
    }

    /**
     * 删除角色组对应的权限
     * @param $enterpriseId
     * @param $roleId
     * @return mixed
     */
    public function delRoleIdAndAcl($enterpriseId, $roleId)
    {
        return $this->cache->hdel($this->RoleRelationAclKey.'::'.$enterpriseId, $roleId);
    }

    /**
     * 获取角色组对应的权限
     * @param $enterpriseId
     * @param $roleId
     * @return mixed
     */
    public function getRoleIdAndAcl($enterpriseId, $roleId)
    {
        $aclResult = $this->cache->hget($this->RoleRelationAclKey.'::'.$enterpriseId, $roleId);
        return !empty($aclResult) ? json_decode($aclResult, true) : [];
    }

    /*******************  end:角色与权限的关系   **************/


    /*******************  start:员工与数据域的关系 **************/

    /**
     * 缓存 员工的userCenterid 和 数据域对象
     * @param $enterpriseId
     * @param $userCenterId
     * @param $dataField
     * @return mixed
     */
    public function cacheStaffUidAndDataField($enterpriseId, $userCenterId, $dataField = [])
    {
        return $this->cache->hset($this->staffRelationDataFieldKey.'::'.$enterpriseId, $userCenterId, json_encode($dataField));
    }

    /**
     * 删除员工的userCenterid 和 数据域对象  的关联
     * @param $enterpriseId
     * @param $userCenterId
     * @return mixed
     */
    public function delStaffUidAndDataField($enterpriseId, $userCenterId)
    {
        return $this->cache->hdel($this->staffRelationDataFieldKey.'::'.$enterpriseId, $userCenterId);
    }

    /**
     * 获取员工的userCenterid 和 数据域对象  的关联
     * @param $enterpriseId
     * @param $userCenterId
     * @return mixed
     */
    public function getStaffUidAndDataField($enterpriseId, $userCenterId)
    {
        $dataField = $this->cache->hget($this->staffRelationDataFieldKey.'::'.$enterpriseId, $userCenterId);
        return !empty($dataField) ? json_decode($dataField, true) : [];
    }

    /*******************  end:员工与数据域的关系   **************/

    /*******************  start:职工id与userCenterId的关系   **************/

    /**
     * 缓存 职工id与userCenterId的关系
     * @param $enterpriseId
     * @param $staffId
     * @param $userCenterId
     * @return mixed
     */
    public function cacheStaffIdAndUserCenterId($enterpriseId, $userCenterId, $staffId)
    {
        return $this->cache->hset($this->staffIdRelationUidKey.'::'.$enterpriseId, $userCenterId, $staffId);
    }

    /**
     * 获取职工id与userCenterId的关系
     * @param $enterpriseId
     * @param $staffId
     * @return mixed
     */
    public function getStaffIdAndUserCenterId($enterpriseId, $userCenterId)
    {
        $staffId = $this->cache->hget($this->staffIdRelationUidKey.'::'.$enterpriseId, $userCenterId);
        return $staffId ? $staffId : '';
    }
    /*******************  end:职工id与userCenterId的关系   **************/


    /*******************  start:userCenterId和职工id的关系   **************/

    /**
     * @param $enterpriseId
     * @param $staffId
     * @param $userCenterId
     * @return mixed
     */
    public function cacheUserCenterIdAndStaffId($enterpriseId, $staffId, $userCenterId)
    {
        return $this->cache->hset($this->uidRelationStaffIdKey.'::'.$enterpriseId, $staffId, $userCenterId);
    }

    /**
     * @param $enterpriseId
     * @param $staffId
     * @return mixed
     */
    public function getUserCenterIdAndStaffId($enterpriseId, $staffId)
    {
        $userCenterId = $this->cache->hget($this->uidRelationStaffIdKey.'::'.$enterpriseId, $staffId);
        return $userCenterId ? $userCenterId : '';
    }
    /*******************  end:userCenterId和职工id的关系   **************/

    /*******************  start:商铺和仓库的关系   **************/

    /**
     * @param $enterpriseId
     * @param $shopId
     * @param $warehouseId
     * @return mixed
     */
    public function cacheShopIdAndWarehouseId($enterpriseId, $shopId, $warehouseId)
    {
        return $this->cache->hset($this->shopIdRelationWarehouseIdKey.'::'.$enterpriseId, $shopId, $warehouseId);
    }

    /**
     * @param $enterpriseId
     * @param $shopId
     * @return mixed
     */
    public function getShopIdAndWarehouseId($enterpriseId, $shopId)
    {
        $warehouseId = $this->cache->hget($this->shopIdRelationWarehouseIdKey.'::'.$enterpriseId, $shopId);
        return $warehouseId ? $warehouseId : '';
    }
    /*******************  end:商铺和仓库的关系   **************/


    /*******************权限缓存 (无序集合)  **************/
    /**
     * 权限新增
     * @param $id
     * @param $value
     * @return mixed
     */
    public function addAuthority($value)
    {
        return $this->cache->sadd($this->authority, strtolower($value));
    }

    /**
     * 权限查询
     * @param $value
     * @return mixed
     */
    public function getAuthority($value)
    {
        return $this->cache->sismember($this->authority, strtolower($value));
    }

    /**
     * 权限删除
     * @param $value
     * @return mixed
     */
    public function delAuthority($value)
    {
        return $this->cache->srem($this->authority, strtolower($value));
    }

    /***********************超级管理员角色*******************
     * 新增
     * @param $enterpriseId
     * @param $value
     * @return mixed
     */
    public function addAdministrator($enterpriseId, $value)
    {
        return $this->cache->sadd($this->Administrator.'::'.$enterpriseId, $value);
    }

    /**
     * 查询
     * @param $enterpriseId
     * @param $value
     * @return mixed
     */
    public function getAdministrator($enterpriseId, $value)
    {
        return $this->cache->sismember($this->Administrator.'::'.$enterpriseId, $value);
    }

    /**
     * 删除
     * @param $enterpriseId
     * @param $value
     * @return mixed
     */
    public function delAdministrator($enterpriseId, $value)
    {
        return $this->cache->srem($this->Administrator.'::'.$enterpriseId, $value);
    }

    /***********************角色绑定权限*******************
     * 新增
     * @param $enterpriseId
     * @param $roleId
     * @param $value
     * @return mixed
     */
    public function addAuthorityBindRole($enterpriseId, $roleId,  $value)
    {
        return $this->cache->sadd($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId, strtolower($value));
    }

    /**
     * 查询
     * @param $enterpriseId
     * @param $roleId
     * @param $value
     * @return mixed
     */
    public function getAuthorityBindRole($enterpriseId, $roleId, $value)
    {
        return $this->cache->sismember($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId, strtolower($value));
    }

    /**
     * 删除
     * @param $enterpriseId
     * @param $roleId
     * @param $value
     * @return mixed
     */
    public function delAuthorityBindRole($enterpriseId, $roleId,  $value = '')
    {
        if($value){
            return $this->cache->srem($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId, strtolower($value));
        }else{
            return $this->cache->del($this->authorityBindRole.'::'.$enterpriseId.'::'.$roleId);
        }
    }
}