package com.ruoyi.web.modules.account.service.impl;

import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.account.constant.MemberConstants;
import com.ruoyi.web.modules.account.service.IMemberTokenService;

/**
 * 会员 Token（Redis）
 */
@Service
public class MemberTokenServiceImpl implements IMemberTokenService
{
    @Autowired
    private RedisCache redisCache;

    @Override
    public String createToken(Long memberId)
    {
        String token = UUID.randomUUID().toString().replace("-", "");
        redisCache.setCacheObject(MemberConstants.REDIS_TOKEN_PREFIX + token, memberId,
                MemberConstants.TOKEN_EXPIRE_DAYS, TimeUnit.DAYS);
        return token;
    }

    @Override
    public Long getMemberIdByToken(String token)
    {
        if (StringUtils.isEmpty(token))
        {
            return null;
        }
        return redisCache.getCacheObject(MemberConstants.REDIS_TOKEN_PREFIX + token);
    }

    @Override
    public void removeToken(String token)
    {
        if (StringUtils.isNotEmpty(token))
        {
            redisCache.deleteObject(MemberConstants.REDIS_TOKEN_PREFIX + token);
        }
    }
}
