<?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="com.owrawww.domain.mapper.BbsMapper">

    <resultMap id="bbsResultMap" type="com.owrawww.domain.Bbs">
        <id     property="id"      column="id"/>
        <result property="title"   column="title"/>
        <result property="name"    column="name"/>
        <result property="regDate" column="in_date"/>
        <result property="body"    column="body"/>
        <result property="viewCnt" column="view_cnt"/>
        <result property="mainImg" column="main_img"/>
    </resultMap>

    <sql id="searchCondition">
        <if test="keyword != null and keyword != ''">
            <choose>
                <when test="searchType == 'title'">
                    AND title LIKE CONCAT('%', #{keyword}, '%')
                </when>
                <when test="searchType == 'content'">
                    AND body LIKE CONCAT('%', #{keyword}, '%')
                </when>
                <otherwise>
                    AND (title LIKE CONCAT('%', #{keyword}, '%')
                      OR body LIKE CONCAT('%', #{keyword}, '%'))
                </otherwise>
            </choose>
        </if>
    </sql>

    <select id="selectList" parameterType="map" resultMap="bbsResultMap">
        SELECT id, title, name, DATE_FORMAT(in_date, '%Y-%m-%d') AS in_date, main_img , count as view_cnt
        FROM bbs_table
        <where>
            top_code = #{topCode}
            AND left_code = #{leftCode}
            AND sub_gubun = #{subGubun}
            <include refid="searchCondition"/>
        </where>
        ORDER BY id DESC
        LIMIT #{size} OFFSET #{offset}
    </select>

    <select id="selectCount" parameterType="map" resultType="int">
        SELECT COUNT(*)
        FROM bbs_table
        <where>
            top_code = #{topCode}
            AND left_code = #{leftCode}
            AND sub_gubun = #{subGubun}
            <include refid="searchCondition"/>
        </where>
    </select>

    <!-- 상세 조회 -->
    <select id="selectById" parameterType="map" resultMap="bbsResultMap">
        SELECT id, title, name, body, count AS view_cnt, main_img,  
               DATE_FORMAT(in_date, '%Y-%m-%d') AS in_date
        FROM bbs_table
        WHERE id = #{id}
          AND top_code  = #{topCode}
          AND left_code = #{leftCode}
          AND sub_gubun = #{subGubun}
    </select>

    <!-- 이전글 (id 내림차순 기준 → id < 현재) -->
    <select id="selectPrev" parameterType="map" resultMap="bbsResultMap">
        SELECT id, title, DATE_FORMAT(in_date, '%Y-%m-%d') AS in_date
        FROM bbs_table
        WHERE id &lt; #{id}
          AND top_code  = #{topCode}
          AND left_code = #{leftCode}
          AND sub_gubun = #{subGubun}
        ORDER BY id DESC
        LIMIT 1
    </select>

    <!-- 다음글 (id 오름차순 기준 → id > 현재) -->
    <select id="selectNext" parameterType="map" resultMap="bbsResultMap">
        SELECT id, title, DATE_FORMAT(in_date, '%Y-%m-%d') AS in_date
        FROM bbs_table
        WHERE id &gt; #{id}
          AND top_code  = #{topCode}
          AND left_code = #{leftCode}
          AND sub_gubun = #{subGubun}
        ORDER BY id ASC
        LIMIT 1
    </select>

    <!-- 조회수 증가 -->
    <update id="incrementViewCnt" parameterType="long">
        UPDATE bbs_table SET count = count + 1 WHERE id = #{id}
    </update>

    <!-- 메인 미리보기 (최신 N건) -->
    <select id="selectPreview" parameterType="map" resultMap="bbsResultMap">
        SELECT id, title, DATE_FORMAT(in_date, '%Y-%m-%d') AS in_date
        FROM bbs_table
        WHERE top_code  = #{topCode}
          AND left_code = #{leftCode}
          AND sub_gubun = #{subGubun}
        ORDER BY id DESC
        LIMIT #{limit}
    </select>

</mapper>
