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

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;
import com.eactive.eai.rms.onl.common.sql.PreparedString;

@Service
public class BapMessageProcSqlService extends ExtractSqlService {

	public String bapMessageProcSql(Map<String, Object> paramMap) throws Exception {
		
		DataSourceContextHolder.setDataSourceType(
				DataSourceTypeManager.getDataSourceType(DataSourceTypeManager.BAP));
		
		StringBuilder sqlBuilder = new StringBuilder();
		
		String infId = (String) paramMap.get("infId");
		
		List<Map<String, Object>> bj01_queryResult = select("TSEAIBJ01", "INFID", infId);
		sqlBuilder.append(makeSql("TSEAIBJ01", "INFID", infId, bj01_queryResult));

		String[] keysForBA06 = 
				extractKeys(bj01_queryResult, new PreparedString("\\?", "TelegramInfo{?_?}"), "BJOBBZWKDSTCD", "BJOBTRANDSTCDNAME");
		List<Map<String, Object>> ba06_queryResult = select("TSEAIBA06", "PRPTYGROUPNAME", keysForBA06);
		sqlBuilder.append(makeSql("TSEAIBA06", "PRPTYGROUPNAME", keysForBA06, ba06_queryResult));
		
		return sqlBuilder.toString();
	}
	
}
