<?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.its.web.mapper.its.notice.NoticeMapper">
    <select id="findNoticeById" parameterType="java.lang.Long" resultType="com.its.web.dto.notice.NoticeDto">
        SELECT
            boardid         as board_id,
            boardno         as board_no,
            categorycd      as categoryCd,
            parentboardno   as parent_board_no,
            regdate         as reg_date,
            bsubject        as b_subject,
            bcontent        as b_content,
            bwriter         as b_writer,
            attachfile      as attach_file,
            attachfileid    as attach_file_id,
            bnotice         as b_notice,
            readcount       as read_count,
            remark          as remark
          FROM
            TB_WWW_BOARD
         WHERE boardid = 1
           AND boardno = #{boardNo}
    </select>

    <select id="findAllNotice" parameterType="com.its.web.pagination.Pagination" resultType="com.its.web.dto.notice.NoticeDto">
        SELECT
            boardid         as board_id,
            boardno         as board_no,
            categorycd      as categoryCd,
            parentboardno   as parent_board_no,
            regdate         as reg_date,
            bsubject        as b_subject,
            bcontent        as b_content,
            bwriter         as b_writer,
            attachfile      as attach_file,
            attachfileid    as attach_file_id,
            bnotice         as b_notice,
            readcount       as read_count,
            remark          as remark
        FROM (SELECT A.*,
                ROWNUM AS RNUM
                FROM (SELECT *
                        FROM TB_WWW_BOARD
                       ORDER BY REGDATE DESC, BOARDNO DESC
                     ) A
              )
        WHERE RNUM <![CDATA[>=]]> #{startRow}
          AND RNUM <![CDATA[<=]]> #{endRow}
        <if test="searchType == 'title'">
          AND BSUBJECT LIKE '%'||#{searchText}||'%'
        </if>
        <if test="searchType == 'content'">
          AND BCONTENT LIKE '%'||#{searchText}||'%'
        </if>
    </select>

    <select id="getNoticeTotalCount" parameterType="java.util.HashMap" resultType="int">
        SELECT
            NVL(COUNT(*), 0) as boardcount
        FROM TB_WWW_BOARD
    </select>

    <update id="updateNoticeReadCount" parameterType="java.lang.String">
        UPDATE TB_WWW_BOARD
           SET READCOUNT = READCOUNT+1
         WHERE BOARDNO = #{boardNo}
    </update>

    <insert id="insertNotice" parameterType="java.util.HashMap">
        INSERT INTO TB_WWW_BOARD (CATEGORYCD,
                                  BOARDNO,
                                  REGDATE,
                                  BSUBJECT,
                                  BCONTENT,
                                  EMAIL,
                                  ATTACHFILE,
                                  ATTACHFILEID,
                                  BNOTICE,
                                  BWRITER,
                                  BOARDID,
                                  READCOUNT)
        VALUES ('N',
                (SELECT NVL(MAX(BOARDNO),0)+1 FROM TB_WWW_BOARD),
                TO_DATE(TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS'),
                #{bSubject},
                #{bContent},
                #{webUserId},
                #{attachFile},
                #{attachFileId},
                NULL,
                '운영자',
                1,
                '0')
    </insert>

    <update id="updateNotice" parameterType="java.util.HashMap">
        UPDATE TB_WWW_BOARD
        SET REGDATE      = TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS'),
            BSUBJECT     = #{bSubject},
            BCONTENT     = #{bContent},
            ATTACHFILE   = #{attachFile},
            ATTACHFILEID = #{attachFileId}
        WHERE BOARDNO    = #{boardNo}
          AND BOARDID    = 1
    </update>

    <delete id="deleteNotice" parameterType="java.util.HashMap">
        DELETE TB_WWW_BOARD
		 WHERE BOARDNO = #{boardNo}
           AND BOARDID = 1
    </delete>

    <select id="findMainNotice" parameterType="java.lang.Integer" resultType="com.its.web.dto.notice.NoticeDto">
        SELECT
            B.boardno         as board_no,
            B.regdate         as reg_date,
            B.bsubject        as b_subject
        FROM (select
                  A.*,
                  ROWNUM
              from (select
                        boardno,
                        DECODE(regdate, NULL, '-', TO_CHAR(TO_DATE(regdate, 'YYYY-MM-DD'), 'YYYY-MM-DD')) regdate,
                        bsubject
                    from TB_WWW_BOARD
                    order by regdate desc, boardno desc) A
             ) B
        where ROWNUM BETWEEN 1 AND #{num}
    </select>

    <select id="getAttachFileCount" parameterType="java.util.HashMap" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            TB_WWW_BOARD
        WHERE BOARDNO = #{boardNo}
          AND ATTACHFILEID LIKE '%'||#{fileId}||'%'
    </select>

    <delete id="deleteBoard" parameterType="java.lang.String">
        DELETE FROM TB_WWW_BOARD WHERE BOARDID = 1 AND BOARDNO = #{boardNo}
    </delete>

    <select id="checkImageName" parameterType="java.util.HashMap" resultType="int">
        SELECT COUNT(*) FROM TB_WWW_BOARD WHERE BOARDNO = #{boarNo} AND ATTACHFILE = #{fileName} || '||'
    </select>
</mapper>