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

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

    <select id="selectListAppNotice" parameterType="hashmap" resultType="hashmap">
        SELECT MN.*
        FROM (
                SELECT MN.*
                      ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
                FROM (
                        SELECT MN.MU_APP_NOTICE_ID                        AS "muAppNoticeId"
                             , MN.MU_MEMBER_ID                            AS "muMemberId"
                             , IFNULL((SELECT MM.NAME
                                         FROM MU_MEMBER AS MM
                                        WHERE MM.USE_YN       = 'Y'
                                          AND MM.MU_MEMBER_ID = MN.MU_MEMBER_ID
                                        LIMIT 0, 1
                                       ),'')                              AS "writeName"
                             , MN.TITLE                                   AS "title"
                             , MN.CONTENT                                 AS "content"
                             , DATE_FORMAT(MN.WRITE_DATE, '%Y-%m-%d')     AS "writeDate"
                        FROM MU_APP_NOTICE MN
                        WHERE MN.USE_YN = 'Y'
                        <if test="appNoticeSearchStartDate != null and appNoticeSearchStartDate != '' and appNoticeSearchEndDate != null and appNoticeSearchEndDate != ''">
                            AND DATE_FORMAT(MN.WRITE_DATE, '%Y-%m-%d') &gt;= #{appNoticeSearchStartDate}
                            AND DATE_FORMAT(MN.WRITE_DATE, '%Y-%m-%d') &lt;= #{appNoticeSearchEndDate}
                        </if>
                        <if test="appNoticeSearchKeywordParam != null and appNoticeSearchKeywordParam != ''">
                            AND ((MN.TITLE LIKE CONCAT('%',TRIM(#{appNoticeSearchKeywordParam}),'%'))
                            OR (MN.CONTENT LIKE CONCAT('%',TRIM(#{appNoticeSearchKeywordParam}),'%')))
                        </if>
                        <choose>
                            <when test="appNoticeSort != null and appNoticeSort != ''">
                                ORDER BY ${appNoticeSort}
                            </when>
                            <otherwise>
                                ORDER BY MN.WRITE_DATE DESC
                            </otherwise>
                        </choose>
                        LIMIT 18446744073709551615
                ) MN, (SELECT @RNUM:=0) R
            WHERE 1 = 1
        ) MN
        WHERE 1 = 1
        LIMIT ${appNoticeStart}, ${appNoticeLimit}
    </select>

    <select id="selectAppNotice" parameterType="hashmap" resultType="hashmap">
        SELECT MN.MU_APP_NOTICE_ID                        AS "muAppNoticeId"
             ,MN.MU_MEMBER_ID                        AS "muMemberId"
             ,MN.TITLE                               AS "title"
             ,MN.CONTENT                             AS "content"
             ,DATE_FORMAT(MN.WRITE_DATE, '%Y-%m-%d') AS "writeDate"
          FROM MU_APP_NOTICE AS MN
         WHERE MN.USE_YN        = 'Y'
           AND MN.MU_APP_NOTICE_ID  = #{muAppNoticeId}
         LIMIT 0, 1
    </select>
    
    <insert id="insertAppNotice" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muAppNoticeId},LPAD((SELECT NEXTVAL(MU_APP_NOTICE_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_APP_NOTICE(
        MU_APP_NOTICE_ID
        ,MU_MEMBER_ID
        ,TITLE
        ,CONTENT
        ,WRITE_DATE
        ,WRITE_TIME
        ,CUD_FLAG
        ,USE_YN
        ,REG_ID
        ,REG_DATE
        ,MOD_ID
        ,MOD_DATE
        ,T_ID
        ,T_DATE
        )VALUES(
        #{id}
        ,#{muMemberId}
        ,#{title}
        ,#{content}
        ,CURDATE()
        ,CURTIME()
        ,'C'
        ,'Y'
        ,#{regId}
        ,NOW()
        ,#{modId}
        ,NOW()
        ,#{tId}
        ,#{tDate}
        )
    </insert>

    <update id="updateAppNotice" parameterType="hashmap">
        UPDATE MU_APP_NOTICE
           SET MOD_ID              = #{modId}
             ,MOD_DATE            = NOW()
             ,CUD_FLAG            = 'U'
             ,TITLE               = #{title}
             ,CONTENT             = #{content}
         WHERE USE_YN        = 'Y'
           AND MU_APP_NOTICE_ID  = #{muAppNoticeId}
    </update>

    <update id="deleteAppNotice" parameterType="hashmap">
        UPDATE MU_APP_NOTICE
           SET MOD_ID            = #{modId}
             ,MOD_DATE	         = NOW()
             ,CUD_FLAG          = 'D'
             ,USE_YN            = 'N'
         WHERE USE_YN        = 'Y'
           AND MU_APP_NOTICE_ID  = #{muAppNoticeId}
    </update>
</mapper>