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

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("bapClassInfoService")
public class BapClassInfoService extends BapBaseService {

	@Autowired
	@Qualifier("bapClassInfoDao")
	private BapClassInfoDao 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 = TelgmClsName
		list   = dao.selectDetail(key);

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		
		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);
	}
	
	public List<Map<String, Object>> getMsgGroupDstcd(HashMap<String, Object> paramMap) throws Exception {
		return dao.getMsgGroupDstcd(paramMap);
	}
	
	@SuppressWarnings("unchecked")
	public HashMap<String,String> mkInserSqlForClassInfo(HashMap<String, Object> classInfo) throws Exception {
		
		StringBuffer classInfoInsertSql = new StringBuffer();
		List<Map<String, Object>> classInfoList = (List<Map<String, Object>>)classInfo.get("list");
		
		// CLASS INFO LIST into BR03
		for (Map<String,Object> step : classInfoList) {
			List<String> columnArr = new ArrayList<String>();
			String[] columnList = {"TELGMCLSNAME","TELGMCLSDESC"};
			columnArr = Arrays.asList(columnList);
			classInfoInsertSql.append("INSERT INTO EAIPKG.TSEAIBR01 (");
			
			// 필드명 입력
			for (String column : columnArr) {
				classInfoInsertSql.append((column.equals(columnArr.get(columnArr.size()-1))) ? column : column+",");
			}
			classInfoInsertSql.append(") VALUES (");
			// 필드값 입력
			for (String column : columnArr) {
				if (column.equals(columnArr.get(columnArr.size()-1))) {
					classInfoInsertSql.append("'"+(step.containsKey(column) ? step.get(column) : "")+"');\n");
				} else {
					classInfoInsertSql.append("'"+(step.containsKey(column) ? step.get(column) : "")+"',");
				}
			}
		}
		HashMap<String,String> retMap = new HashMap<String,String>(); 
		if (classInfo.get("fileName") != null) {
			OutputStream output = null;
			try {
				String sqlDownloadPath = getProperties("SQL_DOWNLOAD", "DOWNLOAD_PATH");
				String sqlDownloadFile = sqlDownloadPath+classInfo.get("fileName");
				output = new FileOutputStream(sqlDownloadFile);
				if (sqlDownloadPath != null) {
					byte[] bt = classInfoInsertSql.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(classInfoInsertSql.toString(), "BAP_"+classInfo.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();
	}
}