<?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.ym.mec.auth.dal.dao.SysMenuDao">

    <resultMap type="com.ym.mec.auth.api.entity.SysMenu" id="SysMenu">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="permission_" property="permission"/>
        <result column="parent_permission_" property="parentPermission"/>
        <result column="path_" property="path"/>
        <result column="parent_id_" property="parentId"/>
        <result column="icon_" property="icon"/>
        <result column="component_" property="component"/>
        <result column="sort_" property="sort"/>
        <result column="keep_alive_" property="keepAlive"/>
        <result column="type_" property="type"/>
        <result column="type_" property="type"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="hidden_" property="hid"/>
        <result column="memo_" property="memo"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="SysMenu">
		SELECT * FROM sys_menu WHERE id_ = #{id}
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="SysMenu">
		SELECT * FROM sys_menu ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.auth.api.entity.SysMenu" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO sys_menu
        (id_,name_,permission_,path_,parent_id_,icon_,component_,sort_,type_,create_time_,update_time_,hidden_,parent_permission_,memo_,keep_alive_)
        VALUES(#{id},#{name},#{permission},#{path},#{parentId},#{icon},#{component},#{sort},#{type},now(),now(),#{hid},#{parentPermission},#{memo},#{keepAlive})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.auth.api.entity.SysMenu">
        UPDATE sys_menu
        <set>
            <if test="parentId != null">
                parent_id_ = #{parentId},
            </if>
            <if test="delFlag != null">
                del_flag_ = #{delFlag},
            </if>
            <if test="keepAlive != null">
                keep_alive_ = #{keepAlive},
            </if>
            <if test="permission != null">
                permission_ = #{permission},
            </if>
            <if test="parentPermission != null">
                parent_permission_ = #{parentPermission},
            </if>
            <if test="path != null">
                path_ = #{path},
            </if>
            <if test="sort != null">
                sort_ = #{sort},
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
            <if test="icon != null">
                icon_ = #{icon},
            </if>
            <if test="component != null">
                component_ = #{component},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="type != null">
                type_ = #{type},
            </if>
            <if test="hid != null">
                hidden_ = #{hid},
            </if>
            <if test="memo != null">
                memo_ = #{memo},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 根据主键删除一条记录 -->
    <update id="delete">
		UPDATE sys_menu SET del_flag_ = 1 WHERE id_ = #{id}
	</update>

    <update id="batchDel" parameterType="java.util.List">
        UPDATE sys_menu SET del_flag_ = 1 WHERE id_ IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
          #{item}
        </foreach>
    </update>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="SysMenu" parameterType="map">
        SELECT sm.* FROM sys_user_role sur
        LEFT JOIN sys_role_menu srm ON sur.role_id_ = srm.role_id_
        LEFT JOIN sys_menu sm ON srm.menu_id_ = sm.id_
        <include refid="queryTree"/>
        GROUP BY sm.id_
        ORDER BY sm.sort_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(DISTINCT sm.id_) FROM sys_user_role sur
        LEFT JOIN sys_role_menu srm ON sur.role_id_ = srm.role_id_
        LEFT JOIN sys_menu sm ON srm.menu_id_ = sm.id_
        <include refid="queryTree"/>
	</select>

    <select id="getMenusByRoleId" resultMap="SysMenu">
		SELECT sm.* FROM sys_role_menu srm
		LEFT JOIN sys_menu sm ON srm.menu_id_ = sm.id_
		WHERE srm.role_id_ = #{roleId} AND sm.del_flag_ = 0
	</select>

    <select id="findByUser" resultMap="SysMenu">
		SELECT sm.* FROM sys_user_role sur
		LEFT JOIN sys_role sr ON sur.role_id_ = sr.id_
		LEFT JOIN sys_role_menu srm ON sr.id_ = srm.role_id_
		LEFT JOIN sys_menu sm ON srm.menu_id_ = sm.id_
		WHERE sur.user_id_ = #{userId} AND sr.del_flag_ = 0 AND sm.del_flag_ = 0
	</select>

    <!-- 根据权限标识查菜单 -->
    <select id="findMenuByPermission" resultMap="SysMenu">
        SELECT * FROM `sys_menu` WHERE `permission_` = #{permission} AND del_flag_ = 0
    </select>
    <sql id="queryTree">
        <where>
            <if test="userId != null">
                AND sur.user_id_ = #{userId}
            </if>
            <if test="hid != null">
                AND sm.hidden_ = #{hid}
            </if>
            <if test="delFlag != null">
                AND sm.del_flag_ = #{delFlag}
            </if>
            <if test="parentId != null">
                AND sm.parent_id_ = #{parentId}
            </if>
        </where>
    </sql>

    <select id="findByParentId" resultMap="SysMenu">
        SELECT sm.* FROM sys_menu sm
        LEFT JOIN sys_role_menu srm ON srm.menu_id_ = sm.id_
        LEFT JOIN sys_user_role sur ON sur.role_id_ = srm.role_id_
        <include refid="queryTree"/>
        GROUP BY sm.id_
        ORDER BY sm.sort_ DESC,sm.id_ DESC
    </select>

    <select id="findList" resultMap="SysMenu">
        SELECT * FROM sys_menu
        <where>
            <if test="delFlag != null">
                AND del_flag_ = #{delFlag}
            </if>
            <if test="parentId != null">
                AND parent_id_ = #{parentId}
            </if>
            <if test="hid != null">
                AND hidden_ = #{hid}
            </if>
        </where>
        ORDER BY sort_ DESC,id_ DESC
    </select>
</mapper>
