<?php
/**
 * redis底层公共类
 * User: 贾济林
 * Date: 2017/01/11
 * Time: 15:51
 * @author:贾济林
 */

namespace App\Redis;

use Illuminate\Support\Facades\Redis;

class MasterRedis
{
    public function exists($key)
    {
        return Redis::exists($key);  //查询key是否存在
    }

    public function getPageLists($key, $nums, $nowPage)
    {
        //起始偏移量
        $offset = $nums * ($nowPage - 1);

        //获取条数
        $totals = $offset + $nums - 1;

        //获取缓存的列表索引并返回
        return $this->getBetweenList($key, $offset, $totals);

    }

    public function getBetweenList($key, $start, $end, $time = '')
    {
        $data = Redis::lrange($key, $start, $end);
        if (!$data) return false;

        // 设置生命周期
        if (!empty($time)) {
            $this->setTime($key, config('setting.list_life_time'));
        }
        return $data;
    }

    public function getHash($key, $time = false)
    {
        $data = Redis::hGetAll($key);
        if (!$data) return false;

        //设置生命周期
        if (empty($time)) {
            $this->setTime($key, config('setting.hash_life_time'));
        } else {
            $this->setTime($key, $time);
        }
        return $data;
    }

    public function getHashFileds($key, $fields, $status = true)
    {
        $i = 0;
        $values = Redis::hMGet($key, $fields);

        if (empty($status)) return $values;

        $data = [];
        foreach ($fields as $field) {
            $data[$field] = $values[$i++];
        }
        return $data;
    }

    public function addHash($key, $data, $time = false)
    {
        if (empty($key) || empty($data)) return false;
        $result = true;
        if (!$this->exists($key)) {
            //写入hash
            if (isset($data['img_url']) &&(!empty($data['img_url']) || is_array($data['img_url']))) unset($data['img_url']);
            if (isset($data['building']) &&(!empty($data['building']) || is_array($data['building']))) unset($data['building']);
            $result = Redis::hMset($key, $data);
        }
        if (!$result) {
            \Log::error('写入hash出错' . $key);
            return false;
        } else {
            //设置生命周期
            if (empty($time)) {
                $this->setTime($key, config('setting.hash_life_time'));
            } else {
                $this->setTime($key, $time);
            }
            return true;
        }
    }

    public function changeOneHash($key, $data)
    {
        //写入hash
        if (!Redis::hMset($key, $data)) return false;
        //设置生命周期
        if (empty($time)) {
            $this->setTime($key, config('setting.hash_life_time'));
        } else {
            $this->setTime($key, $time);
        }
        return true;
    }

    public function delKey($key)
    {
        if (empty($key)) return false;
        return Redis::del($key);
    }

    public function rPushLists($key, $lists, $time = false)
    {
        if (empty($key) || empty($lists)) return false;

        //执行写list操作
        $res = Redis::rpush($key, $lists);
        if (empty($res)) return false;

        //设置生命周期
        if (!empty($time)) {
            $this->setTime($key, config('setting.list_life_time'));
        }
        return $res;
    }

    public function lPushLists($key, $lists, $time = false)
    {
        if (empty($key) || empty($lists)) return false;

        //执行写list操作
        $res = Redis::lpush($key, $lists);
        if (empty($res)) return false;

        //设置生命周期
        if (!empty($time)) {
            $this->setTime($key, config('setting.list_life_time'));
        }
        return $res;
    }

    public function lInsert($key, $position, $old, $new)
    {
        if (empty($key) || empty($position) || empty($old) || empty($new)) return false;

        return \Redis::LINSERT($key, $position, $old, $new);
    }

    public function setTime($key, $time = false)
    {
        if (empty($time)) return Redis::expire($key, config('setting.hash_life_time'));
        return Redis::expire($key, $time);
    }

    public function getLength($key)
    {
        return Redis::llen($key);
    }

    public function delList($key, $guid)
    {
        if ($this->exists($key)) return Redis::lrem($key, 0, $guid);
        return true;
    }

    public function addString($key, $value, $time = false)
    {
        if (empty($key)) return false;
        if (!Redis::Set($key, $value)) return false;
        //设置生命周期
        if (empty($time)) {
            $this->setTime($key, config('setting.string_life_time'));
        } else {
            $this->setTime($key, $time);
        }
        return true;
    }

    public function incre($key)
    {
        if (empty($key)) return false;
        return Redis::incr($key);
    }

    public function hIncrBy($key, $filed, $value)
    {
        return Redis::hIncrBy($key, $filed, $value);
    }

    public function getString($key, $time = false)
    {
        if (empty($key)) return false;
        $data = Redis::get($key);
        if (!$data && $data != 0) return false;

        if ($time == 'old') return $data;
        //设置生命周期
        if (empty($time)) {
            $this->setTime($key, config('setting.string_life_time'));
        } else {
            $this->setTime($key, $time);
        }
        return $data;
    }

    public function destroy()
    {
        return Redis::flushAll();
    }

    public function getKeys($pattern)
    {
        return Redis::keys($pattern);
    }

    public function sadd($key, $string)
    {
        return Redis::sAdd($key, $string);
    }

    public function scard($key)
    {
        return Redis::sCard($key);
    }

    public function srandmember($key, $num)
    {
        return Redis::sRandmember($key, $num);
    }

    public function srem($key, $id)
    {
        return Redis::sRem($key, $id);
    }

    public function checkSadd($key, $value)
    {
        if (!is_string($key) || !is_string($value)) return false;

        return Redis::sIsMember($key, $value);
    }

    public static function setexRedis($key, $val, $time)
    {
        if (!empty($key) && !empty($val)) {
            return Redis::setex($key, $time, $val);
        } else {
            return false;
        }

    }

    public static function hSet($key, $hashKey, $value)
    {
        return Redis::hSet($key, $hashKey, $value);
    }

    public static function getListInIndex($key, $index)
    {
        return Redis::lIndex($key, $index);
    }

}
