package com.qmth.teachcloud.common.cache;

import com.qmth.teachcloud.common.bean.dto.UserAuthenticationDto;
import com.qmth.teachcloud.common.contant.SpringContextHolder;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.service.CommonCacheService;
import com.qmth.teachcloud.common.util.RedisUtil;

import java.util.concurrent.TimeUnit;

/**
 * @Description: 武大开发鉴权用户临时授权cache
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/6/29
 */
public class ThirdUserAuthCacheUtil {
    private static RedisUtil redisUtil = SpringContextHolder.getBean(RedisUtil.class);

    public static void setAuthCode(UserAuthenticationDto userAuthenticationDto) {
        redisUtil.set(RedisKeyHelper.thirdUserAuth(userAuthenticationDto.getUid()), userAuthenticationDto.getCode(), SystemConstant.REDIS_THIRD_USER_AUTH_EXPIRE_TIME, TimeUnit.SECONDS);
    }

    public static String getAuthCode(String key) {
        return (String) redisUtil.get(RedisKeyHelper.thirdUserAuth(key));
    }

    public static void deleteAuthCode(String key) {
        redisUtil.delete(RedisKeyHelper.thirdUserAuth(key));
    }

    public static void setAuthReturnUrl(String key, String url) {
        redisUtil.set(RedisKeyHelper.thirdUserAuthReturnUrl(key), url, SystemConstant.REDIS_DEFAULT_EXPIRE_TIME + 180L, TimeUnit.SECONDS);
    }

    public static String getAuthReturnUrl(String key) {
        return (String) redisUtil.get(RedisKeyHelper.thirdUserAuthReturnUrl(key));
    }

//    public static void deleteAuthReturnUrl(String key) {
//        redisUtil.delete(RedisKeyHelper.thirdUserAuthReturnUrl(key));
//    }

    public static void deleteCache(Long userId) {
        CommonCacheService commonCacheService = SpringContextHolder.getBean(CommonCacheService.class);
        commonCacheService.removeUserCache(userId);
        commonCacheService.removeUserAuthCache(userId);
        commonCacheService.removeUserMenuCache(userId);
        commonCacheService.removeUserRolePrivilegeCache(userId);
    }
}
