<?php

namespace Heanup\Frame\Package\Counter;

/**
 * 计数器类,使用hash数据结构
 * redis连不上server会抛出异常!!
 */
class Hash extends \Heanup\Frame\Package\Counter
{
    const CONFIG_CLASS = 'Redis\\Counter';
    const PREFIX = '';
    const EXPIRE = 2592000; // 30 天

    protected static $hashKeyList = array();

    /**
     * 批量获取一系列的id
     * @param $idList
     * @return array
     */
    public static function hBatchGetAll($idList)
    {
        $data = array();
        foreach ($idList as $id) {
            $data[$id] = self::hGetAll($id);
        }
        return $data;
    }

    /**
     * 读取单个hash
     * @param $id
     * @return array
     */
    public static function hGetAll($id)
    {
        $key = self::getKey($id);
        try {
            $result = self::getConnection()->hMGet($key, self::getHashKeyList());
            foreach ($result as $k => $v) {
                $result[$k] = intval($v);
            }
        } catch (\Exception $ex) {
            $result = null;
        }
        return $result;
    }

    /**
     * @param $id
     * @param $hashKey
     * @param int $value
     * @return bool
     */
    public static function hSet($id, $hashKey, $value)
    {
        $key = self::getKey($id);
        try {
            $result = self::getConnection(true)->hSet($key, $hashKey, $value);
        } catch (\Exception $ex) {
            $result = false;
        }
        return $result;
    }

    /**
     * 加1
     * @param $id
     * @param $hashKey
     * @param int $value 负数时减少
     * @return int
     */
    public static function hIncrBy($id, $hashKey, $value = 1)
    {
        $key = self::getKey($id);
        try {
            $result = self::getConnection(true)->hIncrBy($key, $hashKey, $value);
        } catch (\Exception $ex) {
            $result = false;
        }
        return $result;
    }

    /**
     * 删除
     * @param $id
     * @return bool
     */
    public static function delete($id)
    {
        $key = self::getKey($id);
        try {
            $result = self::getConnection(true)->delete($key);
        } catch (\Exception $ex) {
            $result = false;
        }
        return $result;
    }

    /**
     * 设置过期时间
     * @param $id
     * @param $expire
     * @return mixed
     */
    public static function setExpire($id, $expire = null)
    {
        if (is_null($expire)) {
            $class = get_called_class();
            $expire = $class::EXPIRE;
        } else {
            $expire = intval($expire);
        }
        $key = self::getKey($id);
        try {
            $result = self::getConnection(true)->expire($key, $expire);
        } catch (\Exception $ex) {
            $result = false;
        }
        return $result;
    }

    /**
     * 获取hashKey列表
     * @return mixed
     */
    protected static function getHashKeyList()
    {
        $class = get_called_class();
        $hashKeyList = $class::$hashKeyList;
        return $hashKeyList;
    }

    /**
     * 获取连接池
     * @param bool $isMaster
     * @return \Redis
     */
    protected static function getConnection($isMaster = false)
    {
        $class = get_called_class();
        $configClass = $class::CONFIG_CLASS;
        $redis = new \Heanup\Frame\Library\Redis($configClass);
        $connection = $redis->getConnection($isMaster);
        return $connection;
    }
}