<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="egovframework.com.mtdata.comtsrcdata.mapper.ComtsrcdataMapper">

	<resultMap id="databaseInfoMap" type="egovframework.com.mtdata.database.vo.DatabaseInfoVO">
		<result property="sysId" column="SYS_ID"/>
		<result property="dbConnId" column="DB_CONN_ID"/>
		<result property="dbType" column="DB_TYPE"/>
		<result property="dbConnUser" column="DB_CONN_USER"/>
		<result property="dbConnPw" column="DB_CONN_PW"/>
		<result property="dbConnIp" column="DB_CONN_IP"/>
		<result property="dbConnPort" column="DB_CONN_PORT"/>
		<result property="registerId" column="REGISTER_ID"/>
		<result property="dbNm" column="DB_NM"/>
		<result property="useYn" column="USE_YN"/>
	</resultMap>

<!-- 원천데이터 -> 공유데이터로 copy할때 이용-->
	<select id="selectSrcMetaData" parameterType="ComtTableVO" resultType="ComtTableVO">
		SELECT a.src_data_meta_id, a.extr_id, a.src_data_tbl_nm, b.sys_id
		FROM comtsrcdatameta_new a
		 LEFT JOIN comtextrtblinfo b ON a.extr_id = b.extr_id
		WHERE a.src_data_meta_id = #{srcDataMetaId}
	</select>

	<select id="selectSrcColData" parameterType="ComtsrcdataVO" resultType="ComtColVO">
		SELECT  src_data_col_id,
		        src_data_meta_id,
		        src_data_col_nm
		FROM    comtsrcdatacol_new
	</select>

	<insert id="insertComtExtrtInfo" parameterType="ComtExtrtVO">
		<selectKey resultType="int" keyProperty="extrId" order="BEFORE">
			SELECT nextval('comtextrtblinfo_extr_id_seq') as extrId
		</selectKey>
		INSERT INTO comtextrtblinfo
		(extr_id, extr_ymd, sys_id)
		VALUES
		(#{extrId}, CURRENT_TIMESTAMP, #{sysId})
	</insert>

	<insert id="insertComtTabledataInfo" parameterType="ComtTableVO">
		<selectKey resultType="int" keyProperty="srcDataMetaId" order="BEFORE">
			SELECT nextval('comtsrcdatameta_new_src_data_meta_id_seq') as srcDataMetaId
		</selectKey>
		insert into comtsrcdatameta_new (src_data_meta_id, extr_id, src_data_tbl_nm)
		values (#{srcDataMetaId}, #{extrId}, #{srcDataTblNm})
	</insert>

	<insert id="insertComtSrcdata" parameterType="ComtsrcdataVO">
		<selectKey resultType="int" keyProperty="srcDataId" order="BEFORE">
			SELECT nextval('comtsrcdata_new_src_data_id_seq') as srcDataId
		</selectKey>
		insert into comtsrcdata_new (src_data_id, src_data_meta_id, src_data_vl)
		values (#{srcDataId}, #{srcDataMetaId},  ( (#{srcDataVl})::jsonb ) )
	</insert>

	<insert id="insertComtSrcCol" parameterType="ComtColVO">
		<selectKey resultType="int" keyProperty="srcDataColId" order="BEFORE">
			SELECT nextval('comtsrcdatacol_new_src_data_col_id_seq') as srcDataColId
		</selectKey>
		insert into comtsrcdatacol_new (src_data_col_id, src_data_meta_id, src_data_col_nm)
		values (#{srcDataColId}, #{srcDataMetaId}, #{srcDataColNm} )
	</insert>

<!--sample-->
	<update id="updateDatasrc" parameterType="ComtsrcdataVO">
		update comtsrcdata_new
		set  src_data_vl= ( (#{srcDataVl})::jsonb )
		where src_data_id= 2
	</update>

	<select id="selectSrcData" parameterType="ComtsrcdataVO" resultType="ComtsrcdataVO">
		SELECT  SRC_DATA_META_ID,
				SRC_DATA_ID,
				SRC_DATA_VL
		FROM    comtsrcdata_new
		WHERE	SRC_DATA_META_ID = #{srcDataMetaId}
	</select>

	<select id="selectSrcDataMetaList" resultType="ComtExtrtVO">
		SELECT a.extr_id, a.extr_ymd, a.sys_id, c.sys_nm, b.src_data_meta_id, b.src_data_tbl_nm
		FROM comtextrtblinfo a
		 LEFT JOIN comtsrcdatameta_new b ON a.extr_id = b.extr_id
		 LEFT JOIN inst_sys_info c ON a.sys_id = c.sys_id
	</select>

	<select id="selectSrcDataMeta" parameterType="ComtsrcdataVO" resultType="ComtExtrtVO">
		SELECT a.extr_id, a.extr_ymd, a.sys_id, c.sys_nm, b.src_data_meta_id, b.src_data_tbl_nm
		FROM comtextrtblinfo a
				 LEFT JOIN comtsrcdatameta_new b ON a.extr_id = b.extr_id
				 LEFT JOIN inst_sys_info c ON a.sys_id = c.sys_id
		WHERE b.extr_id = #{extrId}
	</select>

</mapper>
