Files
urbanLifeline/urbanLifelineServ/system/src/main/resources/mapper/user/TbSysUserMapper.xml

236 lines
11 KiB
XML
Raw Normal View History

2025-12-02 13:21:18 +08:00
<?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.user.TbSysUserMapper">
<!-- 结果映射 -->
<resultMap id="BaseResultMap" type="org.xyzh.common.dto.sys.TbSysUserDTO">
<!-- 用户表字段(按 createTableUser.sql -->
<id column="user_id" property="userId" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="email" property="email" jdbcType="VARCHAR"/>
2025-12-19 15:52:48 +08:00
<result column="phone" property="phone" jdbcType="VARCHAR" typeHandler="org.xyzh.common.jdbc.handler.EncryptedStringTypeHandler"/>
2025-12-02 13:21:18 +08:00
<result column="wechat_id" property="wechatId" jdbcType="VARCHAR"/>
2025-12-09 18:07:44 +08:00
<result column="status" property="status" jdbcType="INTEGER"/>
2025-12-02 13:21:18 +08:00
<!-- BaseDTO 中在该表实际存在的字段 -->
<result column="optsn" property="optsn" jdbcType="VARCHAR"/>
2025-12-11 18:30:35 +08:00
<result column="creator" property="creator" jdbcType="VARCHAR"/>
2025-12-02 13:21:18 +08:00
<result column="create_time" property="createTime" jdbcType="DATE"/>
<result column="update_time" property="updateTime" jdbcType="DATE"/>
<result column="delete_time" property="deleteTime" jdbcType="DATE"/>
2025-12-09 18:07:44 +08:00
<result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
2025-12-02 13:21:18 +08:00
</resultMap>
<!-- VO结果映射用于前端展示 -->
<resultMap id="UserVOResultMap" type="org.xyzh.api.system.vo.SysUserVO">
<!-- 用户字段(按表结构对齐) -->
<id column="user_id" property="userId" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="email" property="email" jdbcType="VARCHAR"/>
2025-12-19 15:52:48 +08:00
<result column="phone" property="phone" jdbcType="VARCHAR" typeHandler="org.xyzh.common.jdbc.handler.EncryptedStringTypeHandler"/>
2025-12-02 13:21:18 +08:00
<result column="wechat_id" property="wechatId" jdbcType="VARCHAR"/>
2025-12-09 18:07:44 +08:00
<result column="status" property="status" jdbcType="INTEGER"/>
2025-12-11 18:30:35 +08:00
<result column="creator" property="creator" jdbcType="VARCHAR"/>
2025-12-02 13:21:18 +08:00
<!-- 用户信息关联字段 -->
<result column="avatar" property="avatar" jdbcType="VARCHAR"/>
2025-12-09 18:07:44 +08:00
<result column="gender" property="gender" jdbcType="INTEGER"/>
2025-12-11 18:30:35 +08:00
<result column="username" property="username" jdbcType="VARCHAR"/>
2025-12-09 18:07:44 +08:00
<result column="level" property="level" jdbcType="INTEGER"/>
2025-12-02 13:21:18 +08:00
<result column="id_card" property="idCard" jdbcType="VARCHAR"/>
<result column="address" property="address" jdbcType="VARCHAR"/>
<!-- BaseVO 中在该表实际存在的字段 -->
<result column="optsn" property="optsn" jdbcType="VARCHAR"/>
<result column="create_time" property="createTime" jdbcType="DATE"/>
<result column="update_time" property="updateTime" jdbcType="DATE"/>
<result column="delete_time" property="deleteTime" jdbcType="DATE"/>
2025-12-09 18:07:44 +08:00
<result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
2025-12-02 13:21:18 +08:00
</resultMap>
<!-- 基础列(完全按表结构) -->
<sql id="Base_Column_List">
user_id, password, email, phone, wechat_id, status,
optsn, create_time, update_time, delete_time, deleted
</sql>
<!-- 插入用户(按表字段 + 默认值动态列) -->
<insert id="insertUser" parameterType="org.xyzh.common.dto.sys.TbSysUserDTO">
INSERT INTO sys.tb_sys_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<!-- 必填字段NOT NULL 且无默认值optsn, user_id, password -->
optsn,
user_id,
password,
<!-- 可空/有默认值字段:按是否有入参动态拼接 -->
<if test="email != null and email != ''">email,</if>
<if test="phone != null and phone != ''">phone,</if>
<if test="wechatId != null and wechatId != ''">wechat_id,</if>
<if test="createTime != null">create_time,</if>
<if test="updateTime != null">update_time,</if>
<if test="deleteTime != null">delete_time,</if>
<if test="deleted != null">deleted,</if>
<if test="status != null">status,</if>
</trim>
VALUES
<trim prefix="(" suffix=")" suffixOverrides=",">
<!-- 必填字段对应的值 -->
#{optsn},
#{userId},
#{password},
<!-- 可空/有默认值字段对应的值 -->
<if test="email != null and email != ''">#{email},</if>
2025-12-19 15:52:48 +08:00
<if test="phone != null and phone != ''">#{phone, typeHandler=org.xyzh.common.jdbc.handler.EncryptedStringTypeHandler},</if>
2025-12-02 13:21:18 +08:00
<if test="wechatId != null and wechatId != ''">#{wechatId},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="deleteTime != null">#{deleteTime},</if>
<if test="deleted != null">#{deleted},</if>
<if test="status != null">#{status},</if>
</trim>
</insert>
<!-- 更新用户 -->
<update id="updateUser" parameterType="org.xyzh.common.dto.sys.TbSysUserDTO">
UPDATE sys.tb_sys_user
<set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="password != null and password != ''">
password = #{password},
</if>
<if test="email != null and email != ''">
email = #{email},
</if>
<if test="phone != null and phone != ''">
2025-12-19 15:52:48 +08:00
phone = #{phone, typeHandler=org.xyzh.common.jdbc.handler.EncryptedStringTypeHandler},
2025-12-02 13:21:18 +08:00
</if>
<if test="wechatId != null and wechatId != ''">
wechat_id = #{wechatId},
</if>
<if test="status != null and status != ''">
status = #{status},
</if>
<if test="deptPath != null and deptPath != ''">
dept_path = #{deptPath},
</if>
<if test="remark != null">
remark = #{remark},
</if>
<if test="updateTime != null">
update_time = #{updateTime},
</if>
</set>
WHERE user_id = #{userId}
<if test="deleted != null">
AND deleted = #{deleted}
</if>
</update>
<!-- 删除用户(逻辑删除) -->
<update id="deleteUser" parameterType="org.xyzh.common.dto.sys.TbSysUserDTO">
UPDATE sys.tb_sys_user
SET deleted = true,
delete_time = NOW()
WHERE user_id = #{userId}
</update>
<!-- 根据ID查询用户 -->
<select id="getUserById" resultMap="UserVOResultMap" parameterType="java.lang.String">
SELECT
u.user_id, u.password, u.email, u.phone, u.wechat_id, u.status,
u.optsn, u.create_time, u.update_time, u.delete_time, u.deleted,
2025-12-11 18:30:35 +08:00
ui.avatar, ui.gender, ui.username, ui.level, ui.id_card, ui.address
2025-12-02 13:21:18 +08:00
FROM sys.tb_sys_user u
LEFT JOIN sys.tb_sys_user_info ui ON u.user_id = ui.user_id AND (ui.deleted IS NULL OR ui.deleted = false)
WHERE u.user_id = #{userId}
AND (u.deleted IS NULL OR u.deleted = false)
</select>
<!-- 根据条件查询用户列表 -->
2025-12-09 17:01:34 +08:00
<select id="getUserByFilter" resultMap="UserVOResultMap">
2025-12-02 13:21:18 +08:00
SELECT
2025-12-11 18:30:35 +08:00
u.user_id, ui.username, u.password, u.email, u.phone, u.wechat_id, u.status,
u.optsn, u.creator, u.remark, u.create_time, u.update_time, u.delete_time, u.deleted,
ui.avatar, ui.gender, ui.username, ui.level, ui.id_card, ui.address
2025-12-02 13:21:18 +08:00
FROM sys.tb_sys_user u
LEFT JOIN sys.tb_sys_user_info ui ON u.user_id = ui.user_id AND (ui.deleted IS NULL OR ui.deleted = false)
<where>
<if test="filter.userId != null and filter.userId != ''">
AND u.user_id = #{filter.userId}
</if>
<if test="filter.email != null and filter.email != ''">
AND u.email = #{filter.email}
</if>
2025-12-11 18:30:35 +08:00
<!-- 手机号查询统一使用phoneHash不使用phone字段 -->
2025-12-09 17:01:34 +08:00
<if test="filter.phoneHash != null and filter.phoneHash != ''">
AND u.phone_hash = #{filter.phoneHash}
</if>
2025-12-02 13:21:18 +08:00
<if test="filter.status != null and filter.status != ''">
AND u.status = #{filter.status}
</if>
2025-12-09 17:01:34 +08:00
<if test="filter.username !=null and filter.username !=''">
AND ui.username = #{filter.username}
</if>
2025-12-02 13:21:18 +08:00
<!-- username / userType / deptPath 在表中不存在,按 SQL 为准移除相关条件 -->
AND (u.deleted IS NULL OR u.deleted = false)
</where>
ORDER BY u.create_time DESC
</select>
<!-- 根据条件查询用户分页列表 -->
<select id="getUserPageByFilter" resultMap="UserVOResultMap">
SELECT
2025-12-11 18:30:35 +08:00
u.user_id, ui.username, u.password, u.email, u.phone, u.wechat_id, u.status,
u.optsn, u.creator, u.remark, u.create_time, u.update_time, u.delete_time, u.deleted,
ui.avatar, ui.gender, ui.username, ui.level, ui.id_card, ui.address
2025-12-02 13:21:18 +08:00
FROM sys.tb_sys_user u
LEFT JOIN sys.tb_sys_user_info ui ON u.user_id = ui.user_id AND (ui.deleted IS NULL OR ui.deleted = false)
<where>
<if test="filter.userId != null and filter.userId != ''">
AND u.user_id = #{filter.userId}
</if>
<if test="filter.username != null and filter.username != ''">
2025-12-11 18:30:35 +08:00
AND ui.username LIKE CONCAT('%', #{filter.username}, '%')
2025-12-02 13:21:18 +08:00
</if>
<if test="filter.email != null and filter.email != ''">
AND u.email = #{filter.email}
</if>
2025-12-11 18:30:35 +08:00
<!-- 手机号查询统一使用phoneHash不使用phone字段 -->
<if test="filter.phoneHash != null and filter.phoneHash != ''">
AND u.phone_hash = #{filter.phoneHash}
2025-12-02 13:21:18 +08:00
</if>
<if test="filter.status != null and filter.status != ''">
AND u.status = #{filter.status}
</if>
AND (u.deleted IS NULL OR u.deleted = false)
</where>
ORDER BY u.create_time DESC
LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
</select>
<!-- 根据条件查询用户数量 -->
2025-12-02 16:13:28 +08:00
<select id="getUserCountByFilter" resultType="java.lang.Integer" parameterType="org.xyzh.common.dto.sys.TbSysUserDTO">
2025-12-02 13:21:18 +08:00
SELECT COUNT(1)
FROM sys.tb_sys_user
<where>
<if test="filter.userId != null and filter.userId != ''">
AND user_id = #{filter.userId}
</if>
<if test="filter.email != null and filter.email != ''">
AND email = #{filter.email}
</if>
2025-12-11 18:30:35 +08:00
<!-- 手机号查询统一使用phoneHash不使用phone字段 -->
<if test="filter.phoneHash != null and filter.phoneHash != ''">
AND phone_hash = #{filter.phoneHash}
2025-12-02 13:21:18 +08:00
</if>
<if test="filter.status != null">
AND status = #{filter.status}
</if>
AND (deleted IS NULL OR deleted = false)
</where>
</select>
</mapper>