<?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.boman.system.mapper.SysRoleDataMapper">

    <resultMap type="com.boman.domain.SysRoleData" id="SysRoleDataResult">
        <result property="id" column="id"/>
        <result property="roleId" column="role_id"/>
        <result property="roleName" column="role_name"/>
        <result property="roleSort" column="role_sort"/>
        <result property="tableName" column="table_name"/>
        <result property="dataScope" column="data_scope"/>
        <result property="isUse" column="is_use"/>
        <result property="isDel" column="is_del"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectSysRoleDataVo">
        select id, role_id, role_sort, table_name, data_scope, is_use, is_del, create_by, create_time, update_by, update_time, remark from sys_role_data
    </sql>

    <select id="selectSysRoleDataList" parameterType="com.boman.domain.SysRoleData" resultMap="SysRoleDataResult">
        select d.id, d.role_id, d.role_sort, d.table_name, d.data_scope, d.is_use, d.is_del, d.remark, r.role_name
        from sys_role_data d
        left join sys_role r on d.role_id = r.id
        <where>
            d.is_del = 'N'
            <if test="id != null and id != ''">
                AND d.id = #{id}
            </if>
            <if test="roleId != null and roleId != ''">
                AND d.role_id = #{roleId}
            </if>
            <if test="tableName != null and tableName != ''">
                AND d.table_name like concat('%', #{tableName}, '%')
            </if>
            <if test="dataScope != null and dataScope != ''">
                AND d.data_scope = #{dataScope}
            </if>
            <if test="isUse != null and isUse != ''">
                AND d.is_use = #{isUse}
            </if>
        </where>
        order by d.role_sort
    </select>

    <select id="selectSysRoleDataById" parameterType="Long" resultMap="SysRoleDataResult">
        <include refid="selectSysRoleDataVo"/>
        where is_del = 'N' and id = #{id} order by role_sort asc
    </select>

    <select id="listByRoleIdList" resultMap="SysRoleDataResult">
        <include refid="selectSysRoleDataVo"/>
        where is_del = 'N' and role_id in
        <foreach collection="roleIdList" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
        order by role_sort asc
    </select>

    <select id="listByRoleIdListTableName" resultMap="SysRoleDataResult">
        <include refid="selectSysRoleDataVo"/>
        where is_del = 'N' and table_name = #{tableName} and role_id in
        <foreach collection="roleIdList" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
        order by role_sort asc
    </select>

    <insert id="insertSysRoleData" parameterType="com.boman.domain.SysRoleData">
        insert into sys_role_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="roleId != null">role_id,</if>
            <if test="roleSort != null">role_sort,</if>
            <if test="tableName != null">table_name,</if>
            <if test="dataScope != null">data_scope,</if>
            <if test="isUse != null">is_use,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="roleId != null">#{roleId},</if>
            <if test="roleSort != null">#{roleSort},</if>
            <if test="tableName != null">#{tableName},</if>
            <if test="dataScope != null">#{dataScope},</if>
            <if test="isUse != null">#{isUse},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateSysRoleData" parameterType="com.boman.domain.SysRoleData">
        update sys_role_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="roleId != null">role_id = #{roleId},</if>
            <if test="roleSort != null">role_sort = #{roleSort},</if>
            <if test="tableName != null">table_name = #{tableName},</if>
            <if test="dataScope != null">data_scope = #{dataScope},</if>
            <if test="isUse != null">is_use = #{isUse},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysRoleDataById" parameterType="Long">
        delete from sys_role_data where id = #{id}
    </delete>

    <delete id="deleteSysRoleDataByIds" parameterType="String">
        delete from sys_role_data where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>