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

	<select id="selectTotalUserConsultingChartCount" 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.ETC
						  ,MUCC.MU_USER_CONSULTING_CHART_ID
						  ,MUCC.CHART_NUMBER
						  ,MUCC.CONTENT
						  ,MUCC.WRITE_MEMBER_ID
						  ,IFNULL((SELECT MM.NAME
									 FROM MU_MEMBER AS MM
									WHERE MM.MU_MEMBER_ID = MUCC.WRITE_MEMBER_ID
									LIMIT 0, 1
						   ), '')                                      AS WRITE_MEMBER_NAME
						  ,MUCC.WRITE_DATE
					  FROM MU_RESERVE AS MR
				INNER JOIN MU_USER_CONSULTING_CHART AS MUCC
						ON MR.MU_RESERVE_ID = MUCC.MU_RESERVE_ID
					   AND MR.USE_YN       = 'Y'
					   AND MUCC.USE_YN     = 'Y'
					   AND MUCC.MU_USER_ID = #{muUserId}
					   <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
		   <if test="searchKeywordParam != null and searchKeywordParam != ''">
		   AND (WRITE_MEMBER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%')
		    OR CHART_NUMBER LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%'))
		   </if>
	</select>
	
	<select id="selectListUserConsultingChart" 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_USER_CONSULTING_CHART_ID              AS "muUserConsultingChartId"
									  ,IFNULL(CHART_NUMBER, '')                 AS "chartNumber"
									  ,IFNULL(CONTENT, '')                      AS "content"
									  ,IFNULL(ETC, '')                          AS "etc"
									  ,CASE STATUS
											WHEN 'T'  THEN '예약'
											WHEN 'TN' THEN '예약취소'
											WHEN 'R'  THEN '접수'
											WHEN 'RN' THEN '접수취소'
											WHEN 'G'  THEN '진료중'
											WHEN 'Y'  THEN '진료완료'
											ELSE ''
									   END                                      AS "statusName"
									  ,WRITE_MEMBER_ID                          AS "writeMemberId"
									  ,WRITE_MEMBER_NAME                        AS "writeMemberName"
									  ,DATE_FORMAT(WRITE_DATE,'%Y-%m-%d')       AS "writeDate"
									  ,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.ETC
												  ,MR.STATUS
												  ,MUCC.MU_USER_CONSULTING_CHART_ID
			  									  ,MUCC.CHART_NUMBER
												  ,MUCC.CONTENT
												  ,MUCC.WRITE_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.MU_MEMBER_ID = MUCC.WRITE_MEMBER_ID
															LIMIT 0, 1
												   ), '')                                      AS WRITE_MEMBER_NAME
												  ,MUCC.WRITE_DATE
												  ,MUCC.WRITE_TIME
											  FROM MU_RESERVE AS MR
										INNER JOIN MU_USER_CONSULTING_CHART AS MUCC
												ON MR.MU_RESERVE_ID = MUCC.MU_RESERVE_ID
											   AND MR.USE_YN       = 'Y'
											   AND MUCC.USE_YN     = 'Y'
											   AND MUCC.MU_USER_ID = #{muUserId}
											   <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
								   <if test="searchKeywordParam != null and searchKeywordParam != ''">
								   AND (WRITE_MEMBER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%')
								    OR CHART_NUMBER 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, MR.WRITE_DATE DESC, MR.WRITE_TIME DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MR, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MR
		 WHERE 1 = 1
		 LIMIT ${start}, ${limit}
	</select>
	
	<select id="selectListUserConsultingChartExcel" 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_USER_CONSULTING_CHART_ID              AS "muUserConsultingChartId"
									  ,IFNULL(CHART_NUMBER, '')                 AS "chartNumber"
									  ,IFNULL(CONTENT, '')                      AS "content"
									  ,IFNULL(ETC, '')                          AS "etc"
									  ,CASE STATUS
											WHEN 'T'  THEN '예약'
											WHEN 'TN' THEN '예약취소'
											WHEN 'R'  THEN '접수'
											WHEN 'RN' THEN '접수취소'
											WHEN 'G'  THEN '진료중'
											WHEN 'Y'  THEN '진료완료'
											ELSE ''
									   END                                      AS "statusName"
									  ,WRITE_MEMBER_ID                          AS "writeMemberId"
									  ,WRITE_MEMBER_NAME                        AS "writeMemberName"
									  ,DATE_FORMAT(WRITE_DATE,'%Y-%m-%d')       AS "writeDate"
									  ,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.ETC
												  ,MR.STATUS
												  ,MUCC.MU_USER_CONSULTING_CHART_ID
			  									  ,MUCC.CHART_NUMBER
												  ,MUCC.CONTENT
												  ,MUCC.WRITE_MEMBER_ID
												  ,IFNULL((SELECT MM.NAME
															 FROM MU_MEMBER AS MM
															WHERE MM.MU_MEMBER_ID = MUCC.WRITE_MEMBER_ID
															LIMIT 0, 1
												   ), '')                                      AS WRITE_MEMBER_NAME
												  ,MUCC.WRITE_DATE
												  ,MUCC.WRITE_TIME
											  FROM MU_RESERVE AS MR
										INNER JOIN MU_USER_CONSULTING_CHART AS MUCC
												ON MR.MU_RESERVE_ID = MUCC.MU_RESERVE_ID
											   AND MR.USE_YN       = 'Y'
											   AND MUCC.USE_YN     = 'Y'
											   AND MUCC.MU_USER_ID = #{muUserId}
											   <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
								   <if test="searchKeywordParam != null and searchKeywordParam != ''">
								   AND (WRITE_MEMBER_NAME LIKE CONCAT('%',TRIM(#{searchKeywordParam}),'%')
								    OR CHART_NUMBER 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, MR.WRITE_DATE DESC, MR.WRITE_TIME DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MR, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MR
		 WHERE 1 = 1
	</select>

	<select id="selectListUserConsultingChartTop5" parameterType="hashmap" resultType="hashmap">
		SELECT MUCC.MU_USER_CONSULTING_CHART_ID            AS "muUserConsultingChartId"
			  ,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.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"
			  ,MU.USER_NAME                                AS "userName"
			  ,IFNULL(MU.MU_USER_ID, '')                   AS "muUserId"
			  ,IFNULL(MUCC.WRITE_MEMBER_ID, '')            AS "writeMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MUCC.WRITE_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                      AS "writeMemberName"
			  ,DATE_FORMAT(MUCC.WRITE_DATE, '%Y-%m-%d')    AS "writeDate"
		  FROM MU_RESERVE AS MR
	INNER JOIN MU_USER_CONSULTING_CHART AS MUCC
			ON MR.MU_RESERVE_ID = MUCC.MU_RESERVE_ID
	LEFT OUTER JOIN MU_USER AS MU
			ON MUCC.MU_USER_ID = MU.MU_USER_ID
		   AND MU.USE_YN       = 'Y'
		 WHERE MR.USE_YN       = 'Y'
		   AND MUCC.USE_YN     = 'Y'
		   AND MUCC.MU_USER_ID = #{muUserId}
	  ORDER BY MUCC.WRITE_DATE DESC
		 LIMIT 0, 5
	</select>

	<select id="selectUserConsultingChart" parameterType="hashmap" resultType="hashmap">
		SELECT MUCC.MU_USER_CONSULTING_CHART_ID            AS "muUserConsultingChartId"
			  ,MUCC.MU_RESERVE_ID                          AS "muReserveId"
			  ,IFNULL(MUCC.CHART_NUMBER, '')               AS "chartNumber"
			  ,MU.USER_NAME                                AS "userName"
			  ,IFNULL(MU.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"
			  ,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.PHONE_NUMBER, '')                 AS "phoneNumber"
			  ,IFNULL(MEMBERSHIP_BALANCE,0)                AS "membershipBalance"
			  ,IFNULL(MU.ETC, '')                          AS "etc"
			  ,IFNULL(MUCC.CONTENT, '')                    AS "content"
			  ,IFNULL(MUCC.WRITE_MEMBER_ID, '')            AS "writeMemberId"
			  ,IFNULL((SELECT MM.NAME
						 FROM MU_MEMBER AS MM
						WHERE MM.MU_MEMBER_ID = MUCC.WRITE_MEMBER_ID
						LIMIT 0, 1
			   ), '')                                      AS "writeMemberName"
			  ,DATE_FORMAT(MUCC.WRITE_DATE, '%Y-%m-%d')    AS "writeDate"
			  ,DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d')    AS "reserveDate"
			  ,DATE_FORMAT(MR.RESERVE_TIME, '%H:%i')       AS "reserveTime"
			  ,DATE_FORMAT(MR.RECEPTION_DATE,'%Y-%m-%d,  %H:%i') AS "receptionDateTime"
			  ,MR.TREATMENT_NAME                           AS "treatmentName"
			  ,MR.TREATMENT_PROCEDURE_NAME                 AS "treatmentProcedureName"
			  ,IFNULL(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"
			  ,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"
			   ,IF(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; MR.WRITE_DATE
                    AND MR2.MU_USER_ID = MR.MU_USER_ID)
              , 0) > 0, 'Y', 'N')                     AS "visitYn"
		  FROM MU_USER_CONSULTING_CHART AS MUCC
	LEFT OUTER JOIN MU_RESERVE AS MR
			ON MR.MU_RESERVE_ID = MUCC.MU_RESERVE_ID
	LEFT OUTER JOIN MU_USER AS MU
			ON MUCC.MU_USER_ID = MU.MU_USER_ID
		   AND MU.USE_YN       = 'Y'
    LEFT OUTER JOIN MU_MEMBERSHIP MM
            ON MUCC.MU_USER_ID = MM.MU_USER_ID
		 WHERE MUCC.USE_YN                      = 'Y'
		   AND MUCC.MU_USER_CONSULTING_CHART_ID = #{muUserConsultingChartId}
		 LIMIT 0, 1
	</select>

	<select id="selectUserConsultingChartNumber" parameterType="hashmap" resultType="hashmap">
		SELECT CONCAT('C', DATE_FORMAT(NOW(), '%Y%m%d'), '-', LPAD(IFNULL(MAX(SUBSTRING(CHART_NUMBER, 11)) + 1, 1), 4, '0')) AS "chartNumber"
		  FROM MU_USER_CONSULTING_CHART AS MUCC
		 WHERE CHART_NUMBER LIKE CONCAT('C', DATE_FORMAT(NOW(), '%Y%m%d'), '%')
	</select>

	<insert id="insertUserConsultingChart" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muUserConsultingChartId},LPAD((SELECT NEXTVAL(MU_USER_CONSULTING_CHART_SEQ)), 9, 0))
		</selectKey>
		INSERT INTO MU_USER_CONSULTING_CHART(
					 MU_USER_CONSULTING_CHART_ID
					,MU_RESERVE_ID
					,MU_USER_ID
					,CHART_NUMBER
					,CONTENT
					,WRITE_MEMBER_ID
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muReserveId}
					,#{muUserId}
					,#{chartNumber,jdbcType=VARCHAR}
					,#{content,jdbcType=VARCHAR}
					,#{regId}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	
	<update id="updateUserConsultingChart" parameterType="hashmap">
		UPDATE MU_USER_CONSULTING_CHART
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,CONTENT             = #{content,jdbcType=VARCHAR}
		 WHERE USE_YN                       = 'Y'
		   AND MU_USER_CONSULTING_CHART_ID  = #{muUserConsultingChartId}
	</update>
	
	<update id="deleteUserConsultingChart" parameterType="hashmap">
		UPDATE MU_USER_CONSULTING_CHART
		   SET MOD_ID            = #{modId}
			  ,MOD_DATE	         = NOW()
			  ,CUD_FLAG          = 'D'
			  ,USE_YN            = 'N'
		 WHERE USE_YN                       = 'Y'
		   AND MU_RESERVE_ID  = #{muReserveId}
	</update>
</mapper>