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

    <select id="selectTotalAppContentsCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)  AS "totalCount"
        FROM MU_APP_CONTENT AS MAC
        WHERE MAC.USE_YN = 'Y'
        <if test="appContentCategoryName != null and appContentCategoryName != ''">
            AND MAC.CATEGORY_NAME= #{appContentCategoryName}
        </if>
        <if test="appContentSearchStartDate != null and appContentSearchStartDate != '' and appContentSearchEndDate != null and appContentSearchEndDate != ''">
            AND DATE_FORMAT(MAC.WRITE_DATE, '%Y-%m-%d') &gt;= #{appContentSearchStartDate}
            AND DATE_FORMAT(MAC.WRITE_DATE, '%Y-%m-%d') &lt;= #{appContentSearchEndDate}
        </if>
        <if test="appContentSearchKeywordParam != null and appContentSearchKeywordParam != ''">
            AND ((MAC.TITLE LIKE CONCAT('%',TRIM(#{appContentSearchKeywordParam}),'%'))
            OR (MAC.CONTENT LIKE CONCAT('%',TRIM(#{appContentSearchKeywordParam}),'%')))
        </if>
    </select>

    <select id="selectListAppContents" parameterType="hashmap" resultType="hashmap">
        SELECT MAC.*
          FROM (
                SELECT MAC.*
                     , CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
                FROM (
                        SELECT MAC.MU_APP_CONTENT_ID                      AS "muAppContentId"
                             , MAC.CATEGORY_NAME                          AS "appContentCategoryName"
                             , MAC.TITLE                                  AS "title"
                             , MAC.CONTENT                                AS "content"
                             , MAC.FILE_PATH						      AS "filePath"
                             , MAC.FILE_NAME						      AS "fileName"
                             , MAC.ORIGINAL_FILE_NAME				      AS "originalFileName"
                             , MAC.FILE_PATH2						      AS "filePath2"
                             , MAC.FILE_NAME2						      AS "fileName2"
                             , MAC.ORIGINAL_FILE_NAME2				      AS "originalFileName2"
                             , CONCAT(
                                      DATE_FORMAT(MAC.WRITE_DATE, '%Y-%m-%d')
                                     ,', '
                                     ,DATE_FORMAT(MAC.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 = MAC.REG_ID
                                        LIMIT 0, 1
                                      ),'')                                  AS "writeName"
                          FROM MU_APP_CONTENT MAC
                         WHERE MAC.USE_YN = 'Y'
                           <if test="appContentCategoryName != null and appContentCategoryName != ''">
                               AND MAC.CATEGORY_NAME= #{appContentCategoryName}
                           </if>
                           <if test="appContentSearchStartDate != null and appContentSearchStartDate != '' and appContentSearchEndDate != null and appContentSearchEndDate != ''">
                           AND DATE_FORMAT(MAC.WRITE_DATE, '%Y-%m-%d') &gt;= #{appContentSearchStartDate}
                           AND DATE_FORMAT(MAC.WRITE_DATE, '%Y-%m-%d') &lt;= #{appContentSearchEndDate}
                           </if>
                           <if test="appContentSearchKeywordParam != null and appContentSearchKeywordParam != ''">
                           AND ((MAC.TITLE LIKE CONCAT('%',TRIM(#{appContentSearchKeywordParam}),'%'))
                           OR (MAC.CONTENT LIKE CONCAT('%',TRIM(#{appContentSearchKeywordParam}),'%')))
                           </if>
                           <choose>
                               <when test="appContentSort != null and appContentSort != ''">
                               ORDER BY ${appContentSort}
                               </when>
                               <otherwise>
                               ORDER BY MAC.WRITE_DATE DESC
                               </otherwise>
                           </choose>
                         LIMIT 18446744073709551615
                ) MAC, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) MAC
        WHERE 1 = 1
        <if test="appContentStart != null and appContentStart != '' and appContentLimit != null and appContentLimit != ''">
        LIMIT ${appContentStart}, ${appContentLimit}
        </if>
    </select>

    <select id="selectAppContents" parameterType="hashmap" resultType="hashmap">
        SELECT MAC.MU_APP_CONTENT_ID                    AS "muAppContentId"
             , MAC.TITLE                                AS "title"
             , MAC.CONTENT                              AS "content"
             , MAC.DETAIL                               AS "detail"
             , MAC.FILE_PATH                            AS "filePath"
             , MAC.FILE_NAME                            AS "fileName"
             , MAC.ORIGINAL_FILE_NAME                   AS "originalFileName"
             , MAC.FILE_PATH2                           AS "filePath2"
             , MAC.FILE_NAME2                           AS "fileName2"
             , MAC.ORIGINAL_FILE_NAME2                  AS "originalFileName2"
             , MAC.CATEGORY_NAME                        AS "categoryName"
             , MAC.CATEGORY_NAME                        AS "categoryCode"
          FROM MU_APP_CONTENT   AS MAC
         WHERE MAC.USE_YN        = 'Y'
           AND MAC.MU_APP_CONTENT_ID  = #{muAppContentId}
         LIMIT 0, 1
    </select>
    
    <insert id="insertAppContents" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muAppContentId},LPAD((SELECT NEXTVAL(MU_APP_CONTENT_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_APP_CONTENT(
                    MU_APP_CONTENT_ID
                    ,TITLE
                    ,CONTENT
                    ,DETAIL
                    ,FILE_PATH
                    ,FILE_NAME
                    ,ORIGINAL_FILE_NAME
                    ,FILE_PATH2
                    ,FILE_NAME2
                    ,ORIGINAL_FILE_NAME2
                    ,CATEGORY_CODE
                    ,CATEGORY_NAME
                    ,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}
                    ,#{filePath2}
                    ,#{fileName2}
                    ,#{originalFileName2}
                    ,#{categoryCode}
                    ,#{categoryName}
                    ,CURDATE()
                    ,CURTIME()
                    ,'C'
                    ,'Y'
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
                    ,#{tId}
                    ,#{tDate}
        )
    </insert>

    <update id="updateAppContents" parameterType="hashmap">
        UPDATE MU_APP_CONTENT
           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>
             <if test="filePath2 != null and filePath2 != ''">
             ,FILE_PATH2           = #{filePath2}
             ,FILE_NAME2           = #{fileName2}
             ,ORIGINAL_FILE_NAME2  = #{originalFileName2}
             </if>
             ,CATEGORY_NAME       = #{categoryName}
             ,CATEGORY_CODE       = #{categoryCode}
         WHERE USE_YN        = 'Y'
           AND MU_APP_CONTENT_ID  = #{muAppContentId}
    </update>

    <update id="deleteAppContents" parameterType="hashmap">
        UPDATE MU_APP_CONTENT
           SET MOD_ID            = #{modId}
             ,MOD_DATE	         = NOW()
             ,CUD_FLAG          = 'D'
             ,USE_YN            = 'N'
         WHERE USE_YN        = 'Y'
           AND MU_APP_CONTENT_ID  = #{muAppContentId}
    </update>
</mapper>