package com.eactive.eai.rms.bap.loader.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;

import com.eactive.eai.rms.bap.loader.intf.InterfaceDeployService;
import com.eactive.eai.rms.bap.loader.intf.deploy.InterfaceDeploy;
import com.fasterxml.jackson.databind.MapperFeature;
import org.apache.commons.lang3.StringUtils;
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.loader.dao.LoaderBAPDao;
import com.eactive.eai.rms.common.base.BaseService;
import com.eactive.eai.rms.common.context.MonitoringContext;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.onl.common.util.FileWriteUtil;

@Service("loaderBAPService")
public class TransactionBAPServiceImpl extends BaseService implements TransactionBAPService {

	/** The monitoring context. */
	@Autowired
	@Qualifier("monitoringContext")
	private transient MonitoringContext monitoringContext;

	@Autowired
	@Qualifier("loaderBAPDao")
	private LoaderBAPDao dao;

   @Autowired
   @Qualifier("BapInterfaceDeployService")
   private InterfaceDeployService interfaceDeployService;

	public TransactionBAPServiceImpl() {
		super();
	}

	/**
	 * layout loading
	 */
	public void insertLayoutLoading(DataSourceType dt, String path, String fileName) throws Exception {
		String[] tables = { "TSEAITR07", "TSEAITR08" };
		HashMap map = parse(path + fileName);
		if (map != null) {
			ArrayList list = null;
			for (String table : tables) {
				list = (ArrayList) map.get(table);
				if (list != null) {
					if ("TSEAITR07".equals(table)) {
						// VERSION 정보 없을 경우 "00000" 로 셋팅
						for (int i = 0; i < list.size(); i++) {
							HashMap hm2 = (HashMap) list.get(i);
							if (hm2 != null && !hm2.containsKey("VERINFO")) {
								hm2.put("VERINFO", "00000");
								list.set(i, hm2);
							}
						}

					}
					dao.load(dt, table, list);
				}
			}
		}
	}

	/**
	 * fileinfo loading
	 */
	public void insertFileInfoLoading(DataSourceType dt, String path, String fileName) throws Exception {
		String[] tables = { "TSEAIBJ01" };
		HashMap map = parse(path + fileName);
		if (map != null) {
			ArrayList list = null;
			for (String table : tables) {
				list = (ArrayList) map.get(table);
				if (list != null) {
					// HashMap hm = (HashMap)list.get(0);
					//adapterGroupValid(dt, map, "TSEAIBJ01");
					dao.load(dt, table, list);
				}
			}
		}
	}

	/**
	 * transform loading
	 */
	public void insertTransformLoading(DataSourceType dt, String path, String fileName) throws Exception {
		String[] tables = { "TSEAITR01", "TSEAITR02", "TSEAITR03" };
		HashMap map = parse(path + fileName);
		if (map != null) {
			ArrayList list = null;
			for (String table : tables) {
				list = (ArrayList) map.get(table);
				if (list != null) {
					if ("TSEAITR01".equals(table)) {
						// VERSION 정보 없을 경우 "00000" 로 셋팅
						for (int i = 0; i < list.size(); i++) {
							HashMap hm2 = (HashMap) list.get(i);
							if (hm2 != null && !hm2.containsKey("VERINFO")) {
								hm2.put("VERINFO", "00000");
								list.set(i, hm2);
							}
						}
					}
					dao.load(dt, table, list);
				}
			}
		}

	}

	/**
	 * b2b loading
	 */
	public void insertB2BLoading(DataSourceType dt, String path, String fileName) throws Exception {
		String[] tables = { "TSEAIHE03" };
		HashMap map = parse(path + fileName);
		if (map != null) {
			ArrayList list = null;
			for (String table : tables) {
				list = (ArrayList) map.get(table);
				if (list != null) {
					// HashMap hm = (HashMap)list.get(0);
					adapterGroupValid(dt, map, "TSEAIHE03");
					dao.load(dt, table, list);
				}

			}
		}
	}

