<?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.zuihou.authority.dao.auth.ResourceMapper">

    <select id="findVisibleResource" parameterType="map" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        from c_auth_resource where 1=1
        <!-- 这段逻辑要放到程序中过滤 -->
        <!--        <if test="menuId != null">-->
        <!--            and menu_id = #{menuId, jdbcType=BIGINT}-->
        <!--        </if>-->
        <!--        <if test="type != null">-->
        <!--            and `resource_type` = #{type, jdbcType=VARCHAR}-->
        <!--        </if>-->
        and id in (
        SELECT authority_id FROM c_auth_role_authority ra INNER JOIN c_auth_user_role ur on ra.role_id = ur.role_id
        INNER JOIN c_auth_role r on r.id = ra.role_id
        where ur.user_id = #{userId, jdbcType=BIGINT} and r.`status` = true
        and ra.authority_type = 'RESOURCE'
        )
    </select>

    <insert id="saveOrUpdateUnique" parameterType="com.github.zuihou.authority.entity.auth.Resource">
    insert into c_auth_resource ( id, create_user, create_time, update_user, update_time,
        code, name, menu_id, describe_)
    values (#{id,jdbcType=BIGINT}, #{createUser,jdbcType=BIGINT}, #{createTime,jdbcType=TIMESTAMP},#{updateUser,jdbcType=BIGINT}, #{updateTime,jdbcType=TIMESTAMP},
      #{code,jdbcType=VARCHAR},  #{name,jdbcType=VARCHAR}, #{menuId,jdbcType=BIGINT}, #{tags,jdbcType=VARCHAR},  #{describe,jdbcType=VARCHAR}   )
    ON DUPLICATE KEY UPDATE
      name = #{name,jdbcType=VARCHAR},
      describe_ = #{describe,jdbcType=VARCHAR},
      update_user = #{updateUser,jdbcType=BIGINT},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    </insert>

    <select id="findMenuIdByResourceId" parameterType="map" resultType="java.lang.Long">
        SELECT DISTINCT menu_id from c_auth_resource where id in
        <foreach close=")" collection="resourceIdList" item="id" open="(" separator=",">
            #{id}
        </foreach>
    </select>

</mapper>
