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

	<select id="Board.selectBoardList" parameterType="boardSearchVO" resultType="boardVO">
    select BD_SEQ AS SEQ,
           BD_MB_ID AS USERID,
           BD_GROUP_SEQ AS GROUPSEQ,
           BD_CATEGORY AS CATEGORY,
           BD_NAME AS USERNAME,
           BD_CONTENT AS CONTENT,
           BD_SUBJECT AS SUBJECT,
           BD_PARENT_SEQ AS PARENTSEQ,
           BD_PREV_SEQ AS PREVSEQ,
           BD_NEXT_SEQ AS NEXTSEQ,
           BD_STEP AS STEP,
           BD_DEPTH AS DEPTH,
           BD_REGDATE AS REGDATE,
           BD_HIT AS HIT,
           BD_RECOMMEND AS RECOMMEND,
           BD_IP AS IP,
           BD_NOTICE AS NOTICE,
           BD_HTML AS HTML,
           BD_NUM1 AS NUM1,
           BD_NUM2 AS NUM2,
           BD_DATA1 AS DATA1,
           BD_DATA2 AS DATA2,
           FILECNT
    from H3_BD_${id}_LIST list
            left outer join (select BF_BD_SEQ, count(*) as filecnt
                            from H3_BD_notice_FILE
                            group by BF_BD_SEQ) file
                on list.bd_seq = file.bf_bd_seq
    <where>
      <if test='category != " " or id == "stat_bbs"'>
        AND	BD_CATEGORY = #{category}
      </if>
      <if test="searchKeyword != null and searchKeyword != ''">
        <choose>
          <when test="searchCondition == 0">
            AND	BD_DATA1 LIKE '%' || #{searchKeyword} || '%'
          </when>
          <when test="searchCondition == 1">
            AND	BD_NAME LIKE '%' || #{searchKeyword} || '%'
          </when>
          <when test="searchCondition == 2">
            AND	BD_SUBJECT LIKE '%' || #{searchKeyword} || '%'
          </when>
        </choose>
      </if>
    </where>
    ORDER BY BD_NOTICE DESC, BD_GROUP_SEQ DESC, BD_STEP asc
    limit #{recordCountPerPage} offset #{firstIndex}
	</select>
	
	<select id="Board.selectBoardTotalCnt" parameterType="boardSearchVO" resultType="int">
		SELECT
			COUNT(*)
		FROM 
			H3_BD_${id}_LIST
	</select>
	
	<select id="Board.selectBoardListTotCnt" parameterType="boardSearchVO" resultType="int">
		SELECT
			COUNT(*)
		FROM 
			H3_BD_${id}_LIST
		WHERE 1=1
			<choose>
	            <when test='category == " " and id == "stat_bbs"'>
				</when>
	            <otherwise>
					AND	BD_CATEGORY = #{category}
				</otherwise>
			</choose>
			<if test="searchKeyword != null and searchKeyword != ''">
		        <choose>
		            <when test="searchCondition == 0">
						AND	BD_DATA1 LIKE '%' || #{searchKeyword} || '%'
					</when>
		            <when test="searchCondition == 1">
						AND	BD_NAME LIKE '%' || #{searchKeyword} || '%'
					</when>
		            <when test="searchCondition == 2">
						AND	BD_SUBJECT LIKE '%' || #{searchKeyword} || '%'
					</when>
				</choose>
			</if>	
	</select>
	
	<select id="Board.selectBoard" parameterType="boardVO" resultType="boardVO">
		SELECT
			BBS.BD_SEQ AS SEQ, BBS.BD_MB_ID AS USERID, BBS.BD_GROUP_SEQ AS GROUPSEQ,
			BBS.BD_CATEGORY AS CATEGORY, BBS.BD_NAME AS USERNAME, BBS.BD_CONTENT AS CONTENT,
			BBS.BD_SUBJECT AS SUBJECT, BBS.BD_PARENT_SEQ AS PARENTSEQ, BBS.BD_PREV_SEQ AS PREVSEQ,
			BBS.BD_NEXT_SEQ AS NEXTSEQ, BBS.BD_STEP AS STEP, BBS.BD_DEPTH AS DEPTH,
			BBS.BD_REGDATE AS REGDATE, BBS.BD_HIT AS HIT, BBS.BD_RECOMMEND AS RECOMMEND,
			BBS.BD_IP AS IP, BBS.BD_NOTICE AS NOTICE, BBS.BD_HTML AS HTML,
			BBS.BD_NUM1 AS NUM1, BBS.BD_NUM2 AS NUM2, BBS.BD_DATA1 AS DATA1, BBS.BD_DATA2 AS DATA2,
			#{id} ID
		FROM 
			H3_BD_${id}_LIST BBS
		WHERE
			BBS.BD_SEQ = #{seq}
	</select>
	
	<insert id="Board.insertBoard" parameterType="boardVO">
		INSERT INTO 
			H3_BD_${id}_LIST (
					BD_MB_ID,
					BD_CATEGORY,
					BD_NAME,
					BD_GROUP_SEQ,
					BD_CONTENT,
					BD_SUBJECT,
					BD_PARENT_SEQ,
					BD_PREV_SEQ,
					BD_NEXT_SEQ,
					BD_STEP,
					BD_DEPTH,
					BD_REGDATE,
					BD_HIT,
					BD_RECOMMEND,
					BD_IP,
					BD_NOTICE,
					BD_HTML,
					BD_NUM1,
					BD_NUM2,
					BD_DATA1,
					BD_DATA2,
					BD_PLACE1,
					BD_POSITION
		) VALUES (
					#{userId},
					#{category},
					#{userName},
					#{groupSeq},
					#{content},
					#{subject},
					#{parentSeq},
					#{prevSeq},
					#{nextSeq},
					#{step},
					#{depth},
					NOW(),
					${hit},
					0,
					#{ip,jdbcType=VARCHAR},
					#{notice},
					1,
					0,
					0,
					#{data1,jdbcType=VARCHAR},
					#{data2,jdbcType=VARCHAR},
					#{place1,jdbcType=VARCHAR},
					#{position,jdbcType=VARCHAR}
		)
		<selectKey resultType="int" keyProperty="seq" order="AFTER">
	        SELECT currval(H3_BD_${id}_LIST_SEQ) as seq
	    </selectKey>
	<!-- 
		BD_SEQ, BD_MB_ID, BD_GROUP_SEQ, BD_CATEGORY, BD_NAME, 
				BD_CONTENT, BD_SUBJECT, BD_PARENT_SEQ, BD_PREV_SEQ, BD_NEXT_SEQ, BD_STEP, 
				BD_DEPTH, BD_REGDATE, BD_HIT, BD_RECOMMEND, BD_IP, BD_NOTICE, 
				BD_HTML, BD_NUM1, BD_NUM2, BD_DATA1, BD_DATA2, BD_PLACE1, BD_POSITION
	
	
				#{bd_seq}, #{bd_mb_id}, #{bd_group_seq}, #{bd_category}, #{bd_name},
				#{bd_content}, #{bd_subject}, #{bd_parent_seq}, #{bd_prev_seq}, #{bd_next_seq}, #{bd_step}, 
				#{bd_depth}, #{bd_regdate}, #{bd_hit}, #{bd_recommend}, #{bd_ip}, #{bd_notice}, 
				#{bd_html}, #{bd_num1}, #{bd_num2}, #{bd_data1}, #{bd_data2}, #{bd_place1}, #{bd_position}
	 -->
	</insert>
	
	<update id="Board.updateGrpSeq" parameterType="boardVO">
		UPDATE
			H3_BD_${id}_LIST
		SET 
			BD_GROUP_SEQ = #{seq}
		WHERE 
			BD_SEQ = #{seq}
	</update>
	
	<select id="Board.selectPrevBoard" parameterType="boardVO" resultType="boardVO">
		SELECT
			ALL_LIST.BD_SEQ AS SEQ, ALL_LIST.BD_MB_ID AS USERID, ALL_LIST.BD_GROUP_SEQ AS GROUPSEQ,
			ALL_LIST.BD_CATEGORY AS CATEGORY, ALL_LIST.BD_NAME AS USERNAME, ALL_LIST.BD_CONTENT AS CONTENT,
			ALL_LIST.BD_SUBJECT AS SUBJECT, ALL_LIST.BD_PARENT_SEQ AS PARENTSEQ, ALL_LIST.BD_PREV_SEQ AS PREVSEQ,
			ALL_LIST.BD_NEXT_SEQ AS NEXTSEQ, ALL_LIST.BD_STEP AS STEP, ALL_LIST.BD_DEPTH AS DEPTH,
			ALL_LIST.BD_REGDATE AS REGDATE, ALL_LIST.BD_HIT AS HIT, ALL_LIST.BD_RECOMMEND AS RECOMMEND,
			ALL_LIST.BD_IP AS IP, ALL_LIST.BD_NOTICE AS NOTICE, ALL_LIST.BD_HTML AS HTML,
			ALL_LIST.BD_NUM1 AS NUM1, ALL_LIST.BD_NUM2 AS NUM2, ALL_LIST.BD_DATA1 AS DATA1, ALL_LIST.BD_DATA2 AS DATA2,
			#{id} ID
		FROM  
		(
		    SELECT ROWNUM RNUM, ALL_LIST.*
		    FROM  (
		    	SELECT
		              *
		        FROM H3_BD_${id}_LIST
		        WHERE  1 = 1
		        <choose>
		            <when test='category == " " and id == "stat_bbs"'>
					</when>
		            <otherwise>
						AND	BD_CATEGORY = #{category}
					</otherwise>
				</choose>
		        ORDER BY
		        	BD_GROUP_SEQ DESC,
		            BD_STEP	ASC
		    ) ALL_LIST
		) ALL_LIST
		JOIN
		( 
			SELECT * FROM 
		    (
			    SELECT ROWNUM RNUM, BBS.*
			    FROM  (
			    	SELECT
			              *
			        FROM H3_BD_${id}_LIST BBS
			        WHERE  1 = 1
		        <choose>
		            <when test='category == " " and id == "stat_bbs"'>
					</when>
		            <otherwise>
						AND	BD_CATEGORY = #{category}
					</otherwise>
				</choose>
			        ORDER BY
			        	BBS.BD_GROUP_SEQ DESC,
			            BBS.BD_STEP	ASC
			    ) BBS
			) BBS 
			WHERE BD_SEQ = #{seq}
		)BBS
		ON
		  BBS.RNUM + 1 = ALL_LIST.RNUM
	</select>
	
	<update id="Board.updateGrpStep" parameterType="boardVO">
		UPDATE
			H3_BD_${id}_LIST
		SET 
			BD_STEP = BD_STEP + 1
		WHERE 
			BD_GROUP_SEQ = #{seq} AND
			BD_STEP > #{step}
	</update>
	
	<update id="Board.updatePrevNextSeq" parameterType="boardVO">
		UPDATE
			H3_BD_${id}_LIST
		SET 
			BD_PREV_SEQ = #{prevSeq},
			BD_NEXT_SEQ = #{nextSeq}
		WHERE 
			BD_SEQ = #{seq}
	</update>
	
	<update id="Board.updateBoard" parameterType="boardVO">
		UPDATE
			H3_BD_${id}_LIST
		SET 
			BD_SUBJECT = #{subject},
			BD_CONTENT = #{content},
			BD_NOTICE = #{notice},
			BD_HIT = #{hit}
		WHERE 
			BD_SEQ = #{seq}
	</update>
	
	<delete id="Board.deleteBoard" parameterType="boardVO">
		DELETE
		FROM
			H3_BD_${id}_LIST 
		WHERE 
			BD_SEQ = #{seq}
	</delete>
</mapper>