	/**
	 * adapter loading
	 */
	public void insertAdapterLoading(DataSourceType dt, String path, String fileName) throws Exception {
		HashMap map = parse(path + fileName);
		// TSEAIAD15 삭제 어댑터명 기준
		// TSEAIAD14 삭제 어댑터명 기준
		// TSEAIAD02 삭제
		// TSEAIAD01 삭제
		// TSEAIFR04 삭제
		String[] tables = { "TSEAIAD15", "TSEAIAD14", "TSEAIAD02", "TSEAIAD01", "TSEAIFR04" };
		if (map != null) {
			ArrayList list = null;
			for (String table : tables) {
				list = (ArrayList) map.get(table);
				if (list != null)
					dao.load(dt, table, list);
			}
		}
	}

	/**
	 * adapter loading
	 */
	public void insertAdapterLoadingAll(DataSourceType dt, String path) throws Exception {

		File dir = new File(path);
		for (File file : dir.listFiles()) {
			if (file.isFile() && file.getName().endsWith(".txt")) {
				HashMap map = parse(path + file.getName());
				// TSEAIAD15 삭제 어댑터명 기준
				// TSEAIAD14 삭제 어댑터명 기준
				// TSEAIAD01 삭제
				// TSEAIAD02 삭제
				// TSEAIFR04 삭제
				String[] tables = { "TSEAIAD15", "TSEAIAD14", "TSEAIAD01", "TSEAIAD02", "TSEAIFR04" };
				if (map != null) {
					ArrayList list = null;
					for (String table : tables) {
						list = (ArrayList) map.get(table);
						if (list != null)
							dao.load(dt, table, list);
					}
				}
			}
		}
	}

	/**
	 * messageKey loading
	 */
	public void insertMessageKeyLoading(DataSourceType dt, String path, String fileName) throws Exception {
		HashMap map = parse(path + fileName);
		// TSEAIFR04 삭제
		String[] tables = { "TSEAIFR04" };
		if (map != null) {
			ArrayList list = null;
			for (String table : tables) {
				list = (ArrayList) map.get(table);
				if (list != null) {
					// HashMap hm = (HashMap)list.get(0);
					adapterGroupValid(dt, map, "TSEAIFR04");

					dao.load(dt, table, list);
				}
			}
		}
	}

	/**
	 * interface downloading
	 */
	public void interfaceDownLoading(DataSourceType dt, String path, String eaiSvcName) throws Exception {

		List<LinkedHashMap> listHE01 = dao.selectTableData(dt, "TSEAIHE01", "EAISvcName", eaiSvcName);
		if (listHE01 == null || listHE01.size() == 0) {
			throw new Exception("interface data is empty  interfaceName =[" + eaiSvcName + "]");
		}

		// TAS거래 > 일반거래로 복사 2020.05.06 추가
		for (LinkedHashMap m : listHE01) {
			if ("T".equals((String) m.get("TRANTYPE"))) {
				m.put("TRANTYPE", "R");
			}
		}

		List<LinkedHashMap> listHE02 = dao.selectTableData(dt, "TSEAIHE02", "EAISvcName", eaiSvcName);
		if (listHE02 == null || listHE02.size() == 0) {
			throw new Exception("interface data is empty  interfaceName =[" + eaiSvcName + "]");
		}

		List<LinkedHashMap> listMS02 = dao.selectTableData(dt, "TSEAIMS02", "EAISvcName", eaiSvcName);

		List<LinkedHashMap> listFR01 = dao.selectTableData(dt, "TSEAIFR01", "EAISvcName", eaiSvcName);

		List<LinkedHashMap> listFR03 = dao.selectTableData(dt, "TSEAIFR03", "EAISvcName", eaiSvcName);

		List<LinkedHashMap> listHS04 = dao.selectTableData(dt, "TSEAIHS04", "EAISvcName", eaiSvcName);
		List<LinkedHashMap> listHS05 = new ArrayList<LinkedHashMap>();
		if (listHS04 != null) {
			for (LinkedHashMap m : listHS04) {
				String bzwkSvcKeyName = (String) m.get("BZWKSVCKEYNAME");
				List<LinkedHashMap> list = dao.selectTableData(dt, "TSEAIHS05", "BZWKSVCKEYNAME", bzwkSvcKeyName);
				listHS05.addAll(list);
			}
		}
		List<LinkedHashMap> listFR06 = dao.selectTableData(dt, "TSEAIFR06", "EAISvcName", eaiSvcName);

		LinkedHashMap<String, List> tables = new LinkedHashMap<String, List>();
		tables.put("TSEAIHE01", listHE01);
		tables.put("TSEAIHE02", listHE02);
		tables.put("TSEAIMS02", listMS02);
		tables.put("TSEAIFR01", listFR01);
		tables.put("TSEAIFR03", listFR03);
		tables.put("TSEAIHS04", listHS04);
		tables.put("TSEAIHS05", listHS05);
		tables.put("TSEAIFR06", listFR06);
		FileWriteUtil.FileWrite(path, eaiSvcName + ".txt", tables);

	}

