package org.security.desk.esb;

import lombok.AllArgsConstructor;
import org.security.common.constant.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

/**
 * @author zj 2022/9/2
 * 获取redis自增id
 */

@Component
public class SeqNOFactory {
	private static RedisTemplate<String, Object> redisTemplate;

	@Autowired
	public  void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
		SeqNOFactory.redisTemplate = redisTemplate;
	}

	public static  String getSeqNo(String tranDate, String tranTm){
		return CommonConstant.SYS_ID + "00" + "01" + tranDate + tranTm + SeqNOFactory.getId();
	}
	public static String getId() {
		RedisAtomicLong redisAtomicLong = new RedisAtomicLong(CommonConstant.SEQ_NO, redisTemplate.getConnectionFactory());
		long increment = redisAtomicLong.getAndIncrement();
		if (increment > 999999999L) {
			redisAtomicLong.set(0L);
		}
		increment = redisAtomicLong.getAndIncrement();
		return String.format("%09d", increment);
	}
}
