<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Interface">
  
  <!-- 외부 인터페이스 관리 목록조회 ( PAGE ) -->
  <statement id="selectList" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
			              
	SELECT ROWNO	 AS ROWNO
			,EAISvcName	 AS EAISVCNAME
			,EAIBzwkDstcd	 AS EAIBZWKDSTCD
			,EAITRANNAME	 AS EAITRANNAME
			,EAISvcDesc	 AS EAISVCDESC
			,IO	 AS IO
			,SplizDmndDstcd	 AS SPLIZDMNDDSTCD
			,StndTelgmWtinExtnlDstcd	 AS STNDTELGMWTINEXTNLDSTCD
			,FromName	 AS FROMNAME
			,ToName	 AS TONAME
			,DevUser	 AS DEVUSER
			,DevUserName	 AS DEVUSERNAME
			,BankUser	 AS BANKUSER
			,BankUserName	 AS BANKUSERNAME
			,CreateBy	 AS CREATEBY
			,UpdateBy	 AS UPDATEBY
			,CreateByName	 AS CREATEBYNAME
			,UpdateByName	 AS UPDATEBYNAME
	
	         FROM ( SELECT @NO := @NO + 1 AS ROWNO
	                     , HE01.EAISvcName
	                     , HE01.EAIBzwkDstcd
	                     , SUBSTR( HE01.EAISvcName, 1, LENGTH(HE01.EAISvcName)-2) EAITRANNAME
	                     , HE01.EAISvcDesc
			              , CASE WHEN '$schemaId$' = 'EAI' 
			              		THEN CASE WHEN SUBSTR(HE01.EAISVCNAME, LENGTH(HE01.EAISVCNAME)-1, 2) IN ('S2','R2')
			              				  THEN 'O' ELSE 'I' END
			                     ELSE CASE WHEN SUBSTR(HE01.EAISvcName, LENGTH(HE01.EAISvcName)-1, 2) in ('S2','R1') 
			                 	 			THEN 'O' ELSE 'I' END 
			                 END IO
	                     , SUBSTR(HE01.EAISVCNAME, LENGTH(HE01.EAISVCNAME)-1, 1) SplizDmndDstcd 
                         , SUBSTR(HE01.EAISVCNAME, LENGTH(HE01.EAISVCNAME), 1) StndTelgmWtinExtnlDstcd    
						 , SUBSTR( HE01.GstatSysAdptrBzwkGroupName,2,3) FromName
					     , SUBSTR( HE02.PsvSysAdptrBzwkGroupName,2,3) ToName
						 , HE01.DevUserId DevUser
						 , '' DevUserName
						 , HE01.BankUserId BankUser
						 , '' BankUserName
			             , HE01.CreateBy
			             , HE01.UpdateBy
			             , '' CreateByName
			             , '' UpdateByName
					  FROM $schemaId$.TSEAIHE01 HE01 
          				  LEFT OUTER JOIN $schemaId$.TSEAIHE02 HE02 ON HE01.EAISvcName = HE02.EAISvcName AND HE02.SvcPrcssNo = 1
          				  ,  (SELECT @NO := 0) R
          				  WHERE HE01.EAIBzwkDstcd IN ( select EAIBZWKDSTCD from $schemaId$.TSEAIRM06 where USERID=#loginId#  )
					      <isNotEmpty prepend="AND" property="searchEaiBzwkDstcd">
					            HE01.EAIBzwkDstcd = #searchEaiBzwkDstcd#
					      </isNotEmpty>
					      <isNotEmpty prepend="AND" property="searchEaiSvcName">
					            HE01.EAISvcName LIKE CONCAT( '%' , #searchEaiSvcName# , '%' )
					      </isNotEmpty>
					  ORDER BY HE01.EAISvcName    
		      ) CONTENT
		 WHERE ROWNO BETWEEN #startNum# AND #endNum# 
  </statement>
  
  <!-- 외부 인터페이스 목록조회 COUNT -->
  <statement id="selectListCount" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
    SELECT COUNT(*) CNT
      FROM $schemaId$.TSEAIHE01 HE01 
    WHERE HE01.EAIBzwkDstcd IN ( select EAIBZWKDSTCD from $schemaId$.TSEAIRM06 where USERID=#loginId#  )
   <isNotEmpty prepend="AND" property="searchEaiBzwkDstcd">
          HE01.EAIBzwkDstcd = #searchEaiBzwkDstcd#
   </isNotEmpty>
   <isNotEmpty prepend="AND" property="searchEaiSvcName">
          HE01.EAISvcName LIKE CONCAT( '%' , #searchEaiSvcName# , '%' )
   </isNotEmpty>
  </statement>  
  
  <!-- 외부 인터페이스 DETAIL -->
  <statement id="selectDetail"  parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
	 	 SELECT HE01.EAISVCNAME  
	 	      , HE01.EAIBZWKDSTCD
	 	      , SUBSTR(HE01.EAISvcName, 1, LENGTH(HE01.EAISvcName)-2) EAITRANNAME
	          , HE01.EAISVCDESC
              , CASE WHEN '$schemaId$' = 'EAI' 
              		THEN CASE WHEN SUBSTR(HE01.EAISVCNAME, LENGTH(HE01.EAISVCNAME)-1, 2) IN ('S2','R2')
              				  THEN 'O' ELSE 'I' END
                     ELSE CASE WHEN SUBSTR(HE01.EAISvcName, LENGTH(HE01.EAISvcName)-1, 2) in ('S2','R1') 
                 	 			THEN 'O' ELSE 'I' END 
                 END IO
              , SUBSTR(HE01.EAISvcName, LENGTH(HE01.EAISvcName)-1, 1) SPLIZDMNDDSTCD
	          , SUBSTR(HE01.EAISvcName, LENGTH(HE01.EAISvcName), 1) STNDTELGMWTINEXTNLDSTCD
		      , CASE WHEN HE01.SvcMotivUseDstcd ='SYNC' and HE02.PsvIntfacDsticName ='SYNC' then 'SYNC' 
		             WHEN HE01.SvcMotivUseDstcd ='ASYN' and HE02.PsvIntfacDsticName ='ASYN' then 'ASYN' 
		             WHEN HE01.SvcMotivUseDstcd ='SYNC' and HE02.PsvIntfacDsticName ='ASYN' then 'SYNC_ASYN'
		             WHEN HE01.SvcMotivUseDstcd ='ASYN' and HE02.PsvIntfacDsticName ='SYNC' then 'ASYN_SYNC'
		             end  SVCMOTIVUSEDSTCD  
	          , trim(RecvTranName) SERVICEID
	          , CONCAT( MS02_1.RefMsgIDName 
				, CASE WHEN MS02_2.RefMsgIDName IS NOT NULL THEN CONCAT(',' , MS02_2.RefMsgIDName) ELSE '' END
				, CASE WHEN MS02_3.RefMsgIDName IS NOT NULL THEN CONCAT(',' , MS02_3.RefMsgIDName) ELSE '' END ) 
				AS LAYOUTMAPPINGNAME
	          , FR_1.BzwkFldName BZWKFLDNAME1
	          , IFNULL(FR_1.MsgFldStartSituVal,0) MSGFLDSTARTSITUVAL1
	          , IFNULL(FR_1.MsgFldLen,0) MSGFLDLEN1
	          , FR_2.BzwkFldName BZWKFLDNAME2
	          , IFNULL(FR_2.MsgFldStartSituVal,0) MSGFLDSTARTSITUVAL2
	          , IFNULL(FR_2.MsgFldLen,0) MSGFLDLEN2
	          , HE01.GstatSysAdptrBzwkGroupName FROMADAPTER
	          , HE02.PsvSysAdptrBzwkGroupName TOADAPTER
	          , HE02_2.PsvSysAdptrBzwkGroupName EXTADAPTER
	          , HE01.DEVUSERID
	          , '' DEVUSERNAME
	          , HE01.BANKUSERID  
	          , '' BANKUSERNAME
	          , HS04.BZWKSVCKEYNAME
		  FROM  $schemaId$.TSEAIHE01 HE01 
					  LEFT OUTER JOIN ( 
                            SELECT EAISVCNAME ,hs04.bzwkSvcKeyName
		                         , max( CASE WHEN COLUMNNAME = 'COMMON_SRVCID' THEN COLUMNVALUE ELSE '' END) RecvTranName
		                      FROM $schemaId$.TSEAIHS04 hs04 , $schemaId$.tseaihs05 hs05
		                      where hs04.BZWKSVCKEYNAME =hs05.BZWKSVCKEYNAME
		                     group by hs04.BZWKSVCKEYNAME,EAISVCNAME				
					  ) HS04
					  On HE01.EAISvcName = HS04.EAISvcName
            LEFT OUTER JOIN $schemaId$.TSEAIHE02 HE02   ON HE01.EAISvcName = HE02.EAISvcName AND HE02.SvcPrcssNo = 1
            LEFT OUTER JOIN $schemaId$.TSEAIHE02 HE02_2 ON HE01.EAISvcName = HE02_2.EAISvcName AND HE02_2.SvcPrcssNo = 2
            LEFT OUTER JOIN (SELECT EAISvcName, RefMsgIDName FROM $schemaId$.TSEAIMS02 WHERE RefMsgIDNo=1 ) MS02_1 On HE01.EAISvcName = MS02_1.EAISvcName
            LEFT OUTER JOIN (SELECT EAISvcName, RefMsgIDName FROM $schemaId$.TSEAIMS02 WHERE RefMsgIDNo=2 ) MS02_2 On HE01.EAISvcName = MS02_2.EAISvcName
            LEFT OUTER JOIN (SELECT EAISvcName, RefMsgIDName FROM $schemaId$.TSEAIMS02 WHERE RefMsgIDNo=3 ) MS02_3 On HE01.EAISvcName = MS02_3.EAISvcName
					  LEFT OUTER JOIN (SELECT EAISvcName, BzwkFldName, MsgFldStartSituVal, MsgFldLen FROM $schemaId$.TSEAIFR03 WHERE FldPrcssNo=1 ) FR_1 On HE01.EAISvcName = FR_1.EAISvcName
					  LEFT OUTER JOIN (SELECT EAISvcName, BzwkFldName, MsgFldStartSituVal, MsgFldLen FROM $schemaId$.TSEAIFR03 WHERE FldPrcssNo=2 ) FR_2 On HE01.EAISvcName = FR_2.EAISvcName
		 WHERE HE01.EAISvcName = #eaiSvcName#  
  </statement>
    
  <!-- Detail 추가 정보 -->
  <statement id="selectDetailHE10" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
  	SELECT 
	  	EAISVCNAME
	  	,'REALTIME' AS OCCURDSTCD
		,TRANSKIND
		,TRANSFREQ
		,SIZEONCE
		,PROCTERM
		,REQCHGR
		,REQCHGRTLNO
		,RESCHGR
		,RESCHGRTLNO
		,RMK
	FROM $schemaId$.TSEAIHE10
  	WHERE EAISvcName = #eaiSvcName#
  </statement>

  <statement id="mergeHE10" parameterClass="java.util.HashMap">
            INSERT (EaiSvcName       , TransKind	,TransFreq		,SizeOnce	,ProcTerm,
            		ReqChgr			,ReqChgrTlno		,ResChgr	,ResChgrTlno	,Rmk
            ) VALUES (#eaiSvcName#          , #transKind#	,#transFreq#		,#sizeOnce#		,#procTerm#	,
            			#reqChgr#			,#reqChgrTlno#		,#resChgr#		,#resChgrTlno#  ,#rmk#                 
	        ) ON DUPLICATE KEY UPDATE 
	             TransKind           		= #transKind#,
	             TransFreq 					= #transFreq#,	
	             SizeOnce                 	= #sizeOnce#, 
	             ProcTerm                   = #procTerm#, 		        
	             ReqChgr					= #reqChgr#,
	             ReqChgrTlno				= #reqChgrTlno#,
	             ResChgr					= #resChgr#,
	             ResChgrTlno				= #resChgrTlno#,
	             Rmk						= #rmk#
     
  </statement>
    
  <statement id="deleteHE10" parameterClass="java.util.HashMap">
    DELETE FROM $schemaId$.TSEAIHE10 
     WHERE EAISvcName = #eaiSvcName#
  </statement>
   
   <statement id="selectListHS03"  parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
 	 SELECT 
		  COLUMNNAME
		, COLUMNDESC
		, COLUMNLENGTH
		, ISKEY
		, KEYSEQ
		, ISINTERFACE
		, COMBONAME
		, COMBODEPTH
		, COLUMNDEFAULT
	FROM $schemaId$.TSEAIHS03 
 	</statement>

  <statement id="deleteToHS04" parameterClass="java.util.HashMap">
    DELETE FROM $schemaId$.TSEAIHS04 
     WHERE EAISvcName = CONCAT( #eaiTranName# , #splizDmndDstcd# , #stndTelgmWtinExtnlDstcd# )       
  </statement>

  <statement id="deleteToHS05" parameterClass="java.util.HashMap">
    DELETE FROM $schemaId$.TSEAIHS05 
     WHERE bzwkSvcKeyName in (SELECT bzwkSvcKeyName from $schemaId$.TSEAIHS04 where EAISvcName = CONCAT(#eaiTranName# , #splizDmndDstcd# , #stndTelgmWtinExtnlDstcd#) )       
  </statement>  
  
  <statement id="detailToHS05" resultClass="java.util.HashMap" parameterClass="java.util.HashMap">
        SELECT 
	          HS04.BZWKSVCKEYNAME
	          , EAISVCNAME
	          , COLUMNNAME
	          , COLUMNVALUE
      FROM $schemaId$.TSEAIHS04 HS04 ,$schemaId$.TSEAIHS05 HS05
     WHERE HS04.BzwkSvcKeyName = HS05.BzwkSvcKeyName
       AND EAISVCNAME = CONCAT( #eaiTranName# , #splizDmndDstcd# , #stndTelgmWtinExtnlDstcd# )
  </statement>  
  <!-- 내부표준전문 처리 -->
  <statement id="mergeToHS04" parameterClass="java.util.HashMap">
    INSERT INTO  $schemaId$.TSEAIHS04 (BzwkSvcKeyName  , EAISVCNAME ) 
	VALUES(#bzwkSvcKeyName#, CONCAT(#COMMON_IFID#, #HEADER_MESGDMANDVCD# , #COMMON_HMABDVCD#)  )
	ON DUPLICATE KEY UPDATE EAISVCNAME = CONCAT(#COMMON_IFID#, #HEADER_MESGDMANDVCD# , #COMMON_HMABDVCD#) 
  </statement>    

  <statement id="mergeToHS05" parameterClass="java.util.HashMap">
    INSERT INTO  $schemaId$.TSEAIHS05 (BzwkSvcKeyName    , COLUMNNAME  , COLUMNVALUE ) 
	VALUES(#bzwkSvcKeyName#  , #columnName#, #columnValue#) 
	ON DUPLICATE KEY UPDATE COLUMNVALUE = #columnValue#  
  </statement>    
   
  <!-- EAI메세지 -->
  <statement id="mergeToHE01" parameterClass="java.util.HashMap">
    INSERT INTO  $schemaId$.TSEAIHE01 
    		(EAISvcName        , EAIBzwkDstcd    , SvcHMSEonot    , SvcMotivUseDstcd           , SvcPrcesDsticName , 
                    FlowCtrlRoutName  , IntgraDsticName , SvcBfClmnLogYn , GstatSysAdptrBzwkGroupName , SevrLogLvelNo     , 
                    SvcLogLvelNo      , EAISvcDesc      , SimYn          , CreateBy                   , UpdateBy          , 
                    DevUserid          , BankUserid         , CreateOn     , UpdateOn                 

            ) VALUES (#eaiSvcName#      , #eaiBzwkDstcd#    , #svcHMSEonot#    , #svcMotivUseDstcd#           , #svcPrcesDsticName# ,
                      #flowCtrlRoutName#, #intgraDsticName# , #svcBfClmnLogYn# , #gstatSysAdptrBzwkGroupName# , #sevrLogLvelNo#     ,
                      #svcLogLvelNo#    , #eaiSvcDesc#      , 'N'              , #loginId#                   , #loginId#          , 
                      #devUserId#        , #bankUserId#       , DATE_FORMAT( NOW(), '%Y%m%d%H%i%s')          ,DATE_FORMAT( NOW(), '%Y%m%d%H%i%s')
             
	          )
	ON DUPLICATE KEY UPDATE 
		EAIBzwkDstcd               = IFNULL(#eaiBzwkDstcd#,              ' '),
        SvcMotivUseDstcd           = IFNULL(#svcMotivUseDstcd#,          ' '),
        GstatSysAdptrBzwkGroupName = IFNULL(#gstatSysAdptrBzwkGroupName#,' '),
        EAISvcDesc                 = IFNULL(#eaiSvcDesc#,                ' '),
        UpdateBy                   = IFNULL(#loginId#,                  ' '),
        DevUserId                   = IFNULL(#devUserId#,                DevUserId),
        BankUserId                  = IFNULL(#bankUserd#,                BankUserId),
        UpdateOn                   = DATE_FORMAT( NOW(), '%Y%m%d%H%i%s') ,
        FlowCtrlRoutName           = IFNULL(#flowCtrlRoutName#,           ' ')
  </statement>
  <delete id="deleteNo2ToHE02" parameterClass="java.util.HashMap">
     DELETE FROM $schemaId$.TSEAIHE02 WHERE EAISvcName = #eaiSvcName# AND SvcPrcssNo=2
  </delete>
  
  <statement id="mergeToHE02" parameterClass="java.util.HashMap">
      INSERT INTO  $schemaId$.TSEAIHE02 
	 		(EAISvcName,            SvcPrcssNo,               PsvIntfacDsticName,     PsvSysBzwkDstcd,     PsvSysIDName, 
                    PsvSysSvcDsticName,    PsvSysAdptrBzwkGroupName, FlovrYn,                ChngEonot,           ChngMsgIDName, 
                    BascRspnsMsgCmprCtnt,  BascRspnsChngEonot,       BascRspnsChngMsgIDName, ErrRspnsMsgCmprCtnt, ErrRspnsChngEonot, 
                    ErrRspnsChngMsgIDName, NextSvcPrcssNo,           OutbndRoutName,         ToutVal,             CmpenSvcPrcssDsticName, 
                    SupplDelYn,            HdrCtrlDstcd,             HdrRefClsName
            ) VALUES (#eaiSvcName#,            #svcPrcssNo#,               #psvIntfacDsticName#,      #psvSysBzwkDstcd#,        #psvSysIDName#,
											#psvSysSvcDsticName#,    #psvSysAdptrBzwkGroupName#, #flovrYn#,                 #chngEonot#,              #chngMsgIDName#, 
											#bascRspnsMsgCmprCtnt#,  #bascRspnsChngEonot#,       #bascRspnsChngMsgIDName#,  #errRspnsMsgCmprCtnt#,    #errRspnsChngEonot#,
											#errRspnsChngMsgIDName#, #nextSvcPrcssNo#,           #outbndRoutName#,       #toutVal#,#cmpenSvcPrcssDsticName#,
											#supplDelYn#,            #hdrCtrlDstcd#,             #hdrRefClsName#
	          )
	ON DUPLICATE KEY UPDATE 
		 PsvIntfacDsticName       = IFNULL(#psvIntfacDsticName#       ,' '),
		 PsvSysBzwkDstcd          = IFNULL(#psvSysBzwkDstcd#          ,' '),
		 PsvSysAdptrBzwkGroupName = IFNULL(#psvSysAdptrBzwkGroupName# ,' '),
		 OutbndRoutName           = IFNULL(#outbndRoutName#           ,' '),
		 SupplDelYn               = IFNULL(#supplDelYn#               ,'0'),
		 HdrCtrlDstcd             = IFNULL(#hdrCtrlDstcd#             ,'01'),
		 HdrRefClsName            = IFNULL(#hdrRefClsName#            ,' ')
  </statement>   
   
	<statement id="selectPopListCount" parameterClass="java.util.HashMap" resultClass="java.lang.Integer">
		SELECT COUNT(*) CNT
		  FROM $schemaId$.TSEAIHE01 HE01 
		<dynamic prepend="WHERE">
			<isNotEmpty prepend="AND" property="eaiSvcName">
				UPPER(HE01.EAISvcName) LIKE CONCAT('%' , UPPER(#eaiSvcName#) , '%')
			</isNotEmpty>
		</dynamic>
	</statement>
	
	<statement id="selectPopList" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
		SELECT * 
		  FROM ( SELECT @NO := @NO + 1 AS ROWNO 
					  , HE01.EAISVCNAME
					  , HE01.EAIBZWKDSTCD
					  , SUBSTR( HE01.EAISVCNAME, 1, LENGTH(HE01.EAISVCNAME)-2) EAITRANNAME
					  , HE01.EAISVCDESC
					  , CASE WHEN '$SCHEMAID$' = 'EAI' 
								THEN CASE WHEN SUBSTR(HE01.EAISVCNAME, LENGTH(HE01.EAISVCNAME)-1, 2) IN ('S2','R2')
											THEN 'O' ELSE 'I' END
							 ELSE CASE WHEN SUBSTR(HE01.EAISVCNAME, LENGTH(HE01.EAISVCNAME)-1, 2) IN ('S2','R1') 
										THEN 'O' ELSE 'I' END 
						 END IO
					  , SUBSTR(HE01.EAISVCNAME, LENGTH(HE01.EAISVCNAME)-1, 1) SPLIZDMNDDSTCD 
					  , SUBSTR(HE01.EAISVCNAME, LENGTH(HE01.EAISVCNAME), 1) STNDTELGMWTINEXTNLDSTCD
				   FROM $schemaId$.TSEAIHE01 HE01,  (SELECT @NO := 0) R
					<dynamic prepend="WHERE">
						<isNotEmpty prepend="AND" property="eaiSvcName">
							UPPER(HE01.EAISvcName) LIKE CONCAT('%' , UPPER( #eaiSvcName# ) , '%')
						</isNotEmpty>
					</dynamic>
			   ) TMP
		 WHERE ROWNO BETWEEN #startNum# AND #endNum# 
	</statement>  
</sqlMap>
  