package com.eactive.eai.rms.bat.web.eims;

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

import org.springframework.stereotype.Repository;

import com.eactive.eai.rms.bat.web.eims.vo.BatAdapterVo;
import com.eactive.eai.rms.bat.web.eims.vo.BatInterfaceVo;
import com.eactive.eai.rms.bat.web.eims.vo.BatScheduleSubVo;
import com.eactive.eai.rms.bat.web.eims.vo.BatScheduleVo;
import com.eactive.eai.rms.bat.web.eims.vo.DbAdapterVo;
import com.eactive.eai.rms.bat.web.eims.vo.FileAdapterVo;
import com.eactive.eai.rms.common.base.SqlMapClientTemplateDao;


@Repository("eimsBatDao")
@SuppressWarnings({ "unchecked", "deprecation" })
public class EIMSBatDao extends SqlMapClientTemplateDao {

	//** 1-1-1. 인터페이스목록 조회 
	public int selectIntfidCheck(String intfId)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("intfId", intfId);
		return (Integer) template.queryForObject("EIMSBat.selectIntfidCheck", params);
	}	

	//**1-1-3. 작업오더가 [신규]일 경우
	public void insertIntfidRegister(BatInterfaceVo intfVo)  throws Exception {
		template.insert("EIMSBat.insertIntfidRegister", intfVo);
	}	
	public void deleteIntfidRegister(String intfId)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("intfId", intfId);	
		template.delete("EIMSBat.deleteIntfRegister", params);
	}		
	public void updateSndAdptToIntf(BatInterfaceVo intfVo)  throws Exception {
		template.update("EIMSBat.updateSndAdptToIntf", intfVo);
	}
	
	//**1-1-2. 작업오더가 [수정]일 경우
	public void updateIntfidRegister(BatInterfaceVo intfVo)  throws Exception {
		template.update("EIMSBat.updateIntfidRegister", intfVo);
	}
	
	
	
	
	public List<Map<String, String>> selectListAdptrCheck(String workGrpCd,String intfId, String sndRcvKind, String dbeventwhere) throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("workGrpCd", workGrpCd);
		params.put("intfId", intfId);	
		params.put("sndRcvKind", sndRcvKind);	
		params.put("dbeventwhere", dbeventwhere);	
    	return (List<Map<String, String>>)template.queryForList("EIMSBat.selectListAdptrCheck", params);
    }	
	
	public HashMap<String, Object> selectAdptrCheck(String workGrpCd,String intfId)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("workGrpCd", workGrpCd);
		params.put("intfId", intfId);	
		return (HashMap<String, Object>)template.queryForObject("EIMSBat.selectAdptrCheck", params);
	}	
	
	public String selectCreateAdaptrCd(String adaptrHead, String workGrpCd)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("adaptrHead", adaptrHead);
		params.put("workGrpCd", workGrpCd);		
		return (String)template.queryForObject("EIMSBat.selectCreateAdptrCd", params);
	}
	
	//** 1-1-2. 아답터 신규 생성(기본)
	public void insertAdaptr02Register(BatAdapterVo adapterVo)  throws Exception {
		template.insert("EIMSBat.insertAdaptr02Register", adapterVo);
	}
	public void deleteAdaptr02Register(BatAdapterVo adapterVo)  throws Exception {
		template.delete("EIMSBat.deleteAdapter02", adapterVo);
	}

	//** 1-1-2. 아답터 신규 생성(상세)-DB
	public void insertAdaptr03Register(DbAdapterVo dbAdapterVo)  throws Exception {
		template.insert("EIMSBat.insertAdaptr03Register", dbAdapterVo);
	}
	
	public void mergeAdaptr03Register(DbAdapterVo dbAdapterVo)  throws Exception {
		template.queryForObject("EIMSBat.mergeAdaptr03Register", dbAdapterVo);
	}	
	
	//**1-1-2. 아답터에 인터페이스 ID 매핑-DB
	public void updateAdaptr03Register(DbAdapterVo dbAdapterVo)  throws Exception {
		template.update("EIMSBat.updateAdaptr03Register", dbAdapterVo);
	}	
	
	public void deleteAdaptr03Register(DbAdapterVo dbAdapterVo)  throws Exception {
		template.delete("EIMSBat.deleteAdaptr03Register", dbAdapterVo);
	}

	//** 1-1-2. 아답터 신규 생성(상세)-File
	public void insertSubFileAdaptr(FileAdapterVo fileAdapterVo)  throws Exception {
		template.insert("EIMSBat.insertSubFileAdaptr", fileAdapterVo);
	}	
	
	public void mergeSubFileAdaptr(FileAdapterVo fileAdapterVo)  throws Exception {
		template.queryForObject("EIMSBat.mergeSubFileAdaptr", fileAdapterVo);
	}	
	
	//**1-1-2. 아답터에 인터페이스 ID 매핑-File
	public void updateSubFileAdaptr(FileAdapterVo fileAdapterVo)  throws Exception {
		template.update("EIMSBat.updateSubFileAdaptr", fileAdapterVo);
	}	
		
	public void deleteSubFileAdaptr(FileAdapterVo fileAdapterVo)  throws Exception {
		template.delete("EIMSBat.deleteSubFileAdaptr", fileAdapterVo);
	}
	
	//** 4-1. 인터페이스 아이디로 스케쥴 여부 체크
	public String selectScheCheck(String intfId)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("intfId", intfId);
		return (String)template.queryForObject("EIMSBat.selectScheCheck", params);
	}	

	//** 4-1. 스케쥴 수정
	public void updateScheRegister(BatScheduleVo batScheduleVo)  throws Exception {
		template.update("EIMSBat.updateScheRegister", batScheduleVo);
	}	
	
	//** 4-1. 스케쥴ID 채번	
	public String selectScheID(String bjobMsgScheID)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("bjobMsgScheID", bjobMsgScheID);
		return (String)template.queryForObject("EIMSBat.selectScheID", params);
	}

	//** 4-1. 스케쥴인터페이스 등록
	public void insertScheSubInterface(BatScheduleSubVo batScheduleSubVo)  throws Exception {
		template.insert("EIMSBat.insertScheSubInterface", batScheduleSubVo);
	}	
	public void deleteScheSubInterface(String intfId)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("intfId", intfId);	
		template.delete("EIMSBat.deleteScheSubInterface", params);
	}	
	
	//** 4-1. 스케쥴상세 등록
	public void insertScheRegister(BatScheduleVo batScheduleVo)  throws Exception {
		template.insert("EIMSBat.insertScheRegister", batScheduleVo);
	}	
	public void deleteScheRegister(String intfId)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("intfId", intfId);	
		template.delete("EIMSBat.deleteScheRegister", params);
	}		

	//** 공통. 시스템접속정보조회 
	public HashMap<String, String> selectSystemInfo(String envrdvsncd,String sysdvsncd,String workGrpCd,String batchType)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		if("DD".equals(batchType)) {
			batchType = "DB";
		}
		if("FF".equals(batchType)) {
			batchType = "AP";
		}
		params.put("envrdvsncd", envrdvsncd);
		params.put("sysdvsncd", sysdvsncd);	
		params.put("workgrpcd", workGrpCd);	
		params.put("batchType", batchType);		
		return (HashMap<String, String>)template.queryForObject("EIMSBat.selectSystemInfo", params);
	}	
	
	public HashMap<String, String> selectNextAdptCdSeqDB(String adptrcd,String workGrpcd,String sndRcvKind)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();		
		params.put("adptrcd", adptrcd);
		params.put("workGrpcd", workGrpcd);	
		params.put("sndRcvKind", sndRcvKind);		
		return (HashMap<String, String>)template.queryForObject("EIMSBat.selectNextAdptCdSeqDB", params);
	}	
	
	public HashMap<String, String> selectNextAdptCdSeqFile(String adptrcd, String workGrpcd)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();		
		params.put("adptrcd", adptrcd);	
		params.put("workGrpcd", workGrpcd);
		return (HashMap<String, String>)template.queryForObject("EIMSBat.selectNextAdptCdSeqFile", params);
	}	
	
	//** 공통 내부업무구분코드 코드 가져오기
	public String selectWorkGrpcd(String applCd)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("appCd", applCd);
		return (String) template.queryForObject("EIMSBat.selectWorkGrpcd", params);
	}

	//** EIMS 수신 데이터 로그 저장
	public String insertEimsRcvRegister(HashMap<String,Object> map)  throws Exception {
		return (String)template.insert("EIMSBat.insertEimsRcvRegister", map);
	}
	
	public int selectUserAppCdCheck(String userId, String appCd)  throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("userId", userId);	
		params.put("appCd", appCd);
		return (Integer)template.queryForObject("EIMSBat.selectUserAppCdCheck", params);
	}

	public void deleteAdpterTSEAIEA03(String intfId) throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("intfId", intfId);