	/**
	 * b2b downloading
	 */
	public void b2bDownLoading(DataSourceType dt, String path, String extnlInstiIdnfiName) throws Exception {
		List<LinkedHashMap> listHE03 = dao.selectTableData(dt, "TSEAIHE03", "extnlInstiIdnfiName", extnlInstiIdnfiName);
		if (listHE03 == null || listHE03.size() == 0) {
			throw new Exception("b2b extractor data is empty  extnlInstiIdnfiName =[" + extnlInstiIdnfiName + "]");
		}

		LinkedHashMap<String, List> tables = new LinkedHashMap<String, List>();
		tables.put("TSEAIHE03", listHE03);
		FileWriteUtil.FileWrite(path, extnlInstiIdnfiName + ".txt", tables);

	}

	/**
	 * adapter downloading
	 */
	public void adapterDownLoading(DataSourceType dt, String path, String adapterName) throws Exception {

		List<LinkedHashMap> listAD01 = dao.selectTableData(dt, "TSEAIAD01", "AdptrBzwkGroupName", adapterName);
		if (listAD01 == null || listAD01.size() == 0) {
			throw new Exception("adapter data is empty  adapterName =[" + adapterName + "]");
		}
		List<LinkedHashMap> listAD02 = dao.selectTableData(dt, "TSEAIAD02", "AdptrBzwkGroupName", adapterName);
		if (listAD02 == null || listAD02.size() == 0) {
			throw new Exception("adapter data is empty  adapterName =[" + adapterName + "]");
		}

		List<LinkedHashMap> listAD14 = new ArrayList();
		for (LinkedHashMap lhm : listAD02) {
			List<LinkedHashMap> mlistAD14 = dao.selectTableData(dt, "TSEAIAD14", "prptyGroupName",
					(String) lhm.get("PRPTYGROUPNAME"));
			if (mlistAD14 == null || mlistAD14.size() == 0) {
				throw new Exception("adapter propertyGroup data(TSEAIAD14) is empty  prptyGroupName =["
						+ lhm.get("PRPTYGROUPNAME") + "]");
			}
			if (mlistAD14 != null) {
				for (LinkedHashMap m : mlistAD14) {
					m.put("ADPTRBZWKGROUPNAME", adapterName);
				}
				listAD14.addAll(mlistAD14);
			}

		}

		List<LinkedHashMap> listAD15 = new ArrayList();
		for (LinkedHashMap lhm : listAD02) {
			List<LinkedHashMap> mlistAD15 = dao.selectTableData(dt, "TSEAIAD15", "prptyGroupName",
					(String) lhm.get("PRPTYGROUPNAME"));
			if (mlistAD15 == null || mlistAD15.size() == 0) {
				throw new Exception("adapter propertyGroup data(TSEAIAD15) is empty  prptyGroupName =["
						+ lhm.get("PRPTYGROUPNAME") + "]");
			}
			if (mlistAD15 != null) {
				for (LinkedHashMap m : mlistAD15) {
					m.put("ADPTRBZWKGROUPNAME", adapterName);
				}
				listAD15.addAll(mlistAD15);
			}

		}

		List<LinkedHashMap> listFR04 = dao.selectTableData(dt, "TSEAIFR04", "AdptrBzwkGroupName", adapterName);

		LinkedHashMap<String, List> tables = new LinkedHashMap<String, List>();
		tables.put("TSEAIAD01", listAD01);
		tables.put("TSEAIAD02", listAD02);
		tables.put("TSEAIAD14", listAD14);
		tables.put("TSEAIAD15", listAD15);
		tables.put("TSEAIFR04", listFR04);
		FileWriteUtil.FileWrite(path, adapterName + ".txt", tables);

	}

