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

    <select id="selectTotalWebTreatmentDietCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*) AS "totalCount" 
          from (SELECT HCB.POST_NO, HCB.TITLE, HCB.CONTENT
          FROM HP_CONTENTS_BBS AS HCB
          LEFT OUTER JOIN HP_CONTENTS_BBS_PROCEDURE HCBP ON HCB.CATEGORY_DIV_CD = HCBP.CATEGORY_DIV_CD AND HCB.CATEGORY_NO = HCBP.CATEGORY_NO
          LEFT OUTER JOIN HP_CATEGORY HC ON HCB.CATEGORY_DIV_CD = HC.CATEGORY_DIV_CD AND HCB.CATEGORY_NO = HC.CATEGORY_NO
          LEFT OUTER JOIN MU_TREATMENT_PROCEDURE MTP ON HCBP.MU_TREATMENT_PROCEDURE_ID = MTP.MU_TREATMENT_PROCEDURE_ID
         WHERE HCB.USE_YN = 'Y'
         AND HCB.CATEGORY_DIV_CD = '01'
         
         
         <!-- 
        <if test="webTreatmentDietSearchStartDate != null and webTreatmentDietSearchStartDate != '' and webTreatmentDietSearchEndDate != null and webTreatmentDietSearchEndDate != ''">
           AND DATE_FORMAT(HCB.REG_DATE, '%Y-%m-%d') &gt;= #{webTreatmentDietSearchStartDate}
           AND DATE_FORMAT(HCB.REG_DATE, '%Y-%m-%d') &lt;= #{webTreatmentDietSearchEndDate}
        </if>
        -->
        <if test="webTreatmentDietSearchKeywordParam0 != null and webTreatmentDietSearchKeywordParam0 != ''">
           AND (
           (HC.CATEGORY_NM LIKE CONCAT('%',TRIM(#{webTreatmentDietSearchKeywordParam0}),'%'))           
           )
        </if>
        <if test="webTreatmentDietSearchKeywordParam1 != null and webTreatmentDietSearchKeywordParam1 != ''">
           AND (
           (HCB.TITLE LIKE CONCAT('%',TRIM(#{webTreatmentDietSearchKeywordParam1}),'%'))          
           )
        </if>
        <if test="webTreatmentDietSearchKeywordParam2 != null and webTreatmentDietSearchKeywordParam2 != ''">
           AND (
           (IFNULL((SELECT MM.NAME FROM MU_MEMBER AS MM
           		WHERE MM.USE_YN       = 'Y'
                	AND MM.MU_MEMBER_ID = HCB.REG_ID
                    LIMIT 0, 1
               ),'') LIKE CONCAT('%',TRIM(#{webTreatmentDietSearchKeywordParam2}),'%'))
           )
        </if>
        <if test="webTreatmentDietSearchKeywordParam3 != null and webTreatmentDietSearchKeywordParam3 != ''">
           AND (
           (MTP.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{webTreatmentDietSearchKeywordParam3}),'%'))
           )
        </if>
         GROUP BY HCB.POST_NO, HCB.TITLE, HCB.CONTENT) as HCB
    </select>

    <select id="selectListWebTreatmentDiet" parameterType="hashmap" resultType="hashmap">
        SELECT HCB.*
          FROM (
                SELECT HCB.*
                      ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
                  FROM (
                        SELECT HCB.POST_NO                        AS "muWebTreatmentDietId"
                              ,HCB.TITLE                                  AS "title"
                              ,HCB.CONTENT                                AS "content"
                              ,HC.CATEGORY_NM							  AS "category"
                              ,HC.CATEGORY_NO							  AS "categorylist"
                              ,HCB.THUMBNAIL_BOTTOM_TXT					  AS "thumbnail"
                              ,HCB.HASHTAG					  AS "hashtag"
                              ,CONCAT(DATE_FORMAT(HCB.REG_DATE, '%Y-%m-%d')
                                     ,' ~ '
                                     ,DATE_FORMAT(HCB.REG_DATE, '%Y-%m-%d'))  AS "treatmentdietDate"
                              ,DATE_FORMAT(HCB.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 = HCB.REG_ID
                                        LIMIT 0, 1
                                      ),'')                                  AS "writeName"
                          FROM HP_CONTENTS_BBS HCB
                          LEFT OUTER JOIN HP_CATEGORY HC ON HCB.CATEGORY_DIV_CD = HC.CATEGORY_DIV_CD AND HCB.CATEGORY_NO = HC.CATEGORY_NO
                          LEFT OUTER JOIN HP_CONTENTS_BBS_PROCEDURE HCBP ON HCB.CATEGORY_DIV_CD = HCBP.CATEGORY_DIV_CD AND HCB.CATEGORY_NO = HCBP.CATEGORY_NO
                          LEFT OUTER JOIN MU_TREATMENT_PROCEDURE MTP ON HCBP.MU_TREATMENT_PROCEDURE_ID = MTP.MU_TREATMENT_PROCEDURE_ID
                         WHERE HCB.USE_YN = 'Y'
                         AND HCB.CATEGORY_DIV_CD = '01'
                         <!-- 
                           <if test="webTreatmentDietSearchStartDate != null and webTreatmentDietSearchStartDate != '' and webTreatmentDietSearchEndDate != null and webTreatmentDietSearchEndDate != ''">
                           AND DATE_FORMAT(HCB.REG_DATE, '%Y-%m-%d') &gt;= #{webTreatmentDietSearchStartDate}
                           AND DATE_FORMAT(HCB.REG_DATE, '%Y-%m-%d') &lt;= #{webTreatmentDietSearchEndDate}
                           </if>
                          -->
                           <if test="webTreatmentDietSearchKeywordParam0 != null and webTreatmentDietSearchKeywordParam0 != ''">
					           AND (
					           (HC.CATEGORY_NM LIKE CONCAT('%',TRIM(#{webTreatmentDietSearchKeywordParam0}),'%'))           
					           )
					        </if>
					        <if test="webTreatmentDietSearchKeywordParam1 != null and webTreatmentDietSearchKeywordParam1 != ''">
					           AND (
					           (HCB.TITLE LIKE CONCAT('%',TRIM(#{webTreatmentDietSearchKeywordParam1}),'%'))          
					           )
					        </if>
					        <if test="webTreatmentDietSearchKeywordParam2 != null and webTreatmentDietSearchKeywordParam2 != ''">
					           AND (
					           (IFNULL((SELECT MM.NAME FROM MU_MEMBER AS MM
					           		WHERE MM.USE_YN       = 'Y'
					                	AND MM.MU_MEMBER_ID = HCB.REG_ID
					                    LIMIT 0, 1
					               ),'') LIKE CONCAT('%',TRIM(#{webTreatmentDietSearchKeywordParam2}),'%'))
					           )
					        </if>
					        <if test="webTreatmentDietSearchKeywordParam3 != null and webTreatmentDietSearchKeywordParam3 != ''">
					           AND (
					           (MTP.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{webTreatmentDietSearchKeywordParam3}),'%'))
					           )
					        </if>
                      GROUP BY muWebTreatmentDietId, title, content
                           <choose>
                           <when test="webTreatmentDietSort != null and webTreatmentDietSort != ''">
                      ORDER BY ${webTreatmentDietSort}
                           </when>
                           <otherwise>
                      ORDER BY HCB.REG_DATE DESC
                           </otherwise>
                           </choose>
                         LIMIT 18446744073709551615
                      
                ) HCB, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) HCB
        WHERE 1 = 1
        LIMIT ${webTreatmentDietStart}, ${webTreatmentDietLimit}
    </select>

    <select id="selectWebTreatmentDiet" parameterType="hashmap" resultType="hashmap">
        SELECT HCB.POST_NO                              AS "muWebTreatmentDietId"
        	 ,HCB.CATEGORY_NO                                   AS "categorylist"
             ,HCB.TITLE                                         AS "title"
             ,HCB.CONTENT                                       AS "content"
             ,HCB.HASHTAG						                AS "hashtag"
             ,HCB.THUMBNAIL_BOTTOM_TXT						    AS "thumbnail_bottom_txt"
             ,HAF2.FILE_PATH						    AS "file"
             ,HAF.FILE_PATH						    AS "content_file"             
          FROM HP_CONTENTS_BBS AS HCB
          LEFT OUTER JOIN HP_ATTACH_FILE HAF ON HAF.ATTACHFILE_ID = HCB.CONTENTS_ATTACHFILE_ID
          LEFT OUTER JOIN HP_ATTACH_FILE HAF2 ON HAF2.ATTACHFILE_ID = HCB.THUMBNAIL_ATTACHFILE_ID
         WHERE HCB.USE_YN        = 'Y'
           AND HCB.CATEGORY_DIV_CD = '01'
           AND HCB.CATEGORY_NO = #{categorylist}
           AND HCB.POST_NO  = #{muWebTreatmentDietId}
         LIMIT 0, 1
    </select>
    
    <select id="selectWebTreatmentListDiet" parameterType="hashmap" resultType="hashmap">
        SELECT HCBP.POST_NO                              AS "muWebTreatmentDietId"
        	 ,HCBP.CATEGORY_NO                           AS "categorylist"
             ,HCBP.MU_TREATMENT_ID                       AS "treatmentid"
             ,HCBP.MU_TREATMENT_PROCEDURE_ID             AS "treatmentprocedureid"
             ,MTP.TREATMENT_PROCEDURE_NAME				 AS "treatmentnm"   
             ,MTPP.PRICE								AS "price"
             ,IFNULL(MTPP.DISCOUNT_PRICE, 0)			AS "discountprice"
          FROM HP_CONTENTS_BBS_PROCEDURE AS HCBP
          LEFT OUTER JOIN MU_TREATMENT_PROCEDURE MTP ON HCBP.MU_TREATMENT_ID = MTP.MU_TREATMENT_ID AND HCBP.MU_TREATMENT_PROCEDURE_ID = MTP.MU_TREATMENT_PROCEDURE_ID
          LEFT OUTER JOIN MU_TREATMENT_PROCEDURE_PRICE MTPP ON MTP.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
         WHERE HCBP.USE_YN        = 'Y'
         AND HCBP.CATEGORY_DIV_CD = '01'
         AND HCBP.CATEGORY_NO  = #{categorylist}
         AND HCBP.POST_NO  = #{muWebTreatmentDietId}         
    </select>
    
    <insert id="insertWebTreatmentDiet" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT (MAX(POST_NO) + 1) FROM HP_CONTENTS_BBS WHERE CATEGORY_DIV_CD = '01' AND CATEGORY_NO = ${categorylist}
        </selectKey>
        INSERT INTO HP_CONTENTS_BBS(
                    CATEGORY_DIV_CD
                    ,CATEGORY_NO
                    ,POST_NO
                    ,TITLE
                    ,CONTENT
                    ,THUMBNAIL_BOTTOM_TXT
                  	,THUMBNAIL_ATTACHFILE_ID
                  	,CONTENTS_ATTACHFILE_ID
                    ,HASHTAG                    
                    ,USE_YN
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
        )VALUES(
        			'01'
        			,${categorylist}
                    ,#{id}
                    ,#{title}
                    ,#{content}
                    ,#{thumbnail_bottom_txt}
                    ,#{thumbnailId}
                    ,#{contentId}
                    ,#{hashtag}
                    ,'Y'
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
        )
    </insert>
    
    <insert id="insertWebTreatmentDietContentFile" parameterType="hashmap">
    	<selectKey resultType="string" keyProperty="contentId" 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(
        			 #{contentId}
                    ,#{fileName2}
                    ,#{filePath2}
                    ,'0'
                    ,#{fileType2}
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
        )
        
    </insert>
    
    <insert id="insertWebTreatmentDietThumbnailFile" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="thumbnailId" 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(
        			 #{thumbnailId}
                    ,#{fileName}
                    ,#{filePath}
                    ,'0'
                    ,#{fileType}
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
        )
    </insert>
    
    <update id="updateWebTreatmentListDiet" parameterType="hashmap">
    	UPDATE HP_CONTENTS_BBS_PROCEDURE SET
    		USE_YN = 'N'
    		WHERE CATEGORY_DIV_CD = '01'
    		AND CATEGORY_NO = #{categoryNo}
    		AND POST_NO = #{postNo}
    </update>
    
    <insert id="insertWebTreatmentListDiet" parameterType="hashmap">        	    		
        INSERT INTO HP_CONTENTS_BBS_PROCEDURE(
                    CATEGORY_DIV_CD
                    ,CATEGORY_NO
                    ,POST_NO
                    ,MU_TREATMENT_ID
                    ,MU_TREATMENT_PROCEDURE_ID      
                    ,USE_YN
                    ,REG_ID
                    ,REG_DATE
                    ,MOD_ID
                    ,MOD_DATE
        )VALUES(
        			'01'
        			,#{categoryNo}
        			,#{muWebTreatmentDietId}
                    ,#{treatmentid}
                    ,#{treatmentprocedureid}
                    ,'Y'
                    ,#{regId}
                    ,NOW()
                    ,#{modId}
                    ,NOW()
        )ON DUPLICATE KEY UPDATE
		    USE_YN = 'Y';
    </insert>

    <update id="updateWebTreatmentDiet" parameterType="hashmap">
        UPDATE HP_CONTENTS_BBS
           SET 
             TITLE               = #{title}
             ,CONTENT             = #{content}
             ,THUMBNAIL_BOTTOM_TXT             = #{thumbnail_bottom_txt}
             ,HASHTAG             = #{hashtag}
             <if test="thumbnailId != null and thumbnailId != ''">
           		,THUMBNAIL_ATTACHFILE_ID             = #{thumbnailId}
        	 </if>
             <if test="contentId != null and contentId != ''">
           		,CONTENTS_ATTACHFILE_ID             = #{contentId}
        	</if>             
             ,MOD_ID             = #{modId}
             ,MOD_DATE             = NOW()
         WHERE USE_YN        = 'Y'
           AND CATEGORY_DIV_CD = '01'
           AND CATEGORY_NO = #{categoryNo}
           AND POST_NO = #{postNo}
    </update>

    <update id="deleteWebTreatmentDiet" parameterType="hashmap">
        UPDATE HP_CONTENTS_BBS
           SET MOD_ID            = #{modId}
             ,MOD_DATE	         = NOW()
             ,USE_YN            = 'N'
         WHERE USE_YN        = 'Y'
           AND CATEGORY_NO = #{categorylist}
           AND POST_NO  = #{muWebTreatmentDietId}
    </update>
    
    <select id="selectTotalTreatmentCount" parameterType="hashmap" resultType="Integer">
        SELECT COUNT(*)                         AS "totalCount"
          FROM MU_TREATMENT_PROCEDURE AS MTP
         WHERE MTP.USE_YN = 'Y'
         AND MTP.ENABLE_YN = 'Y'
        <if test="userSearchKeywordParam != null and userSearchKeywordParam != ''">
           AND (MTP.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{userSearchKeywordParam}),'%'))
        </if>
    </select>

    <select id="selectListTreatment" parameterType="hashmap" resultType="hashmap">
        SELECT ROW_NUMBER() OVER (ORDER BY MTP.REG_DATE DESC)            AS "rowNum"
              ,MTP.MU_TREATMENT_PROCEDURE_ID							AS "treatmentProcedureId"
              ,MTP.MU_TREATMENT_ID										AS "treatmentId"
              ,MTP.TREATMENT_PROCEDURE_NAME								AS "treatmentNm"
              ,MTPP.PRICE												AS "price"
              ,IFNULL(MTPP.DISCOUNT_PRICE, 0)							AS "discountPrice"
        FROM MU_TREATMENT_PROCEDURE AS MTP
        LEFT OUTER JOIN MU_TREATMENT_PROCEDURE_PRICE MTPP ON MTP.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
        WHERE MTP.USE_YN = 'Y'
        AND MTPP.USE_YN  = 'Y'
        AND MTP.ENABLE_YN = 'Y'
        <if test="userSearchKeywordParam != null and userSearchKeywordParam != ''">
        	AND (MTP.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{userSearchKeywordParam}),'%'))
        </if>
        <choose>
        <when test="userSort != null and userSort != ''">
    ORDER BY ${userSort}
       </when>
       <otherwise>
    ORDER BY MTP.REG_DATE DESC
       </otherwise>
       </choose>
        <if test="userStart != null and userStart != ''">
            LIMIT ${userStart}
            <if test="userLimit != null and userLimit != ''">
            ,${userLimit}
            </if>
        </if>
    </select>
    
    <select id="selectListTreatmentCategory" 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 = '01'
        ORDER BY HC.CATEGORY_NO ASC
    </select>
</mapper>