//		template.delete("EIMSBat.deleteAdapter02ByIntfIdUsing03", params); // AdapterMasterTable Delete
		template.delete("EIMSBat.deleteAdapter03ByIntfId", params); // DB AdapterInfo delete
	}
	
	public void deleteAdpterTSEAIEA05(String intfId) throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("intfId", intfId);
//		template.delete("EIMSBat.deleteAdapter02ByIntfIdUsing05", params); // AdapterMasterTable Delete
		template.delete("EIMSBat.deleteAdapter05ByIntfId", params); // File AdapterInfo delete
	}

	public void deleteSchedule(String intfId) throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("intfId", intfId);
		template.delete("EIMSBat.deleteScheduleES07", params); // ScheduleTable
		template.delete("EIMSBat.deleteScheduleES09", params); // Schedule-InterfaceMapping
	}
	
	public void deleteInterface(String intfId) throws Exception {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("intfId", intfId);
		template.delete("EIMSBat.deleteInterface", params);
	}

	public boolean isAdapterExist(Map<String, Object> paramMap) {
		Integer count = (Integer) template.queryForObject("EIMSBat.isAdapterExist", paramMap);
		if(count > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean isInterfaceSubExist(String interfaceId, String interfaceSeq) {
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("intfId", interfaceId);
		paramMap.put("intfIdSeq", interfaceSeq);
		Integer count = (Integer) template.queryForObject("EIMSBat.isInterfaceSubExist", paramMap);
		if(count > 0) {
			return true;
		} else {
			return false;
		}
	}

	public int increaseAndGetScheduleIndex(String applCd) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("applCd", applCd);
		template.update("EIMSBat.increaseScheduleIndex", params);
		return (Integer) template.queryForObject("EIMSBat.getScheduleIndex", params);
	}
	
}
