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

    <select id="selectTotalWebPhotoPetitCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*) AS "totalCount" from (SELECT HBFPB.POST_NO, HBFPB.TITLE, HBFPB.CONTENT
          FROM HP_BEFORE_AFTER_PHOTO_BBS AS HBFPB
          LEFT OUTER JOIN HP_CATEGORY HC ON HBFPB.CATEGORY_DIV_CD = HC.CATEGORY_DIV_CD AND HBFPB.CATEGORY_NO = HC.CATEGORY_NO
         WHERE HBFPB.USE_YN = 'Y'
         AND HBFPB.CATEGORY_DIV_CD = '06'
        <if test="webPhotoPetitSearchKeywordParam0 != null and webPhotoPetitSearchKeywordParam0 != ''">
           AND (
           (HC.CATEGORY_NM LIKE CONCAT('%',TRIM(#{webPhotoPetitSearchKeywordParam0}),'%'))           
           )
        </if>
        <if test="webPhotoPetitSearchKeywordParam1 != null and webPhotoPetitSearchKeywordParam1 != ''">
           AND (
           (HBFPB.TITLE LIKE CONCAT('%',TRIM(#{webPhotoPetitSearchKeywordParam1}),'%'))          
           )
        </if>
        <if test="webPhotoPetitSearchKeywordParam2 != null and webPhotoPetitSearchKeywordParam2 != ''">
           AND (
           (IFNULL((SELECT MM.NAME FROM MU_MEMBER AS MM
           		WHERE MM.USE_YN       = 'Y'
                	AND MM.MU_MEMBER_ID = HBFPB.REG_ID
                    LIMIT 0, 1
               ),'') LIKE CONCAT('%',TRIM(#{webPhotoPetitSearchKeywordParam2}),'%'))
           )
        </if>
         GROUP BY HBFPB.POST_NO, HBFPB.TITLE, HBFPB.CONTENT) as HBFPB
    </select>

    <select id="selectListWebPhotoPetit" parameterType="hashmap" resultType="hashmap">
        SELECT HBFPB.*
          FROM (
                SELECT HBFPB.*
                      ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
                  FROM (
                        SELECT HBFPB.POST_NO                        AS "muWebPhotoPetitId"
                              ,HBFPB.TITLE                                  AS "title"
                              ,HBFPB.CONTENT                                AS "content"
                              ,HC.CATEGORY_NM							  AS "category"
                              ,HC.CATEGORY_NO							  AS "categoryno"
                              ,HBFPB.HASHTAG					  AS "hashtag"
                              ,CONCAT(DATE_FORMAT(HBFPB.REG_DATE, '%Y-%m-%d')
                                     ,' ~ '
                                     ,DATE_FORMAT(HBFPB.REG_DATE, '%Y-%m-%d'))  AS "photopetitDate"
                              ,DATE_FORMAT(HBFPB.REG_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 = HBFPB.REG_ID
                                        LIMIT 0, 1
                                      ),'')                                  AS "writeName"
                          FROM HP_BEFORE_AFTER_PHOTO_BBS HBFPB
                          LEFT OUTER JOIN HP_CATEGORY HC ON HBFPB.CATEGORY_DIV_CD = HC.CATEGORY_DIV_CD AND HBFPB.CATEGORY_NO = HC.CATEGORY_NO     
                         WHERE HBFPB.USE_YN = 'Y'
                         AND HBFPB.CATEGORY_DIV_CD = '06'
                         <!-- 
                           <if test="webPhotoPetitSearchStartDate != null and webPhotoPetitSearchStartDate != '' and webPhotoPetitSearchEndDate != null and webPhotoPetitSearchEndDate != ''">
                           AND DATE_FORMAT(HCB.REG_DATE, '%Y-%m-%d') &gt;= #{webPhotoPetitSearchStartDate}
                           AND DATE_FORMAT(HCB.REG_DATE, '%Y-%m-%d') &lt;= #{webPhotoPetitSearchEndDate}
                           </if>
                          -->
                           <if test="webPhotoPetitSearchKeywordParam0 != null and webPhotoPetitSearchKeywordParam0 != ''">
					           AND (
					           (HC.CATEGORY_NM LIKE CONCAT('%',TRIM(#{webPhotoPetitSearchKeywordParam0}),'%'))           
					           )
					        </if>
					        <if test="webPhotoPetitSearchKeywordParam1 != null and webPhotoPetitSearchKeywordParam1 != ''">
					           AND (
					           (HBFPB.TITLE LIKE CONCAT('%',TRIM(#{webPhotoPetitSearchKeywordParam1}),'%'))          
					           )
					        </if>
					        <if test="webPhotoPetitSearchKeywordParam2 != null and webPhotoPetitSearchKeywordParam2 != ''">
					           AND (
					           (IFNULL((SELECT MM.NAME FROM MU_MEMBER AS MM
					           		WHERE MM.USE_YN       = 'Y'
					                	AND MM.MU_MEMBER_ID = HBFPB.REG_ID
					                    LIMIT 0, 1
					               ),'') LIKE CONCAT('%',TRIM(#{webPhotoPetitSearchKeywordParam2}),'%'))
					           )
					        </if>
                      GROUP BY muWebPhotoPetitId, title, content
                           <choose>
                           <when test="webPhotoPetitSort != null and webPhotoPetitSort != ''">
                      ORDER BY ${webPhotoPetitSort}
                           </when>
                           <otherwise>
                      ORDER BY HBFPB.REG_DATE DESC
                           </otherwise>
                           </choose>
                         LIMIT 18446744073709551615
                      
                ) HBFPB, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) HBFPB
        WHERE 1 = 1
        LIMIT ${webPhotoPetitStart}, ${webPhotoPetitLimit}
    </select>

    <select id="selectWebPhotoPetit" parameterType="hashmap" resultType="hashmap">
        SELECT HBFPB.POST_NO                              AS "muWebPhotoPetitId"
        	 ,HBFPB.CATEGORY_NO                                   AS "categoryno"
             ,HBFPB.TITLE                                         AS "title"
             ,HBFPB.CONTENT                                       AS "content"
             ,HBFPB.HASHTAG						                AS "hashtag"
             ,HAF2.FILE_PATH						    AS "beforefile"
             ,HAF.FILE_PATH						    AS "afterfile"             
          FROM HP_BEFORE_AFTER_PHOTO_BBS AS HBFPB
          LEFT OUTER JOIN HP_ATTACH_FILE HAF ON HAF.ATTACHFILE_ID = HBFPB.BEFORE_PHOTO_ATTACHFILE_ID
          LEFT OUTER JOIN HP_ATTACH_FILE HAF2 ON HAF2.ATTACHFILE_ID = HBFPB.AFTER_PHOTO_ATTACHFILE_ID
         WHERE HBFPB.USE_YN        = 'Y'
           AND HBFPB.CATEGORY_DIV_CD = '06'
           AND HBFPB.CATEGORY_NO = #{categoryno}
           AND HBFPB.POST_NO  = #{muWebPhotoPetitId}
         LIMIT 0, 1
    </select>    
    
    <insert id="insertWebPhotoPetit" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT (MAX(POST_NO) + 1) FROM HP_BEFORE_AFTER_PHOTO_BBS WHERE CATEGORY_DIV_CD = '06' AND CATEGORY_NO = ${categoryno}
        </selectKey>
        INSERT INTO HP_BEFORE_AFTER_PHOTO_BBS(
                    CATEGORY_DIV_CD
                    ,CATEGORY_NO
                    ,POST_NO
                    ,TITLE
                    ,CONTENT
                  	,BEFORE_PHOTO_ATTACHFILE_ID
                    ,AFTER_PHOTO_ATTACHFILE_ID
                    ,HASHTAG                    
                    ,USE_YN
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
        )VALUES(
        			'06'
        			,${categoryno}
                    ,#{id}
                    ,#{title}
                    ,#{content}
                    ,#{beforeId}
                    ,#{afterId}
                    ,#{hashtag}
                    ,'Y'
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
        )
    </insert>
    
    <insert id="insertWebPhotoPetitBeforeFile" parameterType="hashmap">
    	<selectKey resultType="string" keyProperty="beforeId" order="BEFORE">
            SELECT CONCAT('ATF-', LPAD(NEXTVAL(seq_attachfile_id), 21, '0'))
        </selectKey>
        INSERT INTO HP_ATTACH_FILE(
                     ATTACHFILE_ID
                    ,ATTACHFILE_NM
                    ,FILE_PATH
                    ,FILE_SIZE
                    ,FILE_TYPE
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
        )VALUES(
        			 #{beforeId}
                    ,#{beforeFileName}
                    ,#{beforeFilePath}
                    ,'0'
                    ,'img'
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
        )
        
    </insert>
    
    <insert id="insertWebPhotoPetitAfterFile" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="afterId" order="BEFORE">
            SELECT CONCAT('ATF-', LPAD(NEXTVAL(seq_attachfile_id), 21, '0'))
        </selectKey>
        INSERT INTO HP_ATTACH_FILE(
                     ATTACHFILE_ID
                    ,ATTACHFILE_NM
                    ,FILE_PATH
                    ,FILE_SIZE
                    ,FILE_TYPE
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
        )VALUES(
        			 #{afterId}
                    ,#{afterFileName}
                    ,#{afterFilePath}
                    ,'0'
                    ,'img'
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
        )
    </insert>
    

    <update id="updateWebPhotoPetit" parameterType="hashmap">
        UPDATE HP_BEFORE_AFTER_PHOTO_BBS
           SET 
             TITLE               = #{title}
             ,CONTENT             = #{content}
             ,HASHTAG             = #{hashtag}
             ,CATEGORY_NO = #{categoryNo}
             <if test="beforeId != null and beforeId != ''">
           		,BEFORE_PHOTO_ATTACHFILE_ID             = #{beforeId}
        	 </if>
             <if test="afterId != null and afterId != ''">
           		,AFTER_PHOTO_ATTACHFILE_ID             = #{afterId}
        	</if>             
             ,MOD_ID             = #{modId}
             ,MOD_DATE             = NOW()
         WHERE USE_YN        = 'Y'
           AND CATEGORY_DIV_CD = '06'
           AND CATEGORY_NO = #{originalCategoryNo}
           AND POST_NO = #{muWebPhotoPetitId}
    </update>

    <update id="deleteWebPhotoPetit" parameterType="hashmap">
        UPDATE HP_BEFORE_AFTER_PHOTO_BBS
           SET MOD_ID            = #{modId}
             ,MOD_DATE	         = NOW()
             ,USE_YN            = 'N'
         WHERE USE_YN        = 'Y'
           AND CATEGORY_NO = #{categoryno}
           AND POST_NO  = #{muWebPhotoPetitId}
    </update>    
    
    <select id="selectListPhotoCategory" parameterType="hashmap" resultType="hashmap">
        SELECT ROW_NUMBER() OVER (ORDER BY HC.REG_DATE DESC)            AS "rowNum"
              ,HC.CATEGORY_NO	AS "categoryNo"
              ,HC.CATEGORY_NM	AS "categoryNm"
        FROM HP_CATEGORY AS HC
        WHERE HC.USE_YN = 'Y'     
        AND HC.CATEGORY_DIV_CD = '06'
        ORDER BY HC.CATEGORY_NO ASC
    </select>
</mapper>