package com.eactive.eai.custom.message;

import java.text.SimpleDateFormat;

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

public final class GUIDGeneratorSBI {

	private static final SimpleDateFormat SDF_YYYYMMDDHHMMSSMI = new SimpleDateFormat("yyyyMMddHHmmssSSS");

	private GUIDGeneratorSBI() {
	}

	/**
	 * 시스템일시(17)+시스템환경코드(1)+시스템코드(3)+시스템고유번호(9)+일련번호(2)
	 * 
	 * @param envDvcd
	 * @return
	 */
	public synchronized static String getGUID(String envDvcd, String systemType) {
		String guid = (SDF_YYYYMMDDHHMMSSMI).format(new java.util.Date()) + envDvcd + systemType + UNIQUE_NUM;

		if (seq < 10) {
			guid = guid + "0" + seq;
		} else {
			guid = guid + seq;
		}

		seq++;
		if (seq > 99) {
			seq = 1;
		}

		return guid;
	}

	private static String UNIQUE_NUM;
	private static int seq = 1;
	static {
		String hostname = "";
		try {
			hostname = java.net.InetAddress.getLocalHost().getHostName();
		} catch (Exception e) {
			hostname = "_______";
		}

		if (hostname.getBytes().length != 7) {
			hostname = StringUtil.stringFormat(hostname, false, '_', 7);
		}

		String serverName = System.getProperty(Keys.SERVER_KEY); // inst.Name
		String instid = "11";
		if (serverName != null && serverName.length() > 2) {
			instid = serverName.substring(serverName.length() - 2, serverName.length());
		}

		UNIQUE_NUM = hostname.toUpperCase() + instid;
	}
}
