<?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.cyksj.mapper.manage.cms.CmsMenuMapper">

    <delete id="deleteMenuAndChild">
        delete from `cms_menu` where id = #{id} or parent_id = #{id}
    </delete>

    <select id="getCmsMenusByUserId" resultType="com.cyksj.model.entity.CmsMenu">
        select distinct m.id,
                        m.title,
                        m.menu_name,
                        m.perms,
                        m.type,
                        m.order_by,
                        m.parent_id
        from `cms_menu` m
                 inner join `cms_role_menu` um on m.id = um.menu_id
                 inner join `cms_user_role` cur on cur.role_id = um.role_id
                 inner join `cms_user` u on u.id = cur.user_id
        where u.id = #{userId}
    </select>

    <select id="getAllCmsUserByMenuId" resultType="com.cyksj.model.entity.CmsUser">
        select u.*
        from `cms_user` u
                 inner join `cms_user_role` cr on cr.user_id = u.id
                 inner join `cms_role_menu` cmr on cmr.role_id = cr.role_id
        where cmr.menu_id = #{menuId}
    </select>

    <select id="getALlMenu" resultType="com.cyksj.model.views.CmsMenuVO">
        select *
        from `cms_menu`
        order by `order_by`
    </select>

    <select id="getCmsMenusList" resultType="com.cyksj.model.views.CmsMenuVO">
        select id,
               title,
               menu_name,
               perms,
               `type`,
               `order_by`,
               `path`,
               created_time,
               update_time,
               parent_id
        from `cms_menu`
        where `type` != 'button' and parent_id = 0 order by `order_by`
    </select>

    <select id="getSelfCmsMenusList" resultType="com.cyksj.model.views.CmsMenuVO">
        select cm.id,
               cm.title,
               cm.menu_name,
               cm.perms,
               cm.type,
               cm.order_by,
               cm.path,
               cm.created_time,
               cm.update_time,
               parent_id
        from `cms_menu` cm
                 inner join `cms_role_menu` crm
                            on crm.menu_id = cm.id
                 inner join `cms_user_role` cur on cur.role_id = crm.role_id
                 inner join `cms_user` cu on cu.id = cur.user_id
        where cu.id = #{adminId} and cm.type != 'button' order by `order_by`
    </select>

    <select id="getCmsMenusByAccount" resultType="com.cyksj.model.entity.CmsMenu">
        select distinct m.id,
                        m.title,
                        m.menu_name,
                        m.perms,
                        m.type,
                        m.order_by,
                        m.parent_id
        from `cms_menu` m
                 inner join `cms_role_menu` um on m.id = um.menu_id
                 inner join `cms_user_role` cur on cur.role_id = um.role_id
                 inner join `cms_user` u on u.id = cur.user_id
        where u.id = #{account}
    </select>
</mapper>