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;

/**
 * 解决系统幂等性的公共方法的实现类
 */
@Service
public class IdempotentServiceImpl implements IIdempotentService {

    @Autowired
    private RedisService redisService;

    @Override
    public String generateToken() {
        String token = IdUtils.fastSimpleUUID();
        redisService.setCacheObject(CacheToolsConstants.IDEMPOTENT_TOKEN + token,
                token, EXPIRE_TIME, TimeUnit.MINUTES);
        return token;
    }

    @Override
    public Boolean validateToken(String token) {
        String redisToken = redisService.getCacheObject(CacheToolsConstants.IDEMPOTENT_TOKEN + token);
        return !StringUtils.isEmpty(redisToken) && redisToken.equals(token);
    }

    @Override
    public void refreshToken(String token) {

    }

}
