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

    <select id="selectTotalWebEventCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)  AS "totalCount"
          FROM MU_WEB_EVENT AS ME
         WHERE ME.USE_YN = 'Y'
        <if test="webEventSearchStartDate != null and webEventSearchStartDate != '' and webEventSearchEndDate != null and webEventSearchEndDate != ''">
           AND DATE_FORMAT(ME.WRITE_DATE, '%Y-%m-%d') &gt;= #{webEventSearchStartDate}
           AND DATE_FORMAT(ME.WRITE_DATE, '%Y-%m-%d') &lt;= #{webEventSearchEndDate}
        </if>
        <if test="webEventSearchKeywordParam != null and webEventSearchKeywordParam != ''">
           AND ((ME.TITLE LIKE CONCAT('%',TRIM(#{webEventSearchKeywordParam}),'%'))
           OR (ME.CONTENT LIKE CONCAT('%',TRIM(#{webEventSearchKeywordParam}),'%')))
        </if>
    </select>

    <select id="selectListWebEvent" parameterType="hashmap" resultType="hashmap">
        SELECT MWE.*
          FROM (
                SELECT MWE.*
                      ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
                  FROM (
                        SELECT MWE.MU_WEB_EVENT_ID                        AS "muWebEventId"
                              ,MWE.TITLE                                  AS "title"
                              ,MWE.CONTENT                                AS "content"
                              ,CONCAT(DATE_FORMAT(MWE.EVENT_START_DATE, '%Y-%m-%d')
                                     ,' ~ '
                                     ,DATE_FORMAT(MWE.EVENT_END_DATE, '%Y-%m-%d'))  AS "eventDate"
                              ,DATE_FORMAT(WRITE_DATE, '%Y-%m-%d')        AS "writeDate"
                              ,IFNULL((SELECT MM.NAME
                                         FROM MU_MEMBER AS MM
                                        WHERE MM.USE_YN       = 'Y'
                                          AND MM.MU_MEMBER_ID = MWE.REG_ID
                                        LIMIT 0, 1
                                      ),'')                                  AS "writeName"
                          FROM MU_WEB_EVENT MWE
                         WHERE MWE.USE_YN = 'Y'
                           <if test="webEventSearchStartDate != null and webEventSearchStartDate != '' and webEventSearchEndDate != null and webEventSearchEndDate != ''">
                           AND DATE_FORMAT(MWE.WRITE_DATE, '%Y-%m-%d') &gt;= #{webEventSearchStartDate}
                           AND DATE_FORMAT(MWE.WRITE_DATE, '%Y-%m-%d') &lt;= #{webEventSearchEndDate}
                           </if>
                           <if test="webEventSearchKeywordParam != null and webEventSearchKeywordParam != ''">
                           AND ((MWE.TITLE LIKE CONCAT('%',TRIM(#{webEventSearchKeywordParam}),'%'))
                            OR (MWE.CONTENT LIKE CONCAT('%',TRIM(#{webEventSearchKeywordParam}),'%')))
                           </if>
                           <choose>
                           <when test="webEventSort != null and webEventSort != ''">
                      ORDER BY ${webEventSort}
                           </when>
                           <otherwise>
                      ORDER BY MWE.WRITE_DATE DESC
                           </otherwise>
                           </choose>
                         LIMIT 18446744073709551615
                ) MWE, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) MWE
        WHERE 1 = 1
        LIMIT ${webEventStart}, ${webEventLimit}
    </select>

    <select id="selectWebEvent" parameterType="hashmap" resultType="hashmap">
        SELECT MWE.MU_WEB_EVENT_ID                              AS "muWebEventId"
             ,MWE.TITLE                                         AS "title"
             ,MWE.CONTENT                                       AS "content"
             ,MWE.FILE_PATH						                AS "filePath"
             ,MWE.FILE_NAME						                AS "fileName"
             ,MWE.ORIGINAL_FILE_NAME				            AS "originalFileName"
             ,DATE_FORMAT(MWE.EVENT_START_DATE, '%Y-%m-%d')		AS "eventStartDate"
             ,DATE_FORMAT(MWE.EVENT_END_DATE, '%Y-%m-%d')		AS "eventEndDate"
          FROM MU_WEB_EVENT AS MWE
         WHERE MWE.USE_YN        = 'Y'
           AND MWE.MU_WEB_EVENT_ID  = #{muWebEventId}
         LIMIT 0, 1
    </select>
    
    <insert id="insertWebEvent" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muWebEventId},LPAD((SELECT NEXTVAL(MU_WEB_EVENT_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_WEB_EVENT(
                    MU_WEB_EVENT_ID
                    ,TITLE
                    ,CONTENT
                    ,CDN_URL
                    ,FILE_PATH
                    ,FILE_NAME
                    ,ORIGINAL_FILE_NAME
                    ,EVENT_START_DATE
                    ,EVENT_END_DATE
                    ,WRITE_DATE
                    ,WRITE_TIME
                    ,CUD_FLAG
                    ,USE_YN
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
                    ,T_ID
                    ,T_DATE
        )VALUES(
                    #{id}
                    ,#{title}
                    ,#{content}
                    ,#{cdnUrl}
                    ,#{filePath}
                    ,#{fileName}
                    ,#{originalFileName}
                    ,#{eventStartDate}
                    ,#{eventEndDate}
                    ,NOW()
                    ,NOW()
                    ,'C'
                    ,'Y'
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
                    ,#{tId}
                    ,#{tDate}
        )
    </insert>

    <update id="updateWebEvent" parameterType="hashmap">
        UPDATE MU_WEB_EVENT
           SET MOD_ID              = #{modId}
             ,MOD_DATE            = NOW()
             ,CUD_FLAG            = 'U'
             ,TITLE               = #{title}
             ,CONTENT             = #{content}
             <if test="filePath != null and filePath != ''">
             ,CDN_URL             = #{cdnUrl}
             ,FILE_PATH           = #{filePath}
             ,FILE_NAME           = #{fileName}
             ,ORIGINAL_FILE_NAME  = #{originalFileName}
             </if>
             ,EVENT_START_DATE    = #{eventStartDate}
             ,EVENT_END_DATE      = #{eventEndDate}
         WHERE USE_YN        = 'Y'
           AND MU_WEB_EVENT_ID  = #{muWebEventId}
    </update>

    <update id="deleteWebEvent" parameterType="hashmap">
        UPDATE MU_WEB_EVENT
           SET MOD_ID            = #{modId}
             ,MOD_DATE	         = NOW()
             ,CUD_FLAG          = 'D'
             ,USE_YN            = 'N'
         WHERE USE_YN        = 'Y'
           AND MU_WEB_EVENT_ID  = #{muWebEventId}
    </update>
</mapper>