<?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="WebReserve">
	
	<select id="selectTotalReserveStatusCount" parameterType="hashmap" resultType="hashmap">
		SELECT SUM(T_TOTAL_COUNT)    AS "tTotalCount"
			  ,SUM(R_TOTAL_COUNT)    AS "rTotalCount"
			  ,SUM(P_TOTAL_COUNT)    AS "pTotalCount"
			  ,SUM(G_TOTAL_COUNT)    AS "gTotalCount"
              ,SUM(TN_TOTAL_COUNT)   AS "tnTotalCount"
		  FROM (
				SELECT COUNT(*)      AS T_TOTAL_COUNT
					  ,0             AS R_TOTAL_COUNT
					  ,0             AS P_TOTAL_COUNT
					  ,0             AS G_TOTAL_COUNT
					  ,0             AS TN_TOTAL_COUNT
				  FROM MU_RESERVE AS MR
				 WHERE MR.USE_YN = 'Y'
				   AND MR.STATUS = 'T'
				   AND MR.RESERVE_DATE = #{searchDate}
				 UNION ALL
				SELECT 0             AS T_TOTAL_COUNT
					  ,COUNT(*)      AS R_TOTAL_COUNT
					  ,0             AS P_TOTAL_COUNT
					  ,0             AS G_TOTAL_COUNT
					  ,0             AS TN_TOTAL_COUNT
				  FROM MU_RESERVE AS MR
				 WHERE MR.USE_YN = 'Y'
				   AND MR.STATUS = 'R'
				   AND MR.RESERVE_DATE = #{searchDate}
				 UNION ALL
				SELECT 0             AS T_TOTAL_COUNT
					  ,0             AS R_TOTAL_COUNT
					  ,COUNT(*)      AS P_TOTAL_COUNT
					  ,0             AS G_TOTAL_COUNT
					  ,0             AS TN_TOTAL_COUNT
				  FROM (
						SELECT MP.PAYMENT_NUMBER                 AS PAYMENT_NUMBER
							  ,GROUP_CONCAT(DISTINCT MP.STATUS)  AS STATUS
						  FROM MU_PAYMENT AS MP
					INNER JOIN (
								SELECT MP.PAYMENT_NUMBER
									  ,MAX(MP.REG_DATE) AS MAX_DATE
								  FROM MU_PAYMENT AS MP
								 WHERE MP.USE_YN = 'Y'
								   AND MP.WRITE_DATE = #{searchDate}
							  GROUP BY MP.PAYMENT_NUMBER
						  ) AS MP2
							ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
						   AND MP.REG_DATE = MP2.MAX_DATE
					  GROUP BY MP.PAYMENT_NUMBER
				  ) AS MP
				 WHERE MP.STATUS IN ('NY', 'Y', 'PY', 'CY')
				   UNION ALL
				SELECT 0             AS T_TOTAL_COUNT
					  ,0             AS R_TOTAL_COUNT
					  ,0             AS P_TOTAL_COUNT
					  ,COUNT(*)      AS G_TOTAL_COUNT
					  ,0             AS TN_TOTAL_COUNT
				 FROM MU_RESERVE AS MR
				 WHERE MR.USE_YN  = 'Y'
				   AND MR.STATUS = 'G'
				   AND MR.RESERVE_DATE = #{searchDate}
				   UNION ALL
				SELECT 0             AS T_TOTAL_COUNT
					  ,0             AS R_TOTAL_COUNT
					  ,0             AS P_TOTAL_COUNT
					  ,0             AS G_TOTAL_COUNT
					  ,COUNT(*)      AS TN_TOTAL_COUNT
				 FROM MU_RESERVE AS MR
				 WHERE MR.USE_YN  = 'Y'
				   AND MR.STATUS = 'TN'
				   AND MR.RESERVE_DATE = #{searchDate}
		  ) AS MR
		 WHERE 1 = 1
	</select>
	
	<select id="selectTotalReserveStatusTTreatmentCount" parameterType="hashmap" resultType="hashmap">
		SELECT CASE
					WHEN MR.TREATMENT_NAME IN ('피부', '제모', '쁘띠', '비만')
						 THEN MR.TREATMENT_NAME
					ELSE '기타'
			   END                                 AS "treatmentName"
			  ,COUNT(*)                       	   AS "totalCount"
		  FROM MU_RESERVE AS MR
		 WHERE MR.USE_YN  = 'Y'
		   AND MR.STATUS  = 'T'
		   AND DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d') = #{searchDate}
	  GROUP BY treatmentName
	</select>

	<select id="selectTotalReserveStatusRTreatmentCount" parameterType="hashmap" resultType="hashmap">
		SELECT CASE
					WHEN MR.TREATMENT_NAME IN ('피부', '제모', '쁘띠', '비만')
						 THEN MR.TREATMENT_NAME
					ELSE '기타'
			   END                                 AS "treatmentName"
			  ,COUNT(*)                       	   AS "totalCount"
		  FROM MU_RESERVE AS MR
		 WHERE MR.USE_YN  = 'Y'
		   AND MR.STATUS  = 'R'
		   AND DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d') = #{searchDate}
	  GROUP BY treatmentName
	</select>

	<select id="selectTotalReserveStatusYTreatmentCount" parameterType="hashmap" resultType="hashmap">
		SELECT CASE
					WHEN MR.TREATMENT_NAME IN ('피부', '제모', '쁘띠', '비만')
						 THEN MR.TREATMENT_NAME
					ELSE '기타'
			   END                                 AS "treatmentName"
			  ,COUNT(*)                       	   AS "totalCount"
		  FROM MU_RESERVE AS MR
	INNER JOIN (
                SELECT MPD.MU_RESERVE_ID
                  FROM (
                        SELECT MP.PAYMENT_NUMBER                 AS PAYMENT_NUMBER
                              ,GROUP_CONCAT(DISTINCT MP.STATUS)  AS STATUS
                          FROM MU_PAYMENT AS MP
                    INNER JOIN (
                                SELECT MP.PAYMENT_NUMBER
                                      ,MAX(MP.REG_DATE) AS MAX_DATE
                                  FROM MU_PAYMENT AS MP
                                 WHERE MP.USE_YN = 'Y'
                                   AND MP.WRITE_DATE = #{searchDate}
                              GROUP BY MP.PAYMENT_NUMBER
                          ) AS MP2
                            ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                           AND MP.REG_DATE = MP2.MAX_DATE
                      GROUP BY MP.PAYMENT_NUMBER
                  ) AS MP
            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                    ON MP.PAYMENT_NUMBER = MPD.PAYMENT_NUMBER
                   AND MP.STATUS IN ('NY', 'Y', 'PY', 'CY')
              GROUP BY MPD.MU_RESERVE_ID
          ) AS MP
		    ON MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
		   AND MR.USE_YN  = 'Y'
	  GROUP BY treatmentName
	</select>

	<select id="selectListReserveTime" parameterType="hashmap" resultType="hashmap">
		WITH RECURSIVE T_TEMP_DATES AS (
			SELECT DATE_FORMAT(CONCAT(#{searchDate}, ' ', #{openStartTime}), '%Y-%m-%d %H:%i') AS RESULT_DATE_TIME
			UNION
			SELECT DATE_FORMAT(DATE_ADD(T_TEMP_DATES.RESULT_DATE_TIME, INTERVAL 30 MINUTE), '%Y-%m-%d %H:%i')  AS RESULT_DATE_TIME
			  FROM T_TEMP_DATES 
			 WHERE DATE_FORMAT(DATE_ADD(T_TEMP_DATES.RESULT_DATE_TIME, INTERVAL 30 MINUTE), '%Y-%m-%d %H:%i') &lt;= CONCAT(#{searchDate}, ' ', #{openEndTime})
		)
		SELECT DATE_FORMAT(RESULT_DATE_TIME, '%Y-%m-%d')  AS "resultDate"
			  ,DATE_FORMAT(RESULT_DATE_TIME, '%H:%i')     AS "resultTime"
		  FROM T_TEMP_DATES
	  ORDER BY RESULT_DATE_TIME;
	</select>
	
	<select id="selectListReserveMember" parameterType="hashmap" resultType="hashmap">
		SELECT MU_MEMBER_ID                                   AS "muMemberId"
			  ,NAME                                           AS "memberName"
			  ,MU_GROUP_ID                                    AS "muGroupId"
			  ,IFNULL((SELECT MG.GROUP_NAME
						 FROM MU_GROUP AS MG
						WHERE MG.USE_YN      = 'Y'
			 			  AND MG.MU_GROUP_ID = MM.MU_GROUP_ID
			 			LIMIT 0, 1
			   ),'')                                          AS "groupName"
			  ,MU_DUTY_ID                                     AS "muDutyId"
			  ,IFNULL((SELECT MD.DUTY_NAME
						 FROM MU_DUTY AS MD
						WHERE MD.USE_YN      = 'Y'
			 			  AND MD.MU_DUTY_ID = MM.MU_DUTY_ID
			 			LIMIT 0, 1
			   ),'')                                          AS "dutyName"
			  ,MM.TYPE                                        AS "type"
			  ,CASE MM.TYPE
					WHEN 'D'  THEN '닥터'
					WHEN 'A'  THEN '상담'
					ELSE ''
			   END                                            AS "typeName"
		  FROM (
				SELECT MM.MU_MEMBER_ID
					  ,MM.NAME
					  ,MM.MU_GROUP_ID
					  ,MM.MU_DUTY_ID
					  ,MM.TYPE
					  ,MM.ORDER_NUMBER
				  FROM MU_MEMBER AS MM
				  JOIN MU_GROUP MG
					ON MM.MU_GROUP_ID =MG.MU_GROUP_ID
			 LEFT JOIN MU_TREATMENT_PROCEDURE_DOCTOR MTPD
					ON MM.MU_MEMBER_ID = MTPD.MU_MEMBER_ID
				   AND MTPD.USE_YN='Y'
			 LEFT JOIN MU_TREATMENT_PROCEDURE MTP
					ON MTP.MU_TREATMENT_PROCEDURE_ID = MTPD.MU_TREATMENT_PROCEDURE_ID
				   AND MTP.USE_YN='Y'
			 LEFT JOIN MU_TREATMENT MT
					ON MT.MU_TREATMENT_ID = MTP.MU_TREATMENT_ID
				   AND MT.USE_YN='Y'
				 WHERE MM.USE_YN = 'Y'
				   AND MM.STATUS = 'Y'
				   AND MM.TYPE IN ('D', 'A')
				   AND MM.MU_MEMBER_ID NOT IN (SELECT MA.MU_MEMBER_ID
												 FROM MU_ATTENDANCE AS MA
												WHERE MA.USE_YN           = 'Y'
												  AND MA.STATUS          != 'D'
												  AND MA.ATTENDANCE_TYPE IN ('C', 'D')
												  AND DATE_FORMAT(MA.START_DATE, '%Y-%m-%d') &lt;= #{searchDate}
												  AND DATE_FORMAT(MA.END_DATE, '%Y-%m-%d') &gt;= #{searchDate})
				   <if test="muTreatmentId != null and muTreatmentId != ''">
				   AND MG.GROUP_NAME IN (SELECT TREATMENT_NAME
											FROM MU_TREATMENT
										   WHERE MU_TREATMENT_ID IN (${muTreatmentIdList}))
				   </if>
				   <if test="muTreatmentProcedureId != null and muTreatmentProcedureId != ''">
				   AND MTP.MU_TREATMENT_PROCEDURE_ID IN (${muTreatmentProcedureIdList})
				   </if>
				   <if test="muMemberId != null and muMemberId != ''">
				   AND MM.MU_MEMBER_ID IN (${muMemberIdList})
				   </if>
			 <!-- 상담자 -->
				 UNION
				SELECT MM.MU_MEMBER_ID
					  ,MM.NAME
					  ,MM.MU_GROUP_ID
					  ,MM.MU_DUTY_ID
					  ,MM.TYPE
					  ,MM.ORDER_NUMBER
				  FROM MU_MEMBER AS MM
				  JOIN MU_GROUP MG
					ON MM.MU_GROUP_ID =MG.MU_GROUP_ID
				 WHERE MM.USE_YN   = 'Y'
				   AND MM.STATUS   = 'Y'
				   AND MM.TYPE     = 'A'
				   AND MM.MU_MEMBER_ID NOT IN (SELECT MA.MU_MEMBER_ID
												 FROM MU_ATTENDANCE AS MA
												WHERE MA.USE_YN           = 'Y'
												  AND MA.STATUS          != 'D'
												  AND MA.ATTENDANCE_TYPE IN ('C', 'D')
												  AND DATE_FORMAT(MA.START_DATE, '%Y-%m-%d') &lt;= #{searchDate}
												  AND DATE_FORMAT(MA.END_DATE, '%Y-%m-%d') &gt;= #{searchDate})

				   <if test="muTreatmentId != null and muTreatmentId != ''">
				   AND MG.GROUP_NAME IN (SELECT TREATMENT_NAME
											FROM MU_TREATMENT
										   WHERE MU_TREATMENT_ID IN (${muTreatmentIdList}))
				   </if>
				   <if test="muTreatmentProcedureId != null and muTreatmentProcedureId != ''">
				   AND MM.MU_GROUP_ID IN (SELECT MU_GROUP_ID
											FROM MU_GROUP MG
										   WHERE MG.USE_YN='Y'
											 AND MG.GROUP_NAME IN (SELECT TREATMENT_NAME
																	 FROM MU_TREATMENT AS MT
																	 JOIN MU_TREATMENT_PROCEDURE MTP
																	   ON MT.MU_TREATMENT_ID=MTP.MU_TREATMENT_ID
																	  AND MT.USE_YN                     = 'Y'
																	  AND MTP.MU_TREATMENT_PROCEDURE_ID IN (${muTreatmentProcedureIdList})))
				   </if>
				   <if test="muMemberId != null and muMemberId != ''">
				   AND MM.MU_MEMBER_ID IN (${muMemberIdList})
				   </if>
			  GROUP BY MM.MU_MEMBER_ID
		  ) AS MM
		 WHERE 1=1
	  ORDER BY TYPE DESC
	          ,ORDER_NUMBER ASC
	</select>

	<select id="selectListReserveApp" parameterType="hashmap" resultType="hashmap">
		SELECT MR.MU_RESERVE_ID                            AS "muReserveId"
			  ,MR.USER_NAME                                AS "userName"
			  ,MR.PHONE_NUMBER                             AS "phoneNumber"
			  ,MR.MU_USER_ID                               AS "muUserId"
			  ,IFNULL(MR.BIRTHDAY, '')                     AS "birthday"
			  ,IFNULL((SELECT MCIT.FILE_PATH
						 FROM MU_CATEGORY_ITEM AS MCIT
						WHERE MCIT.USE_YN              = 'Y'
						  AND MCIT.MU_CATEGORY_ITEM_ID = MR.USER_TYPE
						LIMIT 0, 1
			   ), '')                                      AS "userType"
			  ,MR.USER_TYPE2                               AS "userType2"
			  ,DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d')     AS "reserveDate"
			  ,DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')       AS "reserveTime"
			  ,MR.RESERVE_CATEGORY_ITEM_ID                 AS "reserveCategoryItemId"
			  ,MR.RESERVE_CATEGORY_ITEM_NAME               AS "reserveCategoryItemName"
			  ,MR.RESERVE_APP_YN               			   AS "reserveAppYn"
			  ,MR.MU_TREATMENT_ID                          AS "muTreatmentId"
			  ,MR.TREATMENT_NAME                           AS "treatmentName"
			  ,MR.MU_TREATMENT_PROCEDURE_ID                AS "muTreatmentProcedureId"
			  ,MR.TREATMENT_PROCEDURE_NAME                 AS "treatmentProcedureName"
			  ,IFNULL(MR.ETC, '')                          AS "etc"
			  ,MR.STATUS                                   AS "status"
			  ,MR.STATUS2                                  AS "status2"
			  ,IFNULL((SELECT MU.ETC
						 FROM MU_USER AS MU
						WHERE MU.MU_USER_ID = MR.MU_USER_ID
						LIMIT 0, 1
			   ), '')                                      AS "userEtc"
			  ,MR.VISIT_YN                                 AS "visitYn"
			  ,MR.RESERVE_APP_YN                           AS "reserveAppYn"
			  ,MR.REFUSE_PHOTO_YN                          AS "refusePhotoYn"
		      ,TEMP.RESULT_TIME                            AS "searchTime"
		  FROM (
				WITH RECURSIVE T_TEMP_DATES AS (
					SELECT DATE_FORMAT(CONCAT(#{searchDate}, ' ', #{openStartTime}), '%Y-%m-%d %H:%i') AS RESULT_DATE_TIME
					UNION
					SELECT DATE_FORMAT(DATE_ADD(T_TEMP_DATES.RESULT_DATE_TIME, INTERVAL 30 MINUTE), '%Y-%m-%d %H:%i')  AS RESULT_DATE_TIME
					  FROM T_TEMP_DATES
					 WHERE DATE_FORMAT(DATE_ADD(T_TEMP_DATES.RESULT_DATE_TIME, INTERVAL 30 MINUTE), '%Y-%m-%d %H:%i') &lt;= CONCAT(#{searchDate}, ' ', #{openEndTime})
				)
				SELECT DATE_FORMAT(RESULT_DATE_TIME, '%Y-%m-%d')  AS RESULT_DATE
					  ,DATE_FORMAT(RESULT_DATE_TIME, '%H:%i')     AS RESULT_TIME
				  FROM T_TEMP_DATES
			  ORDER BY RESULT_DATE_TIME
		  ) AS TEMP
	INNER JOIN (
				SELECT MR.MU_RESERVE_ID
					  ,MR.USER_NAME
					  ,MR.PHONE_NUMBER
					  ,MR.MU_USER_ID
					  ,MU.BIRTHDAY
					  ,MU.USER_TYPE
					  ,MU.USER_TYPE2
					  ,MR.RESERVE_DATE
					  ,MR.RESERVE_TIME
					  ,MR.RESERVE_CATEGORY_ITEM_ID
					  ,MR.RESERVE_CATEGORY_ITEM_NAME
					  ,MR.MU_TREATMENT_ID
					  ,MR.TREATMENT_NAME
					  ,MR.MU_TREATMENT_PROCEDURE_ID
					  ,MR.TREATMENT_PROCEDURE_NAME
					  ,MR.ETC
					  ,MR.STATUS
					  ,MR.STATUS2
					  ,MR.RESERVE_APP_YN
					  ,CASE WHEN IFNULL((SELECT COUNT(*)
										   FROM MU_RESERVE AS MR2
										  WHERE MR2.USE_YN  = 'Y'
											AND MR2.STATUS  = 'Y'
											AND DATE_FORMAT(MR2.RESERVE_DATE,'%Y-%m-%d') &lt; #{searchDate}
											AND MR2.MU_USER_ID = MR.MU_USER_ID
								 ), 0) > 0
							  OR IFNULL((SELECT COUNT(*)
										   FROM MU_PAYMENT_DETAIL AS MPD
										   JOIN MU_RESERVE AS MR3
											 ON MPD.MU_RESERVE_ID = MR3.MU_RESERVE_ID
										  WHERE MPD.USE_YN = 'Y'
											AND MR3.USE_YN = 'Y'
											AND DATE_FORMAT(MR3.RESERVE_DATE, '%Y-%m-%d') &lt;= #{searchDate}
											AND MR3.MU_USER_ID = MR.MU_USER_ID
								), 0) > 0
								 THEN 'Y'
							ELSE 'N'
					   END                                        AS VISIT_YN
					  ,MU.REFUSE_PHOTO_YN                         AS REFUSE_PHOTO_YN
				  FROM MU_RESERVE AS MR
			 LEFT JOIN MU_USER AS MU
					ON MR.MU_USER_ID = MU.MU_USER_ID
				   AND MU.USE_YN = 'Y'
				 WHERE MR.USE_YN = 'Y'
				   AND MR.STATUS IN ('T', 'R', 'G', 'Y')
				   AND RESERVE_APP_YN = 'Y'
				   AND RESERVE_MEMBER_ID IS NULL
				   <if test="muTreatmentId != null and muTreatmentId != ''">
				   AND MR.MU_TREATMENT_ID                      = #{muTreatmentId}
				   </if>
				   <if test="muTreatmentProcedureId != null and muTreatmentProcedureId != ''">
				   AND MR.MU_TREATMENT_PROCEDURE_ID            = #{muTreatmentProcedureId}
				   </if>
	 ) AS MR
			ON TEMP.RESULT_DATE = MR.RESERVE_DATE
		   AND TEMP.RESULT_TIME = MR.RESERVE_TIME
		<choose>
		 <when test="reserveReserveVisit eq 'A'.toString()">
		   AND VISIT_YN = 'N'
		 </when>
		 <when test="reserveReserveVisit eq 'B'.toString()">
		   AND VISIT_YN = 'Y'
		 </when>
		 <otherwise>
		 </otherwise>
		</choose>



		<!--SELECT MR.MU_RESERVE_ID                            AS "muReserveId"
			  ,MR.USER_NAME                                AS "userName"
			  ,MR.PHONE_NUMBER                             AS "phoneNumber"
			  ,MR.MU_USER_ID                               AS "muUserId"
			  ,IFNULL(MR.BIRTHDAY, '')                     AS "birthday"
			  ,IFNULL((SELECT MCIT.FILE_PATH
						 FROM MU_CATEGORY_ITEM AS MCIT
						WHERE MCIT.USE_YN              = 'Y'
						  AND MCIT.MU_CATEGORY_ITEM_ID = MR.USER_TYPE
						LIMIT 0, 1
			   ), '')                                      AS "userType"
			  ,MR.USER_TYPE2                               AS "userType2"
			  ,DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d')     AS "reserveDate"
			  ,DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')       AS "reserveTime"
			  ,MR.RESERVE_CATEGORY_ITEM_ID                 AS "reserveCategoryItemId"
			  ,MR.RESERVE_CATEGORY_ITEM_NAME               AS "reserveCategoryItemName"
			  ,MR.RESERVE_APP_YN               			   AS "reserveAppYn"
			  ,MR.MU_TREATMENT_ID                          AS "muTreatmentId"
			  ,MR.TREATMENT_NAME                           AS "treatmentName"
			  ,MR.MU_TREATMENT_PROCEDURE_ID                AS "muTreatmentProcedureId"
			  ,MR.TREATMENT_PROCEDURE_NAME                 AS "treatmentProcedureName"
			  ,IFNULL(MR.ETC, '')                          AS "etc"
			  ,MR.STATUS                                   AS "status"
			  ,MR.STATUS2                                  AS "status2"
			  ,IFNULL((SELECT MU.ETC
						 FROM MU_USER AS MU
						WHERE MU.MU_USER_ID = MR.MU_USER_ID
						LIMIT 0, 1
			   ), '')                                      AS "userEtc"
			  ,MR.VISIT_YN                                 AS "visitYn"
		      ,MR.RESERVE_APP_YN                           AS "reserveAppYn"
		      ,MR.REFUSE_PHOTO_YN                          AS "refusePhotoYn"
		  FROM (
					SELECT MR.MU_RESERVE_ID
						  ,MR.USER_NAME
						  ,MR.PHONE_NUMBER
						  ,MR.MU_USER_ID
						  ,MU.BIRTHDAY
						  ,MU.USER_TYPE
						  ,MU.USER_TYPE2
						  ,MR.RESERVE_DATE
						  ,MR.RESERVE_TIME
						  ,MR.RESERVE_CATEGORY_ITEM_ID
						  ,MR.RESERVE_CATEGORY_ITEM_NAME
						  ,MR.MU_TREATMENT_ID
						  ,MR.TREATMENT_NAME
						  ,MR.MU_TREATMENT_PROCEDURE_ID
						  ,MR.TREATMENT_PROCEDURE_NAME
						  ,MR.ETC
						  ,MR.STATUS
						  ,MR.STATUS2
						  ,MR.RESERVE_APP_YN
						  ,CASE WHEN IFNULL((SELECT COUNT(*)
											   FROM MU_RESERVE AS MR2
											  WHERE MR2.USE_YN  = 'Y'
												AND MR2.STATUS  = 'Y'
												AND DATE_FORMAT(MR2.RESERVE_DATE,'%Y-%m-%d') &lt; #{searchDate}
												AND MR2.MU_USER_ID = MR.MU_USER_ID
									 ), 0) > 0
								  OR IFNULL((SELECT COUNT(*)
                                               FROM MU_PAYMENT_DETAIL AS MPD
                                               JOIN MU_RESERVE AS MR3
                                               	 ON MPD.MU_RESERVE_ID = MR3.MU_RESERVE_ID
                                              WHERE MPD.USE_YN = 'Y'
                                                AND MR3.USE_YN = 'Y'
                                                AND DATE_FORMAT(MR3.RESERVE_DATE, '%Y-%m-%d') &lt;= #{searchDate}
												AND MR3.MU_USER_ID = MR.MU_USER_ID
                                    ), 0) > 0
									 THEN 'Y'
								ELSE 'N'
						   END                                        AS VISIT_YN
					      ,MU.REFUSE_PHOTO_YN                         AS REFUSE_PHOTO_YN
					  FROM MU_RESERVE AS MR
				LEFT OUTER JOIN MU_USER AS MU
						ON MR.MU_USER_ID = MU.MU_USER_ID
					   AND MU.USE_YN = 'Y'
					 WHERE MR.USE_YN = 'Y'
					   AND MR.STATUS IN ('T', 'R', 'G', 'Y')
					   AND RESERVE_APP_YN = 'Y'
					   AND RESERVE_MEMBER_ID IS NULL
					   AND DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d') = #{searchDate}
					   AND DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')   = #{searchTime}
					   <if test="muTreatmentId != null and muTreatmentId != ''">
					   AND MR.MU_TREATMENT_ID                      = #{muTreatmentId}
					   </if>
					   <if test="muTreatmentProcedureId != null and muTreatmentProcedureId != ''">
					   AND MR.MU_TREATMENT_PROCEDURE_ID            = #{muTreatmentProcedureId}
					   </if>
			   ) AS MR
		 WHERE 1=1
		<choose>
		 <when test="reserveReserveVisit eq 'A'.toString()">
		   AND VISIT_YN = 'N'
		 </when>
		 <when test="reserveReserveVisit eq 'B'.toString()">
		   AND VISIT_YN = 'Y'
		 </when>
		 <otherwise>
		 </otherwise>
		</choose>-->
	</select>
	
	<select id="selectListReserve" parameterType="hashmap" resultType="hashmap">
		SELECT MR.MU_RESERVE_ID                            AS "muReserveId"
			  ,MR.USER_NAME                                AS "userName"
			  ,MR.PHONE_NUMBER                             AS "phoneNumber"
			  ,MR.MU_USER_ID                               AS "muUserId"
			  ,IFNULL(MR.BIRTHDAY, '')                     AS "birthday"
			  ,IFNULL((SELECT MCIT.FILE_PATH
						 FROM MU_CATEGORY_ITEM AS MCIT
						WHERE MCIT.USE_YN              = 'Y'
						  AND MCIT.MU_CATEGORY_ITEM_ID = MR.USER_TYPE
						LIMIT 0, 1
			   ), '')                                      AS "userType"
			  ,MR.USER_TYPE2                               AS "userType2"
			  ,DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d')     AS "reserveDate"
			  ,DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')       AS "reserveTime"
			  ,MR.RESERVE_CATEGORY_ITEM_ID                 AS "reserveCategoryItemId"
			  ,MR.RESERVE_CATEGORY_ITEM_NAME               AS "reserveCategoryItemName"
			  ,MR.MU_TREATMENT_ID                          AS "muTreatmentId"
			  ,MR.TREATMENT_NAME                           AS "treatmentName"
			  ,MR.MU_TREATMENT_PROCEDURE_ID                AS "muTreatmentProcedureId"
			  ,MR.TREATMENT_PROCEDURE_NAME                 AS "treatmentProcedureName"
			  ,MR.RESERVE_MEMBER_ID                        AS "reserveMemberId"
			  ,MR.MU_MEMBER_ID                             AS "muMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MR.MU_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                      AS "memberName"
			  ,IFNULL(MR.ETC, '')                          AS "etc"
			  ,MR.STATUS                                   AS "status"
			  ,MR.STATUS2                                  AS "status2"
			  ,IFNULL((SELECT MU.ETC
						 FROM MU_USER AS MU
						WHERE MU.MU_USER_ID = MR.MU_USER_ID
						LIMIT 0, 1
			   ), '')                                      AS "userEtc"
			  ,MR.VISIT_YN                                 AS "visitYn"
			  ,MR.RESERVE_APP_YN                           AS "reserveAppYn"
			  ,MR.REFUSE_PHOTO_YN                          AS "refusePhotoYn"
		      ,TEMP.RESULT_TIME                            AS "searchTime"
		  FROM (
				WITH RECURSIVE T_TEMP_DATES AS (
					SELECT DATE_FORMAT(CONCAT(#{searchDate}, ' ', #{openStartTime}), '%Y-%m-%d %H:%i') AS RESULT_DATE_TIME
					UNION
					SELECT DATE_FORMAT(DATE_ADD(T_TEMP_DATES.RESULT_DATE_TIME, INTERVAL 30 MINUTE), '%Y-%m-%d %H:%i')  AS RESULT_DATE_TIME
					  FROM T_TEMP_DATES
					 WHERE DATE_FORMAT(DATE_ADD(T_TEMP_DATES.RESULT_DATE_TIME, INTERVAL 30 MINUTE), '%Y-%m-%d %H:%i') &lt;= CONCAT(#{searchDate}, ' ', #{openEndTime})
				)
				SELECT DATE_FORMAT(RESULT_DATE_TIME, '%Y-%m-%d')  AS RESULT_DATE
					  ,DATE_FORMAT(RESULT_DATE_TIME, '%H:%i')     AS RESULT_TIME
				  FROM T_TEMP_DATES
			  ORDER BY RESULT_DATE_TIME
		  ) AS TEMP
	INNER JOIN (
				SELECT MR.MU_RESERVE_ID
					  ,MR.USER_NAME
					  ,MR.PHONE_NUMBER
					  ,MR.MU_USER_ID
					  ,IFNULL(MU.BIRTHDAY, '')  AS BIRTHDAY
					  ,MU.USER_TYPE
					  ,MU.USER_TYPE2
					  ,MR.RESERVE_DATE
					  ,MR.RESERVE_TIME
					  ,MR.RESERVE_CATEGORY_ITEM_ID
					  ,MR.RESERVE_CATEGORY_ITEM_NAME
					  ,MR.MU_TREATMENT_ID
					  ,MR.TREATMENT_NAME
					  ,MR.MU_TREATMENT_PROCEDURE_ID
					  ,MR.TREATMENT_PROCEDURE_NAME
					  ,MR.RESERVE_MEMBER_ID
					  ,MR.MU_MEMBER_ID
					  ,MR.ETC
					  ,MR.STATUS
					  ,MR.STATUS2
					  ,MR.RESERVE_APP_YN
					  ,CASE WHEN IFNULL((SELECT COUNT(*)
										   FROM MU_RESERVE AS MR2
										  WHERE MR2.USE_YN  = 'Y'
											AND MR2.STATUS  = 'Y'
											AND DATE_FORMAT(MR2.RESERVE_DATE,'%Y-%m-%d') &lt; #{searchDate}
											AND MR2.MU_USER_ID = MR.MU_USER_ID
								 ), 0) > 0
							  OR IFNULL((SELECT COUNT(*)
										   FROM MU_PAYMENT_DETAIL AS MPD
										   JOIN MU_RESERVE AS MR3
											 ON MPD.MU_RESERVE_ID = MR3.MU_RESERVE_ID
										  WHERE MPD.USE_YN = 'Y'
											AND MR3.USE_YN = 'Y'
											AND DATE_FORMAT(MR3.RESERVE_DATE, '%Y-%m-%d') &lt;= #{searchDate}
											AND MR3.MU_USER_ID = MR.MU_USER_ID
								), 0) > 0
								 THEN 'Y'
							ELSE 'N'
					   END                                        AS VISIT_YN
					  ,MU.REFUSE_PHOTO_YN                         AS REFUSE_PHOTO_YN
				  FROM MU_RESERVE AS MR
			 LEFT JOIN MU_USER AS MU
					ON MR.MU_USER_ID = MU.MU_USER_ID
				   AND MU.USE_YN = 'Y'
				 WHERE MR.USE_YN = 'Y'
				   AND MR.STATUS IN ('T', 'R', 'G', 'Y')
				   AND MR.CRM_MEMBER_SCHEDULE_PID IS NULL
	 ) AS MR
			ON TEMP.RESULT_DATE = MR.RESERVE_DATE
		   AND TEMP.RESULT_TIME = MR.RESERVE_TIME
		   AND MR.RESERVE_MEMBER_ID = #{searchMemberId}
		   <if test="muTreatmentId != null and muTreatmentId != ''">
		   AND MR.MU_TREATMENT_ID                      IN (${muTreatmentIdList})
		   </if>
		   <if test="muTreatmentProcedureId != null and muTreatmentProcedureId != ''">
		   AND MR.MU_TREATMENT_PROCEDURE_ID            IN (${muTreatmentProcedureIdList})
		   </if>
		   <if test="muMemberId != null and muMemberId != ''">
		   AND MR.RESERVE_MEMBER_ID                    IN (${muMemberIdList})
		   </if>
		<choose>
		 <when test="reserveReserveVisit eq 'A'.toString()">
		   AND VISIT_YN = 'N'
		 </when>
		 <when test="reserveReserveVisit eq 'B'.toString()">
		   AND VISIT_YN = 'Y'
		 </when>
		 <otherwise>
		 </otherwise>
		</choose>
    
    
    




		<!-- SELECT MR.MU_RESERVE_ID                            AS "muReserveId"
			  ,MR.USER_NAME                                AS "userName"
			  ,MR.PHONE_NUMBER                             AS "phoneNumber"
			  ,MR.MU_USER_ID                               AS "muUserId"
			  ,IFNULL(MR.BIRTHDAY, '')                     AS "birthday"
			  ,IFNULL((SELECT MCIT.FILE_PATH
						 FROM MU_CATEGORY_ITEM AS MCIT
						WHERE MCIT.USE_YN              = 'Y'
						  AND MCIT.MU_CATEGORY_ITEM_ID = MR.USER_TYPE
						LIMIT 0, 1
			   ), '')                                      AS "userType"
			  ,MR.USER_TYPE2                               AS "userType2"
			  ,DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d')     AS "reserveDate"
			  ,DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')       AS "reserveTime"
			  ,MR.RESERVE_CATEGORY_ITEM_ID                 AS "reserveCategoryItemId"
			  ,MR.RESERVE_CATEGORY_ITEM_NAME               AS "reserveCategoryItemName"
			  ,MR.MU_TREATMENT_ID                          AS "muTreatmentId"
			  ,MR.TREATMENT_NAME                           AS "treatmentName"
			  ,MR.MU_TREATMENT_PROCEDURE_ID                AS "muTreatmentProcedureId"
			  ,MR.TREATMENT_PROCEDURE_NAME                 AS "treatmentProcedureName"
			  ,MR.RESERVE_MEMBER_ID                        AS "reserveMemberId"
			  ,MR.MU_MEMBER_ID                             AS "muMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MR.MU_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                      AS "memberName"
			  ,IFNULL(MR.ETC, '')                          AS "etc"
			  ,MR.STATUS                                   AS "status"
			  ,MR.STATUS2                                  AS "status2"
			  ,IFNULL((SELECT MU.ETC
						 FROM MU_USER AS MU
						WHERE MU.MU_USER_ID = MR.MU_USER_ID
						LIMIT 0, 1
			   ), '')                                      AS "userEtc"
			  ,MR.VISIT_YN                                 AS "visitYn"
		      ,MR.RESERVE_APP_YN                           AS "reserveAppYn"
		      ,MR.REFUSE_PHOTO_YN                          AS "refusePhotoYn"
		  FROM (
					SELECT MR.MU_RESERVE_ID
						  ,MR.USER_NAME
						  ,MR.PHONE_NUMBER
						  ,MR.MU_USER_ID
						  ,IFNULL(MU.BIRTHDAY, '')  AS BIRTHDAY
						  ,MU.USER_TYPE
						  ,MU.USER_TYPE2
						  ,MR.RESERVE_DATE
						  ,MR.RESERVE_TIME
						  ,MR.RESERVE_CATEGORY_ITEM_ID
						  ,MR.RESERVE_CATEGORY_ITEM_NAME
						  ,MR.MU_TREATMENT_ID
						  ,MR.TREATMENT_NAME
						  ,MR.MU_TREATMENT_PROCEDURE_ID
						  ,MR.TREATMENT_PROCEDURE_NAME
						  ,MR.RESERVE_MEMBER_ID
						  ,MR.MU_MEMBER_ID
						  ,MR.ETC
						  ,MR.STATUS
						  ,MR.STATUS2
						  ,MR.RESERVE_APP_YN
						  ,CASE WHEN IFNULL((SELECT COUNT(*)
											   FROM MU_RESERVE AS MR2
											  WHERE MR2.USE_YN  = 'Y'
												AND MR2.STATUS  = 'Y'
												AND DATE_FORMAT(MR2.RESERVE_DATE,'%Y-%m-%d') &lt; #{searchDate}
												AND MR2.MU_USER_ID = MR.MU_USER_ID
									 ), 0) > 0
								  OR IFNULL((SELECT COUNT(*)
                                               FROM MU_PAYMENT_DETAIL AS MPD
                                               JOIN MU_RESERVE AS MR3
                                               	 ON MPD.MU_RESERVE_ID = MR3.MU_RESERVE_ID
                                              WHERE MPD.USE_YN = 'Y'
                                                AND MR3.USE_YN = 'Y'
                                                AND DATE_FORMAT(MR3.RESERVE_DATE, '%Y-%m-%d') &lt;= #{searchDate}
												AND MR3.MU_USER_ID = MR.MU_USER_ID
                                    ), 0) > 0
									 THEN 'Y'
								ELSE 'N'
						   END                                        AS VISIT_YN
					      ,MU.REFUSE_PHOTO_YN                         AS REFUSE_PHOTO_YN
					  FROM MU_RESERVE AS MR
				LEFT OUTER JOIN MU_USER AS MU
						ON MR.MU_USER_ID = MU.MU_USER_ID
					   AND MU.USE_YN = 'Y'
					 WHERE MR.USE_YN = 'Y'
					   AND MR.STATUS IN ('T', 'R', 'G', 'Y')
					   AND MR.CRM_MEMBER_SCHEDULE_PID IS NULL
					   AND DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d') = #{searchDate}
					   AND DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')   = #{searchTime}
					   AND MR.RESERVE_MEMBER_ID                    = #{searchMemberId}
					   <if test="muTreatmentId != null and muTreatmentId != ''">
					   AND MR.MU_TREATMENT_ID                      IN (${muTreatmentIdList})
					   </if>
					   <if test="muTreatmentProcedureId != null and muTreatmentProcedureId != ''">
					   AND MR.MU_TREATMENT_PROCEDURE_ID            IN (${muTreatmentProcedureIdList})
					   </if>
					   <if test="muMemberId != null and muMemberId != ''">
					   AND MR.RESERVE_MEMBER_ID                    IN (${muMemberIdList})
					   </if>
			   ) AS MR
		 WHERE 1=1
		<choose>
		 <when test="reserveReserveVisit eq 'A'.toString()">
		   AND VISIT_YN = 'N'
		 </when>
		 <when test="reserveReserveVisit eq 'B'.toString()">
		   AND VISIT_YN = 'Y'
		 </when>
		 <otherwise>
		 </otherwise>
		</choose> -->
	</select>
	
	<select id="selectListReserveUser" parameterType="hashmap" resultType="hashmap">
		SELECT MR.USER_NAME                                AS "userName"
			  ,MR.PHONE_NUMBER                             AS "phoneNumber"
			  ,MU.MU_USER_ID                               AS "muUserId"
			  ,IFNULL((SELECT MU.BIRTHDAY
						 FROM MU_USER AS MU
						WHERE MU.MU_USER_ID = MR.MU_USER_ID
						LIMIT 0, 1
			   ), '')                                      AS "birthday"
			  ,IFNULL((SELECT MCIT.FILE_PATH
						 FROM MU_CATEGORY_ITEM AS MCIT
						WHERE MCIT.USE_YN              = 'Y'
						  AND MCIT.MU_CATEGORY_ITEM_ID = MU.USER_TYPE
						LIMIT 0, 1
			   ), '')                                      AS "userType"
			  ,MU.USER_TYPE2                               AS "userType2"
		  FROM (
					SELECT MU_RESERVE_ID
					  	  ,MR.USER_NAME
						  ,MR.PHONE_NUMBER
						  ,MR.MU_USER_ID
						  ,MIN(MR.RESERVE_TIME)   AS RESERVE_TIME
						  ,MIN(MR.RECEPTION_DATE) AS RECEPTION_DATE
						  ,MIN(MR.REG_DATE)       AS REG_DATE
					  FROM MU_RESERVE AS MR
					 WHERE MR.USE_YN                               = 'Y'
					   AND MR.STATUS                               = #{status}
					   AND DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d') = #{searchDate}
					   <if test="searchKeywordParam != null and searchKeywordParam != ''">
					   AND MR.USER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%')
					   </if>
					   <if test="muTreatmentId != null and muTreatmentId != ''">
					   AND MR.MU_TREATMENT_ID                      = #{muTreatmentId}
					   </if>
				  GROUP BY MR.USER_NAME
				  		  ,MR.MU_USER_ID 
			   ) AS MR
	LEFT OUTER JOIN MU_USER AS MU
			ON MR.MU_USER_ID = MU.MU_USER_ID
		   AND MU.USE_YN = 'Y'
		 WHERE 1 = 1
	  ORDER BY MR.RESERVE_TIME ASC, MR.REG_DATE ASC,MU_RESERVE_ID ASC
	</select>
	
	<select id="selectListReserveUserHistory" parameterType="hashmap" resultType="hashmap">
		SELECT ROW_NUMBER()
		    	OVER (ORDER BY MR.ORDER_NUMBER ASC
							 , MR.RESERVE_TIME ASC)     AS "rowNum"
		      ,MU_RESERVE_ID                            AS "muReserveId"
			  ,USER_NAME                                AS "userName"
			  ,PHONE_NUMBER                             AS "phoneNumber"
			  ,MU_USER_ID                               AS "muUserId"
			  ,DATE_FORMAT(RESERVE_DATE,'%Y-%m-%d')     AS "reserveDate"
			  ,DATE_FORMAT(RESERVE_TIME, '%H:%i')       AS "reserveTime"
			  ,RESERVE_CATEGORY_ITEM_ID                 AS "reserveCategoryItemId"
			  ,RESERVE_CATEGORY_ITEM_NAME               AS "reserveCategoryItemName"
			  ,MU_TREATMENT_ID                          AS "muTreatmentId"
			  ,TREATMENT_NAME                           AS "treatmentName"
			  ,MU_TREATMENT_PROCEDURE_ID                AS "muTreatmentProcedureId"
			  ,TREATMENT_PROCEDURE_NAME                 AS "treatmentProcedureName"
			  ,RESERVE_MEMBER_ID                        AS "reserveMemberId"
			  ,MU_MEMBER_ID                             AS "muMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MR.MU_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                   AS "memberName"
			  ,IFNULL(ETC, '')                          AS "etc"
			  ,STATUS                                   AS "status"
			  ,STATUS2                                  AS "status2"
		      ,CASE WHEN IFNULL((SELECT COUNT(*)
								   FROM MU_RESERVE AS MR2
								  WHERE MR2.USE_YN = 'Y'
									AND MR2.STATUS  = 'Y'
									AND DATE_FORMAT(MR2.RESERVE_DATE,'%Y-%m-%d') &lt; DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d')
									AND MR2.MU_USER_ID = MR.MU_USER_ID
						 ), 0) > 0
						 OR IFNULL((SELECT COUNT(*)
									  FROM MU_PAYMENT_DETAIL AS MPD
									  JOIN MU_RESERVE AS MR3
									    ON MPD.MU_RESERVE_ID = MR3.MU_RESERVE_ID
									 WHERE MPD.USE_YN = 'Y'
									   AND MR3.USE_YN = 'Y'
									   AND DATE_FORMAT(MR3.RESERVE_DATE, '%Y-%m-%d') &lt;= #{searchDate}
									   AND MR3.MU_USER_ID = MR.MU_USER_ID
                                    ), 0) > 0
						 THEN 'Y'
					ELSE 'N'
			   END                                         AS "visitYn"
		  FROM (
					SELECT MR.MU_RESERVE_ID
						  ,MR.USER_NAME
			 			  ,MR.PHONE_NUMBER
						  ,MR.MU_USER_ID
						  ,MR.RESERVE_DATE
						  ,MR.RESERVE_TIME
						  ,MR.RESERVE_CATEGORY_ITEM_ID
						  ,MR.RESERVE_CATEGORY_ITEM_NAME
						  ,MR.MU_TREATMENT_ID
						  ,MR.TREATMENT_NAME
						  ,IFNULL((SELECT MT.ORDER_NUMBER
									 FROM MU_TREATMENT AS MT
									WHERE MT.MU_TREATMENT_ID = MR.MU_TREATMENT_ID
									LIMIT 0, 1
						   ), '')                                    AS ORDER_NUMBER
						  ,MR.MU_TREATMENT_PROCEDURE_ID
						  ,MR.TREATMENT_PROCEDURE_NAME
						  ,MR.RESERVE_MEMBER_ID
						  ,MR.MU_MEMBER_ID
						  ,MR.ETC
						  ,MR.STATUS
						  ,MR.STATUS2
					  FROM MU_RESERVE AS MR
					 WHERE MR.USE_YN                               = 'Y'
					   AND MR.USER_NAME                            = #{userName}
					   AND MR.PHONE_NUMBER                         = #{phoneNumber}
					   AND MR.STATUS                               = #{status}
					   AND DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d') = #{searchDate}
					   AND MR.CRM_MEMBER_SCHEDULE_PID IS NULL
					   <if test="searchKeywordParam != null and searchKeywordParam != ''">
					   AND MR.USER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%')
					   </if>
					   <if test="muTreatmentId != null and muTreatmentId != ''">
					   AND MR.MU_TREATMENT_ID                      = #{muTreatmentId}
					   </if>
			   ) AS MR
		 WHERE 1=1
	  ORDER BY MR.ORDER_NUMBER ASC, MR.RESERVE_TIME ASC
	</select>
	
	<select id="selectListReserveUserHistoryTop5" parameterType="hashmap" resultType="hashmap">
		SELECT MU_RESERVE_ID                            AS "muReserveId"
			  ,USER_NAME                                AS "userName"
			  ,PHONE_NUMBER                             AS "phoneNumber"
			  ,MU_USER_ID                               AS "muUserId"
			  ,DATE_FORMAT(RESERVE_DATE,'%Y-%m-%d')     AS "reserveDate"
			  ,DATE_FORMAT(RESERVE_TIME, '%H:%i')       AS "reserveTime"
			  ,RESERVE_CATEGORY_ITEM_ID                 AS "reserveCategoryItemId"
			  ,RESERVE_CATEGORY_ITEM_NAME               AS "reserveCategoryItemName"
			  ,MU_TREATMENT_ID                          AS "muTreatmentId"
			  ,TREATMENT_NAME                           AS "treatmentName"
			  ,MU_TREATMENT_PROCEDURE_ID                AS "muTreatmentProcedureId"
			  ,TREATMENT_PROCEDURE_NAME                 AS "treatmentProcedureName"
			  ,RESERVE_MEMBER_ID                        AS "reserveMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MR.RESERVE_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                   AS "reserveMemberName"
			  ,MU_MEMBER_ID                             AS "muMemberId"
			  ,IFNULL(ETC, '')                          AS "etc"
			  ,STATUS                                   AS "status"
			  ,STATUS2                                  AS "status2"
		  FROM (
					SELECT MR.MU_RESERVE_ID 
						  ,MR.USER_NAME 
						  ,MR.PHONE_NUMBER
						  ,MR.MU_USER_ID 
						  ,MR.RESERVE_DATE 
						  ,MR.RESERVE_TIME 
						  ,MR.RESERVE_CATEGORY_ITEM_ID 
						  ,MR.RESERVE_CATEGORY_ITEM_NAME 
						  ,MR.MU_TREATMENT_ID 
						  ,MR.TREATMENT_NAME
						  ,IFNULL((SELECT MT.ORDER_NUMBER
									 FROM MU_TREATMENT AS MT
									WHERE MT.MU_TREATMENT_ID = MR.MU_TREATMENT_ID
									LIMIT 0, 1
						   ), '')                                    AS ORDER_NUMBER
						  ,MR.MU_TREATMENT_PROCEDURE_ID 
						  ,MR.TREATMENT_PROCEDURE_NAME 
						  ,MR.RESERVE_MEMBER_ID 
						  ,MR.MU_MEMBER_ID 
						  ,MR.ETC 
						  ,MR.STATUS 
						  ,MR.STATUS2 
					  FROM MU_RESERVE AS MR
					 WHERE MR.USE_YN        = 'Y'
					   AND MR.MU_USER_ID    = #{muUserId}
			   ) AS MR
		 WHERE 1=1
	  ORDER BY MR.ORDER_NUMBER ASC, MR.RESERVE_DATE DESC, MR.RESERVE_TIME DESC
	  	 LIMIT 0, 5
	</select>
	
	<select id="selectTotalReserveUserHistory2Count" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM (
					SELECT MR.MU_RESERVE_ID
						  ,MR.USER_NAME
						  ,MR.PHONE_NUMBER
						  ,MR.MU_USER_ID
						  ,MR.RESERVE_DATE
						  ,MR.RESERVE_TIME
						  ,MR.RESERVE_CATEGORY_ITEM_ID
						  ,MR.RESERVE_CATEGORY_ITEM_NAME
						  ,MR.MU_TREATMENT_ID
						  ,MR.TREATMENT_NAME
						  ,MR.MU_TREATMENT_PROCEDURE_ID
						  ,MR.TREATMENT_PROCEDURE_NAME
						  ,MR.RESERVE_MEMBER_ID
						  ,IFNULL((SELECT MM.NAME
									 FROM MU_MEMBER AS MM
									WHERE MM.MU_MEMBER_ID = MR.RESERVE_MEMBER_ID
									LIMIT 0, 1
						   ), '')                                      AS RESERVE_MEMBER_NAME
						  ,MR.MU_MEMBER_ID
						  ,IFNULL((SELECT MM.NAME
									 FROM MU_MEMBER AS MM
									WHERE MM.MU_MEMBER_ID = MR.MU_MEMBER_ID
									LIMIT 0, 1
						   ), '')                                      AS MEMBER_NAME
						  ,MR.ETC
						  ,MR.STATUS
						  ,MR.STATUS2
					  FROM MU_RESERVE AS MR
					 WHERE MR.USE_YN               = 'Y'
					   AND MR.MU_USER_ID           = #{muUserId}
					   <if test="searchStartDate != null and searchStartDate != '' and searchEndDate != null and searchEndDate != ''">
					   AND DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d') &gt;= #{searchStartDate}
					   AND DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d') &lt;= #{searchEndDate}
					   </if>
					   <if test="muTreatmentId != null and muTreatmentId != ''">
					   AND MR.MU_TREATMENT_ID      = #{muTreatmentId}
					   </if>
					   <if test="muTreatmentProcedureId != null and muTreatmentProcedureId != ''">
					   AND MR.MU_TREATMENT_PROCEDURE_ID  = #{muTreatmentProcedureId}
					   </if>
					   <if test="reserveMemberId != null and reserveMemberId != ''">
					   AND MR.RESERVE_MEMBER_ID    = #{reserveMemberId}
					   </if>
			   ) AS MR
		 WHERE 1=1
		   <if test="searchKeywordParam != null and searchKeywordParam != ''">
		   AND ((RESERVE_MEMBER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%'))
			OR (MEMBER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%')))
		   </if>
	</select>
	
	<select id="selectListReserveUserHistory2" parameterType="hashmap" resultType="hashmap">
		SELECT MR.*
		  FROM (
					SELECT MR.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MU_RESERVE_ID                            AS "muReserveId"
									  ,USER_NAME                                AS "userName"
									  ,PHONE_NUMBER                             AS "phoneNumber"
									  ,MU_USER_ID                               AS "muUserId"
									  ,DATE_FORMAT(RESERVE_DATE,'%Y-%m-%d')     AS "reserveDate"
									  ,DATE_FORMAT(RESERVE_TIME, '%H:%i')       AS "reserveTime"
									  ,RESERVE_CATEGORY_ITEM_ID                 AS "reserveCategoryItemId"
									  ,RESERVE_CATEGORY_ITEM_NAME               AS "reserveCategoryItemName"
									  ,MU_TREATMENT_ID                          AS "muTreatmentId"
									  ,TREATMENT_NAME                           AS "treatmentName"
									  ,MU_TREATMENT_PROCEDURE_ID                AS "muTreatmentProcedureId"
								      ,CASE
		                               WHEN MR.CRM_MEMBER_SCHEDULE_RES_ETC IS NOT NULL
		                                AND MR.CRM_MEMBER_SCHEDULE_RES_ETC != ''
		                               THEN CONCAT(MR.TREATMENT_PROCEDURE_NAME, '/', MR.CRM_MEMBER_SCHEDULE_RES_ETC)
		                               ELSE TREATMENT_PROCEDURE_NAME
		                               END                                      AS "treatmentProcedureName"
									  ,RESERVE_MEMBER_ID                        AS "reserveMemberId"
									  ,RESERVE_MEMBER_NAME                      AS "reserveMemberName"
									  ,MU_MEMBER_ID                             AS "muMemberId"
									  ,MEMBER_NAME                              AS "memberName"
									  ,IFNULL(ETC, '')                          AS "etc"
									  ,STATUS                                   AS "status"
									  ,CASE MR.STATUS
											WHEN 'T'  THEN '예약'
											WHEN 'TN' THEN '예약취소'
											WHEN 'R'  THEN '접수'
											WHEN 'RN' THEN '접수취소'
											WHEN 'G' THEN '진료중'
											WHEN 'Y' THEN '진료완료'
											ELSE ''
									   END                                      AS "statusName"
									  ,STATUS2                                  AS "status2"
									  ,IFNULL((SELECT MU.ETC
												 FROM MU_USER AS MU
												WHERE MU.MU_USER_ID = MR.MU_USER_ID
												LIMIT 0, 1
									   ), '')                                   AS "userEtc"
								  FROM (
											SELECT MR.MU_RESERVE_ID
												  ,MR.USER_NAME
												  ,MR.PHONE_NUMBER
												  ,MR.MU_USER_ID
												  ,MR.RESERVE_DATE
												  ,MR.RESERVE_TIME
												  ,MR.RESERVE_CATEGORY_ITEM_ID
												  ,MR.RESERVE_CATEGORY_ITEM_NAME
												  ,MR.MU_TREATMENT_ID
												  ,MR.TREATMENT_NAME
												  ,MR.MU_TREATMENT_PROCEDURE_ID
												  ,MR.TREATMENT_PROCEDURE_NAME
												  ,MR.CRM_MEMBER_SCHEDULE_RES_ETC
												  ,MR.RESERVE_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.MU_MEMBER_ID = MR.RESERVE_MEMBER_ID
															LIMIT 0, 1
												   ), '')                                      AS RESERVE_MEMBER_NAME
												  ,MR.MU_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.MU_MEMBER_ID = MR.MU_MEMBER_ID
															LIMIT 0, 1
												   ), '')                                      AS MEMBER_NAME
												  ,MR.ETC
												  ,MR.STATUS
												  ,MR.STATUS2
											  FROM MU_RESERVE AS MR
											 WHERE MR.USE_YN               = 'Y'
											   AND MR.MU_USER_ID           = #{muUserId}
											   AND MR.CRM_MEMBER_SCHEDULE_PID IS NULL
											   <if test="searchStartDate != null and searchStartDate != '' and searchEndDate != null and searchEndDate != ''">
											   AND DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d') &gt;= #{searchStartDate}
											   AND DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d') &lt;= #{searchEndDate}
											   </if>
											   <if test="muTreatmentId != null and muTreatmentId != ''">
											   AND MR.MU_TREATMENT_ID      = #{muTreatmentId}
											   </if>
											   <if test="muTreatmentProcedureId != null and muTreatmentProcedureId != ''">
											   AND MR.MU_TREATMENT_PROCEDURE_ID  = #{muTreatmentProcedureId}
											   </if>
											   <if test="reserveMemberId != null and reserveMemberId != ''">
											   AND MR.RESERVE_MEMBER_ID    = #{reserveMemberId}
											   </if>

										 UNION ALL

											SELECT MR.MU_RESERVE_ID
												  ,MR.USER_NAME
												  ,MR.PHONE_NUMBER
												  ,MR.MU_USER_ID
												  ,MR.RESERVE_DATE
												  ,MR.RESERVE_TIME
												  ,MR.RESERVE_CATEGORY_ITEM_ID
												  ,MR.RESERVE_CATEGORY_ITEM_NAME
												  ,MR.MU_TREATMENT_ID
												  ,MR.CRM_MEMBER_SCHEDULE_RES_PID_NAME
												  ,MR.MU_TREATMENT_PROCEDURE_ID
												  ,CASE
                                                   WHEN MR.CRM_MEMBER_SCHEDULE_RES_PID4_NAME IS NOT NULL
                                                   THEN CONCAT(IFNULL(MR.CRM_MEMBER_SCHEDULE_RES_PID2_NAME, ''), '/', IFNULL(MR.CRM_MEMBER_SCHEDULE_RES_PID3_NAME, ''), '/', IFNULL(MR.CRM_MEMBER_SCHEDULE_RES_PID4_NAME, ''))
                                                   WHEN MR.CRM_MEMBER_SCHEDULE_RES_PID3_NAME IS NOT NULL
                                                   THEN CONCAT(IFNULL(MR.CRM_MEMBER_SCHEDULE_RES_PID2_NAME, ''), '/', IFNULL(MR.CRM_MEMBER_SCHEDULE_RES_PID3_NAME, ''))
                                                   WHEN MR.CRM_MEMBER_SCHEDULE_RES_PID2_NAME IS NOT NULL
                                                   THEN CONCAT(IFNULL(MR.CRM_MEMBER_SCHEDULE_RES_PID2_NAME, ''))
                                                   ELSE ''
                                                   END                                         AS TREATMENT_PROCEDURE_NAME
											      ,MR.CRM_MEMBER_SCHEDULE_RES_ETC
												  ,MR.RESERVE_MEMBER_ID
												  ,MR.CRM_MEMBER_SCHEDULE_R_STAFF_3_ICON       AS RESERVE_MEMBER_NAME
												  ,MR.MU_MEMBER_ID
												  ,MR.CRM_MEMBER_SCHEDULE_RES_CONSULT_NAME     AS MEMBER_NAME
												  ,MR.ETC
												  ,MR.STATUS
												  ,MR.STATUS2
											  FROM MU_RESERVE AS MR
											 WHERE MR.USE_YN               = 'Y'
											   AND MR.MU_USER_ID           = #{muUserId}
											   AND MR.CRM_MEMBER_SCHEDULE_PID IS NOT NULL
											   <if test="searchStartDate != null and searchStartDate != '' and searchEndDate != null and searchEndDate != ''">
											   AND DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d') &gt;= #{searchStartDate}
											   AND DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d') &lt;= #{searchEndDate}
											   </if>
											   <if test="muTreatmentId != null and muTreatmentId != ''">
											   AND MR.MU_TREATMENT_ID      = #{muTreatmentId}
											   </if>
											   <if test="muTreatmentProcedureId != null and muTreatmentProcedureId != ''">
											   AND MR.MU_TREATMENT_PROCEDURE_ID  = #{muTreatmentProcedureId}
											   </if>
											   <if test="reserveMemberId != null and reserveMemberId != ''">
											   AND MR.RESERVE_MEMBER_ID    = #{reserveMemberId}
											   </if>
									   ) AS MR
								 WHERE 1=1
								   <if test="searchKeywordParam != null and searchKeywordParam != ''">
								   AND ((RESERVE_MEMBER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%'))
									OR (MEMBER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%')))
								   </if>
								 <choose>
								 <when test="sort != null and sort != ''">
							  ORDER BY ${sort}
								 </when>
								 <otherwise>
							  ORDER BY MR.RESERVE_DATE DESC, MR.RESERVE_TIME DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MR, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MR
		 WHERE 1 = 1
		 LIMIT ${start}, ${limit}
	</select>
	
	<select id="selectListReserveUserHistory2Excel" parameterType="hashmap" resultType="hashmap">
		SELECT MR.*
		  FROM (
					SELECT MR.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MU_RESERVE_ID                            AS "muReserveId"
									  ,USER_NAME                                AS "userName"
									  ,PHONE_NUMBER                             AS "phoneNumber"
									  ,MU_USER_ID                               AS "muUserId"
									  ,DATE_FORMAT(RESERVE_DATE,'%Y-%m-%d')     AS "reserveDate"
									  ,DATE_FORMAT(RESERVE_TIME, '%H:%i')       AS "reserveTime"
									  ,RESERVE_CATEGORY_ITEM_ID                 AS "reserveCategoryItemId"
									  ,RESERVE_CATEGORY_ITEM_NAME               AS "reserveCategoryItemName"
									  ,MU_TREATMENT_ID                          AS "muTreatmentId"
									  ,TREATMENT_NAME                           AS "treatmentName"
									  ,MU_TREATMENT_PROCEDURE_ID                AS "muTreatmentProcedureId"
									  ,TREATMENT_PROCEDURE_NAME                 AS "treatmentProcedureName"
									  ,RESERVE_MEMBER_ID                        AS "reserveMemberId"
									  ,RESERVE_MEMBER_NAME                      AS "reserveMemberName"
									  ,MU_MEMBER_ID                             AS "muMemberId"
									  ,MEMBER_NAME                              AS "memberName"
									  ,IFNULL(ETC, '')                          AS "etc"
									  ,STATUS                                   AS "status"
									  ,CASE MR.STATUS
											WHEN 'T'  THEN '예약'
											WHEN 'TN' THEN '예약취소'
											WHEN 'R'  THEN '접수'
											WHEN 'RN' THEN '접수취소'
											WHEN 'G' THEN '진료중'
											WHEN 'Y' THEN '진료완료'
											ELSE ''
									   END                                      AS "statusName"
									  ,STATUS2                                  AS "status2"
									  ,IFNULL((SELECT MU.ETC
												 FROM MU_USER AS MU
												WHERE MU.MU_USER_ID = MR.MU_USER_ID
												LIMIT 0, 1
									   ), '')                                   AS "userEtc"
								  FROM (
											SELECT MR.MU_RESERVE_ID
												  ,MR.USER_NAME
												  ,MR.PHONE_NUMBER
												  ,MR.MU_USER_ID
												  ,MR.RESERVE_DATE
												  ,MR.RESERVE_TIME
												  ,MR.RESERVE_CATEGORY_ITEM_ID
												  ,MR.RESERVE_CATEGORY_ITEM_NAME
												  ,MR.MU_TREATMENT_ID
												  ,MR.TREATMENT_NAME
												  ,MR.MU_TREATMENT_PROCEDURE_ID
												  ,MR.TREATMENT_PROCEDURE_NAME
												  ,MR.RESERVE_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.MU_MEMBER_ID = MR.RESERVE_MEMBER_ID
															LIMIT 0, 1
												   ), '')                                      AS RESERVE_MEMBER_NAME
												  ,MR.MU_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.MU_MEMBER_ID = MR.MU_MEMBER_ID
															LIMIT 0, 1
												   ), '')                                      AS MEMBER_NAME
												  ,MR.ETC
												  ,MR.STATUS
												  ,MR.STATUS2
											  FROM MU_RESERVE AS MR
											 WHERE MR.USE_YN               = 'Y'
											   AND MR.MU_USER_ID           = #{muUserId}
											   <if test="searchStartDate != null and searchStartDate != '' and searchEndDate != null and searchEndDate != ''">
											   AND DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d') &gt;= #{searchStartDate}
											   AND DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d') &lt;= #{searchEndDate}
											   </if>
											   <if test="muTreatmentId != null and muTreatmentId != ''">
											   AND MR.MU_TREATMENT_ID      = #{muTreatmentId}
											   </if>
											   <if test="muTreatmentProcedureId != null and muTreatmentProcedureId != ''">
											   AND MR.MU_TREATMENT_PROCEDURE_ID  = #{muTreatmentProcedureId}
											   </if>
											   <if test="reserveMemberId != null and reserveMemberId != ''">
											   AND MR.RESERVE_MEMBER_ID    = #{reserveMemberId}
											   </if>
									   ) AS MR
								 WHERE 1=1
								   <if test="searchKeywordParam != null and searchKeywordParam != ''">
								   AND ((RESERVE_MEMBER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%'))
									OR (MEMBER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%')))
								   </if>
								 <choose>
								 <when test="sort != null and sort != ''">
							  ORDER BY ${sort}
								 </when>
								 <otherwise>
							  ORDER BY MR.RESERVE_DATE DESC, MR.RESERVE_TIME DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MR, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MR
		 WHERE 1 = 1
	</select>

	<select id="selectListReserveTodayBatch" parameterType="hashmap" resultType="hashmap">
		SELECT MR.MU_RESERVE_ID                          AS "muReserveId"
			  ,MR.MU_USER_ID                             AS "muUserId"
			  ,MR.USER_NAME                              AS "userName"
			  ,MR.PHONE_NUMBER                           AS "phoneNumber"
			  ,DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d')  AS "reserveDate"
			  ,DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')     AS "reserveTime"
			  ,MR.TREATMENT_NAME                         AS "treatmentName"
			  ,MR.TREATMENT_PROCEDURE_NAME               AS "treatmentProcedureName"
		  FROM MU_RESERVE AS MR
		 WHERE MR.USE_YN = 'Y'
		   AND MR.CRM_MEMBER_SCHEDULE_PID IS NULL
		   AND MR.STATUS = 'T'
		   AND MR.RESERVE_DATE = #{searchDate}
	</select>

	<select id="selectReserve" parameterType="hashmap" resultType="hashmap">
		SELECT MR.MU_RESERVE_ID                            AS "muReserveId"
			  ,MR.USER_NAME                                AS "userName"
			  ,MR.PHONE_NUMBER                             AS "phoneNumber"
			  ,MR.MU_USER_ID                               AS "muUserId"
			  ,IFNULL((SELECT MCIT.FILE_PATH
						 FROM MU_CATEGORY_ITEM AS MCIT
						WHERE MCIT.USE_YN              = 'Y'
						  AND MCIT.MU_CATEGORY_ITEM_ID = MU.USER_TYPE
						LIMIT 0, 1
			   ), '')                                      AS "userType"
			  ,MU.USER_TYPE2                               AS "userType2"
			  ,IFNULL(MU.BIRTHDAY, '')                     AS "birthday"
			  ,CASE MU.GENDER
					WHEN 'M' THEN '남'
					WHEN 'F' THEN '여'
					ELSE ''
			   END                                         AS "gender"
			  ,IFNULL((SELECT MCIT.CATEGORY_ITEM_NAME
						 FROM MU_CATEGORY_ITEM AS MCIT
						WHERE MCIT.USE_YN              = 'Y'
						  AND MCIT.MU_CATEGORY_ITEM_ID = MU.NATIONALITY
						LIMIT 0, 1
			   ), '')                                      AS "nationality"
			  ,IFNULL(MU.ETC, '')                          AS "etc"
			  ,IFNULL(MU.MEMO, '')                         AS "memo"
			  ,DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d')     AS "reserveDate"
			  ,DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')       AS "reserveTime"
			  ,(SELECT CATEGORY_ITEM_CODE
                  FROM MU_CATEGORY_ITEM
                 WHERE MU_CATEGORY_ITEM_ID = MR.RESERVE_CATEGORY_ITEM_ID)
                                                           AS "reserveCategoryItemCode"
			  ,MR.RESERVE_CATEGORY_ITEM_ID                 AS "reserveCategoryItemId"
			  ,MR.RESERVE_CATEGORY_ITEM_NAME               AS "reserveCategoryItemName"
			  ,MR.MU_TREATMENT_ID                          AS "muTreatmentId"
			  ,MR.TREATMENT_NAME                           AS "treatmentName"
			  ,MR.MU_TREATMENT_PROCEDURE_ID                AS "muTreatmentProcedureId"
			  ,MR.TREATMENT_PROCEDURE_NAME                 AS "treatmentProcedureName"
			  ,MR.RESERVE_MEMBER_ID                        AS "reserveMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MR.RESERVE_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                      AS "reserveMemberName"
			  ,MR.MU_MEMBER_ID                             AS "muMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MR.MU_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                      AS "memberName"
			  ,IFNULL(MR.ETC, '')                          AS "reserveEtc"
			  ,MR.STATUS                                   AS "status"
			  ,MR.STATUS2                                  AS "status2"
			  ,CASE WHEN IFNULL((SELECT COUNT(*)
								   FROM MU_RESERVE AS MR2
								  WHERE MR2.USE_YN = 'Y'
									AND MR2.STATUS  = 'Y'
									AND DATE_FORMAT(MR2.RESERVE_DATE,'%Y-%m-%d') &lt; DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d')
									AND MR2.MU_USER_ID = MR.MU_USER_ID
						 ), 0) > 0
						 OR IFNULL((SELECT COUNT(*)
									  FROM MU_PAYMENT_DETAIL AS MPD
									  JOIN MU_RESERVE AS MR3
									    ON MPD.MU_RESERVE_ID = MR3.MU_RESERVE_ID
									 WHERE MPD.USE_YN = 'Y'
									   AND MR3.USE_YN = 'Y'
									   AND DATE_FORMAT(MR3.RESERVE_DATE, '%Y-%m-%d') &lt;= #{searchDate}
									   AND MR3.MU_USER_ID = MR.MU_USER_ID
                                    ), 0) > 0
						 THEN 'Y'
					ELSE 'N'
			   END                                         AS "visitYn"
			  ,IFNULL((SELECT MUCC.MU_USER_CONSULTING_CHART_ID
						 FROM MU_USER_CONSULTING_CHART AS MUCC
						WHERE MUCC.USE_YN        = 'Y'
						  AND MUCC.MU_RESERVE_ID = MR.MU_RESERVE_ID
						  AND MUCC.MU_USER_ID    = MR.MU_USER_ID
						LIMIT 0, 1
			   ), '')                                      AS "muUserConsultingChartId"
			  ,IFNULL((SELECT MUMC.MU_USER_MEDICAL_CHART_ID
						 FROM MU_USER_MEDICAL_CHART AS MUMC
						WHERE MUMC.USE_YN        = 'Y'
						  AND MUMC.MU_RESERVE_ID = MR.MU_RESERVE_ID
						  AND MUMC.MU_USER_ID    = MR.MU_USER_ID
						LIMIT 0, 1
			   ), '')                                      AS "muUserMedicalChartId"
		      ,IFNULL((SELECT CONCAT(MUP.HEIGHT, 'cm, ', MUP.WEIGHT, 'kg, ', MUP.BMI, 'kg/m2')
		                 FROM MU_USER_PHYSICAL AS MUP
		                WHERE MUP.USE_YN = 'Y'
		                  AND MUP.MU_USER_ID = MR.MU_USER_ID
                     ORDER BY MUP.REG_DATE DESC
                        LIMIT 0, 1), '-')                  AS "userPhysical"
		  FROM MU_RESERVE AS MR
	 LEFT JOIN MU_USER AS MU
			ON MR.MU_USER_ID = MU.MU_USER_ID
		   AND MU.USE_YN   = 'Y'
		 WHERE MR.USE_YN         = 'Y'
		   AND MR.MU_RESERVE_ID  = #{muReserveId}
		 LIMIT 0, 1
	</select>
	
	<select id="selectReserveUser" parameterType="hashmap" resultType="hashmap">
		SELECT MR.MU_RESERVE_ID                            AS "muReserveId"
			  ,MR.USER_NAME                                AS "userName"
			  ,MR.PHONE_NUMBER                             AS "phoneNumber"
			  ,IFNULL(MR.MU_USER_ID, '')                   AS "muUserId"
			  ,IFNULL((SELECT MCIT.FILE_PATH
						 FROM MU_CATEGORY_ITEM AS MCIT
						WHERE MCIT.USE_YN              = 'Y'
						  AND MCIT.MU_CATEGORY_ITEM_ID = MU.USER_TYPE
						LIMIT 0, 1
			   ), '')                                      AS "userType"
			  ,MU.USER_TYPE2                               AS "userType2"
			  ,IFNULL(MU.BIRTHDAY, '')                     AS "birthday"
			  ,CASE MU.GENDER
					WHEN 'M' THEN '남'
					WHEN 'F' THEN '여'
			   END                                         AS "gender"
			  ,IFNULL((SELECT MCIT.CATEGORY_ITEM_NAME
						 FROM MU_CATEGORY_ITEM AS MCIT
						WHERE MCIT.USE_YN              = 'Y'
						  AND MCIT.MU_CATEGORY_ITEM_ID = MU.NATIONALITY
						LIMIT 0, 1
			   ), '')                                      AS "nationality"
			  ,IFNULL(MU.ETC, '')                          AS "etc"
			  ,DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d')     AS "reserveDate"
			  ,DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')       AS "reserveTime"
			  ,MR.RESERVE_CATEGORY_ITEM_ID                 AS "reserveCategoryItemId"
			  ,MR.RESERVE_CATEGORY_ITEM_NAME               AS "reserveCategoryItemName"
			  ,MR.MU_TREATMENT_ID                          AS "muTreatmentId"
			  ,MR.TREATMENT_NAME                           AS "treatmentName"
			  ,MR.MU_TREATMENT_PROCEDURE_ID                AS "muTreatmentProcedureId"
			  ,MR.TREATMENT_PROCEDURE_NAME                 AS "treatmentProcedureName"
			  ,MR.RESERVE_MEMBER_ID                        AS "reserveMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MR.RESERVE_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                      AS "reserveMemberName"
			  ,MR.MU_MEMBER_ID                             AS "muMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MR.MU_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                      AS "memberName"
			  ,IFNULL(MR.ETC, '')                          AS "reserveEtc"
			  ,MR.STATUS                                   AS "status"
			  ,MR.STATUS2                                  AS "status2"
		  FROM MU_RESERVE AS MR
	LEFT OUTER JOIN MU_USER AS MU
			ON MR.MU_USER_ID = MU.MU_USER_ID
		   AND MR.USE_YN   = 'Y'
		 WHERE MR.USE_YN = 'Y'
		   AND MR.STATUS IN ('T', 'R', 'G', 'Y')
		   AND MR.MU_USER_ID                           = #{muUserId}
		   AND DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d') = #{reserveDate}
	  ORDER BY MR.RESERVE_DATE ASC, MR.RESERVE_TIME ASC
	</select>
	
	<select id="selectListReserveUserNoPayment" parameterType="hashmap" resultType="hashmap">
		SELECT MU_RESERVE_ID                                                                  AS "muReserveId"
			  ,DATE_FORMAT(RESERVE_DATE,'%Y-%m-%d')                                           AS "reserveDate"
			  ,DATE_FORMAT(RESERVE_TIME, '%H:%i')                                             AS "reserveTime"
			  ,STATUS                                                                         AS "status"
			  ,STATUS2                                                                        AS "status2"
			  ,MU_TREATMENT_ID                                                                AS "muTreatmentId"
			  ,TREATMENT_NAME                                                                 AS "treatmentName"
			  ,MU_TREATMENT_PROCEDURE_ID                                                      AS "muTreatmentProcedureId"
			  ,TREATMENT_PROCEDURE_NAME                                                       AS "treatmentProcedureName"
			  ,RESERVE_MEMBER_ID                                                              AS "reserveMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MR.RESERVE_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                                                         AS "reserveMemberName"
			  ,IFNULL(TREATMENT_PROCEDURE_PRICE, 0)                                           AS "price"
			  ,IFNULL(TREATMENT_PROCEDURE_VAT, 0)                                             AS "vat"
			  ,IFNULL(discountPrice, 0)                                                      AS "discountPrice"
			  ,IFNULL((TREATMENT_PROCEDURE_PRICE+TREATMENT_PROCEDURE_VAT), 0)  AS "totalAmount"
			  ,MU_PAYMENT_ID                                                                  AS "muPaymentId"
              ,TREATMENT_PROCEDURE_PRICE+TREATMENT_PROCEDURE_VAT-discountPrice-PAYMENT_AMOUNT  AS "nonPayment"
              ,paymentStatus
		  FROM (
				SELECT MR.MU_RESERVE_ID
					  ,MR.RESERVE_DATE
					  ,MR.RESERVE_TIME
					  ,MR.STATUS
					  ,MP.STATUS    as paymentStatus
					  ,MR.STATUS2
					  ,MR.MU_TREATMENT_ID
					  ,MR.TREATMENT_NAME
					  ,MR.MU_TREATMENT_PROCEDURE_ID
					  ,MR.TREATMENT_PROCEDURE_NAME
					  ,MR.RESERVE_MEMBER_ID
					  ,MTPP.PRICE                    AS TREATMENT_PROCEDURE_PRICE
					  ,MTPP.VAT                      AS TREATMENT_PROCEDURE_VAT
					  ,SUM(MPD.DISCOUNT_PRICE)       AS discountPrice
					  ,MAX(MP.MU_PAYMENT_ID)         AS MU_PAYMENT_ID
					  ,SUM(IF(MP.PAYMENT_CATEGORY_ITEM_ID='MM',-MPD.MEMBERSHIP_PRICE,MPD.TOTAL_AMOUNT)) 		 AS PAYMENT_AMOUNT
				  FROM MU_RESERVE AS MR
	   		 LEFT JOIN MU_PAYMENT_DETAIL MPD
				    ON MR.MU_RESERVE_ID = MPD.MU_RESERVE_ID
	         LEFT JOIN MU_PAYMENT MP
				    ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
	         LEFT JOIN (
						SELECT MT.MU_TREATMENT_ID
							  ,MTP.MU_TREATMENT_PROCEDURE_ID
							  ,MTPP.PRICE
							  ,MTPP.VAT
						  FROM MU_TREATMENT AS MT
					INNER JOIN MU_TREATMENT_PROCEDURE AS MTP
							ON MT.MU_TREATMENT_ID = MTP.MU_TREATMENT_ID
					INNER JOIN MU_TREATMENT_PROCEDURE_PRICE AS MTPP
							ON MTP.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
						   AND MTPP.USE_YN = 'Y'
	         ) AS MTPP
					ON MR.MU_TREATMENT_ID = MTPP.MU_TREATMENT_ID
				   AND MR.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
				 WHERE MR.USE_YN = 'Y'
				   AND MR.STATUS IN ('T', 'R', 'G', 'Y')
				   AND MR.STATUS2 IN ('N', 'P', 'NP', 'CN')
				   AND MR.MU_USER_ID = #{muUserId}
			  GROUP BY MR.MU_RESERVE_ID
	      ) AS MR
	 	 WHERE 1 = 1
	  ORDER BY MR.RESERVE_DATE DESC
	          ,MR.RESERVE_TIME DESC
	</select>
	
	<select id="selectListReserveUserOption" parameterType="hashmap" resultType="hashmap">
		SELECT MR.MU_RESERVE_ID                                       AS "muReserveId"
			  ,DATE_FORMAT(MR.RESERVE_DATE,'%Y-%m-%d')                AS "reserveDate"
			  ,DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')                  AS "reserveTime"
			  ,MR.MU_USER_ID                                          AS "muUserId"
			  ,MR.USER_NAME                                           AS "userName"
			  ,MR.TREATMENT_NAME                                      AS "treatmentName"
			  ,MR.MU_TREATMENT_ID                                     AS "muTreatmentId"
			  ,MR.TREATMENT_NAME                                      AS "treatmentName"
			  ,MR.MU_TREATMENT_PROCEDURE_ID                           AS "muTreatmentProcedureId"
			  ,MR.TREATMENT_PROCEDURE_NAME                            AS "treatmentProcedureName"
			  ,MR.RESERVE_MEMBER_ID                                   AS "reserveMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MR.RESERVE_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                                 AS "reserveMemberName"
			  ,MP.PAYMENT_AMOUNT                                      AS "totalAmount"
			  ,MP.PAYMENT_CATEGORY_ITEM_NAME                          AS "paymentCategoryItemName"
		  FROM MU_RESERVE MR
	INNER JOIN MU_PAYMENT_DETAIL MPD
            ON MR.MU_RESERVE_ID = MPD.MU_RESERVE_ID
           AND MR.USE_YN     = 'Y'
           AND MR.STATUS2    = 'Y'
           AND MR.MU_USER_ID = #{muUserId}
	INNER JOIN MU_PAYMENT MP
            ON MPD.MU_PAYMENT_ID = MP.MU_PAYMENT_ID
           AND MP.USE_YN     = 'Y'
		   AND MP.STATUS IN ('Y', 'C', 'R')
	  ORDER BY MR.RECEPTION_DATE DESC
	          ,RESERVE_TIME ASC
	</select>
	
	<select id="selectReserveCount" parameterType="hashmap" resultType="Integer">
		SELECT COUNT(1)       AS "totalCount"
		  FROM MU_RESERVE MR
		 WHERE MR.USE_YN     = 'Y'
		   AND MR.STATUS NOT IN ('TN', 'RN')
		   AND MR.RESERVE_DATE                       = #{reserveDate}
		   AND DATE_FORMAT(MR.RESERVE_TIME, '%H:%i') = #{reserveTime}
	</select>

	<select id="selectListReserveYn" parameterType="hashmap" resultType="hashmap">
		SELECT MR.MU_RESERVE_ID        AS "muReserveId"
		  FROM MU_RESERVE MR
		 WHERE MR.USE_YN     = 'Y'
		   AND MR.STATUS NOT IN ('TN', 'RN')
		   AND MR.RESERVE_DATE                       = #{reserveDate}
		   AND DATE_FORMAT(MR.RESERVE_TIME, '%H:%i') = #{reserveTime}
		   AND MR.USER_NAME                          = #{userName}
		   AND MR.PHONE_NUMBER                       = #{phoneNumber}
		   AND MR.MU_TREATMENT_PROCEDURE_ID          = #{muTreatmentProcedureId}
		   AND MR.RESERVE_MEMBER_ID                  = #{reserveMemberId}
		   AND MR.MU_RESERVE_ID                     != #{muReserveId}
	</select>

	<select id="selectListReserveCheck" parameterType="hashmap" resultType="hashmap">
		SELECT MR.MU_RESERVE_ID        AS "muReserveId"
		  FROM MU_RESERVE AS MR
		 WHERE MR.CRM_MEMBER_SCHEDULE_PID = #{crmMemberSchedulePid}
	</select>

	<insert id="insertReserve" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muReserveId},LPAD((SELECT NEXTVAL(MU_RESERVE_SEQ)), 11, 0))
		</selectKey>
		INSERT INTO MU_RESERVE(
					 MU_RESERVE_ID
					,USER_NAME
					,PHONE_NUMBER
					,MU_USER_ID
					,RESERVE_DATE
					,RESERVE_TIME
				 <if test="reserveCategoryItemCode != null and reserveCategoryItemCode != ''">
					,RESERVE_CATEGORY_ITEM_ID
					,RESERVE_CATEGORY_ITEM_NAME
				 </if>
					,MU_TREATMENT_ID
					,TREATMENT_NAME
					,MU_TREATMENT_PROCEDURE_ID
					,TREATMENT_PROCEDURE_NAME
					,RESERVE_MEMBER_ID
					,MU_MEMBER_ID
					,ETC
					,STATUS
					,STATUS2
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{userName}
					,#{phoneNumber}
					,#{muUserId,jdbcType=VARCHAR}
					,#{reserveDate}
					,#{reserveTime}
				 <if test="reserveCategoryItemCode != null and reserveCategoryItemCode != ''">
					,(SELECT MU_CATEGORY_ITEM_ID
						FROM MU_CATEGORY_ITEM
					   WHERE CATEGORY_ITEM_CODE = #{reserveCategoryItemCode}
					   LIMIT 0, 1)
		            ,#{reserveCategoryItemName}
				 </if>
					,#{muTreatmentId}
					,#{treatmentName}
					,#{muTreatmentProcedureId}
					,#{treatmentProcedureName}
					,#{reserveMemberId}
					,#{muMemberId}
					,#{etc,jdbcType=VARCHAR}
					,#{status,jdbcType=VARCHAR}
					,#{status2}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>

	<insert id="insertReserveBatch" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muReserveId},LPAD((SELECT NEXTVAL(MU_RESERVE_SEQ)), 11, 0))
		</selectKey>
		INSERT INTO MU_RESERVE(
					 MU_RESERVE_ID
					,USER_NAME
					,PHONE_NUMBER
					,MU_USER_ID
					,RESERVE_DATE
					,RESERVE_TIME
					,RESERVE_CATEGORY_ITEM_ID
					,RESERVE_CATEGORY_ITEM_NAME
					,RESERVE_APP_YN
					,MU_TREATMENT_ID
					,TREATMENT_NAME
					,MU_TREATMENT_PROCEDURE_ID
					,TREATMENT_PROCEDURE_NAME
					,RESERVE_MEMBER_ID
					,MU_MEMBER_ID
					,ETC
					,STATUS
					,STATUS2
					,WRITE_DATE
					,WRITE_TIME
					,REMARK
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
					,CRM_MEMBER_SCHEDULE_PID
					,CRM_MEMBER_SCHEDULE_MB_PID
					,CRM_MEMBER_SCHEDULE_MB_HP
					,CRM_MEMBER_SCHEDULE_RES_PID
					,CRM_MEMBER_SCHEDULE_RES_PID2
					,CRM_MEMBER_SCHEDULE_RES_PID3
					,CRM_MEMBER_SCHEDULE_RES_PID4
					,CRM_MEMBER_SCHEDULE_RES_PID_NAME
					,CRM_MEMBER_SCHEDULE_RES_PID2_NAME
					,CRM_MEMBER_SCHEDULE_RES_PID3_NAME
					,CRM_MEMBER_SCHEDULE_RES_PID4_NAME
					,CRM_MEMBER_SCHEDULE_RES_ETC
					,CRM_MEMBER_SCHEDULE_R_STAFF_3
					,CRM_MEMBER_SCHEDULE_R_STAFF_3_ICON
					,CRM_MEMBER_SCHEDULE_R_STAFF_3_NAME
					,CRM_MEMBER_SCHEDULE_RES_CONSULT
					,CRM_MEMBER_SCHEDULE_RES_CONSULT_NAME
					,CRM_MEMBER_SCHEDULE_RES_ACCEPT
					,CRM_MEMBER_SCHEDULE_RES_ACCEPT_NAME
					,CRM_MEMBER_SCHEDULE_RES_STATE
		)     SELECT #{id}                                 AS MU_RESERVE_ID <!-- 식별자 -->
				    ,CMS.MB_NAME                           AS USER_NAME <!-- 고객 이름 -->
				    ,IF(LENGTH(REPLACE(CMS.MB_HP, '-', '')) = 11, REPLACE(CMS.MB_HP, '-', ''), '')
				                                           AS PHONE_NUMBER <!-- 고객 전화번호 -->
				    ,#{muUserId}                           AS MU_USER_ID <!-- 고객 식별자 -->
				    ,CMS.RES_S_DATE                        AS RESERVE_DATE <!-- 예약날짜 -->
				    ,CMS.RES_S_DATE                        AS RESERVE_TIME <!-- 예약시간 -->
				    ,NULL                                  AS RESERVE_CATEGORY_ITEM_ID <!-- 예약처(네이버,카카오 등) 카테고리 식별자 -->
				    ,NULL                                  AS RESERVE_CATEGORY_ITEM_NAME <!-- 예약처 카테고리 이름 -->
				    ,NULL                                  AS RESERVE_APP_YN <!-- 앱예약 유무 -->
				    ,NULL                                  AS MU_TREATMENT_ID <!-- 진료 식별자 -->
				    ,NULL                                  AS TREATMENT_NAME <!-- 진료명 -->
				    ,NULL                                  AS MU_TREATMENT_PROCEDURE_ID <!-- 진료 시술 식별자 -->
				    ,NULL                                  AS TREATMENT_PROCEDURE_NAME <!-- 진료 시술명 -->
				    ,NULL                                  AS RESERVE_MEMBER_ID
				    ,NULL                                  AS MU_MEMBER_ID
				    ,CMS.RES_TEXT                          AS ETC <!-- 메모 -->
				    ,CASE
				     WHEN CMS.RES_STATE = '진료끝'
				     THEN 'Y'
				     WHEN CMS.RES_STATE = '진료대기'
				     THEN 'R'
				     WHEN CMS.RES_STATE = '취소'
				     THEN 'TN'
				     ELSE ''
				     END                                   AS STATUS <!-- 상태(예약:'T',예약취소:'TN', 접수:'R', 접수취소:'RN', 진료중:'G', 진료끝:'Y') -->
				    ,''                                    AS STATUS2 <!-- 수납상태(미납:'N', 결제완료:'Y',결제취소:'D',부분결제:'P',환불:'R') -->
				    ,CMS.REG_DATE                          AS WRITE_DATE <!-- 작성일자 -->
				    ,CMS.REG_DATE                          AS WRITE_TIME <!-- 작성시간 -->
				    ,''                                    AS REMARK <!-- 비고 -->
				    ,'C'                                   AS CUD_FLAG <!-- 작업타입 (C: 등록, U: 수정, D : 삭제) -->
				    ,CASE
				     WHEN UPPER(CMS.LIST_DEL) = 'Y'
				     THEN 'N'
				     WHEN UPPER(CMS.LIST_DEL) = 'N'
				     THEN 'Y'
				     ELSE ''
				     END                                   AS USE_YN <!-- 사용여부 -->
				    ,'BatchSystem'                         AS REG_ID <!-- 등록자 -->
				    ,CMS.REG_DATE                          AS REG_DATE <!-- 등록일자 -->
				    ,'BatchSystem'                         AS MOD_ID <!-- 수정자 -->
				    ,CMS.UP_DATE                           AS MOD_DATE <!-- 수정일자 -->
				    ,#{tId}                                AS T_ID <!-- 트랜잭션 ID -->
				    ,#{tDate}                              AS T_DATE <!-- 트랜잭션 DATE -->
				    ,CMS.PID                               AS CRM_MEMBER_SCHEDULE_PID <!-- 기존 DB 식별자 -->
				    ,CMS.MB_PID                            AS CRM_MEMBER_SCHEDULE_MB_PID <!-- 기존 DB 식별자 -->
				    ,CMS.MB_HP                             AS CRM_MEMBER_SCHEDULE_MB_HP <!-- 기존 DB 식별자 -->
				    ,CMS.RES_PID                           AS CRM_MEMBER_SCHEDULE_RES_PID <!-- 기존 DB 예약종류 (진료 식별자) -->
				    ,CMS.RES_PID2                          AS CRM_MEMBER_SCHEDULE_RES_PID2 <!-- 기존 DB 시술분류 (진료 시술 식별자) -->
				    ,CMS.RES_PID3                          AS CRM_MEMBER_SCHEDULE_RES_PID3 <!-- 기존 DB 시술분류 (진료 시술 식별자) -->
				    ,CMS.RES_PID4                          AS CRM_MEMBER_SCHEDULE_RES_PID4 <!-- 기존 DB 시술분류 (진료 시술 식별자) -->
				    ,(SELECT CMDL.DIVI_NAME
			 	 	    FROM CRM_MEDICAL_DIVI_LIST AS CMDL
			 		   WHERE CMDL.PID = CMS.RES_PID
					   LIMIT 0, 1)                         AS CRM_MEMBER_SCHEDULE_RES_PID_NAME <!-- 기존 DB 예약종류 이름 (진료명) -->
				    ,(SELECT CMDL.DIVI_NAME
					    FROM CRM_MEDICAL_DIVI_LIST AS CMDL
					   WHERE CMDL.PID = CMS.RES_PID2
					   LIMIT 0, 1)                         AS CRM_MEMBER_SCHEDULE_RES_PID2_NAME <!-- 기존 DB 시술분류 이름 (진료 시술명) -->
				    ,(SELECT CMDL.DIVI_NAME
					    FROM CRM_MEDICAL_DIVI_LIST AS CMDL
					   WHERE CMDL.PID = CMS.RES_PID3
					   LIMIT 0, 1)                         AS CRM_MEMBER_SCHEDULE_RES_PID3_NAME <!-- 기존 DB 시술분류 이름 (진료 시술명) -->
				    ,(SELECT CMDL.DIVI_NAME
					    FROM CRM_MEDICAL_DIVI_LIST AS CMDL
					   WHERE CMDL.PID = CMS.RES_PID4
					   LIMIT 0, 1)                         AS CRM_MEMBER_SCHEDULE_RES_PID4_NAME <!-- 기존 DB 시술분류 이름 (진료 시술명) -->
				    ,CMS.RES_ETC                           AS CRM_MEMBER_SCHEDULE_RES_ETC <!-- 기존 DB 시술분류 (진료 시술 비고) -->
				    ,CMS.R_STAFF_3                         AS CRM_MEMBER_SCHEDULE_R_STAFF_3 <!-- 기존 DB 시술의사 식별자 (의사, 상담자 식별자) -->
				    ,(SELECT CSL.STAFF_ICON
				 	    FROM CRM_STAFF_LIST AS CSL
					   WHERE CSL.PID = CMS.R_STAFF_3
					   LIMIT 0, 1)                         AS CRM_MEMBER_SCHEDULE_R_STAFF_3_ICON <!-- 기존 DB 시술의사 식별자 (의사, 상담자 이름) -->
				    ,(SELECT CSL.STAFF_NAME
					    FROM CRM_STAFF_LIST AS CSL
					   WHERE CSL.PID = CMS.R_STAFF_3
					   LIMIT 0, 1)                         AS CRM_MEMBER_SCHEDULE_R_STAFF_3_NAME <!-- 기존 DB 시술의사 식별자 (의사, 상담자 이름) -->
				    ,CMS.RES_CONSULT                       AS CRM_MEMBER_SCHEDULE_RES_CONSULT <!-- 기존 DB 상담자 식별자 (의사, 상담자 식별자) -->
				    ,(SELECT CSL.STAFF_NAME
					    FROM CRM_STAFF_LIST AS CSL
					   WHERE CSL.PID = CMS.RES_CONSULT
					   LIMIT 0, 1)                         AS CRM_MEMBER_SCHEDULE_RES_CONSULT_NAME <!-- 기존 DB 상담자 식별자 (의사, 상담자 이름) -->
				    ,CMS.RES_ACCEPT                        AS CRM_MEMBER_SCHEDULE_RES_ACCEPT <!-- 기존 DB 상담자 식별자 (담당자 식별자) -->
				    ,(SELECT CSL.STAFF_NAME
					    FROM CRM_STAFF_LIST AS CSL
					   WHERE CSL.PID = CMS.RES_ACCEPT
					   LIMIT 0, 1)                         AS CRM_MEMBER_SCHEDULE_RES_ACCEPT_NAME <!-- 기존 DB 상담자 식별자 (담당자 이름) -->
				    ,CMS.RES_STATE                         AS CRM_MEMBER_SCHEDULE_RES_STATE <!-- 기존 DB 예약상태 (진료끝 : Y, 진료대기 : R, 취소 : TN, 나머지 : '') -->
			    FROM CRM_MEMBER_SCHEDULE AS CMS
			   WHERE PID = #{crmMemberSchedulePid}
	</insert>

	<update id="updateReserve" parameterType="hashmap">
		UPDATE MU_RESERVE
		   SET MOD_ID                          = #{modId}
			  ,MOD_DATE                        = NOW()
			  ,CUD_FLAG                        = 'U'
			  ,RESERVE_DATE                    = #{reserveDate}
			  ,RESERVE_TIME                    = #{reserveTime}
			  ,RESERVE_CATEGORY_ITEM_ID        =(SELECT MU_CATEGORY_ITEM_ID
												   FROM MU_CATEGORY_ITEM
												  WHERE CATEGORY_ITEM_CODE = #{reserveCategoryItemCode}
												  LIMIT 0, 1
												)
			  ,RESERVE_CATEGORY_ITEM_NAME      = #{reserveCategoryItemName}
			  ,MU_TREATMENT_ID                 = #{muTreatmentId}
			  ,TREATMENT_NAME                  = #{treatmentName}
			  ,MU_TREATMENT_PROCEDURE_ID       = #{muTreatmentProcedureId}
			  ,TREATMENT_PROCEDURE_NAME        = #{treatmentProcedureName}
			  ,RESERVE_MEMBER_ID               = #{reserveMemberId}
			  ,MU_MEMBER_ID                    = #{muMemberId}
			  ,ETC                             = #{etc}
		 WHERE USE_YN         = 'Y'
		   AND MU_RESERVE_ID  = #{muReserveId}
	</update>
	
	<update id="updateReserveStatus" parameterType="hashmap">
		UPDATE MU_RESERVE
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,STATUS              = #{status}
		 WHERE USE_YN         = 'Y'
		   AND MU_RESERVE_ID  = #{muReserveId}
	</update>
	
	<update id="updateReserveReadyStatus" parameterType="hashmap">
		UPDATE MU_RESERVE
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,RECEPTION_DATE      = IFNULL(RECEPTION_DATE, NOW())
			  ,STATUS              = #{status}
		 WHERE USE_YN         = 'Y'
		   AND MU_RESERVE_ID  = #{muReserveId}
	</update>
	
	<update id="updateReserveStatus2" parameterType="hashmap">
		UPDATE MU_RESERVE
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,STATUS2             = #{status}
		 WHERE USE_YN         = 'Y'
		   AND MU_RESERVE_ID  = #{muReserveId}
	</update>
	
	<update id="deleteReserve" parameterType="hashmap">
		UPDATE MU_RESERVE
		   SET MOD_ID            = #{modId}
			  ,MOD_DATE	         = NOW()
			  ,CUD_FLAG          = 'D'
			  ,USE_YN            = 'N'
		 WHERE USE_YN        = 'Y'
		   AND MU_RESERVE_ID  = #{muReserveId}
	</update>
</mapper>