<?php
namespace Jindouyun\Cache;

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

class EnterpriseCache
{
    private $cache;
    protected $UserRelationEnterpriseKey = 'userRelationEnterprise';//用户id和公司id关联缓存
    protected $EnterpriseIdRelationTokenKey = 'enterpriseIdRelationTokenKey';
    protected $StaffTotalKey = 'EnterpriseStaffTotal';//企业员工总数

    protected $enterpriseSettingKey = 'EnterpriseSetting';//企业设置


    // 企业相关配置信息数据
    protected $enterpriseInfoData = 'enterpriseInfoData';

    public function __construct()
    {
        $this->cache = Factory::cache('user');
    }
    /*******************  start:userCenterId与企业Id的关联   value:enterpriseId  score:roleType   **************/

    /**
     * 增：
     * 缓存userCenterId和enterpriseId关系数据
     * @param $uid int 用户id
     * @param $roleType int 角色
     * @param $enterpriseId  企业id
     * @return
     */
    public function cacheUserCenterIdAndEnterpriseId($uid, $roleType, $enterpriseId)
    {
        $this->cache->zadd($this->UserRelationEnterpriseKey.'::'.$uid, $roleType, $enterpriseId);
    }

    /**
     * 查：
     * 获取用户和企业的关联关系数组
     *
     * @param $userCenterId
     * @param int $enterpriseId 公司ID
     *
     * @return boolean
     */
    public function getUserCenterIdAndEnterpriseIds($userCenterId)
    {
        $result =  $this->cache->zrangebyscore($this->UserRelationEnterpriseKey.'::'.$userCenterId, 1,2, array('withscores' => TRUE));
        return $result;
    }

    /**
     * 删：
     * 删除userCenterId和企业id的绑定关系
     * @param $userCenterId
     * @param int $enterpriseId 被删除的企业id
     */
    public function deleteEnterprise($userCenterId, $enterpriseId)
    {
        $this->cache->zrem($this->UserRelationEnterpriseKey.'::'.$userCenterId , $enterpriseId);
    }
    /*******************  end:userCenterId与企业Id的关联   value:enterpriseId  score:roleType   **************/


    /*******************  start:企业token与企业id的关联   value:Token  score:enterpriseId   **************/

    /**
     * 缓存 企业id 和 token 关联关系
     * @param $enterpriseId
     * @param $token
     * @return ResultWrapper
     */
    public function enterpriseIdAndTokenCache($enterpriseId, $token)
    {
        return $this->cache->zadd($this->EnterpriseIdRelationTokenKey, $enterpriseId, $token);
    }

    /**
     * 根据token获取企业id  反解token需要
     * @param $token
     * @return
     */
    public function getEnterpriseIdByToken($token)
    {
        return $this->cache->zscore($this->EnterpriseIdRelationTokenKey, $token);
    }

    /*******************  end:企业token与企业id的关联   value:Token  score:enterpriseId   **************/

    /**
     * 缓存企业数据
     */
    public function EnterpriseDataCache($enterpriseId, $field, $value)
    {
        return $this->cache->hset($enterpriseId, $field, $value);
    }

    /**
     * 获取企业数据
     */
    public function getEnterpriseData($enterpriseId, $field)
    {
        return $this->cache->hget($enterpriseId, $field);
    }

    /******************************** 缓存企业下员工总数(未删除) **************************/
    public function setStaffTotalByEnterpriseId($enterpriseId, $total)
    {
        $result = $this->cache->zadd($this->StaffTotalKey, $total, $enterpriseId);
        return $result;
    }
    public function getStaffTotalByEnterpriseId($enterpriseId)
    {
        $result = $this->cache->zscore($this->StaffTotalKey, $enterpriseId);
        return $result;
    }
    public function delStaffTotalByEnterpriseId($enterpriseId)
    {
        $result = $this->cache->del($this->StaffTotalKey, $enterpriseId);
        return $result;
    }
    /******************************** 缓存企业下商铺总数(未删除) **************************/
    public function setShopTotalByEnterpriseId($enterpriseId, $total)
    {
        $result = $this->cache->zadd($this->StaffTotalKey, $total, $enterpriseId);
        return $result;
    }
    public function getShopTotalByEnterpriseId($enterpriseId)
    {
        $result = $this->cache->zscore($this->StaffTotalKey, $enterpriseId);
        return $result;
    }
    public function delShopTotalByEnterpriseId($enterpriseId)
    {
        $result = $this->cache->del($this->StaffTotalKey, $enterpriseId);
        return $result;
    }

    /******************************* 缓存企业设置数据 ************************************/
    /**
     * 添加企业设置
     * @param $key
     * @param $value
     * @return bool
     */
    public function setEnterpriseSetting($key,$value)
    {
        $data = json_encode($value);
        $result = $this->cache->hset($this->enterpriseSettingKey, $key, $data);
        if (!$result) {
            return false;
        }
        return true;
    }

    /**
     * 获取企业设置
     * @param $key
     * @return array|mixed
     */
    public function getEnterpriseSetting($key)
    {
        $result = $this->cache->hget($this->enterpriseSettingKey, $key);
        if (!$result) return [];
        return json_decode($result, true);
    }

    /**
     * 删除企业设置
     * @param $key
     * @return mixed
     */
    public function delEnterpriseSetting($key)
    {
        return $this->cache->hdel($this->enterpriseSettingKey, $key);
    }

}