	/**
	 * adapter downloading
	 */
	public void adapterDownLoading(DataSourceType dt, String path, String adapterName, String prefix) throws Exception {

		List<LinkedHashMap> listAD01 = dao.selectTableData(dt, "TSEAIAD01", "AdptrBzwkGroupName", adapterName);
		if (listAD01 == null || listAD01.size() == 0) {
			throw new Exception("adapter data is empty  adapterName =[" + adapterName + "]");
		}
		List<LinkedHashMap> listAD02 = dao.selectTableData(dt, "TSEAIAD02", "AdptrBzwkGroupName", adapterName);
		if (listAD02 == null || listAD02.size() == 0) {
			throw new Exception("adapter data is empty  adapterName =[" + adapterName + "]");
		}
		for (LinkedHashMap ad02 : listAD02) {
			String instance = (String) ad02.get("EAISEVRINSTNCNAME");
			if (instance != null && instance.length() > 5 && prefix != null) {
				String replace = prefix + instance.substring(5);
				ad02.put("EAISEVRINSTNCNAME", replace);
			}
		}

		List<LinkedHashMap> listAD14 = new ArrayList();
		for (LinkedHashMap lhm : listAD02) {
			List<LinkedHashMap> mlistAD14 = dao.selectTableData(dt, "TSEAIAD14", "prptyGroupName",
					(String) lhm.get("PRPTYGROUPNAME"));
			if (mlistAD14 == null || mlistAD14.size() == 0) {
				throw new Exception("adapter propertyGroup data(TSEAIAD14) is empty  prptyGroupName =["
						+ lhm.get("PRPTYGROUPNAME") + "]");
			}
			if (mlistAD14 != null) {
				for (LinkedHashMap m : mlistAD14) {
					m.put("ADPTRBZWKGROUPNAME", adapterName);
				}
				listAD14.addAll(mlistAD14);
			}

		}

		List<LinkedHashMap> listAD15 = new ArrayList();
		for (LinkedHashMap lhm : listAD02) {
			List<LinkedHashMap> mlistAD15 = dao.selectTableData(dt, "TSEAIAD15", "prptyGroupName",
					(String) lhm.get("PRPTYGROUPNAME"));
			if (mlistAD15 == null || mlistAD15.size() == 0) {
				throw new Exception("adapter propertyGroup data(TSEAIAD15) is empty  prptyGroupName =["
						+ lhm.get("PRPTYGROUPNAME") + "]");
			}
			if (mlistAD15 != null) {
				for (LinkedHashMap m : mlistAD15) {
					m.put("ADPTRBZWKGROUPNAME", adapterName);
				}
				listAD15.addAll(mlistAD15);
			}

		}

		List<LinkedHashMap> listFR04 = dao.selectTableData(dt, "TSEAIFR04", "AdptrBzwkGroupName", adapterName);

		LinkedHashMap<String, List> tables = new LinkedHashMap<String, List>();
		tables.put("TSEAIAD01", listAD01);
		tables.put("TSEAIAD02", listAD02);
		tables.put("TSEAIAD14", listAD14);
		tables.put("TSEAIAD15", listAD15);
		tables.put("TSEAIFR04", listFR04);
		FileWriteUtil.FileWrite(path, adapterName + ".txt", tables);

	}

	/**
	 * messageKey downloading
	 */
	public void messageKeyDownLoading(DataSourceType dt, String path, String adapterName) throws Exception {
		List<LinkedHashMap> listFR04 = dao.selectTableData(dt, "TSEAIFR04", "AdptrBzwkGroupName", adapterName);
		LinkedHashMap<String, List> tables = new LinkedHashMap<String, List>();
		tables.put("TSEAIFR04", listFR04);
		FileWriteUtil.FileWrite(path, adapterName + ".txt", tables);
	}

	public HashMap parse(String filePath) throws Exception {
		String delimeter = ";";
		HashMap map = new HashMap();
		ArrayList list = null;

		FileReader fr = new FileReader(filePath);
		if (fr == null) {
			return map;
		}
		BufferedReader br = null;
		String lbuff = null;
		String[] header = null;

		try {
			br = new BufferedReader(fr);
			while ((lbuff = br.readLine()) != null) {
				if (lbuff.indexOf(delimeter) < 0) {
					list = new ArrayList();
					map.put(lbuff.toUpperCase(), list);
					if ((lbuff = br.readLine()) == null) {
						throw new Exception("File Header parsing Error : " + filePath);
					}
					header = lbuff.split(delimeter);
					continue;
				}
				if (header == null)
					continue;
				LinkedHashMap codes = split(header, lbuff, delimeter, header.length);
				list.add(codes);
			}
		} catch (Exception ex) {
			logger.error("parse error", ex);
		} finally {
			if (br != null) {
				br.close();
			}
		}
		return map;
	}

