package kr.gmtc.tss.sqllog;

import java.util.Queue;

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

import kr.gmtc.tss.data.vo.AlarmVO;
import kr.gmtc.tss.data.vo.StandStatusVO;

public class SqlLogger implements Runnable {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private Queue<StandStatusVO> sqlLogStandStatusQueue;
	private Queue<AlarmVO> sqlLogAlarmQueue;;
	
	private boolean isRunning=true;
	private int listChunkSize= 100;
	private long sleepMillis;
	
	public SqlLogger(Queue<StandStatusVO> sqlLogStandStatusQueue, Queue<AlarmVO> sqlLogAlarmQueue, int listChunkSize, long sleepMillis) {
		this.sqlLogStandStatusQueue = sqlLogStandStatusQueue; 
		this.sqlLogAlarmQueue = sqlLogAlarmQueue;
		this.listChunkSize = listChunkSize;
		this.sleepMillis = sleepMillis;
	}
	
	@Override
	public void run() {
		isRunning = true;
		
		while (isRunning) {
			try {
				StandStatusVO []  chunkStatuskArr = getChunkStandStatusArr(sqlLogStandStatusQueue);
				for (int i = 0; i < chunkStatuskArr.length; i++) {
					logger.error(getInsertStandStatusSql(chunkStatuskArr[i]));
				}

				AlarmVO []  chunkAlarmArr = getChunkAlarmArr(sqlLogAlarmQueue);
				
				for (int i = 0; i < chunkAlarmArr.length; i++) {
					logger.error(getInsertAlarmSql(chunkAlarmArr[i]));
				}
			} catch (Exception e) {
				logger.info("-- [SqlLogger] LogSave error : " +e.getLocalizedMessage());
			} 
			
			sleep(sleepMillis);
		}
	}
	
	private StandStatusVO [] getChunkStandStatusArr(Queue<StandStatusVO> sqlQueue) {
		int loopCnt=0;
		
		if(sqlQueue.size()>listChunkSize) {
			loopCnt=listChunkSize;
		}else {
			loopCnt=sqlQueue.size();
		}
		
		StandStatusVO [] chunkArr = new StandStatusVO[loopCnt];
		
		for (int i = 0; i < loopCnt; i++) {
			chunkArr[i]=sqlQueue.poll();
		}
		
		return chunkArr;
	}
	
	private AlarmVO [] getChunkAlarmArr(Queue<AlarmVO> sqlQueue) {
		int loopCnt=0;
		
		if(sqlQueue.size()>listChunkSize) {
			loopCnt=listChunkSize;
		}else {
			loopCnt=sqlQueue.size();
		}
		
		AlarmVO [] chunkArr = new AlarmVO[loopCnt];
		
		for (int i = 0; i < loopCnt; i++) {
			chunkArr[i]=sqlQueue.poll();
		}
		
		return chunkArr;
	}

	private String getInsertStandStatusSql(StandStatusVO vo) {
		String tableName = "SACP_ANALYZE_STAND_STATUS";
		
		StringBuilder sb = new StringBuilder();
		
		sb.append("INSERT INTO ");
		sb.append(tableName);
		sb.append(" (STTUS_DT,STAND_NO,STAND_STTUS_TY,INSTL_PLC,REGISTER_ID)");
		sb.append("VALUES (");
		sb.append("'"); sb.append(""); sb.append("',");
		sb.append("'"); sb.append(vo.getStandNo()); sb.append("',");
		sb.append("'"); sb.append(vo.getStandSttusTy()); sb.append("',");
		sb.append("'"); sb.append(vo.getClsgn()); sb.append("',");
		sb.append("'"); sb.append("ana");
		sb.append("')");
		
		return sb.toString();
	}

	private String getInsertAlarmSql(AlarmVO vo) {
		String tableName = "SACP_ANALYZE_SITTN_NTCN";
		
		StringBuilder sb = new StringBuilder();
		sb.append("INSERT INTO ");
		sb.append(tableName);
		sb.append(" (ANALS_ID,ANALS_DT,SITTN_TY,ANALS_MSG,AREA_TY,AREA_ID,TRGT1_TY,TRGT1_ID,TRGT2_TY,TRGT2_ID,REGISTER_ID)");
		sb.append("VALUES (");
		sb.append("'"); sb.append(vo.getAnalsId()); sb.append("',");
		sb.append("'"); sb.append(vo.getAnalsDt()); sb.append("',");
//		sb.append("'"); sb.append(vo.); sb.append("',");
		sb.append("'"); sb.append(vo.getAnalsMsg()); sb.append("',");
		sb.append("'"); sb.append(vo.getArea().getArea_type()); sb.append("',");
		sb.append("'"); sb.append(vo.getArea().getArea_id()); sb.append("',");
		sb.append("'"); sb.append(vo.getTarget1().getTrgt_ty()); sb.append("',");
		sb.append("'"); sb.append(vo.getTarget1().getTrgt_id()); sb.append("',");
		sb.append("'"); sb.append(vo.getTarget2().getTrgt_ty()); sb.append("',");
		sb.append("'"); sb.append(vo.getTarget2().getTrgt_id()); sb.append("',");
		sb.append("'"); sb.append("ana");
		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;
	}
}
