package com.shkj.wms.utils;

import com.shkj.common.core.redis.RedisCache;
import com.shkj.common.core.text.Convert;
import com.shkj.common.utils.StringUtils;
import com.shkj.common.utils.spring.SpringUtils;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.util.Date;

/**
 * @author lch
 */
public class IntIdUtil {



    private static Date date = new Date();
    private static StringBuilder buf = new StringBuilder();
//    private static int seq = 0;
    private static final int ROTATION = 99999;

    public static synchronized String next() {

        Integer seq = Convert.toInt(SpringUtils.getBean(RedisCache.class).getCacheObject("seq"), 0);

        if (seq > ROTATION) {
            seq = 0;
        }
//        buf.delete(0, buf.length());
//        date.setTime(System.currentTimeMillis());
        String str = String.format("%1$05d", seq++);
        SpringUtils.getBean(RedisCache.class).setCacheObject("seq", seq);
        System.out.println("redis的值为"+seq);
        return str;
    }

    public static Long generateIntId() {
        //生成规则 年份后两位-10【2位】 + 1年内第几天【3位】 + 随机数【5位】
        LocalDate now = LocalDate.now();
        // 因为int的最大数是2147483647 所以前2位=当前年-10
        String yearStr = now.getYear() - 10 + "";
        yearStr = yearStr.substring(2);
        StringBuilder id = new StringBuilder(yearStr + String.format("%1$03d", now.getDayOfYear() )+ next());
        return Long.parseLong(id.toString());
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10000; i++) {
            Long aLong = IntIdUtil.generateIntId();
            System.out.println(aLong + "==>" + Long.parseLong(aLong.intValue() + ""));
        }
    }


}
