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

    <select id="selectCategoryItemList" parameterType="hashmap" resultType="hashmap">
		SELECT
			  MCI.MU_CATEGORY_ITEM_ID			AS "muCategoryItemId"
			 ,MCI.CATEGORY_CODE					AS "categoryCode"
			 ,MC.CATEGORY_NAME					AS "categoryName"
			 ,MCI.CATEGORY_ITEM_CODE  			AS "categoryItemCode"
			 ,MCI.CATEGORY_ITEM_NAME			AS "categoryItemName"
			 ,MCI.FILE_PATH						AS "filePath"
			 ,MCI.FILE_NAME						AS "fileName"
			 ,MCI.ORIGINAL_FILE_NAME			AS "originalFileName"
			 ,MCI.ETC							AS "etc"
			 ,MCI.ORDER_NUMBER					AS "orderNumber"
		FROM
			MU_CATEGORY MC,MU_CATEGORY_ITEM MCI
		WHERE MC.CATEGORY_CODE=MCI.CATEGORY_CODE
		AND MCI.USE_YN        = 'Y'
		<choose>
			<when test="categorySort != null and categorySort != ''">
				ORDER BY #{categorySort}
			</when>
			<otherwise>
				order by ORDER_NUMBER
			</otherwise>
		</choose>
	</select>
	<select id="selectCategoryItem" parameterType="hashmap" resultType="hashmap">
		SELECT
			 MCI.CATEGORY_CODE					AS "categoryCode"
			,MC.CATEGORY_NAME					AS "categoryName"
			,MCI.CATEGORY_ITEM_CODE  			AS "categoryItemCode"
			,MCI.CATEGORY_ITEM_NAME				AS "categoryItemName"
			,MCI.FILE_PATH						AS "filePath"
			,MCI.FILE_NAME						AS "fileName"
			,MCI.ORIGINAL_FILE_NAME				AS "originalFileName"
			,MCI.ETC							AS "etc"
			,MCI.ORDER_NUMBER					AS "orderNumber"
		FROM
		MU_CATEGORY MC,MU_CATEGORY_ITEM MCI
		WHERE MC.CATEGORY_CODE = MCI.CATEGORY_CODE
		AND MCI.USE_YN        = 'Y'
		AND MC.USE_YN        = 'Y'
		AND MC.CATEGORY_CODE = #{categoryCode}
		ORDER BY ORDER_NUMBER
	</select>
	<select id="selectCategoryList" parameterType="hashmap" resultType="hashmap">
		SELECT
			CATEGORY_CODE		AS "categoryCode"
		   ,CATEGORY_NAME		AS "categoryName"
		FROM MU_CATEGORY
		WHERE USE_YN        = 'Y'
		GROUP BY CATEGORY_CODE
		ORDER BY CATEGORY_CODE ASC
	</select>

	<update id="deleteHospitalOther" parameterType="HashMap">
		UPDATE MU_CATEGORY_ITEM
		SET  USE_YN				 = 'N'
		    ,MOD_ID              = #{modId}
		    ,MOD_DATE            = NOW()
		    ,T_ID 			     =#{tId}
		    ,T_DATE 			 =#{tDate}
		WHERE CATEGORY_ITEM_CODE = #{categoryItemCode}
		  AND CATEGORY_CODE      = #{categoryCode}
	</update>

	<select id="selectCategoryId" parameterType="HashMap" resultType="String">
		SELECT MU_CATEGORY_ID
		FROM MU_CATEGORY_ITEM
		WHERE CATEGORY_ITEM_CODE = #{categoryItemCode}
		  AND CATEGORY_CODE      = #{muCategoryCode}
		  AND USE_YN			 = 'Y'
	</select>
	
	<select id="selectCategoryItemId" parameterType="hashmap" resultType="hashmap">
		SELECT MCI.MU_CATEGORY_ITEM_ID          AS "muCategoryItemId"
			  ,MCI.CATEGORY_ITEM_CODE           AS "categoryItemCode"
			  ,MCI.CATEGORY_ITEM_NAME           AS "categoryItemName"
		  FROM MU_CATEGORY AS MC
	INNER JOIN MU_CATEGORY_ITEM AS MCI
			ON MC.MU_CATEGORY_ID = MCI.MU_CATEGORY_ID
		   AND MC.USE_YN              = 'Y'
		   AND MCI.USE_YN             = 'Y'
		   AND MCI.CATEGORY_ITEM_CODE = #{categoryItemCode}
		 LIMIT 0, 1
	</select>

	<insert id="insertHospitalOther" parameterType="hashmap">
		<selectKey resultType="HashMap" keyProperty="categoryKey" order="BEFORE">
			SELECT
			(SELECT MU_CATEGORY_ID
			FROM MU_CATEGORY
			WHERE CATEGORY_CODE = #{categoryCode}) AS muCategoryId,

			(SELECT CONCAT('C', DATE_FORMAT(NOW(), '%Y%m%d'), LPAD(IFNULL(count(1)+1, 1), 4, '0'))
			FROM MU_CATEGORY_ITEM
			WHERE CATEGORY_CODE = #{categoryCode}) AS categoryItemCode
		</selectKey>
		INSERT INTO MU_CATEGORY_ITEM (
		 MU_CATEGORY_ITEM_ID
		,MU_CATEGORY_ID
		,CATEGORY_CODE
		,CATEGORY_ITEM_CODE
		,CATEGORY_ITEM_NAME
		,ORDER_NUMBER
		<if test="filePath != null and filePath != ''">
			,FILE_PATH
		</if>
		<if test="fileName != null and fileName != ''">
			,FILE_NAME
		</if>
		<if test="originalFileName != null and originalFileName != ''">
			,ORIGINAL_FILE_NAME
		</if>
		<if test="etc != null and etc != ''">
			,ETC
		</if>
		,USE_YN
		,REG_ID
		,REG_DATE
		,MOD_ID
		,MOD_DATE
		,T_ID
		,T_DATE
		)
		VALUES(
		 CONCAT(#{id},LPAD((SELECT NEXTVAL(MU_CATEGORY_SEQ)), 9, 0))
		,#{categoryKey.muCategoryId}
		,#{categoryCode}
		,#{categoryKey.categoryItemCode}
		,#{categoryItemName}
		,#{orderNumber}
		<if test="filePath != null and filePath != ''">
			,#{filePath}
		</if>
		<if test="fileName != null and fileName != ''">
			,#{fileName}
		</if>
		<if test="originalFileName != null and originalFileName != ''">
			,#{originalFileName}
		</if>
		<if test="etc != null and etc != ''">
			,#{etc}
		</if>
		,'Y'
		,#{regId}
		,NOW()
		,#{modId}
		,NOW()
		,#{tId}
		,#{tDate}
		)
	</insert>

	<update id="updateHospitalOther" parameterType="hashmap">
		<selectKey resultType="String" keyProperty="muCategoryId" order="BEFORE">
			SELECT MU_CATEGORY_ID
			FROM MU_CATEGORY
			WHERE CATEGORY_CODE = #{categoryCode}
		</selectKey>
		UPDATE MU_CATEGORY_ITEM
		   SET
			MU_CATEGORY_ID 					= #{muCategoryId}
			,CATEGORY_CODE					=#{categoryCode}
			,CATEGORY_ITEM_NAME 			=#{categoryItemName}
			,ORDER_NUMBER                   =#{orderNumber}
			<if test="filePath != null and filePath != ''">
			,FILE_PATH 						=#{filePath}
			</if>
			<if test="fileName != null and fileName != ''">
			,FILE_NAME 						=#{fileName}
			</if>
			<if test="originalFileName != null and originalFileName != ''">
			,ORIGINAL_FILE_NAME 			=#{originalFileName}
			</if>
			<if test="etc != null and etc != ''">
			,ETC 							=#{etc}
			</if>
			,CUD_FLAG 						= 'U'
			,USE_YN 						= 'Y'
			,MOD_ID 						=#{modId}
			,MOD_DATE 						= NOW()
			,T_ID 							=#{tId}
			,T_DATE 						=#{tDate}
		WHERE CATEGORY_ITEM_CODE = #{categoryItemCode}
		AND CATEGORY_CODE      = #{existingCategoryCode}
	</update>

	<update id="updateCategoryItemOrderNumber" parameterType="hashmap">
		UPDATE MU_CATEGORY_ITEM AS MM
		   SET ORDER_NUMBER = (SELECT NUM.NEW_ORDER_NUMBER  AS newOrderNumber
								 FROM (
									   SELECT MM2.MU_CATEGORY_ITEM_ID                            AS MU_CATEGORY_ITEM_ID
											 ,ROW_NUMBER() OVER (ORDER BY MM2.USE_YN DESC
											                             ,MM2.ORDER_NUMBER ASC
											                             ,MM2.MOD_DATE DESC)     AS NEW_ORDER_NUMBER
										 FROM MU_CATEGORY_ITEM AS MM2
										WHERE MM2.USE_YN = 'Y'
										  AND MM2.CATEGORY_CODE = #{categoryCode}
								 ) AS NUM
								WHERE MM.MU_CATEGORY_ITEM_ID = NUM.MU_CATEGORY_ITEM_ID)
		 WHERE MM.USE_YN = 'Y'
		   AND MM.CATEGORY_CODE = #{categoryCode}
	</update>
</mapper>