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

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

    <select id="selectListAppBoard" parameterType="hashmap" resultType="hashmap">
        SELECT MAB.*
        FROM (
                SELECT MAB.*
                     , CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
                FROM (
                        SELECT MAB.MU_APP_BOARD_ID                          AS "muAppBoardId"
                             , MAB.TITLE                                    AS "title"
                             , MAB.CONTENT                                  AS "content"
                             , IFNULL((SELECT COUNT(1)
                                         FROM MU_BOARD_CONTENT
                                        WHERE USE_YN       = 'Y'
                                          AND MU_APP_BOARD_ID = MAB.MU_APP_BOARD_ID
                                        LIMIT 0, 1
                                        ),0)                                AS "contentCount"
                            , CASE
                                   WHEN EXPOSED_YN = 'Y' THEN '사용'
                                   WHEN EXPOSED_YN = 'N' THEN '미사용'
                                   ELSE ''
                              END                                           AS "exposedYn"
                             , CONCAT(
                                      DATE_FORMAT(MAB.WRITE_DATE, '%Y-%m-%d')
                                     ,', '
                                     ,DATE_FORMAT(MAB.WRITE_TIME, '%H:%i')
                                     )                                      AS "writeDate"
                          FROM MU_APP_BOARD MAB
                         WHERE MAB.USE_YN = 'Y'
                           <if test="appBoardSearchStartDate != null and appBoardSearchStartDate != '' and appBoardSearchEndDate != null and appBoardSearchEndDate != ''">
                           AND DATE_FORMAT(MAB.WRITE_DATE, '%Y-%m-%d') &gt;= #{appBoardSearchStartDate}
                           AND DATE_FORMAT(MAB.WRITE_DATE, '%Y-%m-%d') &lt;= #{appBoardSearchEndDate}
                           </if>
                           <if test="appBoardSearchKeywordParam != null and appBoardSearchKeywordParam != ''">
                           AND ((MAB.TITLE LIKE CONCAT('%',TRIM(#{appBoardSearchKeywordParam}),'%'))
                           OR (MAB.CONTENT LIKE CONCAT('%',TRIM(#{appBoardSearchKeywordParam}),'%')))
                           </if>
                           <choose>
                               <when test="appBoardSort != null and appBoardSort != ''">
                               ORDER BY ${appBoardSort}
                               </when>
                               <otherwise>
                               ORDER BY MAB.WRITE_DATE DESC
                               </otherwise>
                           </choose>
                         LIMIT 18446744073709551615
                ) MAB, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) MAB
        WHERE 1 = 1
        LIMIT ${appBoardStart}, ${appBoardLimit}
    </select>

    <select id="selectAppBoard" parameterType="hashmap" resultType="hashmap">
       SELECT MAB.MU_APP_BOARD_ID                     AS "muAppBoardId"
            , MAB.TITLE                               AS "boardTitle"
            , MAB.CONTENT                             AS "boardContent"
            , EXPOSED_YN                              AS "exposedYn"
            , MU_BOARD_CONTENT_ID                     AS "muBoardContentId"
            , MU_APP_CONTENT_ID                       AS "muAppContentId"
            , MBC.TITLE                               AS "title"
            , MBC.CONTENT                             AS "content"
            , MBC.FILE_PATH                           AS "filePath"
            , MBC.FILE_NAME                           AS "fileName"
            , MBC.ORIGINAL_FILE_NAME                  AS "originalFileName"
            , MBC.FILE_PATH2                          AS "filePath2"
            , MBC.FILE_NAME2                          AS "fileName2"
            , MBC.ORIGINAL_FILE_NAME2                 AS "originalFileName2"
            , MBC.CATEGORY_NAME                       AS "appContentCategoryName"
            , DATE_FORMAT(MBC.CONTENT_WRITE_DATE, '%Y-%m-%d') AS "writeDate"
            , '삭제'                                   AS "management"
         FROM MU_APP_BOARD MAB
         JOIN MU_BOARD_CONTENT MBC
           ON MAB.MU_APP_BOARD_ID = MBC.MU_APP_BOARD_ID
          AND MBC.USE_YN='Y'
        WHERE MAB.USE_YN        = 'Y'
          AND MAB.MU_APP_BOARD_ID  = #{muAppBoardId}
    </select>

    <insert id="insertAppBoard" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muAppBoardId},LPAD((SELECT NEXTVAL(MU_APP_BOARD_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_APP_BOARD(
        MU_APP_BOARD_ID
        ,TITLE
        ,CONTENT
        ,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}
        ,#{exposedYn}
        ,CURDATE()
        ,CURTIME()
        ,'C'
        ,'Y'
        ,#{regId}
        ,NOW()
        ,#{modId}
        ,NOW()
        ,#{tId}
        ,#{tDate}
        )
    </insert>

    <insert id="insertBoardContent" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muBoardContentId},LPAD((SELECT NEXTVAL(MU_BOARD_CONTENT_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_BOARD_CONTENT(
                    MU_BOARD_CONTENT_ID
                    ,MU_APP_BOARD_ID
                    ,MU_APP_CONTENT_ID
                    ,TITLE
                    ,CONTENT
                    ,FILE_PATH
                    ,FILE_NAME
                    ,ORIGINAL_FILE_NAME
                    ,FILE_PATH2
                    ,FILE_NAME2
                    ,ORIGINAL_FILE_NAME2
                    ,CATEGORY_NAME
                    ,CONTENT_WRITE_DATE
                    ,WRITE_DATE
                    ,WRITE_TIME
                    ,CUD_FLAG
                    ,USE_YN
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
                    ,T_ID
                    ,T_DATE
        )VALUES(
                    #{id}
                    ,#{muAppBoardId}
                    ,#{muAppContentId}
                    ,#{title}
                    ,#{content}
                    ,#{filePath}
                    ,#{fileName}
                    ,#{originalFileName}
                    ,#{filePath2}
                    ,#{fileName2}
                    ,#{originalFileName2}
                    ,#{categoryName}
                    ,#{contentWriteDate}
                    ,CURDATE()
                    ,CURTIME()
                    ,'C'
                    ,'Y'
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
                    ,#{tId}
                    ,#{tDate}
        )
    </insert>

    <update id="updateAppBoard" parameterType="hashmap">
        UPDATE MU_APP_BOARD
           SET MOD_ID              = #{modId}
             ,MOD_DATE            = NOW()
             ,CUD_FLAG            = 'U'
             ,TITLE               = #{title}
             ,CONTENT             = #{content}
             ,EXPOSED_YN          = #{exposedYn}
         WHERE USE_YN        = 'Y'
           AND MU_APP_BOARD_ID  = #{muAppBoardId}
    </update>

    <update id="updateAppBoardContent" parameterType="hashmap">
        UPDATE MU_BOARD_CONTENT
           SET MOD_ID              = #{modId}
             ,MOD_DATE            = NOW()
             ,CUD_FLAG            = 'U'
             ,USE_YN            = 'N'
         WHERE USE_YN        = 'Y'
           AND MU_APP_BOARD_ID  = #{muAppBoardId}
    </update>

    <update id="deleteAppBoard" parameterType="hashmap">
        UPDATE MU_APP_BOARD
           SET MOD_ID            = #{modId}
             ,MOD_DATE	         = NOW()
             ,CUD_FLAG          = 'D'
             ,USE_YN            = 'N'
         WHERE USE_YN        = 'Y'
           AND MU_APP_BOARD_ID  = #{muAppBoardId}
    </update>
</mapper>