package com.dxhy.common.utils;

/**
 * * @author shijing
 */
public class LocalTrmSeqNum {
    private static int sequence = 0;
    private static final int LENGTH = 6;

    /**
     * YYYYMMDDHHMMSS+6位自增长码(20位)
     * 
     * @return 返回结果
     */
    public static synchronized String getLocalTrmSeqNum() {
        sequence = sequence >= 999999 ? 1 : sequence + 1;
        String datetime = System.currentTimeMillis() + "";
        String s = Integer.toString(sequence);
        return datetime + addLeftZero(s, LENGTH);
    }

    /**
     * YYYYMMDDHHMMSS+6位自增长码(20位)
     * 
     * @author shijing 2015年6月29日下午1:25:23
     * @return 返回结果
     */
    public static synchronized String getSeqNum() {
        sequence = sequence >= 999999 ? 1 : sequence + 1;
        String s = Integer.toString(sequence);
        return addLeftZero(s, 9);
    }

    /**
     * 左填0
     * 
     * @author shijing 2015年6月29日下午1:24:32
     * @param s
     *            请求参数
     * @param length
     *            请求参数
     * @return 返回结果
     */
    public static String addLeftZero(String s, int length) {
        int old = s.length();
        if (length > old) {
            char[] c = new char[length];
            char[] x = s.toCharArray();
            if (x.length > length) {
                throw new IllegalArgumentException(
                    "Numeric value is larger than intended length: " + s + " LEN " + length);
            }
            int lim = c.length - x.length;
            for (int i = 0; i < lim; i++) {
                c[i] = '0';
            }
            System.arraycopy(x, 0, c, lim, x.length);
            return new String(c);
        }
        return s.substring(0, length);

    }

}
