<?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.lframework.xingyun.template.inner.mappers.system.SysMenuMapper">

    <resultMap id="MenuDto" type="com.lframework.xingyun.template.inner.dto.MenuDto">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="path" column="path"/>
        <result property="hidden" column="hidden"/>
        <result property="display" column="display"/>
        <result property="componentType" column="component_type"/>
        <result property="component" column="component"/>
        <result property="requestParam" column="request_param"/>
        <result property="parentId" column="parent_id"/>
        <result property="meta.title" column="title"/>
        <result property="meta.icon" column="icon"/>
        <result property="meta.noCache" column="no_cache"/>
    </resultMap>

    <sql id="MenuDto_sql">
        SELECT id,
               name,
               path,
               icon,
               hidden,
               display,
               component_type,
               component,
               request_param,
               parent_id,
               title,
               no_cache
        FROM sys_menu
    </sql>

    <sql id="SysMenuDto_sql">
        SELECT
            *
        FROM sys_menu
    </sql>

    <select id="query" resultType="com.lframework.xingyun.template.inner.entity.SysMenu">
        <include refid="SysMenuDto_sql"/>
        <where>
            <if test="moduleIds != null">
                <if test="moduleIds.isEmpty()">
                    AND sys_module_id IS NULL
                </if>
                <if test="!moduleIds.isEmpty()">
                    AND (sys_module_id IN <foreach collection="moduleIds" open="(" separator="," close=")" item="item">#{item}</foreach> OR sys_module_id IS NULL)
                </if>
            </if>
        </where>
        ORDER BY code
    </select>
    <select id="selector" resultType="com.lframework.xingyun.template.inner.entity.SysMenu">
        <include refid="SysMenuDto_sql"/>
        <where>
            <if test="vo != null">
                <if test="vo.display != null">
                    AND display = #{vo.display}
                </if>
            </if>
            <if test="moduleIds != null">
                <if test="moduleIds.isEmpty()">
                    AND sys_module_id IS NULL
                </if>
                <if test="!moduleIds.isEmpty()">
                    AND (sys_module_id IN <foreach collection="moduleIds" open="(" separator="," close=")" item="item">#{item}</foreach> OR sys_module_id IS NULL)
                </if>
            </if>
            AND display IN (0, 1)
            <if test="vo.available != null">
                AND available = #{vo.available}
            </if>
        </where>
        ORDER BY code
    </select>
    <select id="findById" resultType="com.lframework.xingyun.template.inner.entity.SysMenu">
        <include refid="SysMenuDto_sql"/>
        WHERE id = #{id}
    </select>
    <select id="getChildrenById" resultType="com.lframework.xingyun.template.inner.entity.SysMenu">
        <include refid="SysMenuDto_sql"/>
        WHERE parent_id = #{id}
    </select>
    <select id="getByRoleId" resultType="com.lframework.xingyun.template.inner.entity.SysMenu">
        <include refid="SysMenuDto_sql"/>
        WHERE id IN (
        SELECT
        menu_id
        FROM sys_role_menu
        WHERE role_id = #{roleId}
        )
        <if test="moduleIds != null">
            <if test="moduleIds.isEmpty()">
                AND sys_module_id IS NULL
            </if>
            <if test="!moduleIds.isEmpty()">
                AND (sys_module_id IN <foreach collection="moduleIds" open="(" separator="," close=")" item="item">#{item}</foreach> OR sys_module_id IS NULL)
            </if>
        </if>
        ORDER BY code
    </select>

    <insert id="collectMenu">
        INSERT INTO sys_menu_collect (id, user_id, menu_id) VALUES (#{id}, #{userId}, #{menuId})
    </insert>
    <delete id="cancelCollectMenu">
        DELETE FROM sys_menu_collect WHERE user_id = #{userId} AND menu_id = #{menuId}
    </delete>

    <select id="getMenuByUserId" resultMap="MenuDto">
        <include refid="MenuDto_sql"/>
        <where>
            <if test="!isAdmin">
                AND id IN (
                SELECT rm.menu_id FROM sys_user_role AS ur
                LEFT JOIN sys_role_menu AS rm ON rm.role_id = ur.role_id
                LEFT JOIN sys_role AS r ON r.id = rm.role_id
                WHERE ur.user_id = #{userId}
                AND r.available = TRUE
                )
            </if>
            <if test="moduleIds != null">
                <if test="moduleIds.isEmpty()">
                    AND sys_module_id IS NULL
                </if>
                <if test="!moduleIds.isEmpty()">
                    AND (sys_module_id IN <foreach collection="moduleIds" open="(" separator="," close=")" item="item">#{item}</foreach> OR sys_module_id IS NULL)
                </if>
            </if>
            AND available = TRUE
            <!-- 只查询目录和菜单 -->
            AND display IN (0, 1)
        </where>
        ORDER BY code
    </select>
    <select id="getPermissionsByUserId" resultType="java.lang.String">
        SELECT permission
        FROM sys_menu
        <where>
            <if test="!isAdmin">
                AND id IN (
                SELECT rm.menu_id FROM sys_user_role AS ur
                LEFT JOIN sys_role_menu AS rm ON rm.role_id = ur.role_id
                LEFT JOIN sys_role AS r ON r.id = rm.role_id
                WHERE ur.user_id = #{userId}
                AND r.available = TRUE
                )
            </if>
            <if test="moduleIds != null">
                <if test="moduleIds.isEmpty()">
                    AND sys_module_id IS NULL
                </if>
                <if test="!moduleIds.isEmpty()">
                    AND (sys_module_id IN <foreach collection="moduleIds" open="(" separator="," close=")" item="item">#{item}</foreach> OR sys_module_id IS NULL)
                </if>
            </if>
            AND available = TRUE
            <!-- 只查询功能和权限 -->
            AND display IN (1, 2)
            AND permission IS NOT NULL
            AND permission != ''
        </where>
    </select>
    <select id="getCollectMenuIds" resultType="java.lang.String">
        SELECT
            mc.menu_id
        FROM sys_menu_collect AS mc
        WHERE mc.user_id = #{userId}
    </select>
    <select id="getRolePermissionByUserId" resultType="java.lang.String">
        SELECT permission FROM sys_role AS r
                                   LEFT JOIN sys_user_role AS ur ON ur.role_id = r.id
        WHERE ur.user_id = #{userId}
          AND r.available = TRUE
          AND r.permission IS NOT NULL
          AND r.permission != ''
    </select>
</mapper>
