<?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.kexue.skills.mapper.SysUserRoleMapper">

    <resultMap type="com.kexue.skills.entity.SysUserRole" id="SysUserRoleMap">
        <result property="roleId" column="role_id" jdbcType="INTEGER"/>
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="SysUserRoleMap">
        select
          role_id, user_id
        from sys_user_role
        where role_id = #{roleId}
    </select>

    <!--查询指定行数据-->
    <select id="getPageList" resultMap="SysUserRoleMap">
        select
          role_id, user_id
        from sys_user_role
        <where>
            <if test="roleId != null">
                and role_id = #{roleId}
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
        </where>
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="SysUserRoleMap">
        select
          role_id, user_id
        from sys_user_role
        limit #{offset}, #{limit}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="SysUserRoleMap">
        select
          role_id, user_id
        from sys_user_role
        <where>
            <if test="roleId != null">
                and role_id = #{roleId}
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="roleId" useGeneratedKeys="true">
        insert into sys_user_role()
        values ()
    </insert>
    
    
    <insert id="insertBatch" parameterType="java.util.List">
    INSERT INTO sys_user_role (
        <!-- 动态生成列名 -->
        <foreach collection="list" item="item" index="index" open="" close="" separator="" >
            <if test="index == 0">
                <foreach collection="tableInfo.fullColumn" item="column" open="" close="" separator=",">
                    ${column.name}
                </foreach>
            </if>
        </foreach>
    ) VALUES
    <!-- 动态生成值 -->
    <foreach collection="list" item="item" index="index" open="" close="" separator=",">
        (
        <foreach collection="tableInfo.fullColumn" item="column" open="" close="" separator=",">
            #{item.${column.property}}
        </foreach>
        )
    </foreach>
</insert>

    <!--通过主键修改数据-->
    <update id="update">
        update sys_user_role
        <set>
        </set>
        where role_id = #{roleId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from sys_user_role where role_id = #{roleId}
    </delete>

    <!--通过用户 ID 查询角色编码列表（关联查询）-->
    <select id="queryRoleCodesByUserId" resultType="java.lang.String">
        select r.role_code
        from sys_user_role ur
        inner join sys_role r on ur.role_id = r.role_id
        where ur.user_id = #{userId}
          and r.delete_flag = '0'
    </select>

</mapper>
