<?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="org.xyzh.system.mapper.MenuMapper">

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.menu.TbSysMenu">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="menu_id" property="menuID" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="parent_id" property="parentID" jdbcType="VARCHAR"/>
        <result column="url" property="url" jdbcType="VARCHAR"/>
        <result column="component" property="component" jdbcType="VARCHAR"/>
        <result column="layout" property="layout" jdbcType="VARCHAR"/>
        <result column="icon" property="icon" jdbcType="VARCHAR"/>
        <result column="order_num" property="orderNum" jdbcType="INTEGER"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="TINYINT"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        m.id, m.menu_id, m.name, m.parent_id, m.url, m.component, m.layout, m.icon, m.order_num, m.type, 
        m.creator, m.updater, m.create_time, m.update_time, m.delete_time, m.deleted
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            deleted = 0
            <if test="name != null and name != ''">
                AND name LIKE CONCAT('%', #{name}, '%')
            </if>
            <if test="type != null">
                AND type = #{type}
            </if>
        </where>
    </sql>
    <sql id="Where_Clause_List">
        <where>
            m.deleted = 0
            <if test="menuID != null and menuID != ''">
                AND m.menu_id = #{menuID}
            </if>
            <if test="name != null and name != ''">
                AND m.name LIKE CONCAT('%', #{name}, '%')
            </if>
            <if test="type != null">
                AND m.type = #{type}
            </if>
        </where>
    </sql>

    <!-- selectAllMenus --> 

    <select id="selectAllMenus" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_sys_menu m
        WHERE m.deleted = 0
        ORDER BY m.menu_id, m.create_time ASC
    </select>

    <!-- selectMenuList --> 
    <select id="selectMenuList" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_sys_menu m
        <include refid="Where_Clause_List"/>
        ORDER BY m.menu_id, m.create_time ASC
    </select>

    <!-- selectMenuById --> 
    <select id="selectMenuById" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_sys_menu m
        WHERE m.deleted = 0 AND m.menu_id = #{menuId}
    </select>

    <!-- 根据用户ID查询菜单列表 -->
    <select id="selectMenusByUserId" resultMap="BaseResultMap">
        SELECT DISTINCT
            <include refid="Base_Column_List"/>
        FROM tb_sys_user_dept_role ur
        INNER JOIN tb_sys_role_permission rp ON rp.role_id = ur.role_id
        INNER JOIN tb_sys_menu_permission mp ON mp.permission_id = rp.permission_id
        INNER JOIN tb_sys_menu m ON m.menu_id = mp.menu_id
        WHERE m.deleted = 0 
        AND rp.deleted = 0
        AND mp.deleted = 0
        AND ur.deleted = 0
        AND ur.user_id = #{userId}
        ORDER BY m.create_time ASC
    </select>

    <!-- 根据角色ID查询菜单列表 -->
    <select id="selectMenusByRoleId" resultMap="BaseResultMap">
        SELECT 
            <include refid="Base_Column_List"/>
        FROM tb_sys_role_permission rp
        INNER JOIN tb_sys_menu_permission mp ON mp.permission_id = rp.permission_id
        INNER JOIN tb_sys_menu m ON m.menu_id = mp.menu_id
        WHERE m.deleted = 0 
        AND mp.deleted = 0
        AND rp.deleted = 0
        AND rp.role_id = #{roleId}
        ORDER BY m.create_time ASC
    </select>

    <!-- 根据父菜单ID查询子菜单列表 -->
    <select id="selectByParentId" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_sys_menu m
        WHERE m.deleted = 0 
        AND parent_id = #{parentId}
        ORDER BY create_time ASC
    </select>

    <!-- 查询菜单树结构 -->
    <select id="selectMenuTree" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_sys_menu m
        WHERE m.deleted = 0
        ORDER BY 
            CASE WHEN parent_id IS NULL OR parent_id = '' THEN 0 ELSE 1 END,
            parent_id,
            create_time ASC
    </select>

    <!-- 检查菜单名称是否存在 -->
    <select id="countByMenuName" resultType="int">
        SELECT COUNT(1)
        FROM tb_sys_menu m
        WHERE deleted = 0 
        AND m.name = #{name}
        <if test="excludeId != null and excludeId != ''">
            AND m.id != #{excludeId}
        </if>
    </select>

    <!-- 批量删除菜单（逻辑删除） -->
    <update id="batchDeleteByIds">
        UPDATE tb_sys_menu m
        SET deleted = 1,
            delete_time = NOW(),
            updater = #{updater}
        WHERE m.deleted = 0
        AND m.id IN
        <foreach collection="menuIds" item="menuId" open="(" separator="," close=")">
            #{menuId}
        </foreach>
    </update>

    <!-- 插入菜单 -->
    <insert id="insert" parameterType="org.xyzh.common.dto.menu.TbSysMenu">
        INSERT INTO tb_sys_menu
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="menuID != null">menu_id,</if>
            <if test="parentID != null">parent_id,</if>
            <if test="name != null">name,</if>
            <if test="type != null">type,</if>
            <if test="url != null">url,</if>
            <if test="component != null">component,</if>
            <if test="icon != null">icon,</if>
            <if test="layout != null">layout,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="creator != null">creator,</if>
            <if test="createTime != null">create_time,</if>
            deleted
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="menuID != null">#{menuID},</if>
            <if test="parentID != null">#{parentID},</if>
            <if test="name != null">#{name},</if>
            <if test="type != null">#{type},</if>
            <if test="url != null">#{url},</if>
            <if test="component != null">#{component},</if>
            <if test="icon != null">#{icon},</if>
            <if test="layout != null">#{layout},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="creator != null">#{creator},</if>
            <if test="createTime != null">#{createTime},</if>
            0
        </trim>
    </insert>

    <!-- 更新菜单 -->
    <update id="updateById" parameterType="org.xyzh.common.dto.menu.TbSysMenu">
        UPDATE tb_sys_menu
        <set>
            <if test="menuID != null">menu_id = #{menuID},</if>
            <if test="parentID != null">parent_id = #{parentID},</if>
            <if test="name != null">name = #{name},</if>
            <if test="type != null">type = #{type},</if>
            <if test="url != null">url = #{url},</if>
            <if test="component != null">component = #{component},</if>
            <if test="icon != null">icon = #{icon},</if>
            <if test="layout != null">layout = #{layout},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="updater != null">updater = #{updater},</if>
            update_time = NOW()
        </set>
        WHERE menu_id = #{menuId} AND deleted = 0
    </update>

    <!-- 根据ID删除（逻辑删除） -->
    <update id="deleteById">
        UPDATE tb_sys_menu 
        SET deleted = 1,
            delete_time = NOW()
        WHERE menu_id = #{menuId} AND deleted = 0
    </update>

    <!-- 检查菜单是否存在 -->
    <select id="checkMenuExists">
        SELECT * FROM tb_sys_menu WHERE menu_id IN
        <foreach collection="menuIds" item="menuId" open="(" separator="," close=")">
            #{menuId}
        </foreach>
    </select>
</mapper>