	private LinkedHashMap split(String[] header, String data, String deli, int size) {
		LinkedHashMap array = new LinkedHashMap();
		String data2 = data + " " + deli;// 마지막 에 ";;" 될경우 length 차이가 나서 보정해주는 로직
		String[] s = data2.split(deli);
		for (int i = 0; i < s.length - 1; i++) {
			array.put(header[i], s[i]);
		}
		return array;
	}

	public HashSet<String> getFileInterfaceAdapterList(HashMap map) throws Exception {
		HashSet<String> adapterList = new HashSet<String>();

		String eaiSvcName = "";
		String srcType = "";
		String tgtType = "";

		String sndRecvType = "";

		ArrayList he01 = (ArrayList) map.get("TSEAIHE01");
		if (he01 == null || he01.size() == 0) {
			throw new Exception("EAI 메시지가 없습니다.");
		}
		HashMap he01row0 = (HashMap) he01.get(0);
		String src = (String) he01row0.get("GSTATSYSADPTRBZWKGROUPNAME"); // 기동어댑터명
		srcType = (String) he01row0.get("SVCMOTIVUSEDSTCD"); // 기동인터페이스 유형

		if (StringUtils.isBlank(src)) {
			throw new Exception("기동 어댑터가 없습니다.");
		}
		adapterList.add(src);

		ArrayList he02 = (ArrayList) map.get("TSEAIHE02");
		if (he02 == null || he02.size() == 0) {
			throw new Exception("EAI 서비스처리정보가 없습니다.");
		}
		HashMap he02row0 = (HashMap) he02.get(0);
		String tgt = (String) he02row0.get("PSVSYSADPTRBZWKGROUPNAME"); // 수동어댑터명
		tgtType = (String) he02row0.get("PSVINTFACDSTICNAME"); // 수동인터페이스 유형
		eaiSvcName = (String) he02row0.get("EAISVCNAME"); // 인터페이스명
		sndRecvType = eaiSvcName.substring(eaiSvcName.length() - 2, eaiSvcName.length() - 1); // 인터페이스 송수신구분

		// S-A 응답일때 빼고 수동어댑터 체크
		if (srcType.equals("SYNC") && tgtType.equals("ASYN") && sndRecvType.equals("R")) {
			;
		} else {
			if (StringUtils.isBlank(tgt)) {
				throw new Exception("수동 어댑터가 없습니다.");
			}
			adapterList.add(tgt);
		}
		// A-S > HE01의 인터페이스유형 ASYN이고 HE02의 첫번째 수동인터페이스가 SYNC일 때 -> 수동인터페이스 2개
		if (srcType.equals("ASYN") && tgtType.equals("SYNC")) {
			if (he02.size() == 1) {
				throw new Exception("기동 응답 서비스가 존재하지 않습니다.");
			}
			HashMap he02row1 = (HashMap) he02.get(1);
			tgt = (String) he02row1.get("PSVSYSADPTRBZWKGROUPNAME");
			if (StringUtils.isBlank(tgt)) {
				throw new Exception("기동 응답 어댑터가 없습니다.");
			}
			adapterList.add(tgt);
		}
		return adapterList;
	}

