package com.eactive.eai.rms.bap.manage.work.sysId;

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

	@Autowired
	@Qualifier("bapSystemIDDao")
	private BapSystemIDDao 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 HashMap<String, Object> selectDetail(HashMap<String, Object> param) throws Exception {
		return dao.selectDetail(param);
	}

	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 HashMap<String,String> mkInsertSqlForSystemId(HashMap<String, Object> systemId) throws Exception {
		
		StringBuffer systemIdInsertSql = new StringBuffer();
		for (String systemID : systemId.get("systemIDList").toString().split(",")) {
			HashMap<String, Object> paramDetail = new HashMap<String,Object>();
			paramDetail.put("systemId", systemID);
			HashMap<String, Object> systemIdDetail = selectDetail(paramDetail);
			
			// 시스템ID 입력 (BA07)
			List<String> columnArr = new ArrayList<String>();
			String[] columnList = {"SYSTEMID","BJOBBZWKDSTCD","OSIDINSTIDSTCD","RMTNODENAME","SYSTEMNAME"};
			columnArr = Arrays.asList(columnList);
			systemIdInsertSql.append("INSERT INTO EAIPKG.TSEAIBA07 (");
			
			// 필드명 입력
			for (String column : columnArr) {
				systemIdInsertSql.append((column.equals(columnArr.get(columnArr.size()-1))) ? column : column+",");
			}
			systemIdInsertSql.append(") VALUES (");
			// 필드값 입력
			for (String column : columnArr) {
				if (column.equals(columnArr.get(columnArr.size()-1))) {
					systemIdInsertSql.append("'"+(systemIdDetail.get(column) != null ? systemIdDetail.get(column) : "")+"');\n");
				} else {
					systemIdInsertSql.append("'"+(systemIdDetail.get(column) != null ? systemIdDetail.get(column) : "")+"',");
				}
			}
		}
		
		HashMap<String,String> retMap = new HashMap<String,String>(); 
		if (systemId.get("fileName") != null) {
			OutputStream output = null;
			try {
				String sqlDownloadPath = getProperties("SQL_DOWNLOAD", "DOWNLOAD_PATH");
				String sqlDownloadFile = sqlDownloadPath+systemId.get("fileName");
				output = new FileOutputStream(sqlDownloadFile);
				if (sqlDownloadPath != null) {
					byte[] bt = systemIdInsertSql.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(systemIdInsertSql.toString(), "BAP_"+systemId.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();
	}
}