<?php


namespace App\Cache;


use App\Consts\BaseConst;
use App\Libs\Utils;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class FinanceCache
{
    /**
     * 获取手机验证码
     *
     * @param $channelId
     * @param $phone
     * @return mixed
     */
    public static function getSmsCode($channelId, $phone)
    {
        $cacheKey = Utils::getCacheKey('finance.sms_code', [$channelId, $phone]);
        return Redis::get($cacheKey);
    }

    /**
     * 设置手机验证码
     *
     * @param $channelId
     * @param $phone
     * @param $code
     * @return bool
     */
    public static function setSmsCode($channelId, $phone, $code)
    {
        $cacheKey = Utils::getCacheKey('finance.sms_code', [$channelId, $phone]);
        Redis::set($cacheKey, $code);
        $ttl = Redis::ttl($cacheKey);
        if ((int)$ttl < 1) {
            // 缓存设置10分钟
            Redis::expire($cacheKey, 600);
            Cache::put($cacheKey, $code, 10);
        }

        return true;
    }

    /**
     * 删除验证码
     *
     * @param $channelId
     * @param $phone
     * @return mixed
     */
    public static function delSmsCode($channelId, $phone)
    {
        $cacheKey = Utils::getCacheKey('finance.sms_code', [$channelId, $phone]);
        Cache::forget($cacheKey);
        return Redis::del($cacheKey);
    }

    /**
     * 添加渠道到当天提现队列
     *
     * @param $chanelId
     * @param $ymd
     * @return bool
     */
    public static function sAddChannelIdToWithDraw($chanelId, $ymd)
    {
        $cacheKey = Utils::getCacheKey('finance.with_draw', [$ymd]);
        Redis::sAdd($cacheKey, $chanelId);
        $ttl = Redis::ttl($cacheKey);
        if ((int)$ttl < 1) {
            Redis::expire($cacheKey, BaseConst::ONE_DAY_SECONDS);
        }

        return true;
    }

    /**
     * 判别渠道是否在日期提现队列中
     *
     * @param $chanelId
     * @param $ymd
     * @return mixed
     */
    public static function checkChannelIdIsInWithDraw($chanelId, $ymd)
    {
        $cacheKey = Utils::getCacheKey('finance.with_draw', [$ymd]);
        return Redis::sIsmember($cacheKey, $chanelId);
    }
}