<?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.zyd.blog.persistence.mapper.SysResourceMapper">
    <resultMap id="rm" type="com.zyd.blog.persistence.beans.SysResources">
        <result property="id" jdbcType="BIGINT" column="id"/>
        <result property="name" jdbcType="VARCHAR" column="name"/>
        <result property="type" jdbcType="VARCHAR" column="type"/>
        <result property="url" jdbcType="VARCHAR" column="url"/>
        <result property="permission" jdbcType="VARCHAR" column="permission"/>
        <result property="parentId" jdbcType="BIGINT" column="parent_id"/>
        <result column="sort" jdbcType="INTEGER" property="sort"/>
        <result property="external" jdbcType="BIT" column="external"/>
        <result property="available" jdbcType="BIT" column="available"/>
        <result property="icon" jdbcType="VARCHAR" column="icon"/>
        <result property="checked" jdbcType="VARCHAR" column="checked"/>
        <association property="parent" javaType="com.zyd.blog.persistence.beans.SysResources">
            <result property="id" jdbcType="BIGINT" column="parent_id"/>
            <result property="name" jdbcType="VARCHAR" column="parent_name"/>
            <result property="type" jdbcType="VARCHAR" column="parent_type"/>
            <result property="url" jdbcType="VARCHAR" column="parent_url"/>
            <result property="parentId" jdbcType="BIGINT" column="parent_parent_id"/>
            <result property="permission" jdbcType="VARCHAR" column="parent_permission"/>
            <result property="available" jdbcType="BIT" column="parent_available"/>
            <result property="icon" jdbcType="VARCHAR" column="parent_icon"/>
        </association>
        <collection property="nodes" column="node_id" javaType="ArrayList" ofType="com.zyd.blog.persistence.beans.SysResources">
            <result property="id" jdbcType="BIGINT" column="node_id"/>
            <result property="name" jdbcType="VARCHAR" column="node_name"/>
            <result property="type" jdbcType="VARCHAR" column="node_type"/>
            <result property="url" jdbcType="VARCHAR" column="node_url"/>
            <result property="parentId" jdbcType="BIGINT" column="node_parent_id"/>
            <result property="permission" jdbcType="VARCHAR" column="node_permission"/>
            <result property="available" jdbcType="BIT" column="node_available"/>
            <result property="icon" jdbcType="VARCHAR" column="node_icon"/>
        </collection>
    </resultMap>
    <select id="findPageBreakByCondition" parameterType="com.zyd.blog.business.vo.ResourceConditionVO" resultMap="rm">
        SELECT
            com.*,
            f.id parent_id,
            f.`name` parent_name,
            f.`icon` parent_icon,
            f.type parent_type
        FROM
            sys_resources com
        LEFT JOIN sys_resources f ON com.parent_id = f.id
        WHERE
            1 = 1
            <if test="keywords !=null and keywords != ''">
                AND (
                    com.name LIKE CONCAT('%',#{keywords , jdbcType=VARCHAR},'%') OR
                    com.url LIKE CONCAT('%',#{keywords , jdbcType=VARCHAR},'%') OR
                    com.permission LIKE CONCAT('%',#{keywords , jdbcType=VARCHAR},'%')
                )
            </if>
        ORDER by
            com.parent_id ASC,
            com.name DESC
    </select>

    <select id="listUrlAndPermission" resultMap="rm">
        SELECT
            url,
            permission
        FROM
            sys_resources
        WHERE
            url IS NOT NULL
        ORDER BY
            sort ASC
    </select>

    <select id="listUserResources" parameterType="java.util.HashMap" resultMap="rm">
        SELECT
            re.id,
            re.`name`,
            re.parent_id,
            re.url,
            re.permission,
            re.icon,
            node.id AS node_id,
            node.`name` AS node_name,
            node.`type` AS node_type,
            node.`url` AS node_url,
            node.parent_id AS node_parent_id,
            node.`permission` AS node_permission,
            node.`available` AS node_available,
            node.icon AS node_icon
        FROM
            sys_resources re
        LEFT JOIN sys_role_resources rr ON re.id = rr.resources_id
        LEFT JOIN sys_user_role ur ON rr.role_id = ur.role_id
        LEFT JOIN sys_resources node ON node.parent_id = re.id AND node.available = 1
        WHERE
            (re.parent_id = 0 OR re.parent_id IS NULL )
        AND re.available = 1
        <if test="userId != null">
            AND ur.user_id = #{userId}
        </if>
        <if test="type != null">
            AND re.type= #{type}
        </if>
        ORDER BY
            re.sort ASC,
            node.sort ASC
    </select>

    <select id="queryResourcesListWithSelected" parameterType="Long" resultMap="rm">
		SELECT
            re.id,
            re.`name`,
            re.parent_id,
            re.url,
            re.type,
            re.icon,
            (
                CASE
                WHEN EXISTS (
                    SELECT
                        1
                    FROM
                        sys_role_resources rr
                    WHERE
                        rr.resources_id = re.id
                    AND rr.role_id = #{rid}
                )
                    THEN
                        'true'
                    ELSE
                        'false'
                    END
            ) AS checked
        FROM
            sys_resources re
        ORDER BY
            re.sort ASC
	</select>

    <select id="listAllAvailableMenu" resultMap="rm">
		SELECT
            r.id,
            r.`name`,
            node.id AS node_id,
            node.`name` AS node_name,
            node.parent_id
        FROM
            sys_resources r
        LEFT JOIN sys_resources node ON (
            node.parent_id = r.id
            AND node.available = 1
            AND node.type = 'menu'
        )
        WHERE
            r.available = 1
        AND r.type = 'menu'
        AND (r.url IS NULL OR r.url = '')
        AND (r.permission IS NULL OR r.permission = '')
        ORDER BY
            r.sort ASC,
            node.sort ASC
	</select>

    <select id="listByUserId" parameterType="Long" resultMap="rm">
		SELECT
            re.id,
            re.`name`,
            re.parent_id,
            re.url,
            re.permission,
            re.icon,
            re.sort
        FROM
            sys_resources re
        INNER JOIN sys_role_resources rr ON re.id = rr.resources_id
        INNER JOIN sys_user_role ur ON rr.role_id = ur.role_id
        WHERE
            ur.user_id = #{userId}
        AND re.available = 1
        ORDER BY
            re.parent_id ASC,
            re.sort ASC
	</select>
</mapper>