	public void adptInstValid(DataSourceType dataSourceType, HashMap map) throws Exception {
		boolean ret = true;
		ArrayList he01 = (ArrayList) map.get("TSEAIHE01");
		if (he01 == null || he01.size() == 0) {
			ret = false;
			throw new Exception("EAI 메시지가 없습니다.");
		}
		HashMap he01row0 = (HashMap) he01.get(0);
		String eaiSvcName = (String) he01row0.get("EAISVCNAME");
		String srcType = (String) he01row0.get("SVCMOTIVUSEDSTCD");
		String srcAdpt = (String) he01row0.get("GSTATSYSADPTRBZWKGROUPNAME"); // 기동어댑터명

		ArrayList he02 = (ArrayList) map.get("TSEAIHE02");
		if (he02 == null || he02.size() == 0) {
			throw new Exception("[EAI서비스코드 : " + eaiSvcName + "] EAI 서비스처리정보가 없습니다.");
		}
		HashMap he02row0 = (HashMap) he02.get(0);
		String tgtAdpt = (String) he02row0.get("PSVSYSADPTRBZWKGROUPNAME"); // 수동어댑터명
		String tgtType = (String) he02row0.get("PSVINTFACDSTICNAME");

		String sndRecvType = eaiSvcName.substring(eaiSvcName.length() - 2, eaiSvcName.length() - 1); // 인터페이스 송수신구분

		List<LinkedHashMap> ouAdptInstList = dao.selectTableData(dataSourceType, "TSEAIAD02", "ADPTRBZWKGROUPNAME",
				tgtAdpt);
		// outBound 어댑터 정보가 없고 그룹명만 있을 때 ____ 비교 X
		if (ouAdptInstList.size() == 0) {
			return;
		}

		// 기동어댑터와 관계없이 수동어댑터 바인딩된 인스턴스가 ALL일때 우선적으로 PASS (A_S 패턴 포함 - 처리순서1인 어댑터로 비교)
		if ("ALL".equals(ouAdptInstList.get(0).get("EAISEVRINSTNCNAME"))) {
			return;
		}

		List<LinkedHashMap> inAdptInstList = dao.selectTableData(dataSourceType, "TSEAIAD02", "ADPTRBZWKGROUPNAME",
				srcAdpt);
		// inBound 어댑터 정보가 없고 그룹명만 있을 때 ____ 비교 X
		if (inAdptInstList.size() == 0) {
			return;
		}
		List<LinkedHashMap> ouAdptInstList2 = new ArrayList<>();

		// S_A 패턴 응답 ___ 비교 X
		if (srcType.equals("SYNC") && tgtType.equals("ASYN") && sndRecvType.equals("R")) {
			return;
		}
		// A_S 패턴
		if (srcType.equals("ASYN") && tgtType.equals("SYNC")) {
			if (he02.size() == 1) {
				throw new Exception("[EAI서비스코드 : " + eaiSvcName + "] 기동 응답 서비스가 존재하지 않습니다.");
			}
			HashMap he02row1 = (HashMap) he02.get(1);
			tgtAdpt = (String) he02row1.get("PSVSYSADPTRBZWKGROUPNAME");
			ouAdptInstList2 = dao.selectTableData(dataSourceType, "TSEAIAD02", "ADPTRBZWKGROUPNAME", tgtAdpt);
		}
		/**
		 * 아래는 SNA 체크임 우체국에서는 안쓰임 시작
		 */
		/*
		 * //IN 어댑터 List의 ADPTRCD는 다 같음. //String adptrName =
		 * (String)inAdptInstList.get(0).get("ADPTRBZWKNAME"); String adptrGrpName =
		 * (String)inAdptInstList.get(0).get("ADPTRBZWKGROUPNAME"); //IN 어댑터가 SNA일때
		 * boolean flag = false; if(adptrGrpName.indexOf("SNA")>=0){
		 * inAdptInstList.clear(); List<LinkedHashMap> snaApList =
		 * inboundErrorInfoDao.selectSnaAdpApList(dataSourceType, adptrGrpName); String svcApCd =
		 * eaiSvcName.substring(0,3); //서비스코드 앞 3자리 for(LinkedHashMap m : snaApList){
		 * String apList = (String)m.get("PRPTY2VAL"); int chk =
		 * apList.indexOf(svcApCd); if(chk>=0){ inAdptInstList.add(m); flag = true; } }
		 * if(!flag){ ret = false; throw new
		 * Exception("[EAI서비스코드 : "+eaiSvcName+"] AP코드("+svcApCd+")가 SNA어댑터["
		 * +adptrGrpName+"]에 존재하지 않습니다"); } } //in out 불일치 int cnt =
		 * inAdptInstList.size(); String srcAdptName = ""; String tgtAdptName = "";
		 * for(HashMap om : ouAdptInstList){ String ouInst =
		 * (String)om.get("EAISEVRINSTNCNAME"); tgtAdptName =
		 * (String)om.get("ADPTRBZWKGROUPNAME"); if("ALL".equals(ouInst)){ cnt=0; break;
		 * } for(HashMap im : inAdptInstList){ String inInst
		 * =(String)im.get("EAISEVRINSTNCNAME"); srcAdptName =
		 * (String)im.get("ADPTRBZWKGROUPNAME"); if(ouInst.equals(inInst)){ cnt--;
		 * continue; } } } if(cnt>0){ ret = false; throw new
		 * Exception("[EAI서비스코드 : "+eaiSvcName+"] 기동어댑터와 수동어댑터의 인스턴스가 일치하지 않습니다 [기동 : "
		 * +srcAdptName+" / 수동 : "+tgtAdptName+"]"); } if(ouAdptInstList2.size()>0){
		 * //A-S 패턴 수동인터페이스 어댑터2 번째꺼 int cnt2 = inAdptInstList.size(); for(HashMap om :
		 * ouAdptInstList2){ String ouInst = (String)om.get("EAISEVRINSTNCNAME");
		 * tgtAdptName = (String)om.get("ADPTRBZWKGROUPNAME"); if("ALL".equals(ouInst)){
		 * cnt2=0; break; } for(HashMap im : inAdptInstList){ String inInst
		 * =(String)im.get("EAISEVRINSTNCNAME"); srcAdptName =
		 * (String)im.get("ADPTRBZWKGROUPNAME"); if(ouInst.equals(inInst)){ cnt2--;
		 * continue; } } } if(cnt2>0){ ret = false; throw new
		 * Exception("[EAI서비스코드 : "+eaiSvcName+"] 기동어댑터와 수동어댑터의 인스턴스가 일치하지 않습니다 [기동 : "
		 * +srcAdptName+" / 수동 : "+tgtAdptName+"]"); } }
		 */
		/**
		 * 아래는 SNA 체크임 우체국에서는 안쓰임 종료
		 */
	}

