<?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">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
<mapper namespace="com.iamberry.wechat.service.mapper.AdminChildPermissionsMapper">

	<!-- 一级菜单所有字段，并可以映射实体类 -->
	<sql id="permissionsField">
		p_id permissionsId, p_name permissionsName, p_url permissionsUrl, p_desc permissionsDesc, p_img permissionsImg
	</sql>
	
	<!-- 二级菜单所有字段，并可以映射实体类 -->
	<sql id="ermissionField">
		e_id ermissionsId, e_name ermissionsName, e_url ermissionsUrl, 
		e_desc ermissionsDesc, p_id permissionId, e_status ermissionsStatus, e_img ermissionsImg
	</sql>
	
	<!-- 二级管理员拥有的权限字段，并映射实体类 -->
	<sql id="haveFidle">
		a_id haveId, permissions_id permissionId, adminChild_id adminChildId
	</sql>
	
	<!-- 根据二级管理员ID，获取所有的权限URL信息 -->
	<select id="selectAdminChildHavePermissionById" parameterType="int" resultType="AdminChildPermissionErmissions">
		(SELECT E_URL ermissionsUrl FROM TB_ADMIN_MENU_ERMISSIONS WHERE E_ID IN (SELECT PERMISSIONS_ID FROM TB_ADMIN_ADMINCHILD_HAVE_PERMISSIONS WHERE ADMINCHILD_ID = #{id}) ORDER BY E_ID DESC)
		UNION
		(SELECT A_URL ermissionsUrl FROM TB_ADMIN_MENU_PERMISSION_AUXILIARY WHERE E_ID IN ((SELECT E_ID ERMISSIONSURL FROM TB_ADMIN_MENU_ERMISSIONS WHERE E_ID IN (SELECT PERMISSIONS_ID FROM TB_ADMIN_ADMINCHILD_HAVE_PERMISSIONS WHERE ADMINCHILD_ID = #{id}))) ORDER BY A_ID DESC)
	</select>
	
	<!-- 根据二级管理员ID，获取所有的权限信息（权限实体类） -->
	<select id="selectAdminChildPermissionEntityById" parameterType="int" resultType="AdminChildPermissionErmissions">
		(SELECT 
			<include refid="ermissionField"/> 
		FROM 
			TB_ADMIN_MENU_ERMISSIONS 
		WHERE 
			E_ID 
		IN 
			(
				SELECT 
					PERMISSIONS_ID 
				FROM 
					TB_ADMIN_ADMINCHILD_HAVE_PERMISSIONS 
				WHERE 
					ADMINCHILD_ID = #{id}
			)
		) 
		ORDER BY E_ID DESC
	</select>
	
	<!-- 一级菜单映射map -->
	<resultMap type="AdminChildPermissions" id="MenuPermissionMap">
		<id column="p_id" property="permissionsId"/>
		<result column="p_name" property="permissionsName"/>
		<result column="p_url" property="permissionsUrl"/>
		<result column="p_desc" property="permissionsDesc"/>
		<result column="p_img" property="permissionsImg"/>
		<collection property="menus" column="p_id" javaType="ArrayList" select="selectErmissionsByAdminChild"/>
	</resultMap>
	
	<!-- 获取一级菜单的信息 -->
	<select id="selectAllPermission" resultType="AdminChildPermissions">
		SELECT 
			<include refid="permissionsField"/>
		FROM 
			TB_ADMIN_MENU_PERMISSIONS 
		ORDER BY P_ID DESC
	</select>
	
	<!-- 超级管理员的菜单信息 -->
	<select id="selectAdminMenuInfoByAdminId" parameterType="int" resultMap="MenuPermissionMap">
		SELECT 
			* 
		FROM 
			TB_ADMIN_MENU_PERMISSIONS 
		ORDER BY P_ID DESC
	</select>
	
	<!-- 根据一级菜单ID，获取所属的二级菜单信息 -->
	<select id="selectErmissionsByAdminChild" parameterType="int" resultType="AdminChildPermissionErmissions">
		SELECT <include refid="ermissionField"/> FROM TB_ADMIN_MENU_ERMISSIONS WHERE P_ID = #{id} AND E_STATUS = 0
	</select>
	
	<!-- 获取二级管理员的权限菜单 -->
	<select id="selectAdinChildPermissionByAdminChildId" parameterType="int" resultMap="MenuPermissionMap">
		SELECT 
			* 
		FROM 
			TB_ADMIN_MENU_PERMISSIONS 
		WHERE 
			P_ID IN (
				SELECT 
					DISTINCT P_ID 
				FROM 
					TB_ADMIN_MENU_ERMISSIONS 
				WHERE 
					E_ID 
				IN 
					(
						SELECT 
							PERMISSIONS_ID 
						FROM 
							TB_ADMIN_ADMINCHILD_HAVE_PERMISSIONS 
						WHERE 
							ADMINCHILD_ID = #{id}
					)
			) 
			ORDER BY P_ID DESC
	</select>
	<!-- 根据二级管理员ID，获取所属的二级管理员权限 -->
	<select id="selectPermissionById" parameterType="Integer" resultType="AdminChildHavePermission">
		SELECT  <include refid="haveFidle"/> FROM TB_ADMIN_ADMINCHILD_HAVE_PERMISSIONS WHERE ADMINCHILD_ID = #{id} ORDER BY A_ID DESC
	</select>
	<!-- 根据二级管理员的ID，新增二级管理员权限  -->
	<insert id="insertPermissionById" parameterType="AdminChildHavePermission">
	INSERT INTO TB_ADMIN_ADMINCHILD_HAVE_PERMISSIONS(a_id,permissions_id,adminChild_id)
		  VALUES ( 
		  #{adminChildHavePermission.haveId},  
		  #{adminChildHavePermission.permissionId}, 
		  #{adminChildHavePermission.adminChildId}
		  )
	</insert>
	<!-- 根据二级管理员的ID，删除所有二级管理员权限  -->
	<delete id="deletePermissionById" parameterType="Integer">
			delete FROM TB_ADMIN_ADMINCHILD_HAVE_PERMISSIONS WHERE adminChild_id = #{adminChildId }
	</delete>
</mapper>
