<?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.securitycontrol.system.mapper.IRoleMapper">
    <!--新增/修改角色-->
    <insert id="addOrUpdateRole">
        <if test="type == 1">
            INSERT INTO sys_role
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="roleName != null and roleName != ''">role_name,</if>
                <if test="roleSort != null and roleSort!=''">role_sort,</if>
                <if test="createTime != null and createTime!=''">create_time,</if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="roleName != null and roleName != ''">#{roleName},</if>
                <if test="roleSort != null and roleSort!=''">#{roleSort},</if>
                <if test="createTime != null and createTime!=''">#{createTime},</if>
            </trim>
        </if>
        <if test="type == 2">
            UPDATE sys_role
            <set>
                <if test="roleName != null and roleName != ''">role_name = #{roleName},</if>
                <if test="roleSort != null and roleSort != ''">role_sort = #{roleSort},</if>
            </set>
            WHERE role_id = #{roleId}
        </if>
    </insert>
    <!--新增角色授权的菜单数据-->
    <insert id="addRoleMenu">
        INSERT INTO sys_role_menu(role_id,menu_id) VALUES
        <foreach collection="menuIds" separator="," item="item">
            (
            #{roleId},#{item}
            )
        </foreach>
    </insert>
    <!--删除角色-->
    <update id="deleteRole">
        UPDATE sys_role SET del_flag = 1 WHERE role_id = #{roleId}
    </update>
    <!--删除角色已授权的菜单数据-->
    <delete id="delRoleMenu">
        DELETE FROM sys_role_menu WHERE role_id = #{roleId}
    </delete>

    <!--角色列表-->
    <select id="getRoleLists" resultType="com.securitycontrol.entity.system.vo.RoleVo">
        SELECT role_id AS roleId,
        role_name AS roleName,
        role_sort AS roleSort,
        create_time AS createTime
        FROM sys_role
        <where>
            <if test="roleName!=null and roleName!=''">
                INSTR(role_name,#{roleName}) > 0
            </if>
            AND del_flag = 0
        </where>
        ORDER BY role_sort, create_time
    </select>
    <!--角色是否存在-->
    <select id="isRoleName" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM sys_role WHERE role_name = #{roleName} AND del_flag = 0
    </select>
    <!--角色详情-->
    <select id="getRoleById" resultType="com.securitycontrol.entity.system.vo.RoleVo">
        SELECT role_id AS roleId,
        role_name AS roleName,
        role_sort AS roleSort
        FROM sys_role WHERE role_id = #{roleId} AND del_flag = 0
    </select>
    <!--是否存在用户使用该角色-->
    <select id="isUserUse" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM sys_user WHERE role_id = #{roleId} AND del_flag = 0
    </select>
    <!--获取角色授权的菜单-->
    <select id="getAuthMenu" resultType="java.lang.String">
        SELECT menu_id FROM sys_role_menu WHERE role_id = #{roleId}
    </select>
</mapper>