package kr.gmtc.tss.sqllog;

import java.text.SimpleDateFormat;
import java.util.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import kr.gmtc.tss.data.vo.AsdeTrackVO;
import kr.gmtc.tss.main.MainServer;

public class SqlLogger implements Runnable {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private Queue<AsdeTrackVO> sqlLogAsdeTrackQueue;
	private Queue<AsdeTrackVO> sqlLogAsdeTrackLastQueue;
	private boolean isRunning=true;
	private int listChunkSize= 100;
	private long sleepMillis;
	
	public SqlLogger(Queue<AsdeTrackVO> sqlLogAsdeTrackQueue,Queue<AsdeTrackVO> sqlLogAsdeTrackLastQueue, int listChunkSize, long sleepMillis) {
		this.sqlLogAsdeTrackQueue = sqlLogAsdeTrackQueue; 
		this.sqlLogAsdeTrackLastQueue = sqlLogAsdeTrackLastQueue;
		this.listChunkSize = listChunkSize;
		this.sleepMillis = sleepMillis;
	}
	
	@Override
	public void run() {
		isRunning=true;
		
		while (isRunning) {
			try {
				AsdeTrackVO []  chunkArr = getChunkTrackArr(sqlLogAsdeTrackQueue);
				for (int i = 0; i < chunkArr.length; i++) {
					logger.error(getInsertAsdeSql(chunkArr[i]));
				}

//				chunkArr = getChunkTrackArr(sqlLogAsdeTrackLastQueue);
//				
//				for (int i = 0; i < chunkArr.length; i++) {
//					logger.error(getInsertLastAsdeSql(chunkArr[i]));
//				}
			} catch (Exception e) {
				logger.info("-- [SqlLogger] LogSave error : " +e.getLocalizedMessage());
			} 
			
			sleep(sleepMillis);
		}
	}
	
	private AsdeTrackVO [] getChunkTrackArr(Queue<AsdeTrackVO> sqlQueue) {
		int loopCnt=0;
		
		if(sqlQueue.size()>listChunkSize) {
			loopCnt=listChunkSize;
		}else {
			loopCnt=sqlQueue.size();
		}
		
		AsdeTrackVO [] chunkArr = new AsdeTrackVO[loopCnt];
		for (int i = 0; i < loopCnt; i++) {
			chunkArr[i]=sqlQueue.poll();
		}
		return chunkArr;
	}

	private String getInsertAsdeSql(AsdeTrackVO vo) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		String now_dt = format.format(vo.getRecptnDt());
		String tableName = "SACP_ASDE_TRACK";
		//String tableName = "SACP_ASDE_TRACK_" + now_dt;
		
		StringBuilder sb = new StringBuilder();
		
		sb.append("INSERT INTO ");
		sb.append(tableName);
		sb.append(" ( RECPTN_DT, "
				+ "TRGT_ID, "
				+ "MODE_S_CD, "
				+ "SSR_CD, "
				+ "CLSGN, "
				+ "AC_REG_NO, "
				+ "TRACK_NO, "
				+ "CAT_TY, "
				+ "LAT, "
				+ "LON, "
				+ "SPD, "
				+ "COS, "
				+ "TRGT_TY, "
				+ "REGISTER_ID)");
		sb.append(" VALUES (");
		sb.append("'"); sb.append(vo.getRecptnDt()); sb.append("',");
		sb.append("'"); sb.append(vo.getTrgtId()); sb.append("',");
		sb.append("'"); sb.append(vo.getModeScd()); sb.append("',");
		sb.append("'"); sb.append(vo.getSsrCd()); sb.append("',");
		sb.append("'"); sb.append(vo.getClsgn()); sb.append("',");
		sb.append("'"); sb.append(vo.getTailNo()); sb.append("',");
		sb.append(vo.getTrackNo()); sb.append(",");
		sb.append(vo.getCatTy()); sb.append(",");
		sb.append(vo.getLat()); sb.append(",");
		sb.append(vo.getLon()); sb.append(",");
		sb.append(vo.getSpd()); sb.append(",");
		sb.append(vo.getCos()); sb.append(",");
		sb.append("'"); sb.append(vo.getTrgtTy()); sb.append("',");
		sb.append("'SYSTEM')");
		//sb.append("\n");
		
