<?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.fateverse.admin.mapper.MenuMapper">

    <resultMap type="cn.fateverse.admin.entity.Menu" id="MenuResult">
        <id property="menuId" column="menu_id"/>
        <result property="menuName" column="menu_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="orderNum" column="order_num"/>
        <result property="path" column="path"/>
        <result property="component" column="component"/>
        <result property="isFrame" column="is_frame"/>
        <result property="isCache" column="is_cache"/>
        <result property="menuType" column="menu_type"/>
        <result property="visible" column="visible"/>
        <result property="state" column="state"/>
        <result property="perms" column="perms"/>
        <result property="icon" column="icon"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectMenuVo">
        select menu_id,
               menu_name,
               parent_id,
               menu_type,
               order_num,
               `path`,
               path_params,
               is_frame,
               component,
               is_cache,
               no_redirect,
               breadcrumb,
               `visible`,
               state,
               ifnull(perms, '') as perms,
               icon,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from sys_menu
    </sql>

    <select id="selectList" resultMap="MenuResult">
        <include refid="selectMenuVo"/>
        <where>
            <if test="menuName != null and menuName != ''">and menu_name like concat('%',#{menuName},'%')</if>
            <if test="state != null and state != ''">and state = #{state}</if>
            <if test="excludeId != null ">and menu_id != #{excludeId} and parent_id != #{excludeId}</if>
            <if test="button">and menu_type in ('D', 'M')</if>
        </where>
    </select>

    <select id="selectListByUserId" resultType="cn.fateverse.admin.entity.Menu">
        select distinct m.menu_id,
                m.menu_name,
                m.parent_id,
                m.order_num,
                m.path,
                m.path_params,
                m.component,
                m.no_redirect,
                m.breadcrumb,
                m.is_frame,
                m.is_cache,
                m.menu_type,
                m.visible,
                m.state,
                m.order_num,
                ifnull(m.perms, '') as perms,
                m.icon,
                m.create_by,
                m.create_time,
                m.update_by,
                m.update_time,
                m.remark
                from sys_menu m
        left join sys_role_menu rm on rm.menu_id = m.menu_id
        left join sys_role r on r.role_id = rm.role_id
        left join sys_user_role ur on ur.role_id = r.role_id
        <where>
            <if test="userId != null">and ur.user_id = #{userId}</if>
            <if test="menuName != null and menuName != ''">and m.menu_name like concat('%',#{menuName},'%')</if>
            <if test="state != null and state != ''">and m.state = #{state}</if>
            <if test="excludeId != null ">and m.menu_id != #{excludeId} and parent_id != #{excludeId}</if>
            <if test="button">and m.menu_type in ('D', 'M')</if>
        </where>
    </select>

    <select id="selectMenuPermsByUserId" parameterType="Long" resultType="java.lang.String">
        select distinct m.perms
        from sys_menu m
                 left join sys_role_menu rm on m.menu_id = rm.menu_id
                 left join sys_user_role ur on rm.role_id = ur.role_id
                 left join sys_role r on r.role_id = ur.role_id
        where m.state = '1'
          and r.state = '1'
          and ur.user_id = #{userId}
    </select>

    <select id="selectById" resultMap="MenuResult">
        <include refid="selectMenuVo"/>
        where menu_id = #{menuId}
    </select>

    <!--第一个select则是通过sys_role_menu,映射表获取到角色对应的菜单列表,第二个select则是去除掉里面的parentId,去除原因为前端组件联动效果影响-->
    <select id="selectCheckedMenuIdByRoleId" resultType="java.lang.Long">
        select distinct m.menu_id
        from sys_menu m
                 left join sys_role_menu rm on rm.menu_id = m.menu_id
        where rm.role_id = #{roleId}
          and m.menu_id not in
              (select distinct m.parent_id
               from sys_menu m
                        left join sys_role_menu rm on rm.menu_id = m.menu_id
               where rm.role_id = #{roleId})
    </select>

    <select id="selectRouterMenuList" resultType="cn.fateverse.admin.entity.Menu">
        select menu_id, menu_name, parent_id, menu_type, order_num, `path`, path_params, component,is_frame, is_cache, no_redirect,
               breadcrumb, `visible`, state, perms ,order_num, icon, create_by, create_time, update_by, update_time, remark
        from sys_menu
        where menu_type in ('D', 'M')
          and state = '1'
    </select>

    <select id="selectRouterMenuListByUserId" resultType="cn.fateverse.admin.entity.Menu">
        select distinct m.menu_id,
                        m.menu_name,
                        m.parent_id,
                        m.order_num,
                        m.path,
                        m.path_params,
                        m.component,
                        m.no_redirect,
                        m.breadcrumb,
                        m.is_frame,
                        m.is_cache,
                        m.menu_type,
                        m.visible,
                        m.state,
                        m.order_num,
                        ifnull(m.perms, '') as perms,
                        m.icon,
                        m.create_by,
                        m.create_time,
                        m.update_by,
                        m.update_time,
                        m.remark
        from sys_menu m
                 left join sys_role_menu rm on rm.menu_id = m.menu_id
                 left join sys_role r on r.role_id = rm.role_id
                 left join sys_user_role ur on ur.role_id = r.role_id
        where
            ur.user_id = #{userId}
            and m.menu_type in ('D', 'M')
            and m.state = '1'
            and r.state = '1'
        order by m.parent_id, m.order_num
    </select>

    <select id="selectByUserId" resultType="cn.fateverse.admin.entity.Menu">
        select distinct m.menu_id,
                        m.menu_name,
                        m.parent_id,
                        m.order_num,
                        m.path,
                        m.path_params,
                        m.component,
                        m.is_frame,
                        m.is_cache,
                        m.no_redirect,
                        m.breadcrumb,
                        m.menu_type,
                        m.visible,
                        m.state,
                        ifnull(m.perms, '') as perms,
                        m.icon,
                        m.create_by,
                        m.create_time,
                        m.update_by,
                        m.update_time,
                        m.remark
        from sys_menu m
                 left join sys_role_menu rm on rm.menu_id = m.menu_id
                 left join sys_role r on r.role_id = rm.role_id
                 left join sys_user_role ur on ur.role_id = rm.role_id
        where ur.user_id = #{userId}
          and r.state = '1'
        order by m.parent_id, m.order_num
    </select>

    <select id="selectCheckedMenuIdByUserId" resultType="java.lang.Long">
        select distinct rm.menu_id
        from sys_role_menu rm
                 left join sys_role r on r.role_id = rm.role_id
                 left join sys_user_role ur on ur.role_id = rm.role_id
        where ur.user_id = #{userId}
          and r.state = '1'
    </select>

    <select id="selectAllMenuId" resultType="java.lang.Long">
        select menu_id
        from sys_menu
    </select>

    <select id="selectCountByParentId" resultType="java.lang.Integer">
        select count(*) from sys_menu where parent_id = #{menuId}
    </select>

    <select id="selectByPerms" resultType="cn.fateverse.admin.entity.Menu">
        <include refid="selectMenuVo"/>
        where perms = #{perms}
    </select>


    <insert id="insert" useGeneratedKeys="true" keyColumn="menu_id" keyProperty="menuId">
        insert into sys_menu
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="menuName != null and menuName != ''">menu_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="path != null and path != ''">`path`,</if>
            <if test="pathParams != null and pathParams != ''">path_params,</if>
            <if test="component != null and component != ''">component,</if>
            <if test="isFrame != null and isFrame != ''">is_frame,</if>
            <if test="isCache != null and isCache != ''">is_cache,</if>
            <if test="menuType != null and menuType != ''">menu_type,</if>
            <if test="state != null and state != ''">state,</if>
            <if test="visible != null and visible != ''">`visible`,</if>
            <if test="perms != null and perms != ''">perms,</if>
            <if test="icon != null and icon != ''">icon,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="menuName != null and menuName != ''">#{menuName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="path != null and path != ''">#{path},</if>
            <if test="pathParams != null and pathParams != ''">#{pathParams},</if>
            <if test="component != null and component != ''">#{component},</if>
            <if test="isFrame != null and isFrame != ''">#{isFrame},</if>
            <if test="isCache != null and isCache != ''">#{isCache},</if>
            <if test="menuType != null and menuType != ''">#{menuType},</if>
            <if test="state != null and state != ''">#{state},</if>
            <if test="visible != null and visible != ''">#{visible},</if>
            <if test="perms != null and perms != ''">#{perms},</if>
            <if test="icon != null and icon != ''">#{icon},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="update">
        update sys_menu
        <set>
            <if test="menuName != null and menuName != ''">menu_name = #{menuName},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="path != null and path != ''">`path` = #{path},</if>
            <if test="pathParams != null and pathParams != ''">path_params = #{pathParams},</if>
            <if test="component != null and component != ''">component = #{component},</if>
            <if test="isFrame != null and isFrame != ''">is_frame = #{isFrame},</if>
            <if test="isCache != null and isCache != ''">is_cache = #{isCache},</if>
            <if test="menuType != null and menuType != ''">menu_type = #{menuType},</if>
            <if test="state != null and state != ''">state = #{state},</if>
            <if test="visible != null and visible != ''">`visible` = #{visible},</if>
            <if test="perms != null and perms != ''">perms = #{perms},</if>
            <if test="icon != null and icon != ''">icon = #{icon},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </set>
        where menu_id = #{menuId}
    </update>

    <delete id="deleteById">
        delete
        from sys_menu
        where menu_id = #{menuId}
    </delete>


</mapper>
