package com.xhpc.common.data.redis;

import com.xhpc.common.api.PileOrderService;
import com.xhpc.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class StaticBeanUtil {

    public static RedisService REDIS;
    public static PileOrderService ORDER_SVC;

    @Autowired
    public PileOrderService pileOrderService;

    @Autowired
    public RedisService redisService;

    @PostConstruct
    public void init() {

        ORDER_SVC = pileOrderService;
        REDIS = redisService;
    }

    public synchronized static String seqHex(String key) {

        String upperCode = "";
        RedisAtomicLong counter = new RedisAtomicLong(key, REDIS.redisTemplate.getConnectionFactory());
        long result = counter.incrementAndGet();
        upperCode = String.format("%04X", result);
        if (upperCode.equals("FFFF")) {
            REDIS.deleteObject(key);
        }
        return upperCode;
    }

    public synchronized static String seqDec(String key) {

        String upperCode = "";
        RedisAtomicLong counter = new RedisAtomicLong(key, REDIS.redisTemplate.getConnectionFactory());
        long result = counter.incrementAndGet();
        upperCode = String.format("%04d", result);
        if (upperCode.equals("9999")) {
            REDIS.deleteObject(key);
        }
        return upperCode;
    }

    public synchronized static String seq18Dec(String key) {

        String upperCode = "";
        RedisAtomicLong counter = new RedisAtomicLong(key, REDIS.redisTemplate.getConnectionFactory());
        long result = counter.incrementAndGet();
        upperCode = String.format("%018d", result);
        return upperCode;
    }

}
