leomon
2025-05-14 3387f6754a07694dda1307849a3ab6fe8a24d7c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?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.oying.modules.system.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.oying.modules.system.domain.User">
        <id column="user_user_id" property="id"/>
        <result column="user_dept_id" property="deptId"/>
        <result column="user_username" property="username"/>
        <result column="user_nick_name" property="nickName"/>
        <result column="user_email" property="email"/>
        <result column="user_phone" property="phone"/>
        <result column="user_gender" property="gender"/>
        <result column="user_avatar_name" property="avatarName"/>
        <result column="user_avatar_path" property="avatarPath"/>
        <result column="user_password" property="password"/>
        <result column="user_is_admin" property="isAdmin"/>
        <result column="user_enabled" property="enabled"/>
        <result column="user_pwd_reset_time" property="pwdResetTime"/>
        <result column="user_create_by" property="createBy"/>
        <result column="user_update_by" property="updateBy"/>
        <result column="user_create_time" property="createTime"/>
        <result column="user_update_time" property="updateTime"/>
        <association property="dept" javaType="com.oying.modules.system.domain.Dept">
            <id column="dept_id" property="id"/>
            <result column="dept_name" property="name"/>
        </association>
        <collection property="jobs" ofType="com.oying.modules.system.domain.Job">
            <id column="job_id" property="id"/>
            <result column="job_name" property="name"/>
        </collection>
        <collection property="roles" ofType="com.oying.modules.system.domain.Role">
            <id column="role_id" property="id"/>
            <result column="role_name" property="name"/>
            <result column="role_level" property="level"/>
            <result column="role_data_scope" property="dataScope"/>
        </collection>
    </resultMap>
 
    <sql id="Base_Column_List">
        u.user_id as user_user_id, u.dept_id as user_dept_id, u.username as user_username,
           u.nick_name as user_nick_name, u.email as user_email, u.phone as user_phone,
           u.gender as user_gender, u.avatar_name as user_avatar_name, u.avatar_path as user_avatar_path,
           u.enabled as user_enabled, u.pwd_reset_time as user_pwd_reset_time, u.create_by as user_create_by,
           u.update_by as user_update_by, u.create_time as user_create_time, u.update_time as user_update_time,
           d.dept_id as dept_id, d.name as dept_name
    </sql>
 
    <sql id="Job_Column_List">
        j.job_id as job_id, j.name as job_name
    </sql>
 
    <sql id="Role_Column_List">
        r.role_id as role_id, r.name as role_name, r.level as role_level, r.data_scope as role_data_scope
    </sql>
 
    <sql id="Whrer_Sql">
        <where>
            <if test="criteria.id != null">
                and u.user_id = #{criteria.id}
            </if>
            <if test="criteria.enabled != null">
                and u.enabled = #{criteria.enabled}
            </if>
            <if test="criteria.deptIds != null and criteria.deptIds.size() != 0">
                and u.dept_id in
                <foreach collection="criteria.deptIds" item="deptId" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
            <if test="criteria.blurry != null and criteria.blurry != ''">
                and (
                u.username like concat('%', #{criteria.blurry}, '%')
                or u.nick_name like concat('%', #{criteria.blurry}, '%')
                or u.email like concat('%', #{criteria.blurry}, '%')
                )
            </if>
            <if test="criteria.createTime != null and criteria.createTime.size() != 0">
                and u.create_time between #{criteria.createTime[0]} and #{criteria.createTime[1]}
            </if>
        </where>
    </sql>
 
    <select id="findAll" resultMap="BaseResultMap">
        select u.*,
        <include refid="Job_Column_List"/>,
        <include refid="Role_Column_List"/>
        from (
        select
        <include refid="Base_Column_List"/>
        from sys_user u
        left join sys_dept d on u.dept_id = d.dept_id
        <include refid="Whrer_Sql"/>
        order by u.user_id desc
        <if test="criteria.offset != null">
            limit #{criteria.offset}, #{criteria.size}
        </if>
        ) u
        left join sys_users_jobs suj on u.user_user_id = suj.user_id
        left join sys_job j on suj.job_id = j.job_id
        left join sys_users_roles sur on u.user_user_id = sur.user_id
        left join sys_role r on sur.role_id = r.role_id
        order by u.user_user_id desc
    </select>
 
    <select id="countAll" resultType="java.lang.Long">
        select count(*)
        from sys_user u
        <include refid="Whrer_Sql"/>
    </select>
 
    <select id="findByUsername" resultMap="BaseResultMap">
        select
        u.password user_password, u.is_admin user_is_admin,
        <include refid="Base_Column_List"/>
        from sys_user u
        left join sys_dept d on u.dept_id = d.dept_id
        where u.username = #{username}
    </select>
 
    <select id="findByEmail" resultType="com.oying.modules.system.domain.User">
        select user_id as id, username from sys_user
        where email = #{email}
    </select>
 
    <select id="findByPhone" resultType="com.oying.modules.system.domain.User">
        select user_id as id, username from sys_user
        where phone = #{phone}
    </select>
 
    <select id="findByRoleId" resultType="com.oying.modules.system.domain.User">
        SELECT u.user_id as id, u.username FROM sys_user u, sys_users_roles r
        WHERE u.user_id = r.user_id AND r.role_id = #{roleId}
        group by u.user_id
    </select>
 
    <select id="findByRoleDeptId" resultType="com.oying.modules.system.domain.User">
        SELECT u.* FROM sys_user u, sys_users_roles r, sys_roles_depts d
        WHERE u.user_id = r.user_id AND r.role_id = d.role_id AND d.dept_id = #{deptId}
        group by u.user_id
    </select>
 
    <select id="findByMenuId" resultType="com.oying.modules.system.domain.User">
        SELECT u.user_id as id, u.username FROM sys_user u, sys_users_roles ur, sys_roles_menus rm
        WHERE u.user_id = ur.user_id AND ur.role_id = rm.role_id AND rm.menu_id = #{menuId}
        group by u.user_id
    </select>
 
    <select id="countByJobs" resultType="int">
        SELECT count(*) FROM sys_user u, sys_users_jobs j
        WHERE u.user_id = j.user_id AND j.job_id IN
        <foreach collection="jobIds" item="jobId" open="(" separator="," close=")">
            #{jobId}
        </foreach>
    </select>
 
    <select id="countByDepts" resultType="int">
        SELECT count(*) FROM sys_user u
        WHERE u.dept_id IN
        <foreach collection="deptIds" item="deptId" open="(" separator="," close=")">
            #{deptId}
        </foreach>
    </select>
 
    <select id="countByRoles" resultType="int">
        SELECT count(*) FROM sys_user u, sys_users_roles r
        WHERE u.user_id = r.user_id AND r.role_id in
        <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
            #{roleId}
        </foreach>
    </select>
 
    <update id="resetPwd">
        update sys_user set password = #{pwd}
        where user_id in
        <foreach collection="userIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>