package com.eactive.eai.rms.bap.manage.message.classmap;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.eactive.eai.rms.bap.common.base.BapBaseService;
import com.eactive.eai.rms.bap.manage.comm.property.BapPropertyDao;
import com.eactive.eai.rms.common.util.SqlUtil;

@Service("bapClassMapService")
public class BapClassMapService extends BapBaseService {

	@Autowired
	@Qualifier("bapClassMapDao")
	private BapClassMapDao dao;
	
	@Autowired
	@Qualifier("bapPropertyDao")
	private BapPropertyDao propDao;	

	public HashMap<String, Object> selectList(int startNum,int endNum, HashMap<String, Object> paramMap) throws Exception {
 
		// 전체 목록수 얻기
		int totalCount = dao.selectListCount(paramMap);

		paramMap.put("startNum" , startNum);// 시작 record
		paramMap.put("endNum"   , endNum);// 끝 record

		List<Map<String, Object>> list = dao.selectList(paramMap);
		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("totalCount", totalCount);
		map.put("list", list);
		
		return map;
	}	

	public int selectTotalCount(HashMap<String, Object> paramMap) throws Exception {

		return dao.selectListCount(paramMap); 		// 전체 목록수 얻기
	}	
	
	public HashMap<String, Object> selectDetail(String key) throws Exception {
		HashMap<String, Object> list;
		// key = telgmClsID
		list   = dao.selectDetail(key);

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		
		return map;

	}	
	public HashMap<String, Object> selectDetailCombo(HashMap<String,Object> paramMap) throws Exception {
		List<Map<String, Object>> msgMetaDstList   = dao.getMsgMetaDstcd(paramMap);
		List<Map<String, Object>> clsNameList   = dao.getTelgmClsName(paramMap);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("msgMetaDstList", msgMetaDstList);
		map.put("clsNameList", clsNameList);
		
		return map;

	}	

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

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

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

	@SuppressWarnings("unchecked")
	public HashMap<String,String> mkInserSqlForClassMap(HashMap<String, Object> classMapInfo) throws Exception {
		
		StringBuffer classMapInsertSql = new StringBuffer();
		List<Map<String, Object>> classMapList = (List<Map<String, Object>>)classMapInfo.get("list");
		
		// FLOW STEP LIST into BR03
		for (Map<String,Object> step : classMapList) {
			List<String> columnArr = new ArrayList<String>();
			String[] columnList = {"TELGMCLSID","TELGMCLSNAME","MSGMETADSTCD"};
			columnArr = Arrays.asList(columnList);
			classMapInsertSql.append("INSERT INTO EAIPKG.TSEAIBR05 (");
			
			// 필드명 입력
			for (String column : columnArr) {
				classMapInsertSql.append((column.equals(columnArr.get(columnArr.size()-1))) ? column : column+",");
			}
			classMapInsertSql.append(") VALUES (");
			// 필드값 입력
			for (String column : columnArr) {
				if (column.equals(columnArr.get(columnArr.size()-1))) {
					classMapInsertSql.append("'"+(step.containsKey(column) ? step.get(column) : "")+"');\n");
				} else {
					classMapInsertSql.append("'"+(step.containsKey(column) ? step.get(column) : "")+"',");
				}
			}
		}
		
		HashMap<String,String> retMap = new HashMap<String,String>(); 
		if (classMapInfo.get("fileName") != null) {
			OutputStream output = null;
			try {
				String sqlDownloadPath = getProperties("SQL_DOWNLOAD", "DOWNLOAD_PATH");
				String sqlDownloadFile = sqlDownloadPath+classMapInfo.get("fileName");
				output = new FileOutputStream(sqlDownloadFile);
				if (sqlDownloadPath != null) {
					byte[] bt = classMapInsertSql.toString().getBytes();
					output.write(bt);
					output.close();
				}
				retMap.put("path", sqlDownloadPath);
			} catch (Exception e) {
				logger.error( e.getMessage(), e);
			} finally {
				if(output != null) try { output.close(); } catch(Exception ee) {logger.error( ee.getMessage(), ee);}
			}
		} else {
			retMap = SqlUtil.executeSql(classMapInsertSql.toString(), "BAP_"+classMapInfo.get("svrType").toString().toUpperCase());
		}
		
		return retMap;
	}
	
	private String getProperties(String propGroup, String propName) throws Exception {
		HashMap<String,Object> params = new HashMap<String,Object>();
		params.put("prptyGroupName", propGroup);
		params.put("prptyName", propName);
		
		HashMap<String, Object> props = propDao.getProperties(params);
		
		return props.get("PRPTY2VAL").toString();
	}
}