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

    <select id="getFeedbackReceptionCnt" resultType="Integer">
        /** FeedbackReceptionSql.getFeedbackReceptionCnt **/
        SELECT COUNT(*) as TOTAL_COUNT
          FROM (SELECT POST_NO
                             , BRANCH_OFFICE_CD
                             , MB_NAME
                             , MB_HP
                             , OPINION_CLASSIFICATION_CD
                             , CONTENT
                             , REG_DATE
                          FROM madeu.HP_CMP_CPL_BBS
                         <where>
                         	<if test="@org.springframework.util.StringUtils@hasLength(branchOfficeCd)">
                         		AND BRANCH_OFFICE_CD = #{branchOfficeCd}
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(mbName)">
                         		AND MB_NAME LIKE CONCAT('%', #{mbName}, '%')
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(mbHp)">
                         		AND MB_HP LIKE CONCAT('%', #{mbHp}, '%')
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(opinionClassificationCd)">
                         		AND OPINION_CLASSIFICATION_CD = #{opinionClassificationCd}
                         	</if>
                         	
                         </where>
			    ) total	
    </select>

    <select id="getFeedbackReceptionList" parameterType="hashmap" resultType="hashmap">
        /** FeedbackReceptionSql.getFeedbackReceptionList **/
        SELECT HCCB.*
          FROM (
                SELECT HCCB.*
                      ,CAST(@RNUM:=@RNUM + 1 AS CHAR) as ROW_NUM
                  FROM (
                        SELECT A.POST_NO
                             , A.BRANCH_OFFICE_CD
                             , A.MB_NAME
                             , A.MB_HP
                             , A.OPINION_CLASSIFICATION_CD
                             , A.CONTENT
                             , DATE_FORMAT(A.REG_DATE, '%Y-%m-%d %H:%i') AS REG_DATE
                             , DATE_FORMAT(B.REG_DATE, '%Y-%m-%d %H:%i') AS REPLY_REG_DATE
                             , (SELECT NAME FROM madeu.MU_MEMBER WHERE MU_MEMBER_ID = B.REG_ID) AS REPLY_REG_NM
                             , B.CONTENT AS REPLY_CONTENT
                          FROM madeu.HP_CMP_CPL_BBS A 
                          LEFT OUTER JOIN madeu.HP_CMP_CPL_BBS_REPLY B
                            ON A.POST_NO = B.POST_NO
                            AND B.REPLY_NO = (
									              SELECT MAX(REPLY_NO) 
									              FROM madeu.HP_CMP_CPL_BBS_REPLY 
									              WHERE POST_NO = A.POST_NO
									          )
                         <where>
                         	<if test="@org.springframework.util.StringUtils@hasLength(branchOfficeCd)">
                         		AND A.BRANCH_OFFICE_CD = #{branchOfficeCd}
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(mbName)">
                         		AND A.MB_NAME LIKE CONCAT('%', #{mbName}, '%')
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(mbHp)">
                         		AND A.MB_HP LIKE CONCAT('%', #{mbHp}, '%')
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(opinionClassificationCd)">
                         		AND A.OPINION_CLASSIFICATION_CD = #{opinionClassificationCd}
                         	</if>
                         	
                         </where> 
                         
                           <choose>
                           <when test="@org.springframework.util.StringUtils@hasLength(gridSort)">
                      ORDER BY ${gridSort}
                           </when>
                           <otherwise>
                      ORDER BY REG_DATE
                           </otherwise>
                           </choose>
                         LIMIT 18446744073709551615
                ) HCCB, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) HCCB
        WHERE 1 = 1
        LIMIT ${gridLimitStart}, ${gridLimitEnd}
    </select>
    
    <select id="getFeedbackReception" parameterType="hashmap" resultType="hashmap">
        /** FeedbackReceptionSql.getFeedbackReception **/        
        SELECT POST_NO
	         , BRANCH_OFFICE_CD
	         , MB_NAME
	         , MB_HP
	         , OPINION_CLASSIFICATION_CD
	         , CONTENT
	         , DATE_FORMAT(REG_DATE, '%Y-%m-%d %H:%i') AS REG_DATE
	      FROM madeu.HP_CMP_CPL_BBS
	     WHERE POST_NO = #{postNo}                         
    </select>
    
    
    <insert id="putFeedbackReception" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="postNo" order="BEFORE">
            SELECT NVL(MAX(POST_NO),0) + 1 FROM HP_CMP_CPL_BBS
        </selectKey>
        /** FeedbackReceptionSql.putFeedbackReception **/
	    INSERT INTO HP_CMP_CPL_BBS(
	        POST_NO,
	        BRANCH_OFFICE_CD,
	        MB_NAME,
	        MB_HP,
	        OPINION_CLASSIFICATION_CD,
	        CONTENT,
	        REG_DATE
	    ) VALUES (
	        #{postNo},
	        #{branchOfficeCd},
	        #{mbName},
	        #{mbHp},
	        #{opinionClassificationCd},
	        #{content},
	        NOW()
        )
    </insert>
    
    <update id="modFeedbackReception" parameterType="hashmap">
        /** FeedbackReceptionSql.modFeedbackReception **/
	    UPDATE HP_CMP_CPL_BBS
	       SET BRANCH_OFFICE_CD = #{branchOfficeCd}
	          ,MB_NAME = #{mbName}
	          ,MB_HP = #{mbHp}
	          ,OPINION_CLASSIFICATION_CD = #{opinionClassificationCd}
	          ,CONTENT = #{content}
	     WHERE POST_NO = #{postNo}
    </update>
    
    <insert id="putFeedbackReceptionReply" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="replyNo" order="BEFORE">
            SELECT NVL(MAX(REPLY_NO),0) + 1 FROM HP_CMP_CPL_BBS_REPLY WHERE POST_NO = #{postNo}
        </selectKey>
        /** FeedbackReceptionSql.putFeedbackReceptionReply **/
	    INSERT INTO HP_CMP_CPL_BBS_REPLY(
	        POST_NO,
	        REG_ID,
	        REG_DATE,
	        REPLY_NO,
	        CONTENT,
	        MSG_SEND_YN
	    ) VALUES (
	        #{postNo},
	        #{loginMemberId},
	        NOW(),
	        #{replyNo},
	        #{replyContent},
	        #{msgSendYn}
        )
    </insert>
</mapper>