package com.eactive.eai.rms.onl.transaction.trackingchart;

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

//import org.codehaus.jackson.map.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eactive.eai.rms.common.acl.user.BizDao;
import com.eactive.eai.rms.common.base.OnlBaseService;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.util.StringUtils;
import com.eactive.eai.rms.onl.common.exception.BizException;
import com.eactive.eai.rms.onl.manage.adapter.adapter.AdapterDao;
import com.eactive.eai.rms.onl.manage.rule.transform.TransformDao;
import com.eactive.eai.rms.onl.transaction.extnl.InterfaceManService;
import com.eactive.eai.rms.onl.transaction.extnl.ui.InterfaceUI;
import com.google.gson.Gson;

@Service("trackingChartService")
public class TrackingChartService extends OnlBaseService {

	@Autowired
	@Qualifier("trackingChartDao")
	private TrackingChartDao dao;

	@Autowired
	@Qualifier("transformDao")
	private TransformDao transformDao;

	@Autowired
	@Qualifier("bizDao")
	private BizDao bizDao;

	@Autowired
	@Qualifier("adapterDao")
	private AdapterDao adapterDao;

	@Autowired
	private ComboService comboService;

	@Autowired
	private InterfaceManService interfaceManService;

	// CODE GROUP LIST
	public HashMap selectCodeGroupList(String searchName) throws Exception {
		HashMap paramMap = new HashMap();
		// 검색 조건
		paramMap.put("searchName", searchName);

		List list = dao.selectCodeGroupList(paramMap);
		HashMap map = new HashMap();

		map.put("list", list);
		return map;
	}

	public HashMap selectPopList(int startNum, int endNum, String eaiSvcName) throws Exception {

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("eaiSvcName", eaiSvcName);
		// 전체 목록수 얻기
		int totalCount = dao.selectPopListCount(paramMap);

		paramMap.put("startNum", startNum);// 시작 record
		paramMap.put("endNum", endNum);// 끝 record

		List<Map<String, Object>> list = dao.selectPopList(paramMap);
		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("totalCount", totalCount);
		map.put("list", list);

		return map;
	}

	public HashMap<String, Object> selectDetail(String eaiSvcName) throws Exception {

		HashMap<String, Object> detail = dao.selectDetail(eaiSvcName);

		String cnvsnName = (String) detail.get("CHNGMSGIDNAME1");
		String bascRspn = (String) detail.get("BASCRSPNSCHNGMSGIDNAME1");
		String errRspn = (String) detail.get("ERRRSPNSCHNGMSGIDNAME1");

		HashMap paramMap = new HashMap();
		paramMap.put("cnvsnName", cnvsnName);
		List transform1 = transformDao.selectTransformSourceResult(paramMap);

		paramMap.put("cnvsnName", bascRspn);
		List transform2 = transformDao.selectTransformSourceResult(paramMap);

		paramMap.put("cnvsnName", errRspn);
		List transform3 = transformDao.selectTransformSourceResult(paramMap);

		List transform = new ArrayList();
		transform.add(transform1);
		transform.add(transform2);
		transform.add(transform3);

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("detail", detail);
		map.put("transform", transform);

		return map;

	}

	public HashMap selectAdapterCombo(HashMap paramMap) throws Exception {
		List adapter = adapterDao.selectListCombo(paramMap);

		HashMap map = new HashMap();

		map.put("adapter", adapter);
		return map;
	}

