<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itbaizhan.trave.mapper.AdminMapper">

    <resultMap id="adminMap" type="com.itbaizhan.trave.pojo.Admin">
        <id property="aid" column="aid"></id>
        <result property="username" column="username"></result>
        <collection property="roles" column="aid" ofType="com.itbaizhan.trave.pojo.Role">
            <result property="roleName" column="roleName"></result>
            <result property="roleDesc" column="roleDesc"></result>
            <collection property="permissions" column="rid" ofType="com.itbaizhan.trave.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 a
        left join admin_role ar on a.aid = ar.aid
        left join role r on ar.rid = r.rid
        left join role_permission rp on r.rid = rp.rid
        left join permission p on rp.pid = p.pid
        where a.aid = #{aid}
    </select>

    <delete id="deleteAdminAllRole" parameterType="int">
        delete from admin_role
        where aid = #{aid}
    </delete>

    <insert id="addAdminRole">
        insert into admin_role values (#{aid},#{rid})
    </insert>

    <select id="finAllPermission" parameterType="String" resultType="com.itbaizhan.trave.pojo.Permission">
        select distinct p.* from admin a
        left join admin_role ar on a.aid = ar.aid
        left join role r on ar.rid = r.rid
        left join role_permission rp on r.rid = rp.rid
        left join permission p on rp.pid = p.pid
        where a.username = #{username}
        and p.pid is not null
    </select>

</mapper>