		return sb.toString();
	}

	private String getInsertLastAsdeSql(AsdeTrackVO vo) {
		StringBuilder sb = new StringBuilder();
		if(MainServer.LAST_DATA_CNT > 0) {
			// update
			sb.append("	UPDATE SACP_ASDE_TRACK_LAST SET");
			//sb.append("	 SET AC_REG_NO = ");	sb.append("'"); sb.append(vo.getAcRegNo()); sb.append("'");
			sb.append("	CLSGN = ");			sb.append("'"); sb.append(vo.getClsgn()); sb.append("'");
			sb.append("	, SSR_CD = ");			sb.append("'"); sb.append(vo.getSsrCd()); sb.append("'");
			sb.append("	, MODE_S_CD = ");		sb.append("'"); sb.append(vo.getModeScd()); sb.append("'");
			sb.append("	, RECPTN_DT = ");		sb.append("'"); sb.append(vo.getRecptnDt()); sb.append("'");
			sb.append("	, CAT_TY = ");			sb.append("'"); sb.append(vo.getCatTy()); sb.append("'");
			sb.append("	, LAT = ");				sb.append(vo.getLat());
			sb.append("	, LON = ");				sb.append(vo.getLon());
			sb.append("	, SPD = ");				sb.append(vo.getSpd());
			sb.append("	, COS = ");				sb.append(vo.getCos());
			//sb.append("	, ALT = ");				sb.append(vo.getAlt());
			//sb.append("	, VIRTL_TRGT_AT = ");	sb.append("'"); sb.append(vo.getVirtlTrgtAt()); sb.append("'");
			//sb.append("	, TRACK_CRRCT_AT = ");	sb.append("'"); sb.append(vo.getTrackCrrctAt()); sb.append("'");
			//sb.append("	, TRACK_CNFDNC = ");	sb.append("'"); sb.append(vo.getTrackCnfdnc()); sb.append("'");
			sb.append("	, UPDUSR_ID = ");		sb.append("'");sb.append(vo.getRegisterId());sb.append("'");
			sb.append("	, UPDT_DT = TO_TIMESTAMP(SYSDATE,'yyyyMMdd')");
			sb.append("	WHERE TRGT_ID = "); 	sb.append("'");sb.append(vo.getTrgtId());sb.append("')");
		} else {
			// insert
			sb.append(" INSERT INTO SACP_ASDE_TRACK_LAST");
			sb.append("  (TRGT_ID, AC_REG_NO, CLSGN, SSR_CD, MODE_S_CD, RECPTN_DT, CAT_TY, LAT, LON, SPD, COS, ALT, VIRTL_TRGT_AT, TRACK_CRRCT_AT, TRACK_CNFDNC, REGISTER_ID)");
			sb.append("	VALUES (");
			sb.append("'"); sb.append(vo.getTrgtId()); sb.append("',");
			//sb.append("'"); sb.append(vo.getAcRegNo()); sb.append("',");
			sb.append("'"); sb.append(vo.getClsgn()); sb.append("',");
			sb.append("'"); sb.append(vo.getSsrCd());sb.append("',");
			sb.append("'"); sb.append(vo.getModeScd()); sb.append("',");
			sb.append("'"); sb.append(vo.getRecptnDt()); sb.append("',");
			sb.append("'"); sb.append(vo.getCatTy()); sb.append("',");
			sb.append(vo.getLat()); sb.append(",");
			sb.append(vo.getLon()); sb.append(",");
			sb.append(vo.getSpd()); sb.append(",");
			sb.append(vo.getCos()); sb.append(",");
			//sb.append(vo.getAlt()); sb.append(",");
			//sb.append("'"); sb.append(vo.getVirtlTrgtAt()); sb.append("',");
			//sb.append("'"); sb.append(vo.getTrackCrrctAt()); sb.append("',");
			//sb.append("'"); sb.append(vo.getTrackCnfdnc()); sb.append("',");
			sb.append("'"); sb.append(vo.getRegisterId()); sb.append("')");
		}
		
		return sb.toString();
	}
	
	private void sleep(long millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			logger.debug("[SqlLogger] sleep error : "+e.getLocalizedMessage());
		}
	}

	public void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}
}
