<?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.SysRoleMapper">

    <resultMap type="com.kexue.skills.entity.SysRole" id="SysRoleMap">
        <result property="roleId" column="role_id" jdbcType="INTEGER"/>
        <result property="roleCode" column="role_code" jdbcType="VARCHAR"/>
        <result property="roleName" column="role_name" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="deleteFlag" column="delete_flag" jdbcType="VARCHAR"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="SysRoleMap">
        select
          role_id, role_code, role_name, create_time, remark, delete_flag
        from sys_role
        where role_id = #{roleId}
    </select>

    <!--查询指定行数据-->
    <select id="getPageList" resultMap="SysRoleMap">
        select
          role_id, role_code, role_name, create_time, remark, delete_flag
        from sys_role
        <where>
            <if test="roleId != null">
                and role_id = #{roleId}
            </if>
            <if test="roleCode != null and roleCode != ''">
                and role_code = #{roleCode}
            </if>
            <if test="roleName != null and roleName != ''">
                and role_name = #{roleName}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="remark != null and remark != ''">
                and remark = #{remark}
            </if>
            <if test="deleteFlag != null and deleteFlag != ''">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="SysRoleMap">
        select
          role_id, role_code, role_name, create_time, remark, delete_flag
        from sys_role
        limit #{offset}, #{limit}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="SysRoleMap">
        select
          role_id, role_code, role_name, create_time, remark, delete_flag
        from sys_role
        <where>
            <if test="roleId != null">
                and role_id = #{roleId}
            </if>
            <if test="roleCode != null and roleCode != ''">
                and role_code = #{roleCode}
            </if>
            <if test="roleName != null and roleName != ''">
                and role_name = #{roleName}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="remark != null and remark != ''">
                and remark = #{remark}
            </if>
            <if test="deleteFlag != null and deleteFlag != ''">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="roleId" useGeneratedKeys="true">
        insert into sys_role(role_code, create_time, remark, delete_flag)
        values (#{roleCode}, #{createTime}, #{remark}, #{deleteFlag})
    </insert>
    
    
    <insert id="insertBatch" parameterType="java.util.List">
    INSERT INTO sys_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_role
        <set>
            <if test="roleCode != null and roleCode != ''">
                role_code = #{roleCode},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="remark != null and remark != ''">
                remark = #{remark},
            </if>
            <if test="deleteFlag != null and deleteFlag != ''">
                delete_flag = #{deleteFlag},
            </if>
        </set>
        where role_id = #{roleId}
    </update>

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

</mapper>
