BAOLH
2026-10-22 e15367cf02e0df778aef410df8129bb822da804c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.system.repository;
 
import io.swagger.models.auth.In;
import me.zhengjie.modules.system.domain.Dept;
import me.zhengjie.modules.system.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
 
import java.util.Date;
import java.util.List;
import java.util.Set;
 
/**
 * @author Zheng Jie
 * @date 2018-11-22
 */
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
 
    /**
     * 根据用户名查询
     *
     * @param username 用户名
     * @return /
     */
    User findByUsername(String username);
 
    /**
     * 根据邮箱查询
     *
     * @param email 邮箱
     * @return /
     */
    User findByEmail(String email);
 
    /**
     * 根据手机号查询
     *
     * @param phone 手机号
     * @return /
     */
    User findByPhone(String phone);
 
    /**
     * 修改密码
     *
     * @param username              用户名
     * @param pass                  密码
     * @param lastPasswordResetTime /
     */
    @Modifying
    @Query(value = "update sys_user set password = ?2 , pwd_reset_time = ?3 where username = ?1", nativeQuery = true)
    void updatePass(String username, String pass, Date lastPasswordResetTime);
 
    /**
     * 修改邮箱
     *
     * @param username 用户名
     * @param email    邮箱
     */
    @Modifying
    @Query(value = "update sys_user set email = ?2 where username = ?1", nativeQuery = true)
    void updateEmail(String username, String email);
 
    /**
     * 根据角色查询用户
     *
     * @param roleId /
     * @return /
     */
    @Query(value = "SELECT u.* FROM sys_user u, sys_users_roles r WHERE" +
            " u.user_id = r.user_id AND r.role_id = ?1", nativeQuery = true)
    List<User> findByRoleId(Long roleId);
 
    /**
     * 根据角色中的部门查询
     *
     * @param deptId /
     * @return /
     */
    @Query(value = "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 = ?1 group by u.user_id", nativeQuery = true)
    List<User> findByRoleDeptId(Long deptId);
 
    /**
     * 根据菜单查询
     *
     * @param id 菜单ID
     * @return /
     */
    @Query(value = "SELECT u.* FROM sys_user u, sys_users_roles ur, sys_roles_menus rm WHERE\n" +
            "u.user_id = ur.user_id AND ur.role_id = rm.role_id AND rm.menu_id = ?1 group by u.user_id", nativeQuery = true)
    List<User> findByMenuId(Long id);
 
    /**
     * 根据Id删除
     *
     * @param ids /
     */
    void deleteAllByIdIn(Set<Long> ids);
 
    /**
     * 根据岗位查询
     *
     * @param ids /
     * @return /
     */
    @Query(value = "SELECT count(1) FROM sys_user u, sys_users_jobs j WHERE u.user_id = j.user_id AND j.job_id IN ?1", nativeQuery = true)
    int countByJobs(Set<Long> ids);
 
    /**
     * 根据部门查询
     *
     * @param deptIds /
     * @return /
     */
    @Query(value = "SELECT count(1) FROM sys_user u WHERE u.dept_id IN ?1", nativeQuery = true)
    int countByDepts(Set<Long> deptIds);
 
    /**
     * 根据角色查询
     *
     * @param ids /
     * @return /
     */
    @Query(value = "SELECT count(1) FROM sys_user u, sys_users_roles r WHERE " +
            "u.user_id = r.user_id AND r.role_id in ?1", nativeQuery = true)
    int countByRoles(Set<Long> ids);
 
    /**
     * 根据状态查询
     *
     * @param enabled
     * @return
     */
    List<User> findAllByEnabled(Boolean enabled);
 
    /**
     * 根据排班查询
     *
     * @param ids /
     * @return /
     */
    @Query(value = "SELECT count(1) FROM sys_user WHERE rostering_id IN ?1", nativeQuery = true)
    int countByRosterings(Long[] ids);
 
    /**
     * 根据级别查询
     *
     * @param ids /
     * @return /
     */
    @Query(value = "SELECT count(1) FROM sys_user WHERE level_id IN ?1", nativeQuery = true)
    int countByLevels(Long[] ids);
 
    /**
     * 根据ID查询
     *
     * @param userIds
     * @return
     */
    List<User> findByIdIn(List<Long> userIds);
 
    /**
     * 根据部门查询
     *
     * @param dept
     * @return
     */
    List<User> findByDeptAndEnabled(Dept dept, Boolean enabled);
 
    @Query(value = "SELECT * FROM sys_user WHERE dept_id IN ?1 AND enabled = ?2", nativeQuery = true)
    List<User> findByDeptIdInAndEnabled(List<Long> deptIds, Boolean enabled);
 
    @Query(value = "select * from sys_user where dept_id = ?1 and enabled=?2", nativeQuery = true)
    List<User> findByDeptIdInAndEnabledWithSingle(Long deptId, Boolean enabled);
 
    @Query(value = "select dept_id from sys_user where user_id = ?1",nativeQuery = true)
    Long findDeptIdByUserId(Long userId);
 
    /**
     * 查询部门
     * @param
     * @return
     */
    @Query(value = "SELECT u.* FROM sys_user u JOIN sys_dept d ON u.dept_id = d.dept_id WHERE d.dept_id = ?;", nativeQuery = true)
    List<User> findByDeptId(Long dept_id);
 
    /**
     * 查询用户
     * @param
     * @return
     */
    @Query(value = "SELECT * FROM sys_user WHERE user_id = ?;", nativeQuery = true)
    List<User> findByUserId(Long user_id);
 
    @Query(value = "select * FROM sys_user WHERE user_id IN ?1 and enabled = true",nativeQuery = true)
    void findByUserIdIn(List<Long> userIds, Long companyId);
 
    @Query(value = "select * FROM sys_user WHERE user_id IN ?1 and company_id = ?2 and enabled = true",nativeQuery = true)
    void findByUserIdInAndCompanyId(List<Long> userIds, Long companyId);
 
    /**
     * 根据用户昵称查询
     *
     * @param nickName 用户昵称
     * @return /
     */
    User findByNickName(String nickName);
}