package net.yyc.common.sequence.sequence.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

import net.yyc.common.sequence.exception.SeqException;
import net.yyc.common.sequence.sequence.Sequence;

/**
 *
 */
public class TimeAndRandomSequence implements Sequence {

    private final String prefix;

    public TimeAndRandomSequence() {
        this("");
    }

    public TimeAndRandomSequence(String prefix) {
        this.prefix = prefix == null ? "" : prefix;
    }

	@Override
	public long nextValue() throws SeqException {

        return Long.valueOf(nextNo());
	}

	/**
	 * 下一个生成序号（带格式）
	 * @return
	 * @throws SeqException
	 */
	@Override
	public String nextNo() throws SeqException {

        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String r = String.format("%04d", new Random().nextInt(10000));
		return prefix + time + r;
	}

}
