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

	<select id="selectListTreatmentOption" parameterType="hashMap" resultType="hashMap">
		SELECT MT.MU_TREATMENT_ID             AS "muTreatmentId"
			  ,MT.TREATMENT_NAME              AS "treatmentName"
		  FROM MU_TREATMENT AS MT
		 WHERE MT.USE_YN     = 'Y'
		   AND MT.TREATMENT_NAME  != '멤버십'
	  ORDER BY MT.ORDER_NUMBER ASC
	</select>

	<select id="selectListTreatmentOption2" parameterType="hashMap" resultType="hashMap">
		SELECT MT.MU_TREATMENT_ID             AS "muTreatmentId"
			  ,MT.TREATMENT_NAME              AS "treatmentName"
		  FROM MU_TREATMENT AS MT
		 WHERE MT.USE_YN     = 'Y'
		   AND MT.MU_TREATMENT_ID != 'T172059821942700000000019'
	  ORDER BY MT.ORDER_NUMBER ASC
	</select>
	<select id="selectMembershipOption" parameterType="hashmap" resultType="hashmap">
		SELECT MT.MU_TREATMENT_ID           AS "muTreatmentId"
			 ,MT.TREATMENT_NAME            AS "treatmentName"
			 ,MT.ORDER_NUMBER              AS "orderNumber"
		FROM MU_TREATMENT AS MT
		WHERE MT.USE_YN           = 'Y'
		  AND MT.TREATMENT_NAME  = '멤버십'
			LIMIT 0, 1
	</select>
	<select id="selectTotalTreatmentCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)  AS "totalCount"
		  FROM MU_TREATMENT AS MT
		 WHERE MT.USE_YN = 'Y'
	</select>
	
	<select id="selectListTreatment" parameterType="hashmap" resultType="hashmap">
		SELECT MT.*
		  FROM (
					SELECT MT.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MT.MU_TREATMENT_ID                 AS "muTreatmentId"
									  ,MT.TREATMENT_NAME                  AS "treatmentName"
									  ,(SELECT COUNT(*)
										  FROM MU_TREATMENT_PROCEDURE AS MTP
										 WHERE MTP.USE_YN = 'Y'
										   AND MTP.MU_TREATMENT_ID = MT.MU_TREATMENT_ID
									   )                                  AS "procedureCount"
								  FROM MU_TREATMENT AS MT
								 WHERE MT.USE_YN = 'Y'
							     AND MT.TREATMENT_NAME !='멤버십'
							  ORDER BY MT.ORDER_NUMBER ASC
								 LIMIT 18446744073709551615
						   ) MT, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MT
		 WHERE 1 = 1
	</select>

	<select id="selectTreatment" parameterType="hashmap" resultType="hashmap">
		SELECT MT.MU_TREATMENT_ID           AS "muTreatmentId"
			  ,MT.TREATMENT_NAME            AS "treatmentName"
			  ,MT.ORDER_NUMBER              AS "orderNumber"
		  FROM MU_TREATMENT AS MT
		 WHERE MT.USE_YN           = 'Y'
		   AND MT.MU_TREATMENT_ID  = #{muTreatmentId}
		 LIMIT 0, 1
	</select>

	<select id="selectTreatmentOrderNumber" parameterType="hashmap" resultType="hashmap">
		SELECT IFNULL(MAX(MT.ORDER_NUMBER), 1)   AS "orderNumber"
		  FROM MU_TREATMENT AS MT
		 WHERE MT.USE_YN           = 'Y'
		   AND MT.MU_TREATMENT_ID  = #{muTreatmentId}
	</select>
	
	<insert id="insertTreatment" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muTreatmentId},LPAD((SELECT NEXTVAL(MU_TREATMENT_SEQ)), 11, 0))
		</selectKey>
		INSERT INTO MU_TREATMENT(
					 MU_TREATMENT_ID
					,ORDER_NUMBER
					,TREATMENT_NAME
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,(SELECT IFNULL(MAX(MT.ORDER_NUMBER), 1)
						FROM MU_TREATMENT AS MT
					   WHERE MT.USE_YN = 'Y'
					 )
					,#{treatmentName}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	
	<update id="deleteTreatmentOrderNumber" parameterType="hashmap">
		UPDATE MU_TREATMENT
		   SET MOD_ID            = #{modId}
			  ,MOD_DATE	         = NOW()
			  ,CUD_FLAG          = 'U'
			  ,ORDER_NUMBER      = #{orderNumber}
		 WHERE USE_YN          = 'Y'
		   AND MU_TREATMENT_ID = #{muTreatmentId}
	</update>
	
	<update id="deleteTreatment" parameterType="hashmap">
		UPDATE MU_TREATMENT
		   SET MOD_ID            = #{modId}
			  ,MOD_DATE	         = NOW()
			  ,CUD_FLAG          = 'D'
			  ,USE_YN            = 'N'
		 WHERE USE_YN          = 'Y'
		   AND MU_TREATMENT_ID = #{muTreatmentId}
	</update>
</mapper>