<?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="com.wsk.travelf.mapper.AdminMapper">

    <resultMap id="adminMap" type="com.wsk.travelf.pojo.Admin">
        <id property="aid" column="aid"></id>
        <result property="username" column="username"></result>
        <collection property="roles" column="aid" ofType="com.wsk.travelf.pojo.Role">
            <result property="roleName" column="roleName"></result>
            <result property="roleDesc" column="roleDesc"></result>
            <collection property="permissions" column="rid" ofType="com.wsk.travelf.pojo.Permission">
                <result property="permissionName" column="permissionName"></result>
                <result property="permissionDesc" column="permissionDesc"></result>
            </collection>
        </collection>
    </resultMap>
    <select id="findDesc" parameterType="int" resultMap="adminMap">
        SELECT * FROM admin
              LEFT JOIN admin_role ON admin.aid = admin_role.aid
              LEFT JOIN role ON admin_role.rid = role.rid
              LEFT JOIN role_permission ON role.rid = role_permission.rid
              LEFT JOIN permission ON role_permission.pid = permission.pid
        WHERE admin.aid = #{aid};
    </select>

    <delete id="deleteAdminAllRoles" parameterType="int">
        delete from admin_role where aid = #{aid}
    </delete>
    
    <insert id="addAdminRole" parameterType="int">
        insert into admin_role values (#{aid},#{rid})
    </insert>

    <select id="findAllPermission" parameterType="string" resultType="com.wsk.travelf.pojo.Permission">
        SELECT DISTINCT permission.* FROM admin
              LEFT JOIN admin_role ON admin.aid = admin_role.aid
              LEFT JOIN role ON admin_role.rid = role.rid
              LEFT JOIN role_permission ON role.rid = role_permission.rid
              LEFT JOIN permission ON role_permission.pid = permission.pid
        WHERE admin.username = #{username} and permission.pid is not null;
    </select>
    
</mapper>