<?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.ningdatech.pmapi.sys.mapper.ResourceMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ningdatech.pmapi.sys.model.entity.Resource">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="created_by" jdbcType="BIGINT" property="createBy"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createOn"/>
        <result column="updated_by" jdbcType="BIGINT" property="updateBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateOn"/>
        <result column="code" jdbcType="VARCHAR" property="code"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="menu_id" jdbcType="BIGINT" property="menuId"/>
        <result column="describe_" jdbcType="VARCHAR" property="describe"/>
        <result column="readonly_" jdbcType="BIT" property="readonly"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,created_by,create_time,updated_by,update_time,
        code, name, menu_id, describe_, readonly_
    </sql>


    <select id="findVisibleResource" parameterType="map" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        from nd_resource where 1=1
        and id in (
        SELECT authority_id FROM nd_role_authority ra INNER JOIN nd_user_role ur on ra.role_id = ur.role_id
        INNER JOIN c_role r on r.id = ra.role_id
        where ur.user_id = #{userId, jdbcType=BIGINT} and r.`state` = true
        and ra.authority_type = 'RESOURCE'
        )
    </select>

    <insert id="saveOrUpdateUnique" parameterType="com.ningdatech.pmapi.sys.model.entity.Resource">
    insert into nd_resource ( id, created_by, create_time, updated_by, 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},
      updated_by = #{updateUser,jdbcType=BIGINT},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    </insert>

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

</mapper>
