package com.eactive.eai.rms.onl.common.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.rms.common.base.SqlMapClientTemplateDao;
import com.eactive.eai.rms.data.entity.man.monitoringProperty.service.MonitoringPropertyService;
import com.eactive.eai.rms.onl.common.dao.DashboardTargetDao;
import com.eactive.eai.rms.onl.tracking.vo.DbStaticsVo;

/**
 * 대시보드에서 사용하기 위한 DB Query 서비스 거래로그건수, 타임아웃건수, 에러로그 건수를 조회하여 리턴한다.
 */

@Service("DashboardTargetService")
public class DashboardTargetServiceImpl extends SqlMapClientTemplateDao implements DashboardTargetService {

	/** DashboardTargetDao table 의 DAO. */
	@Autowired
	@Qualifier("DashboardTargetDao")
	private DashboardTargetDao dashboardTargetDao;

	/** The eai server info service. */
	@Autowired
	@Qualifier("eaiServerInfoService")
	private EaiServerInfoService eaiServerInfoService;

	@Autowired
	private MonitoringPropertyService monitoringPropertyService;

	private static final Logger logger = Logger.getLogger(DashboardTargetServiceImpl.class);

	/**
	 * 해당 서비스의 DB 로그에서 거래 건수를 조회하여 리턴한다.
	 * 
	 * @param jndi
	 * @param schema
	 * @param start
	 * @param end
	 * @param host
	 * @return
	 * @throws Exception
	 */
	public int getProcCount(String start, String end, String host) throws Exception {
		String insts = getInstsInParameter(host);
		return dashboardTargetDao.getProcCount(start, end, insts);
	}

	/**
	 * 해당 서비스의 TSEAISR01에서 거래 건수를 조회하여 리턴한다.
	 * 
	 * @param jndi
	 * @param schema
	 * @param start
	 * @param end
	 * @param host
	 * @return
	 * @throws Exception
	 */
	public HashMap getProcCountWithSR02(String end, String inst) throws Exception {
		return dashboardTargetDao.getProcCountWithSR02(end, inst);
	}

	public List getProcCountDateWithBJ08(String searchStart, String searchEnd, String inst) throws Exception {
		return dashboardTargetDao.getProcCountDateWithBJ08(searchStart, searchEnd, inst);
	}

	public List getProcCountDateWithES01(String searchStart, String searchEnd, String inst) throws Exception {
		return dashboardTargetDao.getProcCountDateWithES01(searchStart, searchEnd, inst);
	}

	public List getProcCountHourWithBJ08(HashMap paramMap) throws Exception {
		return dashboardTargetDao.getProcCountHourWithBJ08(paramMap);
	}

	public List getProcCountHourWithES01(HashMap paramMap) throws Exception {
		return dashboardTargetDao.getProcCountHourWithES01(paramMap);
	}

	public List getItsmErrorLogList(HashMap paramMap) throws Exception {
		return dashboardTargetDao.getItsmErrorLogList(paramMap);
	}

	public String getGroupPeakdayCnt(String key) throws Exception {
		return monitoringPropertyService.findPrpty2ValById("Monitoring", key);
	}

	/**
	 * 해당 서비스의 TSEAISR01에서 거래 건수를 조회하여 리턴한다.
	 * 
	 * @param jndi
	 * @param schema
	 * @param start
	 * @param end
	 * @param host
	 * @return
	 * @throws Exception
	 */
	public List getHourProcCountWithSR02(HashMap paramMap) throws Exception {
		return dashboardTargetDao.getHourProcCountWithSR02(paramMap);

	}

	public List getProcCountDayWithSR02(String searchStart, String searchEnd, String inst) throws Exception {
		return dashboardTargetDao.getProcCountDayWithSR02(searchStart, searchEnd, inst);

	}

	/**
	 * 해당 서비스의 DB 로그에서 타임아웃 에러 건수를 조회하여 리턴한다.
	 * 
	 * @param jndi
	 * @param schema
	 * @param start
	 * @param end
	 * @param host
	 * @return
	 * @throws Exception
	 */
	public int getTimeoutCount(String start, String end, String host) throws Exception {
		String insts = getInstsInParameter(host);
		return dashboardTargetDao.getTimeoutCount(start, end, insts);
	}