	public HashMap selectDetailCombo(String userId) throws Exception {
		HashMap paramMap = new HashMap();
		paramMap.put("userId", userId);

		// 업무구분
		List listBiz = bizDao.selectUserBizCodes(paramMap);

		// 당타발구분
		List<ComboVo> listIo = comboService.getFromCode("IO_TYPE");
		// 전문요청구분
		List<ComboVo> listReqRes = comboService.getFromCode("REQRES_TYPE");
		// 송수신방법
		List<ComboVo> listSyncAsync = comboService.getFromCode("SYNCASYNC_TYPE");

		// 거래흐름(INBOUND);
		paramMap = new HashMap();
		paramMap.put("adptrIoDstcd", "IN");
		List listFrom = adapterDao.selectListCombo(paramMap);
		// 거래흐름(OUTBOUND);
		paramMap = new HashMap();
		paramMap.put("adptrIoDstcd", "OU");
		List listTo = adapterDao.selectListCombo(paramMap);
		// 거래흐름(ASBOUND);
		paramMap = new HashMap();
		paramMap.put("adptrIoDstcd", "OU");
		paramMap.put("asyncDstCd", "As");
		List listExt = adapterDao.selectListCombo(paramMap);

		HashMap map = new HashMap();

		map.put("bizList", listBiz);
		map.put("ioList", listIo);
		map.put("reqResList", listReqRes);
		map.put("syncAsyncList", listSyncAsync);
		map.put("fromList", listFrom);
		map.put("toList", listTo);
		map.put("extList", listExt);

		return map;
	}

	public List insert(HashMap<String, Object> map, String transformData) throws Exception {

		// interface insert;
		// interfaceService.insert(map);
		List<String> syncCnvsnForRld = new ArrayList(); // 재로딩

		HashMap detail = dao.selectDetail((String) map.get("eaiSvcName"));
		if (detail != null)
			throw new BizException("중복된 인터페이스입니다.");
		// 2.등록
		ObjectMapper objectMapper = new ObjectMapper();
		InterfaceUI interfaceUI = objectMapper.convertValue(map, InterfaceUI.class);

		interfaceManService.createInterfaceJummun(interfaceUI);

		Gson gson = new Gson();
		HashMap[] list = gson.fromJson(transformData, HashMap[].class);

		HashMap paramMap = new HashMap();
		for (int i = 0; i < list.length; i++) {
			String cnvsnName = (String) list[i].get("cnvsnName");
			String cnvsnDesc = (String) list[i].get("cnvsnDesc");
			String loutTarget = (String) list[i].get("loutTarget");
			String loutSource = (String) list[i].get("loutSource");
			String cnvsnType = null;
			String chngEonot = null;

			if (!"".equals(cnvsnName) && cnvsnName != null) {
				chngEonot = "1";
				cnvsnType = (String) list[i].get("cnvsnType");
				/*
				 * if("ASYN".equals((String)map.get("svcMotivUseDstcd")) ){ cnvsnType =
				 * (String)list[i].get("cnvsnType"); }else{ cnvsnType =
				 * cnvsnName.substring(cnvsnName.lastIndexOf("_")+1);
				 * 
				 * }
				 */

				paramMap = new HashMap();
				paramMap.put("eaiSvcName", map.get("eaiSvcName"));
				paramMap.put("cnvsnName", list[i].get("cnvsnName"));
				paramMap.put("cnvsnType", cnvsnType);
				paramMap.put("chngEonot", chngEonot);// 0.미사용 1.사용

				// 1.EAI Message에 셋팅
				dao.updateTransfromEaiSvcName(paramMap);

				paramMap = new HashMap();

				// 2. TR01,TR02 삭제 및 등록

				paramMap.put("cnvsnName", cnvsnName);
				paramMap.put("cnvsnDesc", cnvsnDesc);
				paramMap.put("eaiSevrDstcd", map.get("eaiSevrDstcd"));
				paramMap.put("useYn", 1);

				dao.deleteTransform(paramMap);
				dao.insertTransform(paramMap);

				List oldLoutNames = transformDao.selectTransformSourceResult(paramMap);
				String oldLoutSrc = null;
				String oldLoutTgt = null;

				// 2-1.SRC 등록
				// String srcLayoutName
				// =StringUtils.split((String)map.get("layoutMappingName"),",")[i];

				paramMap.put("loutName", loutSource);
				paramMap.put("sourcRsultDstcd", "SRC_LAYOUT");
				dao.insertTransformSourceResult(paramMap);

				// 2-2.TGT 등록
				paramMap.put("loutName", loutTarget);
				paramMap.put("sourcRsultDstcd", "TGT_LAYOUT");
				dao.insertTransformSourceResult(paramMap);

				for (int j = 0; j < oldLoutNames.size(); j++) {
					HashMap t = (HashMap) oldLoutNames.get(j);
					if ("SRC_LAYOUT".equals(t.get("SOURCRSULTDSTCD")))
						oldLoutSrc = (String) t.get("LOUTNAME");
					if ("TGT_LAYOUT".equals(t.get("SOURCRSULTDSTCD")))
						oldLoutTgt = (String) t.get("LOUTNAME");

				}

				if (!loutSource.equals(oldLoutSrc) || !loutTarget.equals(oldLoutTgt)) {
					paramMap = new HashMap();
					paramMap.put("cnvsnName", cnvsnName);
					dao.deleteTransformItem(paramMap);
					// syncCnvsn.add(oldCnvsnName);

				}
				syncCnvsnForRld.add(cnvsnName);
			}

		}

		return syncCnvsnForRld;

	}

