<?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.macro.mall.dao.UmsAdminRoleRelationDao">
    <!--批量新增回写主键支持-->
    <insert id="insertList">
        INSERT INTO ums_admin_role_relation (admin_id, role_id) VALUES
        <foreach collection="list" separator="," item="item" index="index">
            (#{item.adminId,jdbcType=BIGINT},
            #{item.roleId,jdbcType=BIGINT})
        </foreach>
    </insert>
    <select id="getRoleList" resultMap="com.macro.mall.mapper.UmsRoleMapper.BaseResultMap">
        select r.*
        from ums_admin_role_relation ar left join ums_role r on ar.role_id = r.id
        where ar.admin_id = #{adminId}
    </select>
    <select id="getRolePermissionList" resultMap="com.macro.mall.mapper.UmsPermissionMapper.BaseResultMap">
        select p.*
        from ums_admin_role_relation ar left join ums_role r on ar.role_id = r.id
            left join ums_role_permission_relation rp on r.id = rp.role_id
            left join ums_permission p on rp.permission_id=p.id
            where ar.admin_id = #{adminId} and p.id is not null
    </select>
    <select id="getPermissionList" resultMap="com.macro.mall.mapper.UmsPermissionMapper.BaseResultMap">
        SELECT
            p.*
        FROM
            ums_admin_role_relation ar
            LEFT JOIN ums_role r ON ar.role_id = r.id
            LEFT JOIN ums_role_permission_relation rp ON r.id = rp.role_id
            LEFT JOIN ums_permission p ON rp.permission_id = p.id
        WHERE
            ar.admin_id = #{adminId}
            AND p.id IS NOT NULL
            AND p.id NOT IN (
                SELECT
                    p.id
                FROM
                    ums_admin_permission_relation pr
                    LEFT JOIN ums_permission p ON pr.permission_id = p.id
                WHERE
                    pr.type = - 1
                    AND pr.admin_id = #{adminId}
            )
        UNION
        SELECT
            p.*
        FROM
            ums_admin_permission_relation pr
            LEFT JOIN ums_permission p ON pr.permission_id = p.id
        WHERE
            pr.type = 1
            AND pr.admin_id = #{adminId}
    </select>
</mapper>