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

	<select id="selectTotalTreatmentProcedureOptionCount" parameterType="hashMap" resultType="hashMap">
		SELECT COUNT(*)                           AS "totalCount"
		  FROM MU_TREATMENT AS MT
	INNER JOIN MU_TREATMENT_PROCEDURE AS MTP
			ON MT.MU_TREATMENT_ID                 = MTP.MU_TREATMENT_ID
		   AND MT.USE_YN                          = 'Y'
		   AND MTP.USE_YN                         = 'Y'
	</select>

	<select id="selectListTreatmentProcedureOption" parameterType="hashMap" resultType="hashMap">
		SELECT MTP.*
		  FROM (
					SELECT MTP.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MT.MU_TREATMENT_ID                 AS "muTreatmentId"
									  ,MT.TREATMENT_NAME                  AS "treatmentName"
									  ,MTP.MU_TREATMENT_PROCEDURE_ID      AS "muTreatmentProcedureId"
									  ,MTP.TREATMENT_PROCEDURE_NAME       AS "treatmentProcedureName"
								  FROM MU_TREATMENT AS MT
							INNER JOIN MU_TREATMENT_PROCEDURE AS MTP
									ON MT.MU_TREATMENT_ID                 = MTP.MU_TREATMENT_ID
								   AND MT.USE_YN                          = 'Y'
								   AND MTP.USE_YN                         = 'Y'
								   AND MTP.ENABLE_YN                      = 'Y'
								   <if test="treatmentProcedureSearchKeywordParam != null and treatmentProcedureSearchKeywordParam != ''">
								   AND MTP.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{treatmentProcedureSearchKeywordParam}),'%')
								   </if>
								  WHERE TREATMENT_NAME != '멤버십'
								 <choose>
									 <when test="treatmentProcedureSort != null and treatmentProcedureSort != ''">
								  ORDER BY ${treatmentProcedureSort}
									 </when>
									 <otherwise>
								  ORDER BY MT.ORDER_NUMBER ASC ,MTP.TREATMENT_PROCEDURE_NAME ASC
									 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MTP, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MTP
		 WHERE 1 = 1
		 LIMIT ${treatmentProcedureStart}, ${treatmentProcedureLimit}
	</select>

	<select id="selectListTreatmentProcedureOption2" parameterType="hashMap" resultType="hashMap">
		SELECT MT.MU_TREATMENT_ID                 AS "muTreatmentId"
			  ,MT.TREATMENT_NAME                  AS "treatmentName"
			  ,MTP.MU_TREATMENT_PROCEDURE_ID      AS "muTreatmentProcedureId"
			  ,MTP.TREATMENT_PROCEDURE_NAME       AS "treatmentProcedureName"
		  FROM MU_TREATMENT AS MT
	INNER JOIN MU_TREATMENT_PROCEDURE AS MTP
			ON MT.MU_TREATMENT_ID = MTP.MU_TREATMENT_ID
		   AND MT.USE_YN          = 'Y'
		   AND MTP.USE_YN         = 'Y'
		   AND MTP.ENABLE_YN                      = 'Y'
		   AND MT.MU_TREATMENT_ID IN (${muTreatmentIdList})
		WHERE TREATMENT_NAME != '멤버십'
	  ORDER BY MT.ORDER_NUMBER ASC ,MTP.TREATMENT_PROCEDURE_NAME ASC
	</select>

	<select id="selectListTreatmentProcedureOption3" parameterType="hashMap" resultType="hashMap">
		SELECT MT.MU_TREATMENT_ID                 AS "muTreatmentId"
			  ,MT.TREATMENT_NAME                  AS "treatmentName"
			  ,MTP.MU_TREATMENT_PROCEDURE_ID      AS "muTreatmentProcedureId"
			  ,MTP.TREATMENT_PROCEDURE_NAME       AS "treatmentProcedureName"
		  FROM MU_TREATMENT AS MT
	INNER JOIN MU_TREATMENT_PROCEDURE AS MTP
			ON MT.MU_TREATMENT_ID = MTP.MU_TREATMENT_ID
		   AND MT.USE_YN          = 'Y'
		   AND MTP.USE_YN         = 'Y'
		   AND MTP.ENABLE_YN      = 'Y'
		WHERE TREATMENT_NAME != '멤버십'
	  ORDER BY MT.ORDER_NUMBER ASC ,MTP.TREATMENT_PROCEDURE_NAME ASC
	</select>

	<select id="selectTotalTreatmentProcedureCount" parameterType="hashmap" resultType="hashmap">
		SELECT COUNT(*)	AS "totalCount"
		FROM (
			SELECT MTP.MU_TREATMENT_PROCEDURE_ID
			FROM MU_TREATMENT_PROCEDURE MTP
			LEFT JOIN MU_TREATMENT_PROCEDURE_PRODUCT MTPP
			ON MTP.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
			AND MTPP.USE_YN = 'Y'
			WHERE MTP.USE_YN = 'Y'
			AND MTP.MU_TREATMENT_ID = #{muTreatmentId}
			GROUP BY MTP.MU_TREATMENT_PROCEDURE_ID, MTP.TREATMENT_PROCEDURE_NAME
			<if test="treatmentProcedureSearchKeywordParam != null and treatmentProcedureSearchKeywordParam != ''">
				HAVING (
					MTP.TREATMENT_PROCEDURE_NAME LIKE CONCAT('%', TRIM(#{treatmentProcedureSearchKeywordParam}), '%')
					OR IFNULL(GROUP_CONCAT(MTPP.PRODUCT_NAME SEPARATOR ', '), '') LIKE CONCAT('%', TRIM(#{treatmentProcedureSearchKeywordParam}), '%')
					OR IFNULL((SELECT GROUP_CONCAT(MM.NAME)
								FROM MU_TREATMENT_PROCEDURE_DOCTOR AS MTPD
								INNER JOIN MU_MEMBER AS MM
								ON MM.MU_MEMBER_ID = MTPD.MU_MEMBER_ID
								AND MM.USE_YN = 'Y'
								AND MTPD.USE_YN = 'Y'
								WHERE MTPD.MU_TREATMENT_PROCEDURE_ID = MTP.MU_TREATMENT_PROCEDURE_ID
								), '') LIKE CONCAT('%', TRIM(#{treatmentProcedureSearchKeywordParam}), '%')
				)
			</if>
		) AS MTP
	</select>

	<select id="selectListTreatmentProcedure" parameterType="hashmap" resultType="hashmap">
		SELECT MTP.*
		  FROM (
					SELECT MTP.*
						  ,CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
					  FROM (
								SELECT MU_TREATMENT_PROCEDURE_ID           AS "muTreatmentProcedureId"
									  ,MU_TREATMENT_ID                     AS "muTreatmentId"
									  ,TREATMENT_PROCEDURE_NAME            AS "procedureName"
									  ,MU_PRODUCT_ID                       AS "muProductId"
									  ,PRODUCT_NAME                        AS "productName"
									  ,PRODUCT_VOLUME                      AS "productVolume"
									  ,FORMAT(PROCEDURE_PRICE, 0)          AS "procedurePrice"
									  ,FORMAT(PROCEDURE_PRICE_VAT, 0)      AS "procedurePriceVat"
									  ,FORMAT(ADDITIONAL_AMOUNT, 0)        AS "additionalAmount"
									  ,FORMAT(
                                              (PROCEDURE_PRICE_VAT
                                              +PROCEDURE_PRICE), 0)        AS "procedurePriceTotal"
                                      ,INSURANCE_TYPE                      AS "insuranceType"
									  ,DOCTOR_NAME                         AS "doctorName"
									  ,ENABLE_YN                           AS "enableYn"
									  ,DATE_FORMAT(WRITE_DATE, '%Y-%m-%d') AS "writeDate"
								  FROM (
											SELECT MTP.MU_TREATMENT_PROCEDURE_ID
												  ,MTP.MU_TREATMENT_ID
												  ,MTP.TREATMENT_PROCEDURE_NAME
												  ,GROUP_CONCAT(MTPP.MU_PRODUCT_ID)       AS MU_PRODUCT_ID
												  ,IFNULL((GROUP_CONCAT(MTPP.PRODUCT_NAME SEPARATOR ', ')
												  ),'')                                   AS PRODUCT_NAME
												  ,IFNULL((GROUP_CONCAT(CONCAT(MTPP.VOLUME,MTPP.UNIT_NAME) SEPARATOR ', ')
                                                   ),'')                                   AS PRODUCT_VOLUME
												  ,IFNULL((SELECT SUM(MTPP.PRICE)
															 FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
															WHERE MTPP.USE_YN       = 'Y'
															  AND MTPP.MU_TREATMENT_PROCEDURE_ID = MTP.MU_TREATMENT_PROCEDURE_ID
												   ),0)                                    AS PROCEDURE_PRICE
												  ,IFNULL((SELECT SUM(MTPP.VAT)
															 FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
															WHERE MTPP.USE_YN       = 'Y'
															  AND MTPP.MU_TREATMENT_PROCEDURE_ID = MTP.MU_TREATMENT_PROCEDURE_ID
												   ),0)                                    AS PROCEDURE_PRICE_VAT
												  ,IFNULL((SELECT
                                                               CASE INSURANCE_TYPE
                                                                   WHEN 'Y' THEN '급여'
                                                                   WHEN 'N' THEN '비급여'
                                                                   ELSE ''
                                                                       END
                                                            FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                                            WHERE MTPP.USE_YN       = 'Y'
                                                              AND MTPP.MU_TREATMENT_PROCEDURE_ID = MTP.MU_TREATMENT_PROCEDURE_ID
                                                           ),0)                                    AS INSURANCE_TYPE
                                                   ,IFNULL((SELECT GROUP_CONCAT(MM.NAME)
															 FROM MU_TREATMENT_PROCEDURE_DOCTOR AS MTPD
													   INNER JOIN MU_MEMBER AS MM
															   ON MM.MU_MEMBER_ID = MTPD.MU_MEMBER_ID
															  AND MM.USE_YN         = 'Y'
															  AND MTPD.USE_YN       = 'Y'
															WHERE MTPD.MU_TREATMENT_PROCEDURE_ID = MTP.MU_TREATMENT_PROCEDURE_ID
												   ),'')                                   AS DOCTOR_NAME
											      ,IFNULL((SELECT SUM(MTPP.ADDITIONAL_AMOUNT)
                                                           FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                                           WHERE MTPP.USE_YN       = 'Y'
                                                           AND MTPP.MU_TREATMENT_PROCEDURE_ID = MTP.MU_TREATMENT_PROCEDURE_ID
                                                    ),0)			                        AS ADDITIONAL_AMOUNT
												  ,MTP.ENABLE_YN
												  ,MTP.WRITE_DATE
											  FROM MU_TREATMENT_PROCEDURE MTP
											  LEFT JOIN MU_TREATMENT_PROCEDURE_PRODUCT MTPP
											  ON MTP.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
												AND MTPP.USE_YN='Y'
											 WHERE MTP.USE_YN = 'Y'
											   AND MTP.MU_TREATMENT_ID = #{muTreatmentId}
                                           GROUP BY
                                             MTP.MU_TREATMENT_PROCEDURE_ID, MTP.TREATMENT_PROCEDURE_NAME
									   ) AS MTP
								 WHERE 1=1
								   <if test="treatmentProcedureSearchKeywordParam != null and treatmentProcedureSearchKeywordParam != ''">
								   AND ((TREATMENT_PROCEDURE_NAME LIKE CONCAT('%',TRIM(#{treatmentProcedureSearchKeywordParam}),'%'))
									OR (PRODUCT_NAME LIKE CONCAT('%',TRIM(#{treatmentProcedureSearchKeywordParam}),'%'))
									OR (DOCTOR_NAME LIKE CONCAT('%',TRIM(#{treatmentProcedureSearchKeywordParam}),'%')))
								   </if>
								 <choose>
								 <when test="treatmentProcedureSort != null and treatmentProcedureSort != ''">
							  ORDER BY ${treatmentProcedureSort}
								 </when>
								 <otherwise>
							  ORDER BY ENABLE_YN DESC, WRITE_DATE DESC
								 </otherwise>
								 </choose>
								 LIMIT 18446744073709551615
						   ) MTP, (SELECT @RNUM:=0) R
					 WHERE 1 = 1
			   ) MTP
		 WHERE 1 = 1
		<if test="treatmentProcedureStart != null and treatmentProcedureStart != ''">
			LIMIT ${treatmentProcedureStart}, ${treatmentProcedureLimit}
		</if>
	</select>
	<select id="selectTreatmentProcedureCount" parameterType="hashmap" resultType="Integer">
		SELECT COUNT(1)
		FROM (
			SELECT GROUP_CONCAT(IF(PRODUCT_NAME='',NULL,PRODUCT_NAME)
					ORDER BY PRODUCT_NAME SEPARATOR ', ') 				AS productNames
				 ,GROUP_CONCAT(IF(UNIT_NAME='',NULL,UNIT_NAME)
					ORDER BY UNIT_NAME SEPARATOR ', ') 					AS unitNames
				 ,GROUP_CONCAT(IF(VOLUME='',NULL,VOLUME)
				    ORDER BY PRODUCT_NAME SEPARATOR ', ') 				AS volumes
			FROM MU_TREATMENT_PROCEDURE MTP
					 JOIN MU_TREATMENT_PROCEDURE_PRICE MTPP
						  ON MTP.MU_TREATMENT_PROCEDURE_ID = MTPP.MU_TREATMENT_PROCEDURE_ID
							  AND MTPP.USE_YN='Y'
					 LEFT JOIN MU_TREATMENT_PROCEDURE_PRODUCT MTPP2
							   ON MTPP2.MU_TREATMENT_PROCEDURE_ID = MTP.MU_TREATMENT_PROCEDURE_ID
								   AND MTPP2.USE_YN='Y'
			WHERE MTP.USE_YN='Y'
			  AND MTP.TREATMENT_PROCEDURE_NAME =#{procedureName}
			  AND INSURANCE_TYPE = #{insuranceType}
			  AND PRICE = #{price}
			  AND VAT = #{vat}
			GROUP BY MTP.MU_TREATMENT_PROCEDURE_ID
				   , MTP.TREATMENT_PROCEDURE_NAME
				   , INSURANCE_TYPE
				   , PRICE
				   , VAT
			HAVING 1=1
			<if test="productNames != null and productNames != ''">
			   AND productNames = #{productNames}
			</if>
			<if test="volumes != null and volumes != ''">
			   AND volumes = #{volumes}
			</if>
			<if test="unitNames != null and unitNames != ''">
			   AND unitNames = #{unitNames}
			</if>
		)AS MTP
	</select>
	<select id="selectTreatmentProcedure" parameterType="hashmap" resultType="hashmap">
		SELECT MU_TREATMENT_PROCEDURE_ID                                           AS "muTreatmentProcedureId"
			  ,MU_TREATMENT_ID                                                     AS "muTreatmentId"
			  ,TREATMENT_PROCEDURE_NAME                                            AS "procedureName"
			  ,ENABLE_YN                                                           AS "enableYn"
			  ,DATE_FORMAT(WRITE_DATE, '%Y-%m-%d')                                 AS "writeDate"
		  FROM (
					SELECT MTP.MU_TREATMENT_PROCEDURE_ID
						  ,MTP.MU_TREATMENT_ID
						  ,MTP.TREATMENT_PROCEDURE_NAME
						  ,MTP.ENABLE_YN
						  ,MTP.WRITE_DATE
					  FROM MU_TREATMENT_PROCEDURE AS MTP
		   LEFT OUTER JOIN MU_TREATMENT_PROCEDURE_PRODUCT AS MP
						ON MTP.MU_TREATMENT_PROCEDURE_ID = MP.MU_TREATMENT_PROCEDURE_ID
					   AND MP.USE_YN = 'Y'
					 WHERE MTP.USE_YN                    = 'Y'
					   AND MTP.MU_TREATMENT_PROCEDURE_ID = #{muTreatmentProcedureId}
			   ) AS MTP
		 WHERE 1 = 1
		 LIMIT 0, 1
	</select>

	<select id="selectTreatmentProcedure2" parameterType="hashmap" resultType="hashmap">
		SELECT MTP.MU_TREATMENT_PROCEDURE_ID         AS "muTreatmentProcedureId"
			  ,MTP.TREATMENT_PROCEDURE_NAME          AS "treatmentProcedureName"
			  ,MT.MU_TREATMENT_ID                    AS "muTreatmentId"
			  ,MT.TREATMENT_NAME                     AS "treatmentName"
		  FROM MU_TREATMENT AS MT
	INNER JOIN MU_TREATMENT_PROCEDURE AS MTP
			ON MT.MU_TREATMENT_ID            = MTP.MU_TREATMENT_ID
		   AND MT.USE_YN                     = 'Y'
		   AND MTP.USE_YN                    = 'Y'
		   AND MTP.ENABLE_YN                 = 'Y'
		   AND MTP.MU_TREATMENT_PROCEDURE_ID = #{muTreatmentProcedureId}
		 LIMIT 0, 1
	</select>

	<insert id="insertTreatmentProcedure" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muTreatmentProcedureId},LPAD((SELECT NEXTVAL(MU_TREATMENT_PROCEDURE_SEQ)), 10, 0))
		</selectKey>
		INSERT INTO MU_TREATMENT_PROCEDURE(
					 MU_TREATMENT_PROCEDURE_ID
					,MU_TREATMENT_ID
					,TREATMENT_PROCEDURE_NAME
					,ENABLE_YN
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muTreatmentId}
					,#{procedureName}
					,#{enableYn}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	<select id="selectTreatmentProdureNumber" resultType="String">
		select CONCAT('TP', DATE_FORMAT(NOW(), '%Y%m%d'), '-', LPAD(IFNULL(MAX(SUBSTRING(MU_TREATMENT_PROCEDURE_NUMBER, 11)) + 1, 1), 3, '0')) AS number
                FROM MU_TREATMENT_PROCEDURE AS MTP
                WHERE MU_TREATMENT_PROCEDURE_NUMBER LIKE CONCAT('TP', DATE_FORMAT(NOW(), '%Y%m%d'), '%')
	</select>
	<update id="updateTreatmentProcedure" parameterType="hashmap">
		UPDATE MU_TREATMENT_PROCEDURE
		   SET MOD_ID                    = #{modId}
			  ,MOD_DATE                  = NOW()
			  ,CUD_FLAG                  = 'U'
			  ,TREATMENT_PROCEDURE_NAME  = #{procedureName}
		 WHERE USE_YN                        = 'Y'
		   AND MU_TREATMENT_PROCEDURE_ID     = #{muTreatmentProcedureId}
	</update>

	<update id="updateTreatmentProcedureEnableYn" parameterType="hashmap">
		UPDATE MU_TREATMENT_PROCEDURE
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,ENABLE_YN           = #{enableYn}
		 WHERE USE_YN                     = 'Y'
		   AND MU_TREATMENT_PROCEDURE_ID  = #{muTreatmentProcedureId}
	</update>

	<update id="updateTreatmentProcedureTreatmentId" parameterType="hashmap">
		UPDATE MU_TREATMENT_PROCEDURE
		   SET MOD_ID              = #{modId}
			  ,MOD_DATE            = NOW()
			  ,CUD_FLAG            = 'U'
			  ,MU_TREATMENT_ID     = #{muTreatmentId}
		 WHERE USE_YN                     = 'Y'
		   AND MU_TREATMENT_PROCEDURE_ID  = #{muTreatmentProcedureId}
	</update>

	<update id="deleteTreatmentProcedure" parameterType="hashmap">
		UPDATE MU_TREATMENT_PROCEDURE
		   SET MOD_ID            = #{modId}
			  ,MOD_DATE	         = NOW()
			  ,CUD_FLAG          = 'D'
			  ,USE_YN            = 'N'
		 WHERE USE_YN                     = 'Y'
		   AND MU_TREATMENT_PROCEDURE_ID  = #{muTreatmentProcedureId}
	</update>
</mapper>