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

    <select id="selectTotalWebPopupCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)  AS "totalCount"
          FROM MU_WEB_POPUP AS MWP
        WHERE MWP.USE_YN = 'Y'
        <if test="homepageDivCd != null and homepageDivCd != ''">
        	AND MWP.HOMEPAGE_DIV_CD = #{homepageDivCd}
        </if>
        <if test="webPopupSearchStartDate != null and webPopupSearchStartDate != '' and webPopupSearchEndDate != null and webPopupSearchEndDate != ''">
            AND DATE_FORMAT(MWP.WRITE_DATE, '%Y-%m-%d') &gt;= #{webPopupSearchStartDate}
            AND DATE_FORMAT(MWP.WRITE_DATE, '%Y-%m-%d') &lt;= #{webPopupSearchEndDate}
        </if>
        <if test="webPopupSearchKeywordParam != null and webPopupSearchKeywordParam != ''">
            AND MWP.TITLE LIKE CONCAT('%',TRIM(#{webPopupSearchKeywordParam}),'%')
        </if>
    </select>

    <select id="selectListWebPopup" parameterType="hashmap" resultType="hashmap">
        SELECT MWP.*
          FROM (
                SELECT MWP.*
                      ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
                  FROM (
                        SELECT MWP.MU_WEB_POPUP_ID                           AS "muWebPopupId"
                              ,MWP.TITLE                                     AS "title"
                              ,MWP.FILE_PATH						         AS "filePath"
                              ,MWP.FILE_NAME						         AS "fileName"
                              ,MWP.ORIGINAL_FILE_NAME				         AS "originalFileName"
                              ,CONCAT(DATE_FORMAT(MWP.WRITE_DATE, '%Y-%m-%d')
                                     ,', '
                                     ,DATE_FORMAT(MWP.WRITE_TIME, '%H:%i'))  AS "writeDate"
                              ,IFNULL((SELECT MM.NAME
                                         FROM MU_MEMBER AS MM
                                        WHERE MM.USE_YN       = 'Y'
                                          AND MM.MU_MEMBER_ID = MWP.REG_ID
                                        LIMIT 0, 1
                                      ),'')                                  AS "writeName"
                              ,MWP.HOMEPAGE_DIV_CD                     AS "homepageDivCd"
                          FROM MU_WEB_POPUP MWP
                         WHERE MWP.USE_YN = 'Y'
                           <if test="webPopupSearchStartDate != null and webPopupSearchStartDate != '' and webPopupSearchEndDate != null and webPopupSearchEndDate != ''">
                           AND DATE_FORMAT(MWP.WRITE_DATE, '%Y-%m-%d') &gt;= #{webPopupSearchStartDate}
                           AND DATE_FORMAT(MWP.WRITE_DATE, '%Y-%m-%d') &lt;= #{webPopupSearchEndDate}
                           </if>
                           <if test="webPopupSearchKeywordParam != null and webPopupSearchKeywordParam != ''">
                           AND MWP.TITLE LIKE CONCAT('%',TRIM(#{webPopupSearchKeywordParam}),'%')
                           </if>
                           <if test="homepageDivCd != null and homepageDivCd != ''">
					        	AND MWP.HOMEPAGE_DIV_CD = #{homepageDivCd}
					        </if>
                          <choose>
                          <when test="webPopupSort != null and webPopupSort != ''">
                      ORDER BY ${webPopupSort}
                          </when>
                          <otherwise>
                      ORDER BY MWP.HOMEPAGE_DIV_CD, MWP.WRITE_DATE DESC
                          </otherwise>
                          </choose>
                         LIMIT 18446744073709551615
                ) AS MWP, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) AS MWP
        WHERE 1 = 1
      <if test="webPopupStart != null and webPopupStart != '' and webPopupLimit != null and webPopupLimit != ''">
        LIMIT ${webPopupStart}, ${webPopupLimit}
      </if>
    </select>

    <select id="selectWebPopup" parameterType="hashmap" resultType="hashmap">
        SELECT MWP.MU_WEB_POPUP_ID                    AS "muWebPopupId"
              ,MWP.TITLE                                AS "title"
              ,MWP.URL                                  AS "url"
              ,MWP.FILE_PATH                            AS "filePath"
              ,MWP.FILE_NAME                            AS "fileName"
              ,MWP.ORIGINAL_FILE_NAME                   AS "originalFileName"
              ,MWP.HOMEPAGE_DIV_CD                     AS "homepageDivCd"
          FROM MU_WEB_POPUP   AS MWP
         WHERE MWP.USE_YN        = 'Y'
           AND MWP.MU_WEB_POPUP_ID  = #{muWebPopupId}
         LIMIT 0, 1
    </select>

    <select id="selectWebPopupCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)   AS "count"
          FROM MU_WEB_POPUP   AS MWP
         WHERE MWP.USE_YN = 'Y'
         <if test="homepageDivCd != null and homepageDivCd != ''">
        	AND MWP.HOMEPAGE_DIV_CD = #{homepageDivCd}
        </if>
    </select>

    <insert id="insertWebPopup" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muWebPopupId},LPAD((SELECT NEXTVAL(MU_WEB_POPUP_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_WEB_POPUP(
                    MU_WEB_POPUP_ID
                    ,TITLE
                    ,URL
                    ,CDN_URL
                    ,FILE_PATH
                    ,FILE_NAME
                    ,ORIGINAL_FILE_NAME
                    ,WRITE_DATE
                    ,WRITE_TIME
                    ,CUD_FLAG
                    ,USE_YN
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
                    ,T_ID
                    ,T_DATE
                    ,HOMEPAGE_DIV_CD
        )VALUES(
                    #{id}
                    ,#{title}
                    ,#{url}
                    ,#{cdnUrl}
                    ,#{filePath}
                    ,#{fileName}
                    ,#{originalFileName}
                    ,NOW()
                    ,NOW()
                    ,'C'
                    ,'Y'
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
                    ,#{tId}
                    ,#{tDate}
                    ,#{homepageDivCd}
        )
    </insert>

    <update id="updateWebPopup" parameterType="hashmap">
        UPDATE MU_WEB_POPUP
           SET MOD_ID              = #{modId}
              ,MOD_DATE            = NOW()
              ,CUD_FLAG            = 'U'
              ,TITLE               = #{title}
              ,URL                 = #{url}
              ,HOMEPAGE_DIV_CD     = #{homepageDivCd}
              <if test="filePath != null and filePath != ''">
              ,CDN_URL             = #{cdnUrl}
              ,FILE_PATH           = #{filePath}
              ,FILE_NAME           = #{fileName}
              ,ORIGINAL_FILE_NAME  = #{originalFileName}
              </if>
         WHERE USE_YN        = 'Y'
           AND MU_WEB_POPUP_ID  = #{muWebPopupId}
    </update>

    <update id="deleteWebPopup" parameterType="hashmap">
        UPDATE MU_WEB_POPUP
           SET MOD_ID            = #{modId}
              ,MOD_DATE	         = NOW()
              ,CUD_FLAG          = 'D'
              ,USE_YN            = 'N'
         WHERE USE_YN        = 'Y'
           AND MU_WEB_POPUP_ID  = #{muWebPopupId}
    </update>
</mapper>