	// CODE UPDATE
	public HashMap update(HashMap<String, Object> map, String transformData) throws Exception {

		List<String> syncCnvsnForRmv = new ArrayList(); // 삭제되는 cnvsnName 저장
		List<String> syncCnvsnForRld = new ArrayList(); // 재로딩
		Gson gson = new Gson();
		HashMap[] list = gson.fromJson(transformData, HashMap[].class);
		HashMap<String, Object> detail = dao.selectDetail((String) map.get("eaiSvcName"));

		HashMap paramMap = new HashMap();

		// Pre value 추출
		String oldcnvsn = (String) detail.get("CHNGMSGIDNAME1");
		String oldBasRspn = (String) detail.get("BASCRSPNSCHNGMSGIDNAME1");
		String oldErrRspn = (String) detail.get("ERRRSPNSCHNGMSGIDNAME1");
		if (oldcnvsn == null)
			oldcnvsn = "";
		if (oldBasRspn == null)
			oldBasRspn = "";
		if (oldErrRspn == null)
			oldErrRspn = "";

		List<String> oldcnvsnNames = new ArrayList();
		oldcnvsnNames.add(oldcnvsn);
		if (!"ASYN".equals((String) map.get("svcMotivUseDstcd")))
			oldcnvsnNames.add(oldBasRspn);
		oldcnvsnNames.add(oldErrRspn);

		// 인터페이스 초기화 및 저장
		ObjectMapper objectMapper = new ObjectMapper();
		InterfaceUI interfaceUI = objectMapper.convertValue(map, InterfaceUI.class);
		interfaceManService.createInterfaceJummun(interfaceUI);
		// System.out.println("INTERFACE_UPDATED");
		for (int i = 0; i < list.length; i++) { // Asyn 1~2 , Sync 1~3
			String cnvsnName = StringUtils.getStringValue((String) list[i].get("cnvsnName"), "");
			String oldCnvsnName = StringUtils.getStringValue((String) oldcnvsnNames.get(i), "");
			String cnvsnDesc = (String) list[i].get("cnvsnDesc");
			String loutTarget = (String) list[i].get("loutTarget");
			String loutSource = (String) list[i].get("loutSource");

			String cnvsnType = null;
			String chngEonot = null;

			if ("".equals(cnvsnName) || cnvsnName == null) {
				chngEonot = "0";
				cnvsnName = "";
			} else {
				chngEonot = "1";
			}
			cnvsnType = (String) list[i].get("cnvsnType");
			/*
			 * if("ASYN".equals((String)map.get("svcMotivUseDstcd")) ){ cnvsnType =
			 * (String)list[i].get("cnvsnType"); }else{ if(cnvsnName == null ||
			 * "".equals(cnvsnName)){ if("".equals(oldCnvsnName) || oldCnvsnName == null){
			 * continue; }else{ cnvsnType =
			 * oldCnvsnName.substring(oldCnvsnName.lastIndexOf("_")+1); } }else{ cnvsnType =
			 * cnvsnName.substring(cnvsnName.lastIndexOf("_")+1); } }
			 */

			paramMap = new HashMap();
			paramMap.put("eaiSvcName", map.get("eaiSvcName"));
			paramMap.put("cnvsnName", cnvsnName);
			paramMap.put("cnvsnType", cnvsnType);
			paramMap.put("chngEonot", chngEonot);// 0.미사용 1.사용
			paramMap.put("eaiSevrDstcd", map.get("eaiSevrDstcd"));
			paramMap.put("useYn", 1);
			// 1.EAI Message에 셋팅
			if (cnvsnType != null && !"".equals(cnvsnType))
				dao.updateTransfromEaiSvcName(paramMap);

			// 2. TR01,TR02 삭제 및 등록
			paramMap = new HashMap();
			paramMap.put("searchCnvsnName", oldCnvsnName); // 기존
			int cnt = transformDao.selectListCount(paramMap);
			if (!cnvsnName.equals(oldCnvsnName))
				cnt = cnt + 1; // 다르면 이미 HE02 업데이트로 cnt가 -1 된 상태이므로 아래 조건문 비교를 위해 +1
			if ("".equals(oldCnvsnName))
				cnt = 0; // oldCnvsnName이 ""이면 전체 조회가 되므로 Cnt 판별이 어려우므로 0으로 처리

			boolean owned = oldCnvsnName.indexOf((String) map.get("eaiSvcName")) > -1 ? true : false;
			if (cnt < 2)
				owned = true; // cnt <2 조건은 own Data가 이미 삭제된 상태에서 처리대상 데이터가 유일하게 가지고 있는 데이터 이므로 조건에 포함

			// owned는 삭제 처리에만 해당

			if ("".equals(cnvsnName) || cnvsnName == null) { // 빈칸이면 삭제로 본다.
				if (!"".equals(oldCnvsnName)) { // 기존 데이터가 있다면 // HE02에서 가져온 값 //TR01 또는 TR02의 값과 별개
					if (owned == true) {
						paramMap = new HashMap();
						paramMap.put("cnvsnName", oldCnvsnName);
						// 공유되는 변환코드 링크 끊기 To HE02
						List<Map<String, Object>> sharedTrans = dao.selectSharedTrans(paramMap);

						for (int j = 0, n = sharedTrans.size(); j < n; j++) {

							Map m = sharedTrans.get(j);
							// String mCnvsnName = (String)m.get("CNVSNNAME");
							String mCnvsnType = (String) m.get("CNVSNTYPE");
							String mEaiSvcName = (String) m.get("EAISVCNAME");

							if (mCnvsnType != null && !"".equals(mCnvsnType)) {
								HashMap mParam = new HashMap();
								mParam.put("cnvsnName", "");
								mParam.put("eaiSvcName", mEaiSvcName);
								mParam.put("cnvsnType", mCnvsnType);
								mParam.put("chngEonot", "0");// 0.미사용 1.사용
								dao.updateTransfromEaiSvcName(mParam);
							}
						}

						paramMap = new HashMap();
						paramMap.put("cnvsnName", oldCnvsnName);

						dao.deleteTransform(paramMap);
						dao.deleteTransformSourceResult(paramMap);
						dao.deleteTransformItem(paramMap);

						syncCnvsnForRmv.add(oldCnvsnName);
					}
				}
			} else {
				if ("".equals(oldCnvsnName) || oldCnvsnName == null)
					oldCnvsnName = cnvsnName;

				if (owned || cnvsnName.equals(oldCnvsnName)) {
					paramMap = new HashMap();
					paramMap.put("cnvsnName", oldCnvsnName);
					dao.deleteTransform(paramMap);

				} else {
					paramMap = new HashMap();
					paramMap.put("cnvsnName", cnvsnName);
					dao.deleteTransform(paramMap);
				}

				paramMap = new HashMap();
				paramMap.put("cnvsnName", cnvsnName);
				paramMap.put("cnvsnDesc", cnvsnDesc);
				paramMap.put("eaiSevrDstcd", map.get("eaiSevrDstcd"));
				paramMap.put("useYn", 1);

				dao.insertTransform(paramMap);

				// update 될 cnvsn DB 정보를 불러온다
				List oldLoutNames = transformDao.selectTransformSourceResult(paramMap);
				String oldLoutSrc = null;
				String oldLoutTgt = null;

				for (int j = 0; j < oldLoutNames.size(); j++) {
					HashMap t = (HashMap) oldLoutNames.get(j);
					if ("SRC_LAYOUT".equals(t.get("SOURCRSULTDSTCD")))
						oldLoutSrc = (String) t.get("LOUTNAME");
					if ("TGT_LAYOUT".equals(t.get("SOURCRSULTDSTCD")))
						oldLoutTgt = (String) t.get("LOUTNAME");

				}

				// TR02 삭제
				if (owned || cnvsnName.equals(oldCnvsnName)) {
					paramMap = new HashMap();
					paramMap.put("cnvsnName", oldCnvsnName);
					dao.deleteTransformSourceResult(paramMap);
				} else {
					paramMap = new HashMap();
					paramMap.put("cnvsnName", cnvsnName);
					dao.deleteTransformSourceResult(paramMap);
				}

				paramMap = new HashMap();
				paramMap.put("cnvsnName", cnvsnName);

				// 2-1.SRC 등록
				paramMap.put("loutName", loutSource);
				paramMap.put("sourcRsultDstcd", "SRC_LAYOUT");
				dao.insertTransformSourceResult(paramMap);

				// 2-2.TGT 등록
				paramMap.put("loutName", loutTarget);
				paramMap.put("sourcRsultDstcd", "TGT_LAYOUT");
				dao.insertTransformSourceResult(paramMap);

				// 3. TR03 삭제
				if (!cnvsnName.equals(oldCnvsnName) || !loutSource.equals(oldLoutSrc)
						|| !loutTarget.equals(oldLoutTgt)) {
					if (!"".equals(oldCnvsnName)) {
						if (owned) {
							paramMap = new HashMap();
							paramMap.put("cnvsnName", oldCnvsnName);
							dao.deleteTransformItem(paramMap);
						}
					}
				}

				if (owned) {
					syncCnvsnForRmv.add(oldCnvsnName);
				}
				syncCnvsnForRld.add(cnvsnName);
			}

		} // end for loop

		HashMap syncCnvsn = new HashMap();
		syncCnvsn.put("forRmv", syncCnvsnForRmv);
		syncCnvsn.put("forRld", syncCnvsnForRld);
		return syncCnvsn;

	}

