package net.lab1024.sa.base.module.support.repeatsubmit.ticket;

import net.lab1024.sa.base.module.support.repeatsubmit.annoation.RepeatSubmit;
import org.springframework.data.redis.core.ValueOperations;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
 * 凭证（redis实现）
 *
 * @Author YouChain: 罗伊
 * @Date 2020-11-25 20:56:58 * @Email huoj@youchain56.com
 */
public class RepeatSubmitRedisTicket extends AbstractRepeatSubmitTicket {

    private final ValueOperations<String, String> redisValueOperations;

    public RepeatSubmitRedisTicket(ValueOperations<String, String> redisValueOperations,
                                   Function<String, String> ticketFunction) {
        super(ticketFunction);
        this.redisValueOperations = redisValueOperations;
    }

    @Override
    public Long getTicketTimestamp(String ticket) {
        String ticketLastTime = redisValueOperations.get(ticket);
        return ticketLastTime == null ? null : Long.valueOf(ticketLastTime);
    }

    @Override
    public void putTicket(String ticket) {
        redisValueOperations.getOperations().delete(ticket);
        this.getTicketTimestamp(ticket);
    }

}
