<?php


namespace App\Cache;


use App\Consts\ErrorConst;
use App\Libs\Utils;
use Illuminate\Support\Facades\Redis;

class StatisticCache
{
    /**
     * 以下传参的通用说明
     * key_name的具体传参值从CacheKeys.php中获取,以statistic为前缀
     * day的值默认为当天,可通过传不同日期增删改对应的值,day的格式为date('Ymd')
     */


    /**
     * 获取pv
     * @param $key_name
     * @param $day
     * @return mixed
     */
    public static function getPV($key_name, $day = ''): int
    {
        $key_name = 'statistic.' . $key_name . '_pv';
        if (!$day) $day = date('Ymd');
        $cacheKey = Utils::getCacheKey($key_name, [$day]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
        $result = Redis::get($cacheKey);
        return $result ? $result : 0;
    }

    /**
     * 设置pv
     *
     * @param $key_name
     * @param $day
     * @return mixed
     */
    public static function setPV($key_name, $day = '')
    {
        $key_name = 'statistic.' . $key_name . '_pv';
        if (!$day) $day = date('Ymd');
        $cacheKey = Utils::getCacheKey($key_name, [$day]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
        return Redis::incr($cacheKey);
    }

    /**
     * 删除pv
     *
     * @param $key_name
     * @param $day
     * @return mixed
     */
    public static function delPV($key_name, $day = '')
    {
        $key_name = 'statistic.' . $key_name . '_pv';
        if (!$day) $day = date('Ymd');
        $cacheKey = Utils::getCacheKey($key_name, [$day]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
        return Redis::del($cacheKey);
    }

    /**
     * 获取uv
     *
     * @param $key_name
     * @param $day
     * @return mixed
     */
    public static function getUV($key_name, $day = ''): int
    {
        $key_name = 'statistic.' . $key_name . '_uv';
        if (!$day) $day = date('Ymd');
        $cacheKey = Utils::getCacheKey($key_name, [$day]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
//        $result = Redis::pfcount($cacheKey);
        $result = Redis::scard($cacheKey);
        return $result ? $result : 0;
    }

    /**
     * 设置uv
     *
     * @param $key_name
     * @param $day
     * @param $uid
     * @return mixed
     */
    public static function setUV($key_name, $uid, $day = '')
    {
        $key_name = 'statistic.' . $key_name . '_uv';
        if (!$day) $day = date('Ymd');
        $cacheKey = Utils::getCacheKey($key_name, [$day]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
//        return Redis::pfadd($cacheKey, $uid);
        return Redis::sadd($cacheKey, $uid);
    }

    /**
     * 删除uv
     *
     * @param $key_name
     * @param $day
     * @return mixed
     */
    public static function delUV($key_name, $day = '')
    {
        $key_name = 'statistic.' . $key_name . '_uv';
        if (!$day) $day = date('Ymd');
        $cacheKey = Utils::getCacheKey($key_name, [$day]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
        return Redis::del($cacheKey);
    }

    /**
     * 获取模板uv
     *
     * @param $template_id
     * @return mixed
     */
    public static function getTemplateUV($template_id): int
    {
        $key_name = 'statistic.template_uv';
        $cacheKey = Utils::getCacheKey($key_name, [$template_id]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
//        $result = Redis::pfcount($cacheKey);
        $result = Redis::scard($cacheKey);
        return $result ? $result : 0;
    }

    /**
     * 设置模板uv
     *
     * @param $uid
     * @param $template_id
     * @return mixed
     */
    public static function setTemplateUV($uid, $template_id = 0)
    {
        $key_name = 'statistic.template_uv';
        $cacheKey = Utils::getCacheKey($key_name, [$template_id]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
//        return Redis::pfadd($cacheKey, $uid);
        return Redis::sadd($cacheKey, $uid);
    }

    /**
     * 删除模板uv
     *
     * @param $template_id
     * @return mixed
     */
    public static function delTemplateUV($template_id = 0)
    {
        $key_name = 'statistic.template_uv';
        $cacheKey = Utils::getCacheKey($key_name, [$template_id]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
        return Redis::del($cacheKey);
    }

    /**
     * 获取订阅统计总记录数目(按日)
     *
     * @param $day
     * @return mixed
     */
    public static function getSubscribeCount($day = '')
    {
        if (!$day) $day = date('Ymd');
        $cacheKey = Utils::getCacheKey('statistic.subscribe_books', [$day]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
        $result = Redis::llen($cacheKey);
        return $result ? $result : 0;
    }

    /**
     * 获取订阅统计(按日)
     *
     * @param $day
     * @param $start_index
     * @param $end_index
     * @return mixed
     */
    public static function getSubscribe($day = '', $start_index = 0, $end_index = -1)
    {
        if (!$day) $day = date('Ymd');
        $cacheKey = Utils::getCacheKey('statistic.subscribe_books', [$day]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
        $result = Redis::lrange($cacheKey, $start_index, $end_index);
        return $result ? $result : [];
    }

    /**
     * 设置订阅统计(按日)
     *
     * @param $data
     * @param $day
     * @return mixed
     */
    public static function setSubscribe(array $data, $day = '')
    {
        if (!$day) $day = date('Ymd');
        $cacheKey = Utils::getCacheKey('statistic.subscribe_books', [$day]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
        if (!Redis::exists($cacheKey)) {
            $result = Redis::lpush($cacheKey, json_encode($data, true));
            $seven_days_later = date('Y-m-d 23:59:59', strtotime('+7 day'));
            $expire_at = strtotime($seven_days_later) - time();
            Redis::expire($cacheKey, $expire_at);
        } else {
            $result = Redis::lpush($cacheKey, json_encode($data, true));
        }

        return $result;
    }

    /**
     * 删除订阅统计(按日)
     *
     * @param $day
     * @return mixed
     */
    public static function delSubscribe($day = '')
    {
        if (!$day) $day = date('Ymd');
        $cacheKey = Utils::getCacheKey('statistic.subscribe_books', [$day]);
        if (!$cacheKey) Utils::throwError(ErrorConst::REDIS_KEY_NOT_EXIST);
        return Redis::del($cacheKey);
    }
}
