package cn.com.countrygarden.bi.main.util;

import java.net.InetAddress;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class SequenceUUID {
    /**
     * 获得Sequence的静态方法
     * @return
     */
    public static String getSequence() {
        return new SequenceUUID().getUUID();
    }

    protected static String midValue = null;

    public String getUUID() {
        long timeNow = System.currentTimeMillis();
        int timeLow = (int) timeNow & 0xFFFFFFFF;

        if (midValue == null) {
            try {
                InetAddress inet = InetAddress.getLocalHost();
                byte[] bytes = inet.getAddress();
                String hexInetAddress = hexFormat(getInt(bytes), 8);
                String thisHashCode = hexFormat(System.identityHashCode(this), 8);
                midValue = hexInetAddress + thisHashCode;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        SecureRandom oRandom = new SecureRandom();
        int node = oRandom.nextInt();

        return (hexFormat(timeLow, 8) + midValue + hexFormat(node, 8));
    }

    private static int getInt(byte[] bytes) {
        int i = 0;
        int j = 24;

        for (int k = 0; j >= 0; k++) {
            int l = bytes[k] & 0xff;
            i += (l << j);
            j -= 8;
        }

        return i;
    }

    private static String hexFormat(int i, int j) {
        String s = Integer.toHexString(i);

        return padHex(s, j) + s;
    }

    private static String padHex(String s, int i) {
        StringBuffer tmpBuffer = new StringBuffer();

        if (s.length() < i) {
            for (int j = 0; j < (i - s.length()); j++) {
                tmpBuffer.append('0');
            }
        }

        return tmpBuffer.toString();
    }


    /**
     * 生成随机文件名：当前年月日时分秒+五位随机数
     *
     * @return
     */
    public static String getRandomFileName() {

        SimpleDateFormat simpleDateFormat;
        simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String str = simpleDateFormat.format(date);
        Random random = new Random();
        int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
        return str + rannum;// 当前时间
    }
}
