<?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.services.mapper.system.PowerMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.bunny.dao.entity.system.Power">
        <id column="id" property="id"/>
        <id column="create_time" property="createTime"/>
        <id column="update_time" property="updateTime"/>
        <id column="create_user" property="createUser"/>
        <id column="update_user" property="updateUser"/>
        <id column="is_deleted" property="isDeleted"/>
        <id column="parent_id" property="parentId"/>
        <id column="power_code" property="powerCode"/>
        <id column="power_name" property="powerName"/>
        <id column="request_url" property="requestUrl"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, create_time, update_time, create_user, update_user, is_deleted, parent_id, power_code, power_name, request_url
    </sql>

    <!-- 分页查询权限内容 -->
    <select id="selectListByPage" resultType="cn.bunny.dao.vo.system.rolePower.PowerVo">
        select
        power.*,
        create_user.username as create_username,
        update_user.username as update_username
        from sys_power power
        left join sys_user create_user on create_user.id = power.create_user
        left join sys_user update_user on update_user.id = power.update_user
        <where>
            power.is_deleted = 0
            <if test="dto.powerCode != null and dto.powerCode != ''">
                and power.power_code like CONCAT('%',#{dto.powerCode},'%')
            </if>
            <if test="dto.powerName != null and dto.powerName != ''">
                and power.power_name like CONCAT('%',#{dto.powerName},'%')
            </if>
            <if test="dto.requestUrl != null and dto.requestUrl != ''">
                and power.request_url like CONCAT('%',#{dto.requestUrl},'%')
            </if>
        </where>
    </select>

    <!-- 物理删除权限 -->
    <delete id="deleteBatchIdsWithPhysics">
        delete
        from sys_power
        where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <!-- 根据用户id查询当前用户所有权限 -->
    <select id="selectListByUserId" resultType="cn.bunny.dao.entity.system.Power">
        SELECT p.*
        FROM sys_user u,
             sys_user_role ur,
             sys_role_power rp,
             sys_power p
        WHERE u.id = ur.user_id
          AND u.is_deleted = 0
          AND ur.role_id = rp.role_id
          AND rp.power_id = p.id
          AND u.id = #{userId}
    </select>
</mapper>
