<?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.comtrls.mapper.ComtrlsdataMapper">

	<insert id="insertComtrlsMetadataInfo" parameterType="ComtrlsMetaDataVO">
		<selectKey resultType="int" keyProperty="rlsDataId" order="BEFORE">
			SELECT nextval('comtrlstrgtmetadata_rls_data_id_seq') as rlsDataId
		</selectKey>
		insert into comtrlstrgtmetadata (rls_data_id,
		                                 rls_data_nm,
		                                 rls_data_dc,
		                                 rls_data_aplcnt_id,
		                                 rls_data_rvw_id,
		                                 rls_data_mng_dept_id,
		                                 rls_data_src_inst_id,
		                                 reg_dt,
		                                 crt_ymd,
		                                 rls_data_stts_chg,
		                                 rls_yn)
		values (
		#{rlsDataId},
		#{rlsDataNm},
		#{rlsDataDc},
		#{rlsDataAplcntId},
		#{rlsDataRvwId},
		#{rlsDataMngDeptId},
		#{rlsDataSrcInstId},
		CURRENT_TIMESTAMP,
		#{crt_ymd},
		#{rlsDataSttsChg},
		#{rlsYn})
	</insert>

	<insert id="insertComtrlsSrcdata" parameterType="ComtrlsSrcdataVO">
		<selectKey resultType="int" keyProperty="rlsDataVlId" order="BEFORE">
			SELECT nextval('comtrlsdata_rls_data_vl_id_seq') as rlsDataVlId
		</selectKey>
		insert into comtrlsdata (rls_data_vl_id, rls_data_id, rls_data_vl)
		values (#{rlsDataVlId}, #{rlsDataId},  ( (#{rlsDataVl})::jsonb ) )
	</insert>

	<insert id="insertComtSrcCol" parameterType="ComtrlsColVO">
		<selectKey resultType="int" keyProperty="rlsDataColId" order="BEFORE">
			SELECT nextval('comtrlsdatacol_rls_data_col_id_seq') as rlsDataColId
		</selectKey>
		insert into comtrlsdatacol (rls_data_col_id, rls_data_id, rls_data_col_nm)
		values (#{rlsDataColId}, #{rlsDataId}, #{rlsDataColNm} )
	</insert>

	<delete id="deleteComtrlsCol" parameterType="java.util.Map">
		DELETE FROM comtrlsdatacol
		WHERE
		<if test="rlsDataColIdArr != null">
			rls_data_col_id IN
			<foreach collection="rlsDataColIdArr" item="item" open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
	</delete>

	<delete id="deleteComtrlsData" parameterType="java.util.Map">
		UPDATE COMTRLSDATA
		SET RLS_DATA_VL = RLS_DATA_VL
		<if test="rlsDataColIdArr != null">
			<foreach collection="rlsDataColIdArr" item="item" open="(" close=")" separator=",">
				- #{item}
			</foreach>
		</if>
		WHERE RLS_DATA_ID = #{rlsDataId}
	</delete>

	<sql id="selectComtrlsMetaData">
		SELECT
			rls_data_id,
			rls_data_nm,
			rls_data_dc,
			rls_data_aplcnt_id,
			rls_data_rvw_id,
			rls_data_mng_dept_id,
			rls_data_src_inst_id,
			reg_dt,
			crt_ymd,
			inq_cnt,
			dwnld_type,
			dwnld_cnt,
			rls_data_file_path,
			rls_data_stts_chg,
			rls_yn
		FROM comtrlstrgtmetadata
		WHERE 1=1
	</sql>

	<select id="selectComtrlsMetaDataList" parameterType="ComtrlsMetaDataVO" resultType="ComtrlsMetaDataVO">
		<include refid="selectComtrlsMetaData" />
	</select>

	<select id="selectComtrlsMetaDataInfo" parameterType="ComtrlsMetaDataVO" resultType="ComtrlsMetaDataVO">
		<include refid="selectComtrlsMetaData" />
		AND rls_data_id = #{rlsDataId}
	</select>

	<select id="selectComtrlsSrcData" parameterType="ComtrlsSrcdataVO" resultType="ComtrlsSrcdataVO">
		SELECT  RLS_DATA_VL_ID,
				RLS_DATA_VL,
				RLS_DATA_ID
		FROM    comtrlstrgtmetadata
		WHERE	RLS_DATA_ID = #{rlsDataId}
	</select>

</mapper>
