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

	<resultMap id="susaResult" type="susaVO">
		<result property="idx" column="ss_idx" />
		<result property="place1" column="ss_place1" />
		<result property="agency" column="ss_agency" />
		<result property="type" column="ss_type" />
		<result property="sdate" column="ss_sdate" />
		<result property="edate" column="ss_edate" />
		<result property="chargeNm" column="ss_charge_nm" />
		<result property="title" column="ss_title" />
		<result property="contents" column="ss_contents" />
		<result property="result" column="ss_result" />
		<result property="regdate" column="ss_regDate" />
		<result property="writer" column="ss_writer" />
		<result property="fileCnt" column="file_cnt" />
		<result property="place1Str" column="PLACE1_STR" />
	</resultMap>

	<select id="Susa.select" parameterType="susaVO" resultMap="susaResult">
	<![CDATA[	
		SELECT
			*
		FROM 
			H3_SUSA_LIST
		WHERE
			SS_IDX = #{idx}
	]]>
	</select>

	<select id="Susa.selectListAll" parameterType="susaSearchVO" resultMap="susaResult">
	<![CDATA[
		SELECT	ALL_LIST.*, 
				(SELECT COUNT(*) FROM H3_SUSA_FILE WHERE SF_SS_SEQ = ALL_LIST.SS_IDX) AS FILE_CNT,
				(SELECT CODENM FROM TCODE WHERE CODE1 = 'C001' AND CODE2 = ALL_LIST.SS_PLACE1) AS PLACE1_STR
       	FROM	(
	  				SELECT 	*
					FROM 	H3_SUSA_LIST
					WHERE	1=1  
	]]>
						<if test="type != null and type != ''">
							AND SS_TYPE = #{type}
						</if>
						<if test="place1 != null and place1 != ''">
							AND SS_PLACE1 = #{place1}
						</if>
						<if test="agencyCode != null and agencyCode != ''">
							AND SS_AGENCY = #{agencyCode}
						</if>
						<if test="searchKeyword != null and searchKeyword != ''">
							AND SS_TITLE LIKE '%' || #{searchKeyword} || '%'
						</if>
	<![CDATA[ 
					ORDER BY SS_IDX DESC
				) ALL_LIST
  	]]>
	</select>

	<select id="Susa.selectList" parameterType="susaSearchVO" resultMap="susaResult">
		<![CDATA[
        SELECT 	* 
        FROM 	(
        			SELECT
        			       (row_number() over()) as RNUM, ALL_LIST.*,
        					(SELECT COUNT(*) FROM H3_SUSA_FILE WHERE SF_SS_SEQ = ALL_LIST.SS_IDX) AS FILE_CNT,
        					(SELECT CODENM FROM TCODE WHERE CODE1 = 'C001' AND CODE2 = ALL_LIST.SS_PLACE1) AS PLACE1_STR
           			FROM	(
		  						SELECT 	*
								FROM 	H3_SUSA_LIST
								WHERE	1=1  
		]]>
									<if test="type != null and type != ''">
										AND SS_TYPE = #{type}
									</if>
									<if test="place1 != null and place1 != ''">
										AND SS_PLACE1 = #{place1}
									</if>
									<if test="agencyCode != null and agencyCode != ''">
										AND SS_AGENCY = #{agencyCode}
									</if>
									<if test="searchKeyword != null and searchKeyword != ''">
										AND SS_TITLE LIKE '%' || #{searchKeyword} || '%'
									</if>
		<![CDATA[ 
								ORDER BY SS_IDX DESC
							) ALL_LIST
				) AS list
	  	WHERE
	  		RNUM > #{firstIndex} AND  
	  		RNUM <= #{firstIndex} + #{recordCountPerPage} 
	  	]]>
	</select>

	<select id="Susa.selectListCnt" parameterType="susaSearchVO" resultType="int">
	<![CDATA[	
		SELECT
			COUNT(*)
		FROM 
			H3_SUSA_LIST
		WHERE 1=1 
	]]>
		<if test="type != null and type != ''">
			AND SS_TYPE = #{type}
		</if>	
		<if test="place1 != null and place1 != ''">
			AND SS_PLACE1 = #{place1}
		</if>
		<if test="agencyCode != null and agencyCode != ''">
			AND SS_AGENCY = #{agencyCode}
		</if>
		<if test="searchKeyword != null and searchKeyword != ''">
			AND SS_TITLE LIKE '%' || #{searchKeyword} || '%'
		</if>
	</select>

	<select id="Susa.selectTotalCnt" parameterType="susaSearchVO" resultType="int">
	<![CDATA[	
		SELECT
			COUNT(*)
		FROM 
			H3_SUSA_LIST
		WHERE 1=1
	]]>
		<if test="type != null and type != ''">
			AND SS_TYPE = #{type}
		</if>	
	</select>

	<insert id="Susa.insert" parameterType="susaVO">
		INSERT INTO H3_SUSA_LIST
		(
	    	SS_TYPE, SS_AGENCY, SS_CHARGE_NM, SS_SDATE, SS_REGDATE,
	        SS_CONTENTS, SS_PLACE1, SS_EDATE, SS_TITLE, SS_RESULT, 
	        SS_WRITER
	    )
		VALUES
		(
	    	#{type}, #{agency}, #{chargeNm}, #{sdate}, NOW(),
	        #{contents}, #{place1}, #{edate}, #{title}, #{result},
	        #{writer}
	    )
		<selectKey resultType="int" keyProperty="idx" order="AFTER">
			SELECT H3_SUSA_LIST_SS_IDX_SEQ.CURRVAL FROM DUAL
		</selectKey>
	</insert>

	<update id="Susa.update" parameterType="susaVO">
	<![CDATA[
		UPDATE 
			H3_SUSA_LIST
		SET 
			SS_TYPE = #{type},
			SS_TITLE = #{title},
			SS_SDATE = #{sdate}, 
			SS_EDATE = #{edate},
			SS_RESULT = #{result}, 
			SS_WRITER = #{writer},
			SS_CONTENTS = #{contents},
			SS_CHARGE_NM = #{chargeNm}
		WHERE 
			SS_IDX = #{idx}
	]]>
	</update>

	<delete id="Susa.delete" parameterType="susaVO">
	<![CDATA[
		DELETE 
		FROM
			H3_SUSA_LIST
		WHERE 
			SS_IDX = #{idx}
	]]>
	</delete>

</mapper>
