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

	<select id="selectAuthMenuRelation" parameterType="hashMap" resultType="hashMap">
		SELECT MAMR.MU_AUTH_MENU_RELATION_ID         AS "muAuthMenuRelationId"
			  ,MM.MU_MENU_ID                         AS "muMenuId"
			  ,MA.MU_AUTH_ID                         AS "muAuthId"
			  ,MAMR.SELECT_USE_YN                    AS "selectUseYn"
			  ,MAMR.INSERT_USE_YN                    AS "insertUseYn"
			  ,MAMR.UPDATE_USE_YN                    AS "updateUseYn"
			  ,MAMR.DELETE_USE_YN                    AS "deleteUseYn"
			  ,MAMR.DOWNLOAD_USE_YN                  AS "downloadUseYn"
		  FROM MU_AUTH_MENU_RELATION AS MAMR
	INNER JOIN MU_MENU AS MM
			ON MM.MU_MENU_ID = MAMR.MU_MENU_ID
	INNER JOIN MU_AUTH AS MA
			ON MA.MU_AUTH_ID = MAMR.MU_AUTH_ID 
 		   AND MAMR.USE_YN   = 'Y'
 		   AND MM.USE_YN     = 'Y' 
 		   AND MA.USE_YN     = 'Y'
		   AND MM.MENU_CODE  = #{menuClass}
		   AND MA.MU_AUTH_ID = #{muAuthId}
		 LIMIT 0, 1
	</select>
	
	<select id="selectListAuthMenuRelation" parameterType="hashmap" resultType="hashmap">
		SELECT MM.MU_MENU_ID                         AS "muMenuId"
			  ,MM.MENU_CODE                          AS "menuCode"
			  ,MM.MENU_NAME                          AS "menuName"
			  ,MAMR.MU_AUTH_MENU_RELATION_ID         AS "muAuthMenuRelationId"
			  ,MAMR.MU_AUTH_ID                       AS "muAuthId"
			  ,MAMR.SELECT_USE_YN                    AS "selectUseYn"
			  ,MAMR.INSERT_USE_YN                    AS "insertUseYn"
			  ,MAMR.UPDATE_USE_YN                    AS "updateUseYn"
			  ,MAMR.DELETE_USE_YN                    AS "deleteUseYn"
			  ,MAMR.DOWNLOAD_USE_YN                  AS "downloadUseYn"
			  ,(SELECT COUNT(*)
				  FROM MU_MENU AS MM2
				 WHERE MM2.USE_YN = 'Y'
				   AND LENGTH(MM2.MENU_CODE) > 4
				   AND LEFT(MM2.MENU_CODE, 2) = MM.MENU_CODE
			   )                                     AS "subMenuCount"
		  FROM MU_MENU AS MM
	 LEFT JOIN MU_AUTH_MENU_RELATION AS MAMR
			ON MM.MU_MENU_ID = MAMR.MU_MENU_ID
		   AND MAMR.USE_YN      = 'Y'
		   AND MAMR.MU_AUTH_ID  = #{muAuthId}
		 WHERE MM.USE_YN             = 'Y'
		   AND LENGTH(MM.MENU_CODE) != 4
		   AND (SELECT COUNT(*)
                FROM MU_MENU AS MM2
                WHERE MM2.USE_YN = 'Y'
                 AND LENGTH(MM2.MENU_CODE) > 8
                 AND LEFT(MM2.MENU_CODE, 8) = MM.MENU_CODE
                ) &lt; 1
	  ORDER BY MM.MENU_CODE ASC
	</select>
	
	<insert id="insertAuthMenuRelation" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muAuthMenuRelationId},LPAD((SELECT NEXTVAL(MU_AUTH_MENU_RELATION_SEQ)), 9, 0))
		</selectKey>
		INSERT INTO MU_AUTH_MENU_RELATION(
					 MU_AUTH_MENU_RELATION_ID
					,MU_AUTH_ID
					,MU_MENU_ID
					,SELECT_USE_YN
					,INSERT_USE_YN
					,UPDATE_USE_YN
					,DELETE_USE_YN
					,DOWNLOAD_USE_YN
					,WRITE_DATE
					,WRITE_TIME
					,CUD_FLAG
					,USE_YN
					,REG_ID
					,REG_DATE
					,MOD_ID
					,MOD_DATE
					,T_ID
					,T_DATE
		)VALUES(
					 #{id}
					,#{muAuthId}
					,#{muMenuId}
					,#{selectUseYn}
					,#{insertUseYn}
					,#{updateUseYn}
					,#{deleteUseYn}
					,#{downloadUseYn}
					,CURDATE()
					,CURTIME()
					,'C'
					,'Y'
					,#{regId}
					,NOW()
					,#{modId}
					,NOW()
					,#{tId}
					,#{tDate}
		)
	</insert>
	
	<update id="deleteAuthMenuRelation" parameterType="hashmap">
		UPDATE MU_AUTH_MENU_RELATION
		   SET MOD_ID            = #{modId}
			  ,MOD_DATE	         = NOW()
			  ,CUD_FLAG          = 'D'
			  ,USE_YN            = 'N'
		 WHERE USE_YN        = 'Y'
		   AND MU_AUTH_ID  = #{muAuthId}
	</update>
</mapper>