	// CODE DELETE
	public HashMap delete(String eaiSvcName, String transformData) throws Exception {
		List<String> syncEaiSvcName = new ArrayList();
		boolean forSync = false;
		// Gson gson = new Gson();
		// HashMap[] list = gson.fromJson(transformData, HashMap[].class);

		HashMap<String, Object> detail = dao.selectDetail(eaiSvcName);
		// Pre value 추출
		String oldcnvsn = (String) detail.get("CHNGMSGIDNAME1");
		String oldBasRspn = (String) detail.get("BASCRSPNSCHNGMSGIDNAME1");
		String oldErrRspn = (String) detail.get("ERRRSPNSCHNGMSGIDNAME1");
		if (oldcnvsn == null)
			oldcnvsn = "";
		if (oldBasRspn == null)
			oldBasRspn = "";
		if (oldErrRspn == null)
			oldErrRspn = "";

		List<String> oldcnvsnNames = new ArrayList();
		oldcnvsnNames.add(oldcnvsn);
		oldcnvsnNames.add(oldBasRspn);
		oldcnvsnNames.add(oldErrRspn);
		try {
			for (int i = 0; i < oldcnvsnNames.size(); i++) {
				// String cnvsnName = (String)list[i].get("cnvsnName");
				String cnvsnName = (String) oldcnvsnNames.get(i);
				HashMap paramMap = new HashMap();
				paramMap.put("searchCnvsnName", cnvsnName); // 기존
				int cnt = transformDao.selectListCount(paramMap);

				boolean owned = false;
				if (cnvsnName.indexOf(eaiSvcName) > -1 || cnt < 2)
					owned = true;

				paramMap = new HashMap();
				paramMap.put("cnvsnName", cnvsnName);

				if (owned) {

					// 공유되는 변환코드 삭제 To HE02
					List<Map<String, Object>> sharedTrans = dao.selectSharedTrans(paramMap);

					for (int j = 0, n = sharedTrans.size(); j < n; j++) {
						Map m = sharedTrans.get(j);
						// String mCnvsnName = (String)m.get("CNVSNNAME");
						String mCnvsnType = (String) m.get("CNVSNTYPE");
						String mEaiSvcName = (String) m.get("EAISVCNAME");

						HashMap mParam = new HashMap();
						mParam.put("cnvsnName", "");
						mParam.put("eaiSvcName", mEaiSvcName);
						mParam.put("cnvsnType", mCnvsnType);
						mParam.put("chngEonot", "0");// 0.미사용 1.사용
						dao.updateTransfromEaiSvcName(mParam);
						syncEaiSvcName.add(mEaiSvcName);
					}

					// 1.delete transform //TR01 삭제
					dao.deleteTransform(paramMap);
					// 2.delete transform //TR02 삭제
					dao.deleteTransformSourceResult(paramMap);
					// 3.delete transform //TR03 삭제
					dao.deleteTransformItem(paramMap);

					// paramMap = new HashMap();

					forSync = true;
				}
			}
		} catch (Exception e) {

			logger.error("CODE DELETE error", e);

		}

		// 4. 인터페이스 삭제
		InterfaceUI interfaceUI = new InterfaceUI();
		interfaceUI.setEaiSvcName(eaiSvcName);
		interfaceManService.delete(interfaceUI);

		HashMap retMap = new HashMap();
		retMap.put("forSync", new Boolean(forSync));
		retMap.put("syncEaiSvcName", syncEaiSvcName);
		return retMap;

	}

