Files
schoolNews/schoolNewsServ/system/src/main/resources/mapper/PermissionMapper.xml
2025-10-06 16:20:05 +08:00

180 lines
7.1 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 namespace="org.xyzh.system.mapper.PermissionMapper">
<!-- 基础结果映射 -->
<resultMap id="BaseResultMap" type="org.xyzh.common.dto.permission.TbSysPermission">
<id column="id" property="id" jdbcType="VARCHAR"/>
<result column="permission_id" property="permissionID" jdbcType="VARCHAR"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="code" property="code" jdbcType="VARCHAR"/>
<result column="description" property="description" jdbcType="VARCHAR"/>
<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="BOOLEAN"/>
</resultMap>
<!-- 基础字段 -->
<sql id="Base_Column_List">
id, permission_id, name, code, description, creator, updater,
create_time, update_time, delete_time, deleted
</sql>
<!-- 通用条件 -->
<sql id="Where_Clause">
<where>
deleted = 0
<if test="name != null and name != ''">
AND name LIKE CONCAT('%', #{name}, '%')
</if>
<if test="code != null and code != ''">
AND code = #{code}
</if>
<if test="permissionType != null">
AND permission_type = #{permissionType}
</if>
<if test="status != null">
AND status = #{status}
</if>
</where>
</sql>
<!-- 根据用户ID查询权限列表 -->
<select id="selectPermissionsByUserId" resultMap="BaseResultMap">
SELECT DISTINCT
p.id, p.permission_id, p.name, p.code, p.description,
p.creator, p.updater,
p.create_time, p.update_time, p.delete_time, p.deleted
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_permission p ON p.permission_id = rp.permission_id
WHERE p.deleted = 0
AND rp.deleted = 0
AND ur.deleted = 0
AND ur.user_id = #{userId}
ORDER BY p.create_time ASC
</select>
<!-- 根据角色ID查询权限列表 -->
<select id="selectPermissionsByRoleId" resultMap="BaseResultMap">
SELECT
p.id, p.permission_id, p.name, p.code, p.description,
p.creator, p.updater,
p.create_time, p.update_time, p.delete_time, p.deleted
FROM tb_sys_role_permission rp
INNER JOIN tb_sys_permission p ON p.permission_id = rp.permission_id
WHERE p.deleted = 0
AND rp.deleted = 0
AND rp.role_id = #{roleId}
ORDER BY p.create_time ASC
</select>
<!-- 根据权限编码查询权限 -->
<select id="selectBycode" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM tb_sys_permission
WHERE deleted = 0
AND code = #{code}
LIMIT 1
</select>
<!-- 检查权限名称是否存在 -->
<select id="countByname" resultType="int">
SELECT COUNT(1)
FROM tb_sys_permission
WHERE deleted = 0
AND name = #{name}
<if test="excludeId != null and excludeId != ''">
AND id != #{excludeId}
</if>
</select>
<!-- 检查权限编码是否存在 -->
<select id="countBycode" resultType="int">
SELECT COUNT(1)
FROM tb_sys_permission
WHERE deleted = 0
AND code = #{code}
<if test="excludeId != null and excludeId != ''">
AND id != #{excludeId}
</if>
</select>
<!-- 批量删除权限(逻辑删除) -->
<update id="batchDeleteByIds">
UPDATE tb_sys_permission
SET deleted = 1,
delete_time = NOW(),
updater = #{updater}
WHERE deleted = 0
AND id IN
<foreach collection="permissionIds" item="permissionId" open="(" separator="," close=")">
#{permissionId}
</foreach>
</update>
<!-- 插入权限 -->
<insert id="insert" parameterType="org.xyzh.common.dto.permission.TbSysPermission">
INSERT INTO tb_sys_permission
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="permissionID != null">permission_id,</if>
<if test="name != null">name,</if>
<if test="code != null">code,</if>
<if test="permissionType != null">permission_type,</if>
<if test="resourceUrl != null">resource_url,</if>
<if test="httpMethod != null">http_method,</if>
<if test="description != null">description,</if>
<if test="status != null">status,</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="permissionID != null">#{permissionID},</if>
<if test="name != null">#{name},</if>
<if test="code != null">#{code},</if>
<if test="permissionType != null">#{permissionType},</if>
<if test="resourceUrl != null">#{resourceUrl},</if>
<if test="httpMethod != null">#{httpMethod},</if>
<if test="description != null">#{description},</if>
<if test="status != null">#{status},</if>
<if test="creator != null">#{creator},</if>
<if test="createTime != null">#{createTime},</if>
0
</trim>
</insert>
<!-- 更新权限 -->
<update id="updateById" parameterType="org.xyzh.common.dto.permission.TbSysPermission">
UPDATE tb_sys_permission
<set>
<if test="permissionID != null">permission_id = #{permissionID},</if>
<if test="name != null">name = #{name},</if>
<if test="code != null">code = #{code},</if>
<if test="permissionType != null">permission_type = #{permissionType},</if>
<if test="resourceUrl != null">resource_url = #{resourceUrl},</if>
<if test="httpMethod != null">http_method = #{httpMethod},</if>
<if test="description != null">description = #{description},</if>
<if test="status != null">status = #{status},</if>
<if test="updater != null">updater = #{updater},</if>
update_time = NOW()
</set>
WHERE id = #{id} AND deleted = 0
</update>
<!-- 根据ID删除逻辑删除 -->
<update id="deleteById">
UPDATE tb_sys_permission
SET deleted = 1,
delete_time = NOW()
WHERE id = #{id} AND deleted = 0
</update>
</mapper>