<?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 != null and category != ""'>
        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>
			<if test='category != null and category != ""'>
				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>
	</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(#{id}||'_bd_seq') as seq
    </selectKey>
	</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 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,
			     #{id} ID
		FROM H3_BD_${id}_LIST
    <where>
      <if test='seq != null and seq != 0'>
        AND BD_SEQ = #{seq}
      </if>
      <if test='category != " " and id != "stat_bbs"'>
        AND	BD_CATEGORY = #{category}
      </if>
    </where>
    order by bd_seq desc
    limit 1 offset 1
  </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>
