package com.hzs.system.tools.service.impl;

import com.hzs.common.core.constant.CacheToolsConstants;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.uuid.IdUtils;
import com.hzs.system.tools.service.IIdempotentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 解决系统幂等性的公共方法的实现类
 * @author: sui q
 * @time: 2022/8/27 13:49
 * @description:
 * @classname: IdempotentServiceImpl
 * @package_name: com.hzs.system.tools.service.impl
 */
@Service
public class IdempotentServiceImpl implements IIdempotentService {

    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    /**
     * 生成唯一的token
     * @Description:
     * @param
     * @return: String
     * @throws
     * @Author: sui q
     * @Date: 2022/8/27 13:47
     */
    @Override
    public String generateToken() {
        String token = IdUtils.fastSimpleUUID();
        redisService.setCacheObject(CacheToolsConstants.IDEMPOTENT_TOKEN+token,
                token, EXPIRE_TIME, TimeUnit.MINUTES);
        return token;
    }

    /**
     * 验证token，是否存在系统中
     * @Description:
     * @param token
     * @return: Boolean
     * @throws
     * @Author: sui q
     * @Date: 2022/8/27 13:48
     */
    @Override
    public Boolean validateToken(String token) {
        String redisToken = redisService.getCacheObject(CacheToolsConstants.IDEMPOTENT_TOKEN + token);
        if(StringUtils.isEmpty(redisToken) || !redisToken.equals(token)){
            return false;
        }
        return true;
    }

    /**
     * 刷新token
     * @Description:
     * @param token
     * @return: void
     * @throws
     * @Author: sui q
     * @Date: 2022/8/27 13:48
     */
    @Override
    public void refreshToken(String token) {

    }
}
