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

    <select id="getAppointmentCnt" resultType="Integer">
        /** AppointmentSql.getAppointmentCnt **/
        SELECT COUNT(*) as TOTAL_COUNT
          FROM (SELECT 1
                          FROM MU_RESERVE A, MU_TREATMENT_PROCEDURE_PRICE B
						 WHERE A.MU_TREATMENT_PROCEDURE_ID = B.MU_TREATMENT_PROCEDURE_ID
						   AND A.RESERVE_HOMEPAGE_YN = 'Y'
                         <where>
                         	<if test="@org.springframework.util.StringUtils@hasLength(userName)">
                         		AND A.USER_NAME LIKE CONCAT('%',#{userName},'%')
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(treatmentName)">
                         		AND A.TREATMENT_NAME LIKE CONCAT('%', #{treatmentName}, '%')
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(treatmentProcedureName)">
                         		AND A.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%', #{treatmentProcedureName}, '%')
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(phoneNumber)">
                         		AND A.PHONE_NUMBER = CONCAT('%', #{phoneNumber}, '%')
                         	</if>
                         	
                         </where>
			    ) total	
    </select>

    <select id="getAppointmentList" parameterType="hashmap" resultType="hashmap">
        /** AppointmentSql.getAppointmentList **/
        SELECT R.*
          FROM (
                SELECT R.*
                      ,CAST(@RNUM:=@RNUM + 1 AS CHAR) as ROW_NUM
                  FROM (
                        SELECT A.MU_RESERVE_ID
						     , A.USER_NAME
						     , A.RESERVE_DATE
						     , A.RESERVE_TIME
						     , A.MU_TREATMENT_ID
						     , A.TREATMENT_NAME
						     , A.MU_TREATMENT_PROCEDURE_ID
						     , A.TREATMENT_PROCEDURE_NAME
						     , A.PHONE_NUMBER
						     , B.PRICE
						     , B.VAT
						     , B.DISCOUNT_PRICE
						  FROM MU_RESERVE A, MU_TREATMENT_PROCEDURE_PRICE B
						 WHERE A.MU_TREATMENT_PROCEDURE_ID = B.MU_TREATMENT_PROCEDURE_ID
						   AND A.RESERVE_HOMEPAGE_YN = 'Y'
                         <where>
                         	<if test="@org.springframework.util.StringUtils@hasLength(userName)">
                         		AND A.USER_NAME LIKE CONCAT('%',#{userName},'%')
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(treatmentName)">
                         		AND A.TREATMENT_NAME LIKE CONCAT('%', #{treatmentName}, '%')
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(treatmentProcedureName)">
                         		AND A.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%', #{treatmentProcedureName}, '%')
                         	</if>
                         	<if test="@org.springframework.util.StringUtils@hasLength(phoneNumber)">
                         		AND A.PHONE_NUMBER = CONCAT('%', #{phoneNumber}, '%')
                         	</if>
                         	
                         </where> 
                         
                           <choose>
                           <when test="@org.springframework.util.StringUtils@hasLength(gridSort)">
                      ORDER BY ${gridSort}
                           </when>
                           <otherwise>
                      ORDER BY REG_DATE
                           </otherwise>
                           </choose>
                         LIMIT 18446744073709551615
                ) R, (SELECT @RNUM:=0) R
                WHERE 1 = 1
        ) R
        WHERE 1 = 1
        LIMIT ${gridLimitStart}, ${gridLimitEnd}
    </select>
    
</mapper>