<?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.lantone.security.mapper.RoleSoftwareMenuMapper">
    <select id="getMenus" resultType="com.lantone.security.dto.GetMenusTreeDTO">
        SELECT
            m.id,
            m.`name`,
            m.parent_id,
            m.`status`,
            m.order_no,
            m.remark,
            m.type,
            (
            CASE
            WHEN m.type = 0 THEN
            "目录"
            WHEN m.type = 1 THEN
            "菜单"
            WHEN m.type = 2 THEN
            "按钮"
            WHEN m.type = 3 THEN
            "功能"
            ELSE
            "未知类型"
            END
            ) typeName,
            m.`code`,
            IF (
            m.`status` = 1,
            "启用",
            "禁用"
            ) statusName,
            r.id resourceId,
            r.url permission
        FROM
        sys_software_menu sm,
        sys_role_software_menu rsm,
        sys_menu m
        LEFT JOIN sys_menu_resource mr ON m.id = mr.menu_id
        AND mr.is_deleted = 'N'
        LEFT JOIN sys_resource r ON mr.resource_id = r.id
        AND r.is_deleted = 'N'
        WHERE m.id = sm.menu_id
        AND sm.id = rsm.software_menu_id
        AND m.creator = #{roleSoftwareMenuVos.creator}
        <if test="roleSoftwareMenuVos.softwareId != null and roleSoftwareMenuVos.softwareId !=''">
            AND sm.software_id = #{roleSoftwareMenuVos.softwareId}
        </if>
        <if test="roleSoftwareMenuVos.roles !=null and roleSoftwareMenuVos.roles.size!=0">
            AND rsm.role_id IN
            <foreach collection="roleSoftwareMenuVos.roles" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        AND m.is_deleted = 'N'
        ORDER BY
        m.gmt_create,
        m.order_no DESC
    </select>

    <select id="getUserMenuResourceTree" resultType="com.lantone.security.dto.UserMenuResourceTreeDTO">
            SELECT DISTINCT
                t1.menuId,
                t1.parentId,
                t1.softwareId,
                t1.softwareMenuId,
                t1.menuType,
                t1.menuName,
                t2.softwareResourceId,
                t1.softwareName
                FROM
                (
                SELECT
                r.id roleId,
                m.id menuId,
                m.parent_id parentId,
                sm.software_id softwareId,
                sm.id softwareMenuId,
                m.type menuType,
                m.`name` menuName,
                s.`name` softwareName
                FROM
                sys_role r
                JOIN sys_role_software_menu rsm ON r.id = rsm.role_id
                JOIN sys_software_menu sm ON rsm.software_menu_id = sm.id
                JOIN sys_menu m ON sm.menu_id = m.id
                JOIN sys_software s ON s.id = sm.software_id
                WHERE
                r.is_deleted = 'N'
                AND rsm.is_deleted = 'N'
                AND sm.is_deleted = 'N'
                AND m.is_deleted = 'N'
                AND s.is_deleted = 'N'
                AND r.`status` = '1'
                AND m.`status` = '1'
                <if test="roles !=null and roles.size!=0">
                    AND r.id IN
                    <foreach collection="roles" item="id" open="(" close=")" separator=",">
                        #{id}
                    </foreach>
                </if>
                <if test="menuIds !=null and menuIds.size!=0">
                    AND m.id NOT IN
                    <foreach collection="menuIds" item="id" open="(" close=")" separator=",">
                        #{id}
                    </foreach>
                </if>
            ) t1
        LEFT JOIN (
            SELECT
                r.id roleId,
                mr.menu_id menuId,
                sr.software_id softwareId,
                sr.id softwareResourceId
            FROM
                sys_role r
            JOIN sys_role_software_resource rsr ON r.id = rsr.role_id
            JOIN sys_software_resource sr ON rsr.software_resource_id = sr.id
            JOIN sys_resource re ON sr.resource_id = re.id
            JOIN sys_menu_resource mr ON mr.resource_id = re.id
            WHERE
                r.is_deleted = 'N'
            AND rsr.is_deleted = 'N'
            AND mr.is_deleted = 'N'
            AND sr.is_deleted = 'N'
            AND re.is_deleted = 'N'
            AND r.`status` = '1'
            AND re.`status` = '1'
        ) t2 ON t1.roleId = t2.roleId
        AND t1.softwareId = t2.softwareId
        AND t1.menuId = t2.menuId
    </select>
    <select id="getMenuResourceRelation" resultType="com.lantone.security.dto.UserMenuResourceTreeDTO">
        SELECT DISTINCT
            t1.*,
        IF (
            t2.menuId IS NULL,
            "0",
            "1"
        ) relation
        FROM
            (
                SELECT
                    t1.menuId,
                    t1.parentId,
                    t1.softwareId,
                    t1.softwareMenuId,
                    t1.menuType,
                    t1.menuName,
                    t2.softwareResourceId,
                    t1.softwareName
                    FROM
                    (
                    SELECT
                    r.id roleId,
                    m.id menuId,
                    m.parent_id parentId,
                    sm.software_id softwareId,
                    sm.id softwareMenuId,
                    m.type menuType,
                    m.`name` menuName,
                    s.`name` softwareName
                    FROM
                    sys_role r
                    JOIN sys_role_software_menu rsm ON r.id = rsm.role_id
                    JOIN sys_software_menu sm ON rsm.software_menu_id = sm.id
                    JOIN sys_menu m ON sm.menu_id = m.id
                    JOIN sys_software s ON s.id = sm.software_id
                    WHERE
                    r.is_deleted = 'N'
                    AND rsm.is_deleted = 'N'
                    AND sm.is_deleted = 'N'
                    AND m.is_deleted = 'N'
                    AND s.is_deleted = 'N'
                    AND r.`status` = '1'
                    AND m.`status` = '1'
                    <if test="loginRoles !=null and loginRoles.size!=0">
                        AND r.id IN
                        <foreach collection="loginRoles" item="id" open="(" close=")" separator=",">
                            #{id}
                        </foreach>
                    </if>
                    <if test="menuIds !=null and menuIds.size!=0">
                        AND m.id NOT IN
                        <foreach collection="menuIds" item="id" open="(" close=")" separator=",">
                            #{id}
                        </foreach>
                    </if>
                    ) t1
                LEFT JOIN (
                    SELECT
                        r.id roleId,
                        mr.menu_id menuId,
                        sr.software_id softwareId,
                        sr.id softwareResourceId
                    FROM
                        sys_role r
                    JOIN sys_role_software_resource rsr ON r.id = rsr.role_id
                    JOIN sys_software_resource sr ON rsr.software_resource_id = sr.id
                    JOIN sys_resource re ON sr.resource_id = re.id
                    JOIN sys_menu_resource mr ON mr.resource_id = re.id
                    WHERE
                        r.is_deleted = 'N'
                    AND rsr.is_deleted = 'N'
                    AND mr.is_deleted = 'N'
                    AND sr.is_deleted = 'N'
                    AND re.is_deleted = 'N'
                    AND r.`status` = '1'
                    AND re.`status` = '1'
                ) t2 ON t1.roleId = t2.roleId
                AND t1.softwareId = t2.softwareId
                AND t1.menuId = t2.menuId
            ) t1
        LEFT JOIN (
            SELECT
                t1.menuId,
                t1.parentId,
                t1.softwareId,
                t1.softwareMenuId,
                t1.menuType,
                t1.menuName,
                t2.softwareResourceId
            FROM
                (
                    SELECT
                        r.id roleId,
                        m.id menuId,
                        m.parent_id parentId,
                        sm.software_id softwareId,
                        sm.id softwareMenuId,
                        m.type menuType,
                        m.`name` menuName
                    FROM
                        sys_role r
                    JOIN sys_role_software_menu rsm ON r.id = rsm.role_id
                    JOIN sys_software_menu sm ON rsm.software_menu_id = sm.id
                    JOIN sys_menu m ON sm.menu_id = m.id
                    WHERE
                        r.is_deleted = 'N'
                    AND rsm.is_deleted = 'N'
                    AND sm.is_deleted = 'N'
                    AND m.is_deleted = 'N'
                    AND r.`status` = '1'
                    AND m.`status` = '1'
                    <if test="selectRoles !=null and selectRoles.size!=0">
                        AND r.id IN
                        <foreach collection="selectRoles" item="id" open="(" close=")" separator=",">
                            #{id}
                        </foreach>
                    </if>
                ) t1
            LEFT JOIN (
                SELECT
                    r.id roleId,
                    mr.menu_id menuId,
                    sr.software_id softwareId,
                    sr.id softwareResourceId
                FROM
                    sys_role r
                JOIN sys_role_software_resource rsr ON r.id = rsr.role_id
                JOIN sys_software_resource sr ON rsr.software_resource_id = sr.id
                JOIN sys_resource re ON sr.resource_id = re.id
                JOIN sys_menu_resource mr ON mr.resource_id = re.id
                WHERE
                    r.is_deleted = 'N'
                AND rsr.is_deleted = 'N'
                AND mr.is_deleted = 'N'
                AND sr.is_deleted = 'N'
                AND re.is_deleted = 'N'
                AND r.`status` = '1'
                AND re.`status` = '1'
            ) t2 ON t1.roleId = t2.roleId
            AND t1.softwareId = t2.softwareId
            AND t1.menuId = t2.menuId
        ) t2 ON t1.menuId = t2.menuId
        AND t1.softwareId = t2.softwareId
    </select>

    <select id="getUserShowMemuTree" resultType="com.lantone.security.dto.GetUserShowTreeDTO">
        SELECT DISTINCT
            m.id menuId,
            m.parent_id parentId,
            sm.software_id softwareId,
            sm.id softwareMenuId,
            m.type menuType,
            m.`name` menuName,
            m.code
        FROM
        sys_role r
        JOIN sys_role_software_menu rsm ON r.id = rsm.role_id
        JOIN sys_software_menu sm ON rsm.software_menu_id = sm.id
        JOIN sys_menu m ON sm.menu_id = m.id AND m.type != 3
        LEFT JOIN sys_menu_resource mr ON mr.menu_id = m.id
        LEFT JOIN sys_resource res ON res.id = mr.resource_id AND res.is_deleted = 'N'
        WHERE
        r.is_deleted = 'N'
        AND rsm.is_deleted = 'N'
        AND sm.is_deleted = 'N'
        AND m.is_deleted = 'N'
        AND r.`status` = '1'
        AND m.`status` = '1'
        AND sm.software_id = #{softwareId}
        <if test="roleIds !=null and roleIds.size!=0">
        AND r.id IN
        <foreach collection="roleIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </if>
    <if test="menuIds !=null and menuIds.size!=0">
        AND sm.id NOT IN
        <foreach collection="menuIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </if>
    </select>
</mapper>