<?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="cn.stylefeng.guns.sys.modular.system.mapper.RoleMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.stylefeng.guns.sys.modular.system.entity.Role">
        <id column="role_id" property="roleId" />
        <result column="pid" property="pid" />
        <result column="name" property="name" />
        <result column="description" property="description" />
        <result column="sort" property="sort" />
        <result column="version" property="version" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="create_user" property="createUser" />
        <result column="update_user" property="updateUser" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        role_id AS "roleId", pid AS "pid", name AS "name", description AS "description", sort AS "sort", version AS "version", create_time AS "createTime", update_time AS "updateTime", create_user AS "createUser", update_user AS "updateUser"
    </sql>

    <select id="selectRoles" resultType="map">
        select
        <include refid="Base_Column_List"/>
        from sys_role
        <if test="condition != null">
            where name like CONCAT('%',#{condition},'%')
        </if>
        order by sort asc
    </select>

    <select id="selectRoles" resultType="map" databaseId="oracle">
        select
        <include refid="Base_Column_List"/>
        from sys_role
        <if test="condition != null">
            where name like '%' || #{condition} || '%'
        </if>
        order by sort asc
    </select>

    <delete id="deleteRolesById">
		delete from sys_relation where role_id = #{roleId}
	</delete>

    <select id="roleTreeList" resultType="cn.stylefeng.guns.base.pojo.node.ZTreeNode">
		select role_id AS id, pid as "pId",
		name as name, (case when (pid = 0 or pid is null) then 'true'
		else 'false' end) as "open" from sys_role
	</select>

    <select id="roleTreeList" resultType="cn.stylefeng.guns.base.pojo.node.ZTreeNode" databaseId="oracle">
		select role_id AS id, pid as "pId",
		name as name, (case when (pid = 0 or pid is null) then '1'
		else '0' end) as "open" from sys_role
	</select>

    <select id="roleTreeListByRoleId" resultType="cn.stylefeng.guns.base.pojo.node.ZTreeNode">
        SELECT
        r.role_id as id,
        pid as "pId",
        name AS "name",
        (
        CASE
        WHEN (pid = 0 OR pid IS NULL) THEN
        'true'
        ELSE
        'false'
        END
        ) as "open",
        (
        CASE
        WHEN (r1.role_id = 0 OR r1.role_id IS NULL) THEN
        'false'
        ELSE
        'true'
        END
        ) as "checked"
        FROM
        sys_role r
        LEFT JOIN (
        SELECT
        role_id
        FROM
        sys_role
        WHERE
        role_id IN

        <foreach collection="array" index="index" item="i" open="(" separator="," close=")">
            #{i}
        </foreach>

        ) r1 ON r.role_id = r1.role_id
        ORDER BY pid,sort ASC
    </select>

    <select id="roleTreeListByRoleId" resultType="cn.stylefeng.guns.base.pojo.node.ZTreeNode" databaseId="oracle">
        SELECT
        r.role_id as id,
        pid as "pId",
        name AS "name",
        (
        CASE
        WHEN (pid = 0 OR pid IS NULL) THEN
        '1'
        ELSE
        '0'
        END
        ) as "open",
        (
        CASE
        WHEN (r1.role_id = 0 OR r1.role_id IS NULL) THEN
        '0'
        ELSE
        '1'
        END
        ) as "checked"
        FROM
        sys_role r
        LEFT JOIN (
        SELECT
        role_id
        FROM
        sys_role
        WHERE
        role_id IN

        <foreach collection="array" index="index" item="i" open="(" separator="," close=")">
            #{i}
        </foreach>

        ) r1 ON r.role_id = r1.role_id
        ORDER BY pid,sort ASC
    </select>

</mapper>
