package com.eactive.eai.rms.onl.statistics.jspforselectlist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

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.OnlBaseService;
import com.eactive.eai.rms.common.context.MonitoringContext;
import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.common.util.CommonUtil;

@Service("jspForSelectListService")
public class JspForSelectListService extends OnlBaseService {
    @Autowired
    @Qualifier("monitoringContext")
    private MonitoringContext monitoringContext;	
    
    @Autowired
    @Qualifier("jspForSelectListDao")
    private JspForSelectListDao dao;
    
    public List selectLogList2(String date, int itemCnt, int pageCnt,String svrDstcd) throws Exception {
		String use = monitoringContext.getStringProperty(MonitoringContext.RMS_JSP_SELECT_USE);
		List list = new ArrayList();
		//int cnt=0;
		if("Y".equals(use)){
			if(itemCnt>10000){
				HashMap map = new HashMap();
				map.put("errMsg", "itemCnt(갯수)는 10000개 이하로 조회 가능합니다.");
				list.add(map);
				return list;
			}
			if(itemCnt==0){
				HashMap map = new HashMap();
				map.put("errMsg", "itemCnt(갯수)가 0개 입니다. 조회 가능한 숫자를 입력해주세요.");
				list.add(map); 
				return list;
			}
			DataSourceType dataSourceType = DataSourceTypeManager.getDataSourceType(svrDstcd);
			DataSourceContextHolder.setDataSourceType(dataSourceType);	

			int startNum = (pageCnt-1)*itemCnt+1;
			int endNum = pageCnt*itemCnt;
			
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("startNum", startNum);
			paramMap.put("endNum", endNum);
			if(dataSourceType != null) {  //취약점소스 수정 2020.08.25
				paramMap.put("schemaId",dataSourceType.getSchema());
			}
            paramMap.put("date", date);
            paramMap.put("tableName", CommonUtil.getLogTable(date, true));
            List tmpList = dao.selectLogList2(paramMap);
            for(int i=0;i<tmpList.size(); i++){
            	HashMap tmpMap = new HashMap();
            	tmpMap = (HashMap) tmpList.get(i);
            	tmpMap.put("SVRNAME", DataSourceContextHolder.getDataSourceType().getText());
            	list.add(tmpMap);
            }
		}else{
			HashMap map = new HashMap();
			map.put("errMsg", "현재 설정값='사용안함'으로 사용할 수 없습니다.");
			list.add(map);
		}
		return list;
	}	
    
	public List selectLogList(String date) throws Exception {
		String use = monitoringContext.getStringProperty(MonitoringContext.RMS_JSP_SELECT_USE);
		List list = new ArrayList();
		if("Y".equals(use)){
			for (DataSourceType dataSourceType : DataSourceTypeManager.getOnlineDataSourceTypes()){
				DataSourceContextHolder.setDataSourceType(dataSourceType);	
				
				HashMap<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("schemaId",dataSourceType.getSchema());
	            paramMap.put("date", date);
	            paramMap.put("startNum", 1);
	            paramMap.put("date", date);
	            paramMap.put("tableName", CommonUtil.getLogTable(date, true));
	            List tmpList = dao.selectLogList(paramMap);
	            for(int i=0;i<tmpList.size(); i++){
	            	HashMap tmpMap = new HashMap();
	            	tmpMap = (HashMap) tmpList.get(i);
	            	tmpMap.put("SVRNAME", DataSourceContextHolder.getDataSourceType().getText());
	            	list.add(tmpMap);
	            }
	    	}
		}else{
			HashMap map = new HashMap();
			map.put("errMsg", "현재 설정값='사용안함'으로 사용할 수 없습니다.");
			list.add(map);
		}
		return list;
	}	
	public List selectEaiMsgList() throws Exception {
		String use = monitoringContext.getStringProperty(MonitoringContext.RMS_JSP_SELECT_USE);
		List list = new ArrayList();
		if("N".equals(use)){
			HashMap map = new HashMap();
			map.put("errMsg", "현재 설정값='사용안함'으로 사용할 수 없습니다.");
			list.add(map);
		}else{
			for (DataSourceType dataSourceType : DataSourceTypeManager.getOnlineDataSourceTypes()){
				DataSourceContextHolder.setDataSourceType(dataSourceType);	
				
				HashMap<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("schemaId",dataSourceType.getSchema());
	            List tmpList = dao.selectEaiMsgList(paramMap);
	            for(int i=0;i<tmpList.size(); i++){
	            	HashMap tmpMap = new HashMap();
	            	tmpMap = (HashMap) tmpList.get(i);
	            	tmpMap.put("SVRNAME", DataSourceContextHolder.getDataSourceType().getText());
	            	list.add(tmpMap);
	            }
	    	}
		}
		return list;
	}	
	public List selectEaiMsgList2(int itemCnt, int pageCnt,String svrDstcd) throws Exception {
		String use = monitoringContext.getStringProperty(MonitoringContext.RMS_JSP_SELECT_USE);
		List list = new ArrayList();
		if("N".equals(use)){
			HashMap map = new HashMap();
			map.put("errMsg", "현재 설정값='사용안함'으로 사용할 수 없습니다.");
			list.add(map);
		}else{
			DataSourceType dataSourceType = DataSourceTypeManager.getDataSourceType(svrDstcd);
			DataSourceContextHolder.setDataSourceType(dataSourceType);	

			if(itemCnt>10000){
				HashMap map = new HashMap();
				map.put("errMsg", "itemCnt(갯수)는 10000개 이하로 조회 가능합니다.");
				list.add(map);
				return list;
			}
			if(itemCnt==0){
				HashMap map = new HashMap();
				map.put("errMsg", "itemCnt(갯수)가 0개 입니다. 조회 가능한 숫자를 입력해주세요.");
				list.add(map); 
				return list;
			}
			int startNum = (pageCnt-1)*itemCnt+1;
			int endNum = pageCnt*itemCnt;
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("startNum", startNum);
			paramMap.put("endNum", endNum);
			if(dataSourceType != null) {  //취약점소스 수정 2020.08.25
				paramMap.put("schemaId",dataSourceType.getSchema());
			}
            List tmpList = dao.selectEaiMsgList2(paramMap);
            for(int i=0;i<tmpList.size(); i++){
            	HashMap tmpMap = new HashMap();
            	tmpMap = (HashMap) tmpList.get(i);
            	tmpMap.put("SVRNAME", DataSourceContextHolder.getDataSourceType().getText());
            	list.add(tmpMap);
            }
		}
		return list;
	}
}