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

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

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;

@Service
public class EaiMsgSqlService extends ExtractSqlService {
	
	public String eaiSvcSql(HashMap<String, Object> paramMap) throws Exception {
		
		StringBuilder sqlBuilder = new StringBuilder();
		
		String eaiSvcName = (String) paramMap.get("eaiSvcName");
		
		List<Map<String, Object>> he01_queryResult = select("TSEAIHE01", "EAISVCNAME", eaiSvcName);
		sqlBuilder.append(makeSql("TSEAIHE01", "EAISVCNAME", eaiSvcName, he01_queryResult));
		
		List<Map<String, Object>> he02_queryResult = select("TSEAIHE02", "EAISVCNAME", eaiSvcName);
		sqlBuilder.append(makeSql("TSEAIHE02", "EAISVCNAME", eaiSvcName, he02_queryResult));

		List<Map<String, Object>> ms02_queryResult = select("TSEAIMS02", "EAISVCNAME", eaiSvcName);
		sqlBuilder.append(makeSql("TSEAIMS02", "EAISVCNAME", eaiSvcName, ms02_queryResult));
		
		List<Map<String, Object>> fr01_queryResult = select("TSEAIFR01", "EAISVCNAME", eaiSvcName);
		sqlBuilder.append(makeSql("TSEAIFR01", "EAISVCNAME", eaiSvcName, fr01_queryResult));
		
		List<Map<String, Object>> fr03_queryResult = select("TSEAIFR03", "EAISVCNAME", eaiSvcName);
		sqlBuilder.append(makeSql("TSEAIFR03", "EAISVCNAME", eaiSvcName, fr03_queryResult));
		
		List<Map<String, Object>> fr06_queryResult = select("TSEAIFR06", "EAISVCNAME", eaiSvcName);
		sqlBuilder.append(makeSql("TSEAIFR06", "EAISVCNAME", eaiSvcName, fr06_queryResult));
		
		List<Map<String, Object>> hs04_queryResult = select("TSEAIHS04", "EAISVCNAME", eaiSvcName);
		sqlBuilder.append(makeSql("TSEAIHS04", "EAISVCNAME", eaiSvcName, hs04_queryResult));
		
		String[] keysForHS05 = extractKeys(hs04_queryResult, "BZWKSVCKEYNAME");
		List<Map<String, Object>> hs05_queryResult = select("TSEAIHS05", "BZWKSVCKEYNAME", keysForHS05);
		sqlBuilder.append(makeSql("TSEAIHS05", "BZWKSVCKEYNAME", keysForHS05, hs05_queryResult));
		
		String[] transColNames = {"CHNGMSGIDNAME", "BASCRSPNSCHNGMSGIDNAME", "ERRRSPNSCHNGMSGIDNAME"};
		String[] keysForTR01 = extractKeys(he02_queryResult, transColNames);
		List<Map<String, Object>> tr01_queryResult = select("TSEAITR01", "CNVSNNAME", keysForTR01);
		sqlBuilder.append(makeSql("TSEAITR01", "CNVSNNAME", keysForTR01, tr01_queryResult));
		
		List<Map<String, Object>> tr02_queryResult = select("TSEAITR02", "CNVSNNAME", keysForTR01); // same key with TR01
		sqlBuilder.append(makeSql("TSEAITR02", "CNVSNNAME", keysForTR01, tr02_queryResult));
		
		List<Map<String, Object>> tr03_queryResult = select("TSEAITR03", "CNVSNNAME", keysForTR01); // same key with TR01
		sqlBuilder.append(makeSql("TSEAITR03", "CNVSNNAME", keysForTR01, tr03_queryResult));
		
		String[] keysForTR07_1 = extractKeys(tr02_queryResult, "LOUTNAME");
		String[] keysForTR07_2 = extractKeys(ms02_queryResult, "REFMSGIDNAME");
		String[] keysForTR07_3 = extractKeys(fr03_queryResult, "BZWKFLDNAME", ".*(?=\\.)");
		String[] keysForTR07 = ArrayUtils.addAll(ArrayUtils.addAll(keysForTR07_1, keysForTR07_2), keysForTR07_3);
		keysForTR07 = Arrays.stream(keysForTR07).distinct().toArray(String[]::new);
		List<Map<String, Object>> tr07_queryResult = select("TSEAITR07", "LOUTNAME", keysForTR07);
		sqlBuilder.append(makeSql("TSEAITR07", "LOUTNAME", keysForTR07, tr07_queryResult));
		
		List<Map<String, Object>> tr08_queryResult = select("TSEAITR08", "LOUTNAME", keysForTR07); // same key with TR07
		sqlBuilder.append(makeSql("TSEAITR08", "LOUTNAME", keysForTR07, tr08_queryResult));
		
		return sqlBuilder.toString();
	}

}
