Bonus-Cloud-Material-Mall/bonus-modules/bonus-material-mall/src/main/resources/mapper/material/menuCollect/MenuCollectMapper.xml

76 lines
3.0 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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 接口全类名一致 -->
<mapper namespace="com.bonus.material.menuCollect.mapper.MenuCollectMapper">
<!-- 通用结果集映射(字段与实体属性对应,避免重复编写) -->
<resultMap id="BaseResultMap" type="com.bonus.material.menuCollect.domain.MenuCollect">
<!-- 原有字段映射保持不变 -->
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="menu_id" property="menuId"/>
<result column="collect_time" property="collectTime"/>
<result column="menu_name" property="menuName"/>
<!-- 新增菜单原始path和父级ID用于Java拼接路由 -->
<result column="path" property="menuPath"/>
<result column="parent_id" property="menuParentId"/>
<!-- 保留fullRoutePath映射接收Java拼接后的最终路由 -->
<result column="full_route_path" property="fullRoutePath"/>
</resultMap>
<!-- 新增菜单收藏collect_time 数据库自动填充,无需插入 -->
<insert id="insertMenuCollect" parameterType="com.bonus.material.menuCollect.domain.MenuCollect">
INSERT INTO sys_menu_collect (user_id, menu_id)
VALUES (#{userId}, #{menuId})
</insert>
<!-- 物理删除用户ID+菜单ID 精准删除 -->
<delete id="deleteByUserAndMenu">
DELETE
FROM sys_menu_collect
WHERE user_id = #{userId}
AND menu_id = #{menuId}
</delete>
<!-- 批量物理删除根据用户ID删除所有收藏 -->
<delete id="deleteBatchByUserId">
DELETE
FROM sys_menu_collect
WHERE user_id = #{userId}
</delete>
<!-- 校验收藏状态:查询记录数 -->
<select id="checkCollect" resultType="int">
SELECT COUNT(1)
FROM sys_menu_collect
WHERE user_id = #{userId}
AND menu_id = #{menuId}
</select>
<!-- 根据用户ID查询收藏列表 -->
<select id="selectListByUserId" resultMap="BaseResultMap">
SELECT smc.id,
smc.user_id,
smc.menu_id,
smc.collect_time,
sm.menu_name,
sm.path, -- 仅查当前菜单的原始path用于Java拼接
sm.parent_id -- 仅查当前菜单的父级ID用于追溯上级
FROM sys_menu_collect smc
LEFT JOIN sys_menu sm ON smc.menu_id = sm.menu_id
WHERE smc.user_id = #{userId}
ORDER BY smc.collect_time DESC
</select>
<!-- 统计菜单收藏人数 -->
<select id="selectCollectCountByMenuId" resultType="int">
SELECT COUNT(DISTINCT user_id)
FROM sys_menu_collect
WHERE menu_id = #{menuId}
</select>
<select id="selectAllMenu" resultType="com.bonus.system.api.domain.SysMenu">
SELECT menu_id AS menuId, parent_id AS parentId, path AS path
FROM sys_menu
</select>
</mapper>