<?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.github.wxiaoqi.security.admin.mapper.ElementMapper">
    <resultMap id="BaseResultMap" type="com.github.wxiaoqi.security.admin.entity.Element">
        <!--
          WARNING - @mbggenerated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="code" jdbcType="VARCHAR" property="code"/>
        <result column="type" jdbcType="VARCHAR" property="type"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="uri" jdbcType="VARCHAR" property="uri"/>
        <result column="menu_id" jdbcType="VARCHAR" property="menuId"/>
        <result column="parent_id" jdbcType="VARCHAR" property="parentId"/>
        <result column="path" jdbcType="VARCHAR" property="path"/>
        <result column="method" jdbcType="VARCHAR" property="method"/>
        <result column="description" jdbcType="VARCHAR" property="description"/>
        <result column="crt_time" jdbcType="DATE" property="crtTime"/>
        <result column="crt_user" jdbcType="VARCHAR" property="crtUser"/>
        <result column="crt_name" jdbcType="VARCHAR" property="crtName"/>
        <result column="crt_host" jdbcType="VARCHAR" property="crtHost"/>
        <result column="attr1" jdbcType="VARCHAR" property="attr1"/>
        <result column="attr2" jdbcType="VARCHAR" property="attr2"/>
        <result column="attr3" jdbcType="VARCHAR" property="attr3"/>
        <result column="attr4" jdbcType="VARCHAR" property="attr4"/>
        <result column="attr5" jdbcType="VARCHAR" property="attr5"/>
        <result column="attr6" jdbcType="VARCHAR" property="attr6"/>
        <result column="attr7" jdbcType="VARCHAR" property="attr7"/>
        <result column="attr8" jdbcType="VARCHAR" property="attr8"/>
    </resultMap>
    <select id="selectAuthorityElementByUserId" resultMap="BaseResultMap">
    select distinct t.code,t.type,t.name,t.uri,t.method,m.title as menu_id from base_resource_authority ra
        inner join base_element t
       on ra.resource_id = t.id
        and ra.authority_id in (
          select group_id from base_group_member where user_id = #{userId}
          union select group_id from base_group_leader where user_id = #{userId}
        )
        and ra.authority_type = 'group'
        and ra.resource_type = 'button'
        inner join base_menu m
       on t.menu_id = m.id
  </select>
    <select id="selectAuthorityElementByAppUserId" resultMap="BaseResultMap">
    SELECT DISTINCT
        t.CODE,
        t.type,
        t.NAME,
        t.uri,
        t.method,
        m.title AS menu_id
    FROM
        base_resource_authority ra
        INNER JOIN base_element t ON ra.resource_id = t.id
        AND ra.authority_id IN ( SELECT group_id FROM app_group_member WHERE user_id = #{userId} )
        AND ra.authority_type = 'group'
        AND ra.resource_type = 'button'
        INNER JOIN base_menu m ON t.menu_id = m.id
  </select>
    <select id="selectAuthorityMenuElementByUserId" resultMap="BaseResultMap">
        select distinct t.* from base_resource_authority ra
        inner join base_element t
        on ra.resource_id = t.id
        and ra.authority_id in (
        select group_id from base_group_member where user_id = #{userId}
        union select group_id from base_group_leader where user_id = #{userId}
        )
        and ra.authority_type = 'group'
        and ra.resource_type = 'button'
        and t.menu_id = #{menuId}
    </select>
    <select id="selectAuthorityElementByClientId" resultMap="BaseResultMap">
        select distinct t.* from auth_client_service cs
         inner join base_element t
         on t.id = cs.service_id
         and cs.client_id = #{clientId}
    </select>
    <select id="selectAllElementPermissions" resultMap="BaseResultMap">
        select distinct t.code,t.type,t.name,t.uri,t.method,m.title as menu_id from base_element t
        inner join base_menu m
        on t.menu_id = m.id
    </select>
</mapper>