<?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.carapi.sys.mapper.RoleMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ningdatech.carapi.sys.entity.Role">
        <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="category" jdbcType="VARCHAR" property="category"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="code" jdbcType="VARCHAR" property="code"/>
        <result column="describe_" jdbcType="VARCHAR" property="describe"/>
        <result column="state" jdbcType="BIT" property="state"/>
        <result column="readonly_" jdbcType="BIT" property="readonly"/>
    </resultMap>

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

    <!-- 通用查询结果列 -->
    <sql id="R_Column_List">
        r.id,  r.name, r.code, r.describe_, r.state, r.readonly_, r.category
    </sql>

    <select id="findRoleByUserId" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="R_Column_List"/>
        from c_role r INNER JOIN c_user_role ur on r.id = ur.role_id
        where state = true and ur.user_id = #{userId, jdbcType=BIGINT}
    </select>


    <select id="findUserIdByCode" parameterType="map" resultType="java.lang.Long">
        select ur.user_id from c_user_role ur INNER JOIN c_role r on r.id = ur.role_id
        where r.state = true
        and r.`code` in
        <foreach close=")" collection="codes" item="code" open="(" separator=",">
            #{code}
        </foreach>
    </select>
</mapper>