	public void insertMapping(HashMap<String, Object> map, String gridData, String cnvsnName) throws Exception {

		HashMap map1 = new HashMap();
		map1.put("cnvsnName", map.get("cnvsnName"));
		map1.put("cnvsnDesc", map.get("cnvsnDesc"));
		// TR01
		// ------------------------ INTERFACE 업데이트 시 자동업데이트 되므로 삭제 ---------//
		// transformDao.deleteTransform(map1);
		// transformDao.insert(map1);
		// System.out.println(map1);
		/*
		 * //TR02 transformDao.deleteTransformSourceResult(map);
		 * 
		 * map1.put("loutName", map.get("srcLayout")); map1.put("sourcRsultDstcd",
		 * "SRC_LAYOUT"); transformDao.insertTransformSourceResult(map1);
		 * 
		 * //2-2.TGT 등록 map1.put("loutName", map.get("tgtLayout"));
		 * map1.put("sourcRsultDstcd", "TGT_LAYOUT");
		 * transformDao.insertTransformSourceResult(map1);
		 */

		Gson gson = new Gson();
		HashMap[] list = gson.fromJson(gridData, HashMap[].class);

		HashMap<String, Object> paramMap = new HashMap();

		paramMap.put("cnvsnName", cnvsnName);
		dao.deleteTransformItem(paramMap);
		for (int i = 0; i < list.length; i++) {
			paramMap.put("cnvsnCmdName", list[i].get("CNVSNCMDNAME"));
			paramMap.put("cnvsnRsultItemPathName", list[i].get("CNVSNRSULTITEMPATHNAME"));
			paramMap.put("cnvsnItemBascVal", StringUtils.getStringValue(list[i].get("CNVSNITEMBASCVAL"), ""));
			paramMap.put("cnvsnItemSerno", list[i].get("CNVSNITEMSERNO"));

			dao.insertTransformItem(paramMap);
		}

	}