	/**
	 * 해당 서비스의 DB 로그에서 에러 건수를 조회하여 리턴한다.
	 * 
	 * @param jndi
	 * @param schema
	 * @param start
	 * @param end
	 * @param host
	 * @return
	 * @throws Exception
	 */
	public int getErrorCount(String start, String end, String host) throws Exception {
		String insts = getInstsInParameter(host);
		return dashboardTargetDao.getErrorCount(start, end, insts);
	}

	public void updatePeakTps(String key, String val) throws Exception {
		dashboardTargetDao.updatePeakTps(key, val);
	}

	public void insertPeakTpsHistory(HashMap<String, Object> paramMap) throws Exception {
		dashboardTargetDao.insertPeakTpsHistory(paramMap);
	}

	public int[] getBatchProcCountByDate(String date, boolean eai) throws Exception {
		int ret[] = { 0, 0, 0, 0, 0, 0 };
		try {
			HashMap<String, Integer> list = null;
			if (eai) {
				list = dashboardTargetDao.getEaiBatchProcCountByDate(date);
			} else {
				list = dashboardTargetDao.getBatchProcCountByDate(date);
			}
			if (list != null && list.size() > 0) {
				ret[0] = list.get("SEND_SEND");
				ret[1] = list.get("SEND_RECV");
				ret[2] = list.get("FAIL_SEND");
				ret[3] = list.get("FAIL_RECV");
				ret[4] = list.get("QUED_SEND");
				ret[5] = list.get("QUED_RECV");
			}

		} catch (Exception e) {
			// e.printStackTrace();
			logger.error("getBatchProcCountByDate error", e);
		}
		return ret;
	}

	/**
	 * 배치거래 건수 를 조회하여 리턴한다. - 대시보드 메인 화면의 배치건수를 표시하기 위함.
	 * 
	 * @param jndi
	 * @param schema
	 * @return
	 * @throws Exception
	 */
	public int[] getBatchProcCount(boolean eai) throws Exception {
		int ret[] = { 0, 0, 0, 0, 0, 0 };
		try {
			HashMap<String, Integer> list = null;
			if (eai)
				list = dashboardTargetDao.getEaiBatchProcCount();
			else
				list = dashboardTargetDao.getBatchProcCount();

			if (list != null && list.size() > 0) {
				ret[0] = list.get("SEND_SEND");
				ret[1] = list.get("SEND_RECV");
				ret[2] = list.get("FAIL_SEND");
				ret[3] = list.get("FAIL_RECV");
				ret[4] = list.get("QUED_SEND");
				ret[5] = list.get("QUED_RECV");
			}
		} catch (Exception e) {
			// e.printStackTrace();
			logger.error("getBatchProcCount error", e);
		}
		return ret;
	}

	/**
	 * 해당 서비스의 DB 로그에서 거래 건수를 조회하여 리턴한다. ( 전지점 거래테스트 - 2009.10.24 - 를 위해 만든 임시함수.)
	 * serviceNum 1:내부, 2:대외, 3:공동망
	 * 
	 * @param jndi
	 * @param schema
	 * @param start
	 * @param end
	 * @param host
	 * @return
	 * @throws Exception
	 */
	public int getProcCountBranchTest(String start, String end, String serviceNum, String host, String commDiv)
			throws Exception {
		String insts = getInstsInParameter(host);
		return dashboardTargetDao.getProcCountBranchTest(start, end, serviceNum, insts, commDiv);
	}

	/**
	 * 해당 서비스의 DB 로그에서 타임아웃 에러 건수를 조회하여 리턴한다. ( 전지점 거래테스트 - 2009.10.24 - 를 위해 만든
	 * 임시함수.) serviceNum 1:내부, 2:대외, 3:공동망
	 * 
	 * @param jndi
	 * @param schema
	 * @param start
	 * @param end
	 * @param host
	 * @return
	 * @throws Exception
	 */
	public int getTimeoutCountBranchTest(String start, String end, String serviceNum, String host, String commDiv)
			throws Exception {
		String insts = getInstsInParameter(host);
		return dashboardTargetDao.getTimeoutCountBranchTest(start, end, serviceNum, insts, commDiv);
	}

