package com.eactive.eai.rms.bap.manage.comm.adapter;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.onl.common.sql.ExtractSqlService;

@Service
public class BapAdapterSqlService extends ExtractSqlService {
	
	public String bapAdapterSql(Map<String, Object> paramMap) throws Exception {
		
		DataSourceContextHolder.setDataSourceType(
				DataSourceTypeManager.getDataSourceType(DataSourceTypeManager.BAP));
		
		StringBuilder sqlBuilder = new StringBuilder();
		
		String bapAdapterName = (String) paramMap.get("adptrBzwkGroupName");
		
		List<Map<String, Object>> ba01_queryResult = select("TSEAIBA01", "ADPTRBZWKGROUPNAME", bapAdapterName);
		sqlBuilder.append(makeSql("TSEAIBA01", "ADPTRBZWKGROUPNAME", bapAdapterName, ba01_queryResult));
		
		List<Map<String, Object>> ba02_queryResult = select("TSEAIBA02", "ADPTRBZWKGROUPNAME", bapAdapterName);
		sqlBuilder.append(makeSql("TSEAIBA02", "ADPTRBZWKGROUPNAME", bapAdapterName, ba02_queryResult));
		
		String[] keysForBA03 = extractKeys(ba02_queryResult, "PRPTYGROUPNAME");
		List<Map<String, Object>> ba03_queryResult = select("TSEAIBA03", "PRPTYGROUPNAME", keysForBA03);
		sqlBuilder.append(makeSql("TSEAIBA03", "PRPTYGROUPNAME", keysForBA03, ba03_queryResult));
		
		String[] keysForBA04 = extractKeys(ba03_queryResult, "PRPTYGROUPNAME");
		List<Map<String, Object>> ba04_queryResult = select("TSEAIBA04", "PRPTYGROUPNAME", keysForBA04);
		sqlBuilder.append(makeSql("TSEAIBA04", "PRPTYGROUPNAME", keysForBA04, ba04_queryResult));
		
		return sqlBuilder.toString();
	}

}
