<?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="cn.bunny.service.mapper.AdminPowerMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.bunny.entity.system.admin.AdminPower">
        <id column="id" property="id"/>
        <result column="power_name" property="powerName"/>
        <result column="power_code" property="powerCode"/>
        <result column="description" property="description"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, power_name, power_code, description, create_time, update_time, update_user, is_delete
    </sql>

    <!-- 查询用户权限信息 -->
    <select id="selectByPowerWithRoleIdList">
        SELECT *
        FROM admin_power
        WHERE id IN (SELECT arp.power_id
        FROM admin_role_power arp
        WHERE arp.role_id IN <foreach collection="roleIdList" item="roleId" open="(" close=")" separator=",">
        #{roleId}
    </foreach> )
    </select>

    <!-- 查询用户权限 -->
    <select id="queryByUserIdWithPower" resultType="cn.bunny.entity.system.admin.AdminPower">
        SELECT ap.*
        FROM admin_user_role aur
                 INNER JOIN admin_role_power arp ON aur.role_id = arp.role_id
                 INNER JOIN admin_power ap ON ap.id = arp.power_id
        WHERE aur.user_id = #{userId}
    </select>
</mapper>