	/**
	 * 해당 서비스의 DB 로그에서 에러 건수를 조회하여 리턴한다. ( 전지점 거래테스트 - 2009.10.24 - 를 위해 만든 임시함수.)
	 * serviceNum = 1:내부, 2:대외, 3:공동
	 * 
	 * @param jndi
	 * @param schema
	 * @param start
	 * @param end
	 * @param host
	 * @return
	 * @throws Exception
	 */
	public int getErrorCountBranchTest(String start, String end, String serviceNum, String host, String commDiv)
			throws Exception {

		String insts = getInstsInParameter(host);
		return dashboardTargetDao.getErrorCountBranchTest(start, end, serviceNum, insts, commDiv);
	}

	/**
	 * 거래 통계 집계 테이블에서 최대,최소 일자를 구한다.
	 * 
	 * @param jndi
	 * @param schema
	 * @param hostName
	 * @return
	 * @throws Exception
	 */
	public HashMap<String, String> getMinMaxDate(String hostName) throws Exception {

		return dashboardTargetDao.getMinMaxDate(hostName);
	}

	/**
	 * 거래 통계 집계 테이블에서 최대,최소 시간을 구한다.
	 * 
	 * @param jndi
	 * @param schema
	 * @param baseDate
	 * @param hostName
	 * @return
	 * @throws Exception
	 */
	public HashMap<String, String> getMinMaxTime(String baseDate, String hostName) throws Exception {

		return dashboardTargetDao.getMinMaxTime(baseDate, hostName);
	}

	/**
	 * 거래 통계 집계 테이블에 데이터를 추가한다.
	 * 
	 * @param jndi
	 * @param schema
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	public int insertTradeStatics(final DbStaticsVo vo) throws Exception {

		return dashboardTargetDao.insertTradeStatics(vo);
	}

	public HashMap<String, String> sumTradeStatics(String eaiMoniJobDstcd, String eaiMoniSvcDstcd, String statcYmd,
			String hostName) throws Exception {

		return dashboardTargetDao.sumTradeStatics(eaiMoniJobDstcd, eaiMoniSvcDstcd, statcYmd, hostName);
	}

	/**
	 * 대시보드 에서 사용하는 거래통계 집계 테이블 데이터를 0으로 초기화 함으로써, 대시보드의 거래건수를 0으로 초기화 한다.
	 * 
	 * @param jndi
	 * @param schema
	 * @param statcYmd
	 * @param statcHMS
	 * @throws Exception
	 */
	public void resetTradeStatics(String statcYmd, String statcHMS) throws Exception {
		dashboardTargetDao.resetTradeStatics(statcYmd, statcHMS);
	}

	public HashMap<String, String> selectEAISevrDstcd() throws Exception {
		return dashboardTargetDao.selectEAISevrDstcd();
	}

//	 /**
//	  * 일괄전송의 IP, PORT 정보를 리턴한다.
//	  * @return
//	  */
//	 public List<Map<String, String>> getEaiBatchServerIpList( String schemaName ) {
//		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
//		
//        try {
//        	Map<String, String> retList = dashboardTargetDao.getEaiBatchServerIpList(schemaName );
//	
//	        result.add(retList);        
//		} catch (Exception e) {
//			//e.printStackTrace();
//			logger.error("getEaiBatchServerIpList error", e); 
//		}
//        return result;
//	}

	private String getInstsInParameter(String host) {

		List<Map<String, String>> instList = eaiServerInfoService.getEaiServerIpListByHostName(host);

		if (instList == null || instList.size() <= 0) {
			return "''";
		}

		StringBuffer insts = new StringBuffer();
		insts.append("'");
		for (Map<String, String> inst : instList) {
			insts.append(inst.get("EAISVRINSTNM")).append("','");
		}

		return insts.substring(0, insts.length() - 2);
	}

	@Override
	public List<HashMap<String, String>> getEaiBatchErrorList(HashMap<String, String> paramMap) {
		List<HashMap<String, String>> result = dashboardTargetDao.getEaiBatchErrorList(paramMap);
		return result;
	}

	@Override
	public List<HashMap<String, String>> getFepBatchErrorList(HashMap<String, String> paramMap) {
		List<HashMap<String, String>> result = dashboardTargetDao.getFepBatchErrorList(paramMap);
		return result;
	}

}