	public void deleteMapping(String cnvsnName) throws Exception {
		HashMap<String, Object> paramMap = new HashMap();

		paramMap.put("cnvsnName", cnvsnName);
		dao.deleteTransformItem(paramMap);

	}

	public void clone(HashMap<String, Object> map, String transformData) throws Exception {
		String newEaiSvcName = (String) map.get("newEaiSvcName");
		String newEaiTranName = newEaiSvcName.substring(0, newEaiSvcName.length() - 2);

		map.put("eaiSvcName", newEaiSvcName);
		map.put("eaiTranName", newEaiTranName);
		map.put("bzwkSvcKeyName", newEaiTranName);
		HashMap detail = dao.selectDetail((String) map.get("eaiSvcName"));
		if (detail != null)
			throw new Exception("중복된 인터페이스입니다.");
		ObjectMapper objectMapper = new ObjectMapper();
		InterfaceUI interfaceUI = objectMapper.convertValue(map, InterfaceUI.class);

		// 2.등록
		interfaceManService.createInterfaceJummun(interfaceUI);

		Gson gson = new Gson();
		HashMap[] list = gson.fromJson(transformData, HashMap[].class);

		HashMap paramMap = new HashMap();
		for (int i = 0; i < list.length; i++) {
			String cnvsnName = (String) list[i].get("cnvsnName");
			// String cnvsnDesc = (String)list[i].get("cnvsnDesc");
			// String loutTarget = (String)list[i].get("loutTarget");
			String cnvsnType = null;
			String chngEonot = null;

			if (!"".equals(cnvsnName) && cnvsnName != null) {
				chngEonot = "1";
				cnvsnType = (String) list[i].get("cnvsnType");
				/*
				 * if("ASYN".equals((String)map.get("svcMotivUseDstcd")) ){ cnvsnType =
				 * (String)list[i].get("cnvsnType"); }else{ cnvsnType =
				 * cnvsnName.substring(cnvsnName.lastIndexOf("_")+1);
				 * 
				 * }
				 */

				paramMap = new HashMap();
				paramMap.put("eaiSvcName", map.get("eaiSvcName"));
				paramMap.put("cnvsnName", list[i].get("cnvsnName"));
				paramMap.put("cnvsnType", cnvsnType);
				paramMap.put("chngEonot", chngEonot);// 0.미사용 1.사용

				// 1.EAI Message에 셋팅
				dao.updateTransfromEaiSvcName(paramMap);
			}

		}

	}

