<?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="SyncUp">	

	<statement id="insertPtlApiSpecInfoIntfSnd" parameterClass="java.util.HashMap">    	               
		INSERT INTO $schemaId$.PTL_API_SPEC_INFO_STG_TO_PRD_INTF_SND (
		EAI_SND_DATA_CRTN_DT, EAI_SND_DATA_CRTN_SEQ_NBR, EAI_SND_DATA_PRCS_TP_CD, EAI_SND_STS_CD, EAI_SND_DTTM,
		EAI_TX_ID, EAI_PRTT_SND_INDEX, EAI_RCV_SYS_RCV_STS_NBR, API_ID, API_NAME, API_SIMPLE_DESCRIPTION, API_URL,
		API_METHOD, API_CONTENT_TYPE, API_REQUEST_SPEC, 	API_RESPONSE_SPEC, SAMPLE_REQUEST, SAMPLE_RESPONSE,
		RESPONSE_TYPE, MOCK_URL, FILE_ID, DESCRIPTION, 	CREATED_BY, CREATED_DATE, LAST_MODIFIED_BY, LAST_MODIFIED_DATE,
		TESTBED_SPEC, DISPLAY_YN, DISPLAY_ORG, DISPLAY_ROLE_CODE
		)	
		SELECT DATE_FORMAT(NOW(), '%Y%m%d%H') AS EAI_SND_DATA_CRTN_DT,
			ROW_NUMBER() OVER (ORDER BY API_ID) AS EAI_SND_DATA_CRTN_SEQ_NBR,
			'I' AS EAI_SND_DATA_PRCS_TP_CD, 'N' AS EAI_SND_STS_CD, CURRENT_TIMESTAMP() EAI_SND_DTTM,
			" " AS EAI_TX_ID, 0 AS EAI_PRTT_SND_INDEX, -1 AS EAI_RCV_SYS_RCV_STS_NBR,
		    API_ID, API_NAME, 	API_SIMPLE_DESCRIPTION, API_URL, API_METHOD, API_CONTENT_TYPE,
			API_REQUEST_SPEC, API_RESPONSE_SPEC, 	SAMPLE_REQUEST, 	SAMPLE_RESPONSE, RESPONSE_TYPE, MOCK_URL,
			FILE_ID, DESCRIPTION, 	CREATED_BY, CREATED_DATE, LAST_MODIFIED_BY, LAST_MODIFIED_DATE, TESTBED_SPEC,
			DISPLAY_YN, DISPLAY_ORG, DISPLAY_ROLE_CODE	
		FROM $schemaId$.PTL_API_SPEC_INFO
		WHERE STR_TO_DATE(LAST_MODIFIED_DATE, '%Y%m%d%H%i%s%f') <![CDATA[ >= ]]> NOW() - INTERVAL 1 DAY
		AND DISPLAY_YN = 'Y' 
 	</statement>
 	
 	<statement id="deletePtlApiSpecInfoRcv" parameterClass="java.util.HashMap">
		DELETE FROM $schemaId$.PTL_API_SPEC_INFO
		WHERE API_ID IN
		(
			SELECT API_ID
			FROM $schemaId$.PTL_API_SPEC_INFO_STG_TO_PRD_INTF_RCV
			WHERE STR_TO_DATE(EAI_SND_DATA_CRTN_DT, '%Y%m%d%H') <![CDATA[ >= ]]> NOW() - INTERVAL 7 DAY
			AND BIZ_PRCS_RSLT_CD = 'N'	
		)
	</statement>
	
	<statement id="insertPtlApiSpecInfoRcv" parameterClass="java.util.HashMap">
		INSERT INTO $schemaId$.PTL_API_SPEC_INFO(
			API_ID, API_NAME, 	API_SIMPLE_DESCRIPTION, API_URL, API_METHOD, API_CONTENT_TYPE,
			API_REQUEST_SPEC, API_RESPONSE_SPEC, SAMPLE_REQUEST, SAMPLE_RESPONSE,
			RESPONSE_TYPE, MOCK_URL, FILE_ID, DESCRIPTION, 	CREATED_BY, CREATED_DATE,
			LAST_MODIFIED_BY, LAST_MODIFIED_DATE, TESTBED_SPEC, DISPLAY_YN,
			DISPLAY_ORG, DISPLAY_ROLE_CODE	
		)
		SELECT R.API_ID, API_NAME, API_SIMPLE_DESCRIPTION, API_URL, API_METHOD,
			API_CONTENT_TYPE, API_REQUEST_SPEC, API_RESPONSE_SPEC, SAMPLE_REQUEST,
			SAMPLE_RESPONSE, RESPONSE_TYPE, MOCK_URL, FILE_ID, DESCRIPTION, CREATED_BY,
			CREATED_DATE, LAST_MODIFIED_BY, LAST_MODIFIED_DATE, TESTBED_SPEC, DISPLAY_YN,
			DISPLAY_ORG, DISPLAY_ROLE_CODE	
		FROM  $schemaId$.PTL_API_SPEC_INFO_STG_TO_PRD_INTF_RCV R,
	    (
			SELECT API_ID, MAX(EAI_SND_DATA_CRTN_DT) EAI_SND_DATA_CRTN_DT
			FROM  $schemaId$.PTL_API_SPEC_INFO_STG_TO_PRD_INTF_RCV
			WHERE STR_TO_DATE(EAI_SND_DATA_CRTN_DT, '%Y%m%d%H') >=  NOW() - INTERVAL 7 DAY 
			GROUP BY API_ID
		) C
		WHERE R.EAI_SND_DATA_CRTN_DT = C.EAI_SND_DATA_CRTN_DT
		AND R.API_ID = C.API_ID
		AND BIZ_PRCS_RSLT_CD = 'N'			
 	</statement>
	
 	<statement id="updatePtlApiSpecInfoIntfRcv" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
		UPDATE $schemaId$.PTL_API_SPEC_INFO_STG_TO_PRD_INTF_RCV
		SET BIZ_PRCS_RSLT_CD = 'S',
			BIZ_PRCS_DTTM = CURRENT_TIMESTAMP()
		WHERE STR_TO_DATE(EAI_SND_DATA_CRTN_DT, '%Y%m%d%H')  <![CDATA[ >= ]]> NOW() - INTERVAL 7 DAY
		AND BIZ_PRCS_RSLT_CD = 'N' 
 	</statement>
 	
 	<statement id="insertPtlOrgIntfSnd" parameterClass="java.util.HashMap">
		INSERT INTO $schemaId$.PTL_ORG_PRD_TO_STG_INTF_SND (
		EAI_SND_DATA_CRTN_DT, EAI_SND_DATA_CRTN_SEQ_NBR, EAI_SND_DATA_PRCS_TP_CD, EAI_SND_STS_CD, EAI_SND_DTTM,
		EAI_TX_ID, EAI_PRTT_SND_INDEX, EAI_RCV_SYS_RCV_STS_NBR, 
		ID,	CORP_REG_NO, 	COMP_REG_NO, ORG_NAME, ORG_DESC, ORG_STATUS, 	ORG_TYPE, CREATED_BY, CREATED_DATE,
		LAST_MODIFIED_BY, LAST_MODIFIED_DATE, APPROVAL_STATUS, CEO_NAME, ORG_ADDR, ORG_SECTORS,
		ORG_INDUSTRYTYPE, ORG_PHONE_NUMBER, IP_WHITELIST, COMP_REG_FILE, SERVICE_NAME, SC_PHONE_NUMBER, ORG_CODE
		)	
		SELECT DATE_FORMAT(NOW(), '%Y%m%d%H') AS EAI_SND_DATA_CRTN_DT,
			ROW_NUMBER() OVER (ORDER BY ID) AS EAI_SND_DATA_CRTN_SEQ_NBR,
			'I' AS EAI_SND_DATA_PRCS_TP_CD, 'N' AS EAI_SND_STS_CD, CURRENT_TIMESTAMP() EAI_SND_DTTM,
			" " AS EAI_TX_ID, 0 AS EAI_PRTT_SND_INDEX, -1 AS EAI_RCV_SYS_RCV_STS_NBR,
		    ID,	CORP_REG_NO, 	COMP_REG_NO, ORG_NAME, ORG_DESC, ORG_STATUS, 	ORG_TYPE, CREATED_BY, CREATED_DATE,
			LAST_MODIFIED_BY, LAST_MODIFIED_DATE, APPROVAL_STATUS, CEO_NAME, ORG_ADDR, ORG_SECTORS,
			ORG_INDUSTRYTYPE, ORG_PHONE_NUMBER, IP_WHITELIST, COMP_REG_FILE, SERVICE_NAME, SC_PHONE_NUMBER, ORG_CODE	
		FROM $schemaId$.PTL_ORG
		WHERE STR_TO_DATE(LAST_MODIFIED_DATE, '%Y%m%d%H%i%s%f') <![CDATA[ >= ]]> NOW() - INTERVAL 1 DAY
		AND APPROVAL_STATUS = "COMPLETED" 
 	</statement>
 	
 	<statement id="deletePtlOrgRcv" parameterClass="java.util.HashMap">
		DELETE FROM $schemaId$.PTL_ORG
		WHERE ID IN
		(
			SELECT ID
			FROM $schemaId$.PTL_ORG_PRD_TO_STG_INTF_RCV
			WHERE STR_TO_DATE(EAI_SND_DATA_CRTN_DT, '%Y%m%d%H') <![CDATA[ >= ]]> NOW() - INTERVAL 7 DAY
			AND BIZ_PRCS_RSLT_CD = 'N'	
		)
	</statement>
	
	<statement id="insertPtlOrgRcv" parameterClass="java.util.HashMap">
		INSERT INTO $schemaId$.PTL_ORG(
			ID,	CORP_REG_NO, 	COMP_REG_NO, ORG_NAME, ORG_DESC, ORG_STATUS, 	ORG_TYPE, CREATED_BY, CREATED_DATE,
			LAST_MODIFIED_BY, LAST_MODIFIED_DATE, APPROVAL_STATUS, CEO_NAME, ORG_ADDR, ORG_SECTORS,
			ORG_INDUSTRYTYPE, ORG_PHONE_NUMBER, IP_WHITELIST, COMP_REG_FILE, SERVICE_NAME, SC_PHONE_NUMBER, ORG_CODE
		)
		SELECT  R.ID,	CORP_REG_NO, 	COMP_REG_NO, ORG_NAME, ORG_DESC, ORG_STATUS, 	ORG_TYPE, CREATED_BY, CREATED_DATE,
			LAST_MODIFIED_BY, LAST_MODIFIED_DATE, APPROVAL_STATUS, CEO_NAME, ORG_ADDR, ORG_SECTORS,
			ORG_INDUSTRYTYPE, ORG_PHONE_NUMBER, IP_WHITELIST, COMP_REG_FILE, SERVICE_NAME, SC_PHONE_NUMBER, ORG_CODE
		FROM $schemaId$.PTL_ORG_PRD_TO_STG_INTF_RCV R,
	    (
			SELECT ID, MAX(EAI_SND_DATA_CRTN_DT) EAI_SND_DATA_CRTN_DT
			FROM $schemaId$.PTL_ORG_PRD_TO_STG_INTF_RCV
			WHERE STR_TO_DATE(EAI_SND_DATA_CRTN_DT, '%Y%m%d%H') <![CDATA[ >= ]]>  NOW() - INTERVAL 7 DAY 
			GROUP BY ID
		) C
		WHERE R.EAI_SND_DATA_CRTN_DT = C.EAI_SND_DATA_CRTN_DT
		AND R.ID = C.ID
		AND BIZ_PRCS_RSLT_CD = 'N'
 	</statement>
	
 	<statement id="updatePtlOrgIntfRcv" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
		UPDATE $schemaId$.PTL_ORG_PRD_TO_STG_INTF_RCV
		SET BIZ_PRCS_RSLT_CD = 'S',
			BIZ_PRCS_DTTM = CURRENT_TIMESTAMP()
		WHERE STR_TO_DATE(EAI_SND_DATA_CRTN_DT, '%Y%m%d%H')  <![CDATA[ >= ]]> NOW() - INTERVAL 7 DAY
		AND BIZ_PRCS_RSLT_CD = 'N' 
 	</statement>
		
</sqlMap>
