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

    <select id="selectTotalWebMainBannerCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)  AS "totalCount"
          FROM MU_WEB_MAIN_BANNER AS MWMB
         WHERE MWMB.USE_YN = 'Y'
        <if test="webMainBannerSearchType != null and webMainBannerSearchType != ''">
           AND MWMB.BANNER_TYPE = #{webMainBannerSearchType}
        </if>

        <if test="webMainBannerSearchKeywordParam != null and webMainBannerSearchKeywordParam != ''">
           AND MWMB.TITLE LIKE CONCAT('%',TRIM(#{webMainBannerSearchKeywordParam}),'%')
        </if>
    </select>

    <select id="selectListWebMainBanner" parameterType="hashmap" resultType="hashmap">
        SELECT MWMB.*
          FROM (
                SELECT MWMB.*
                      ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
                  FROM (
                        SELECT MWMB.MU_WEB_MAIN_BANNER_ID                    AS "muWebMainBannerId"
                              ,CASE
                               WHEN MWMB.BANNER_TYPE = 'DT'
                               THEN '다이어트 상단'
                               WHEN MWMB.BANNER_TYPE = 'DB'
                               THEN '다이어트 하단'
                               WHEN MWMB.BANNER_TYPE = 'PT'
                               THEN '쁘띠 상단'
                               WHEN MWMB.BANNER_TYPE = 'PB'
                               THEN '쁘띠 하단'
                               ELSE ''
                                END                                          AS "bannerType"
                              ,MWMB.TITLE                                    AS "title"
                              ,MWMB.FILE_PATH						         AS "filePath"
                              ,MWMB.FILE_NAME						         AS "fileName"
                              ,MWMB.ORIGINAL_FILE_NAME				         AS "originalFileName"
                              ,MWMB.FILE_PATH2						         AS "filePath2"
                              ,MWMB.FILE_NAME2						         AS "fileName2"
                              ,MWMB.ORIGINAL_FILE_NAME2				         AS "originalFileName2"
                              ,CONCAT(DATE_FORMAT(MWMB.WRITE_DATE, '%Y-%m-%d')
                                     ,', '
                                     ,DATE_FORMAT(MWMB.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 = MWMB.REG_ID
                                        LIMIT 0, 1
                                      ),'')                                  AS "writeName"
                          FROM MU_WEB_MAIN_BANNER AS MWMB
                         WHERE MWMB.USE_YN = 'Y'
                           <if test="webMainBannerSearchType != null and webMainBannerSearchType != ''">
                           AND MWMB.BANNER_TYPE = #{webMainBannerSearchType}
                           </if>

                           <if test="webMainBannerSearchKeywordParam != null and webMainBannerSearchKeywordParam != ''">
                           AND MWMB.TITLE LIKE CONCAT('%',TRIM(#{webMainBannerSearchKeywordParam}),'%')
                           </if>
                         <choose>
                         <when test="webMainBannerSort != null and webMainBannerSort != ''">
                      ORDER BY ${webMainBannerSort}
                         </when>
                         <otherwise>
                      ORDER BY MWMB.WRITE_DATE DESC
                         </otherwise>
                         </choose>
                         LIMIT 18446744073709551615
                ) AS MWMB, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) AS MWMB
        WHERE 1 = 1
      <if test="webMainBannerStart != null and webMainBannerStart != '' and webMainBannerLimit != null and webMainBannerLimit != ''">
        LIMIT ${webMainBannerStart}, ${webMainBannerLimit}
      </if>
    </select>

    <select id="selectWebMainBanner" parameterType="hashmap" resultType="hashmap">
        SELECT MWMB.MU_WEB_MAIN_BANNER_ID                AS "muWebMainBannerId"
              ,MWMB.BANNER_TYPE                          AS "bannerType"
              ,CASE
               WHEN MWMB.BANNER_TYPE = 'DT'
               THEN '다이어트 상단'
               WHEN MWMB.BANNER_TYPE = 'DB'
               THEN '다이어트 하단'
               WHEN MWMB.BANNER_TYPE = 'PT'
               THEN '쁘띠 상단'
               WHEN MWMB.BANNER_TYPE = 'PB'
               THEN '쁘띠 하단'
               ELSE ''
                END                                      AS "bannerTypeName"
              ,MWMB.TITLE                                AS "title"
              ,MWMB.URL                                  AS "url"
              ,MWMB.FILE_PATH                            AS "filePath"
              ,MWMB.FILE_NAME                            AS "fileName"
              ,MWMB.ORIGINAL_FILE_NAME                   AS "originalFileName"
              ,MWMB.FILE_PATH2                           AS "filePath2"
              ,MWMB.FILE_NAME2                           AS "fileName2"
              ,MWMB.ORIGINAL_FILE_NAME2                  AS "originalFileName2"
          FROM MU_WEB_MAIN_BANNER   AS MWMB
         WHERE MWMB.USE_YN        = 'Y'
           AND MWMB.MU_WEB_MAIN_BANNER_ID  = #{muWebMainBannerId}
         LIMIT 0, 1
    </select>

    <select id="selectWebMainBannerCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)   AS "count"
          FROM MU_WEB_MAIN_BANNER   AS MWMB
         WHERE MWMB.USE_YN = 'Y'
           AND MWMB.BANNER_TYPE = #{bannerType}
    </select>

    <insert id="insertWebMainBanner" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muWebMainBannerId},LPAD((SELECT NEXTVAL(MU_WEB_MAIN_BANNER_SEQ)), 10, 0))
        </selectKey>
        INSERT INTO MU_WEB_MAIN_BANNER(
                    MU_WEB_MAIN_BANNER_ID
                    ,BANNER_TYPE
                    ,TITLE
                    ,URL
                    ,CDN_URL
                    ,FILE_PATH
                    ,FILE_NAME
                    ,ORIGINAL_FILE_NAME
                    ,FILE_PATH2
                    ,FILE_NAME2
                    ,ORIGINAL_FILE_NAME2
                    ,WRITE_DATE
                    ,WRITE_TIME
                    ,CUD_FLAG
                    ,USE_YN
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
                    ,T_ID
                    ,T_DATE
        )VALUES(
                    #{id}
                    ,#{bannerType}
                    ,#{title}
                    ,#{url}
                    ,#{cdnUrl}
                    ,#{filePath}
                    ,#{fileName}
                    ,#{originalFileName}
                    ,#{filePath2}
                    ,#{fileName2}
                    ,#{originalFileName2}
                    ,NOW()
                    ,NOW()
                    ,'C'
                    ,'Y'
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
                    ,#{tId}
                    ,#{tDate}
        )
    </insert>

    <update id="updateWebMainBanner" parameterType="hashmap">
        UPDATE MU_WEB_MAIN_BANNER
           SET MOD_ID              = #{modId}
              ,MOD_DATE            = NOW()
              ,CUD_FLAG            = 'U'
              ,BANNER_TYPE         = #{bannerType}
              ,TITLE               = #{title}
              ,URL                 = #{url}
              <if test="filePath != null and filePath != ''">
              ,CDN_URL             = #{cdnUrl}
              ,FILE_PATH           = #{filePath}
              ,FILE_NAME           = #{fileName}
              ,ORIGINAL_FILE_NAME  = #{originalFileName}
              </if>
              <if test="filePath2 != null and filePath2 != ''">
              ,FILE_PATH2           = #{filePath2}
              ,FILE_NAME2           = #{fileName2}
              ,ORIGINAL_FILE_NAME2  = #{originalFileName2}
              </if>
         WHERE USE_YN        = 'Y'
           AND MU_WEB_MAIN_BANNER_ID  = #{muWebMainBannerId}
    </update>

    <update id="deleteWebMainBanner" parameterType="hashmap">
        UPDATE MU_WEB_MAIN_BANNER
           SET MOD_ID            = #{modId}
              ,MOD_DATE	         = NOW()
              ,CUD_FLAG          = 'D'
              ,USE_YN            = 'N'
         WHERE USE_YN        = 'Y'
           AND MU_WEB_MAIN_BANNER_ID  = #{muWebMainBannerId}
    </update>
</mapper>