	public String checkInterface(String eaiSvcName) throws Exception {

		HashMap<String, Object> paramMap = new HashMap();
		paramMap.put("eaiSvcName", eaiSvcName);
		int totalCount = dao.selectPopListCount(paramMap);

		if (totalCount < 1)
			return "N";
		else
			return "Y";

	}

	public List selectCntList(String transformData, String eaiSvcName) throws Exception {

		Gson gson = new Gson();
		HashMap[] list = gson.fromJson(transformData, HashMap[].class);
		List mList = new ArrayList();
		// List oList = new ArrayList();

		HashMap<String, Object> detail = dao.selectDetail(eaiSvcName);
		// Pre value 추출
		String oldcnvsn = (String) detail.get("CHNGMSGIDNAME1");
		String oldBasRspn = (String) detail.get("BASCRSPNSCHNGMSGIDNAME1");
		String oldErrRspn = (String) detail.get("ERRRSPNSCHNGMSGIDNAME1");

		if (oldcnvsn == null)
			oldcnvsn = "";
		if (oldBasRspn == null)
			oldBasRspn = "";
		if (oldErrRspn == null)
			oldErrRspn = "";

		List<String> oldcnvsnNames = new ArrayList();
		oldcnvsnNames.add(oldcnvsn);
		if (!"ASYN".equals((String) detail.get("svcMotivUseDstcd")))
			oldcnvsnNames.add(oldBasRspn);
		oldcnvsnNames.add(oldErrRspn);

		// HashMap<String, Object> map =new HashMap<String, Object>() ;
		for (int i = 0; i < list.length; i++) {
			boolean owned = false;
			if ("".equals(list[i].get("cnvsnName"))) // cnvsnName이 제거된다면
				list[i].put("cnvsnName", (String) oldcnvsnNames.get(i)); // oldCnvsnName으로 조회

			if (!"".equals(list[i].get("cnvsnName"))) {
				String cnvsnName = (String) list[i].get("cnvsnName");
				if (cnvsnName.indexOf(eaiSvcName) > 0)
					owned = true;

				HashMap paramMap = new HashMap();
				paramMap.put("searchCnvsnName", cnvsnName);
				int cnt = transformDao.selectListCount(paramMap);

				if (cnt > 1 && owned == true) {
					mList.add(cnvsnName);

				}
			}
		}

		return mList;

	}
}
