<?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.kmall.common.dao.SysMenuDao">

    <select id="queryObject" resultType="com.kmall.common.entity.SysMenuEntity">
		select * from sys_menu where menu_id = #{value}
	</select>

    <insert id="save" parameterType="com.kmall.common.entity.SysMenuEntity">
		insert into sys_menu
		(
			`menu_id`, 
			`parent_id`, 
			`name`, 
			`url`, 
			`perms`, 
			`type`, 
			`icon`, 
			`order_num`,
			`status`
		)
		values
		(
			#{menuId}, 
			#{parentId}, 
			#{name}, 
			#{url}, 
			#{perms}, 
			#{type}, 
			#{icon}, 
			#{orderNum},
			#{status}
		)
	</insert>

    <select id="queryListParentId" resultType="com.kmall.common.entity.SysMenuEntity">
		select * from sys_menu where parent_id = #{parentId} AND status = 0 order by order_num asc
	</select>

    <select id="queryNotButtonList" resultType="com.kmall.common.entity.SysMenuEntity">
		select * from sys_menu where type != 2 AND status = 0 order by order_num asc
	</select>

    <select id="queryList" resultType="com.kmall.common.entity.SysMenuEntity">
        select m.*,(select p.name from sys_menu p where p.menu_id = m.parent_id) as parentName
        from sys_menu m WHERE 1=1 and m.status = 0
        <if test="menuName != null and menuName.trim() != ''">
            and m.name like concat('%',#{menuName},'%')
        </if>
        <if test="parentName != null and parentName.trim() != ''">
            AND exists(SELECT 1 FROM sys_menu WHERE m.parent_id=sys_menu.menu_id AND sys_menu.name LIKE concat('%',#{parentName},'%'))
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by m.${sidx} ${order}
            </when>
            <otherwise>
                order by m.order_num asc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <!-- 查询用户的权限列表 -->
    <select id="queryUserList" resultType="com.kmall.common.entity.SysMenuEntity">
		select distinct m.*,(select p.name from sys_menu p where p.menu_id = m.parent_id) as parentName
			from sys_user_role ur 
			LEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id 
			LEFT JOIN sys_menu m on rm.menu_id = m.menu_id AND m.status = 0
		where ur.user_id = #{userId} order by m.order_num asc
	</select>

    <select id="queryTotal" resultType="int">
		select count(*) from sys_menu m WHERE 1=1
        <if test="menuName != null and menuName.trim() != ''">
            and m.name like concat('%',#{menuName},'%')
        </if>
        <if test="parentName != null and parentName.trim() != ''">
            AND exists(SELECT 1 FROM sys_menu WHERE m.parent_id=sys_menu.menu_id AND sys_menu.name LIKE concat('%',#{parentName},'%'))
        </if>
	</select>

    <update id="update" parameterType="com.kmall.common.entity.SysMenuEntity">
        update sys_menu
        <set>
            <if test="parentId != null">`parent_id` = #{parentId},</if>
            <if test="name != null">`name` = #{name},</if>
            <if test="url != null">`url` = #{url},</if>
            <if test="perms != null">`perms` = #{perms},</if>
            <if test="type != null">`type` = #{type},</if>
            <if test="icon != null">`icon` = #{icon},</if>
            <if test="orderNum != null">`order_num` = #{orderNum},</if>
            <if test="status != null">`status` = #{status}</if>
        </set>
        where menu_id = #{menuId}
    </update>

    <delete id="deleteBatch">
        delete from sys_menu where menu_id in
        <foreach item="menuId" collection="array" open="(" separator="," close=")">
            #{menuId}
        </foreach>
        ;
        delete from sys_role_menu where menu_id in
        <foreach item="menuId" collection="array" open="(" separator="," close=")">
            #{menuId}
        </foreach>
    </delete>

</mapper>