package com.eactive.eai.custom.message;

import com.eactive.eai.common.server.Keys;
import com.eactive.eai.common.util.DatetimeUtil;
import com.eactive.eai.common.util.StringUtil;

public class GUIDGeneratorKBANK {
    private static String UNIQUE_NUM;
    private static int seq = 1;
    
    static {
    	String uniqueValue = System.getProperty(Keys.SERVER_KEY);
    	if(uniqueValue.length() < 8) {
    		uniqueValue = StringUtil.stringFormat(uniqueValue, false, '0', 8);
    	}else if(uniqueValue.length() > 8){
    		uniqueValue = uniqueValue.substring(uniqueValue.length() - 8);
    	}
    	UNIQUE_NUM = uniqueValue.toUpperCase();
    }
    
    public synchronized static String makeGUID() {
    	StringBuilder sb = new StringBuilder();
    	sb.append(DatetimeUtil.getCurrentDateTime());
    	sb.append(UNIQUE_NUM);
    	
    	char[] formatSeq = {'0','0','0','0','0','0','0','0'};
    	char[] seqCharArray = String.valueOf(seq).toCharArray();
    	int destPos = formatSeq.length - seqCharArray.length;
    	System.arraycopy(seqCharArray, 0, formatSeq, destPos, seqCharArray.length);
    	
    	sb.append(formatSeq);
    	
    	seq ++;
    	if(seq > 99999999) {
    		seq = 1;
    	}
    	
    	return sb.toString() ;
    }
}
