<?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.popup.PopupMapper">
    <select id="findAllPopup" resultType="com.its.web.dto.admin.TbWwwPopupDto" parameterType="com.its.web.pagination.Pagination">
        SELECT
            popupid
            , title
            , url
            , TO_CHAR(post_from, 'YYYY-MM-DD') as post_from
            , TO_CHAR(post_to, 'YYYY-MM-DD') as post_to
            , img_id
            , img_name
            , TO_CHAR(regdate, 'YYYY-MM-DD') as regdate
            , use_yn
            , del_yn
        FROM (SELECT A.*,
                     ROWNUM AS RNUM
                FROM (SELECT *
                        FROM TB_WWW_POPUP
                        WHERE DEL_YN = 'N'
                       ORDER BY popupid DESC, regdate DESC
                ) A
        )
        <![CDATA[
        WHERE RNUM >= #{startRow}
          AND RNUM <= #{endRow}
        ]]>
    </select>

    <select id="findMainPopup" resultType="com.its.web.dto.admin.TbWwwPopupDto" parameterType="java.lang.Integer">
        SELECT
             popupid,
             title
            , post_from
            , post_to
          FROM (SELECT A.*,
                  ROWNUM AS RNUM
                FROM (SELECT
                           popupid,
                           title,
                           DECODE(post_from, NULL, '-', TO_CHAR(TO_DATE(post_from, 'YYYY-MM-DD'), 'YYYY-MM-DD')) post_from,
                           DECODE(post_to, NULL, '-', TO_CHAR(TO_DATE(post_to, 'YYYY-MM-DD'), 'YYYY-MM-DD')) post_to
                        FROM TB_WWW_POPUP
                        WHERE use_yn = 'Y'
                          AND del_yn = 'N'
                       ORDER BY popupid DESC, regdate DESC
                ) A
              )
        WHERE ROWNUM BETWEEN 1 AND #{num}
    </select>
    <select id="findPopupTotalPage" resultType="java.lang.Integer">
        SELECT
            count(*)
          FROM TB_WWW_POPUP
         WHERE del_yn = 'N'
    </select>

    <select id="findPopup" parameterType="java.lang.String" resultType="com.its.web.dto.admin.TbWwwPopupDto">
        SELECT
             popupid
             , title
             , url
             , TO_CHAR(post_from, 'YYYY-MM-DD') AS post_from
             , TO_CHAR(post_to, 'YYYY-MM-DD') AS post_to
             , img_id
             , img_name
             , TO_CHAR(regdate, 'YYYY-MM-DD') AS regdate
             , use_yn
             , del_yn
          FROM TB_WWW_POPUP
         WHERE popupid = #{popupId}
           AND del_yn = 'N'
    </select>

    <insert id="insertPopup" parameterType="java.util.HashMap">
        INSERT INTO TB_WWW_POPUP (popupid
                                 , title
                                 , url
                                 , post_from
                                 , post_to
                                 , img_id
                                 , img_name
                                 , regdate
                                 , use_yn
                                 , del_yn)
              VALUES ((SELECT NVL(MAX(popupid),0)+1 FROM TB_WWW_POPUP),
                      #{title},
                      #{url},
                      TO_DATE(#{postForm}, 'YYYY-MM-DD'),
                      TO_DATE(#{postTo}, 'YYYY-MM-DD'),
                      #{imgId},
                      #{imgName},
                      TO_DATE(sysdate, 'YYYY-MM-DD'),
                      'Y',
                      'N'
                      )
    </insert>

    <update id="updatePopup" parameterType="java.util.HashMap">
        UPDATE TB_WWW_POPUP
           SET title     = #{title},
               url       = #{url},
               post_from = TO_DATE(#{postFrom}, 'YYYY-MM-DD'),
               post_to   = TO_DATE(#{postTo}, 'YYYY-MM-DD'),
               img_id    = #{imgId},
               img_name  = #{imgName}
         WHERE popupid   = #{popupId}
    </update>

    <update id="deletePopup" parameterType="java.lang.String" >
        UPDATE TB_WWW_POPUP
        SET DEL_YN = 'Y'
        WHERE popupid = #{popupId}
    </update>
</mapper>