	public void adapterGroupValid(DataSourceType dt, HashMap map, String table) throws Exception {

		if (map != null) {
			if ("TSEAIFR04".equals(table)) {
				ArrayList list = (ArrayList) map.get(table);
				if (list != null) {
					HashMap hm = (HashMap) list.get(0);
					String adapterGroupName = (String) hm.get("ADPTRBZWKGROUPNAME");
					List adptList = dao.selectTableData(dt, "TSEAIAD01", "ADPTRBZWKGROUPNAME", adapterGroupName);
					if (adptList.size() == 0) {
						String ioDstcd = (String) hm.get("IODSTCD");
						throw new Exception("메시지 키 관리 [어댑터명 : " + adapterGroupName + " / 기능구분 : " + ioDstcd
								+ "] 에 대한 어댑터가 해당서버에 존재하지 않습니다.");
					}
				}
			} else if ("TSEAIHE03".equals(table)) {
				ArrayList list = (ArrayList) map.get(table);
				if (list != null) {
					HashMap hm = (HashMap) list.get(0);
					String adapterGroupName = (String) hm.get("PSVSYSADPTRBZWKGROUPNAME");
					List adptList = dao.selectTableData(dt, "TSEAIAD01", "ADPTRBZWKGROUPNAME", adapterGroupName);
					if (adptList.size() == 0) {
						String extnlInstiIdfiName = (String) hm.get("EXTNLINSTIIDNFINAME");
						String svcPrcssNo = (String) hm.get("SVCPRCSSNO");
						throw new Exception("B2B기관 정보 관리 [기관코드 : " + extnlInstiIdfiName + " / 서비스처리순서 : " + svcPrcssNo
								+ "] 에 대한 수동 어댑터가 해당서버에 존재하지 않습니다. [" + adapterGroupName + "]");
					}
				}
			}
		}
	}

	@Override
	public void fileInfoDownLoading(DataSourceType dt, String path, String fileName) throws Exception {

      InterfaceDeploy interfaceDeploy = interfaceDeployService.selectDeploy(fileName);
      if (interfaceDeploy == null) {
         throw new Exception ("layout data is empty  layoutName =["+fileName+"]") ;
		}

      com.eactive.eai.rms.common.converter.ElinkObjectMapper mapper = new com.eactive.eai.rms.common.converter.ElinkObjectMapper();
      mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

      String json = mapper.writeValueAsString(interfaceDeploy);
//      System.out.println(json);
      logger.debug(json);

      FileWriteUtil.FileWrite(path , fileName+".txt", json, "UTF-8");
	}

	@Override
	public void insertTableLoading(DataSourceType dt, String path, String fileName) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void tableDownLoading(DataSourceType dt, String path, String tableName) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void tranBlock(DataSourceType dt, String cmd, String type, String value) throws Exception {
		// TODO Auto-generated method stub
		
	}

}