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

    <select id="selectListAppEventOption" parameterType="hashmap" resultType="hashmap">
        SELECT MAE.MU_APP_EVENT_ID  AS "muAppEventId"
              ,MAE.TITLE            AS "title"
          FROM MU_APP_EVENT AS MAE
         WHERE MAE.USE_YN = 'Y'
      ORDER BY MAE.WRITE_DATE DESC
              ,MAE.WRITE_TIME DESC
    </select>

    <select id="selectAppEventExposedCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)  AS "exposedCount"
        FROM MU_APP_EVENT AS ME
        WHERE ME.EXPOSED_YN = 'Y'
          AND ME.USE_YN = 'Y'
    </select>

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

    <select id="selectListAppEvent" parameterType="hashmap" resultType="hashmap">
        SELECT MAE.*
        FROM (
                SELECT MAE.*
                     , CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
                FROM (
                        SELECT MAE.MU_APP_EVENT_ID                        AS "muAppEventId"
                             , MAE.TITLE                                  AS "title"
                             , MAE.CONTENT                                AS "content"
                             , CONCAT(
                                      DATE_FORMAT(MAE.EVENT_START_DATE, '%Y-%m-%d')
                                     ,' ~ '
                                     ,DATE_FORMAT(MAE.EVENT_END_DATE, '%Y-%m-%d')
                                     )                                    AS "eventDate"
                             , CASE
                                 WHEN EXPOSED_YN = 'Y' THEN '사용'
                                 WHEN EXPOSED_YN = 'N' THEN '미사용'
                                 ELSE ''
                               END                                        AS "exposedYn"
                             , 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 = MAE.REG_ID
                                        LIMIT 0, 1
                                      ),'')                                  AS "writeName"
                          FROM MU_APP_EVENT MAE
                         WHERE MAE.USE_YN = 'Y'
                           <if test="appEventSearchStartDate != null and appEventSearchStartDate != '' and appEventSearchEndDate != null and appEventSearchEndDate != ''">
                           AND DATE_FORMAT(MAE.WRITE_DATE, '%Y-%m-%d') &gt;= #{appEventSearchStartDate}
                           AND DATE_FORMAT(MAE.WRITE_DATE, '%Y-%m-%d') &lt;= #{appEventSearchEndDate}
                           </if>
                           <if test="appEventSearchKeywordParam != null and appEventSearchKeywordParam != ''">
                           AND ((MAE.TITLE LIKE CONCAT('%',TRIM(#{appEventSearchKeywordParam}),'%'))
                           OR (MAE.CONTENT LIKE CONCAT('%',TRIM(#{appEventSearchKeywordParam}),'%')))
                           </if>
                           <choose>
                               <when test="appEventSort != null and appEventSort != ''">
                               ORDER BY ${appEventSort}
                               </when>
                               <otherwise>
                               ORDER BY MAE.WRITE_DATE DESC
                               </otherwise>
                           </choose>
                         LIMIT 18446744073709551615
                ) MAE, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) MAE
        WHERE 1 = 1
        LIMIT ${appEventStart}, ${appEventLimit}
    </select>

    <select id="selectAppEvent" parameterType="hashmap" resultType="hashmap">
        SELECT MAE.MU_APP_EVENT_ID                              AS "muAppEventId"
             ,MAE.TITLE                                         AS "title"
             ,MAE.CONTENT                                       AS "content"
             ,MAE.DETAIL                                        AS "detail"
             ,MAE.FILE_PATH						                AS "filePath"
             ,MAE.FILE_NAME						                AS "fileName"
             ,MAE.ORIGINAL_FILE_NAME				            AS "originalFileName"
             ,DATE_FORMAT(MAE.EVENT_START_DATE, '%Y-%m-%d')		AS "eventStartDate"
             ,DATE_FORMAT(MAE.EVENT_END_DATE, '%Y-%m-%d')		AS "eventEndDate"
             ,MAE.EXPOSED_YN                                  AS "exposedYn"
          FROM MU_APP_EVENT AS MAE
         WHERE MAE.USE_YN        = 'Y'
           AND MAE.MU_APP_EVENT_ID  = #{muAppEventId}
         LIMIT 0, 1
    </select>
    
    <insert id="insertAppEvent" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muAppEventId},LPAD((SELECT NEXTVAL(MU_APP_EVENT_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_APP_EVENT(
        MU_APP_EVENT_ID
        ,TITLE
        ,CONTENT
        ,DETAIL
        ,FILE_PATH
        ,FILE_NAME
        ,ORIGINAL_FILE_NAME
        ,EVENT_START_DATE
        ,EVENT_END_DATE
        ,EXPOSED_YN
        ,WRITE_DATE
        ,WRITE_TIME
        ,CUD_FLAG
        ,USE_YN
        ,REG_ID
        ,REG_DATE
        ,MOD_ID
        ,MOD_DATE
        ,T_ID
        ,T_DATE
        )VALUES(
        #{id}
        ,#{title}
        ,#{content}
        ,#{detail}
        ,#{filePath}
        ,#{fileName}
        ,#{originalFileName}
        ,#{eventStartDate}
        ,#{eventEndDate}
        ,#{exposedYn}
        ,CURDATE()
        ,CURTIME()
        ,'C'
        ,'Y'
        ,#{regId}
        ,NOW()
        ,#{modId}
        ,NOW()
        ,#{tId}
        ,#{tDate}
        )
    </insert>

    <update id="updateAppEvent" parameterType="hashmap">
        UPDATE MU_APP_EVENT
           SET MOD_ID              = #{modId}
             ,MOD_DATE            = NOW()
             ,CUD_FLAG            = 'U'
             ,TITLE               = #{title}
             ,CONTENT             = #{content}
             ,DETAIL              = #{detail}
             <if test="filePath != null and filePath != ''">
             ,FILE_PATH           = #{filePath}
             ,FILE_NAME           = #{fileName}
             ,ORIGINAL_FILE_NAME  = #{originalFileName}
             </if>
             ,EVENT_START_DATE    = #{eventStartDate}
             ,EVENT_END_DATE      = #{eventEndDate}
             ,EXPOSED_YN        = #{exposedYn}
         WHERE USE_YN        = 'Y'
           AND MU_APP_EVENT_ID  = #{muAppEventId}
    </update>

    <update id="deleteAppEvent" parameterType="hashmap">
        UPDATE MU_APP_EVENT
           SET MOD_ID            = #{modId}
             ,MOD_DATE	         = NOW()
             ,CUD_FLAG          = 'D'
             ,USE_YN            = 'N'
         WHERE USE_YN        = 'Y'
           AND MU_APP_EVENT_ID  = #{muAppEventId}
    </update>
</mapper>