月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-11 fee67ca8a0760315047a52fc4101a8f4f80b7a7f
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package com.moon.server.controller.sys;
 
import com.moon.server.annotation.SysLog;
import com.moon.server.controller.all.BaseController;
import com.moon.server.entity.all.ResponseMsg;
import com.moon.server.entity.sys.RoleEntity;
import com.moon.server.entity.sys.UserEntity;
import com.moon.server.entity.ctrl.UserUpdateEntity;
import com.moon.server.helper.StringHelper;
import com.moon.server.service.sys.TokenService;
import com.moon.server.service.sys.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.util.List;
 
@SuppressWarnings("ALL")
@Api(tags = "运维管理\\用户管理")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    UserService userService;
 
    @Autowired
    TokenService tokenService;
 
    @SysLog()
    @ApiOperation(value = "分页查询并返回记录数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uname", value = "用户名", dataType = "String", paramType = "query", example = "室"),
            @ApiImplicitParam(name = "depcode", value = "单位编码", dataType = "String", paramType = "query", example = "00"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "pageIndex", value = "分页数(从1开始)", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping(value = "/selectByPageAndCount")
    public ResponseMsg<List<UserEntity>> selectByPageAndCount(String uname, String depcode, Integer pageSize, Integer pageIndex) {
        try {
            if (pageSize < 1 || pageIndex < 1) {
                return fail("每页页数或分页数小于1", null);
            }
 
            int count = userService.selectCount(uname, depcode);
            if (count == 0) {
                return success(0, null);
            }
 
            List<UserEntity> rs = userService.selectByPage(uname, depcode, pageSize, pageSize * (pageIndex - 1));
 
            return success(count, rs);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "根据角色+单位分页查询并返回记录数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uname", value = "用户名", dataType = "String", paramType = "query", example = "室"),
            @ApiImplicitParam(name = "roleid", value = "角色ID", dataType = "Integer", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "depcode", value = "单位编码", dataType = "String", paramType = "query", example = "00"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "pageIndex", value = "分页数(从1开始)", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping(value = "/selectByPageForRole")
    public ResponseMsg<List<UserEntity>> selectByPageForRole(String uname, Integer roleid, String depcode, Integer pageSize, Integer pageIndex) {
        try {
            if (pageSize < 1 || pageIndex < 1) {
                return fail("每页页数或分页数小于1", null);
            }
            int count = userService.selectCountForRole(uname, roleid, depcode);
            if (count == 0) {
                return success(0, null);
            }
 
            List<UserEntity> rs = userService.selectByPageForRole(uname, roleid, depcode, pageSize, pageSize * (pageIndex - 1));
 
            return success(count, rs);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "根据ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping(value = "/selectUser")
    public ResponseMsg<UserEntity> selectUser(int id) {
        try {
            UserEntity userEntity = userService.selectUser(id);
 
            return success(userEntity);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "根据用户ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户ID", dataType = "String", paramType = "query", example = "admin")
    })
    @GetMapping(value = "/selectByUid")
    public ResponseMsg<UserEntity> selectByUid(String uid) {
        try {
            if (StringHelper.isEmpty(uid)) {
                fail("用户ID不能为空", null);
            }
 
            UserEntity userEntity = userService.selectByUid(uid);
 
            return success(userEntity);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "查询所有")
    @GetMapping(value = "/selectUserAll")
    public ResponseMsg<List<UserEntity>> selectUserAll() {
        try {
            List<UserEntity> list = userService.selectUserAll();
 
            return success(list);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "查询是/否为管理员")
    @GetMapping(value = "/selectForIsAdmin")
    public ResponseMsg<Boolean> selectForIsAdmin(HttpServletRequest req) {
        try {
            UserEntity ue = tokenService.getCurrentUser(req);
            if (ue == null) {
                return fail("用户未登录", false);
            }
 
            Integer rows = userService.selectForIsAdmin(ue.getId());
 
            return success("成功", rows > 0);
        } catch (Exception ex) {
            return fail(ex, false);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "查询是/否为管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping(value = "/selectIsAdmin")
    public ResponseMsg<Boolean> selectIsAdmin(Integer id) {
        try {
            UserEntity ue = userService.selectUser(id);
            if (ue == null) {
                return fail("用户不存在", false);
            }
 
            Integer rows = userService.selectForIsAdmin(ue.getId());
 
            return success("成功", rows > 0);
        } catch (Exception ex) {
            return fail(ex, false);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "查询管理员用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "管理员类别", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping(value = "/selectAdminUsers")
    public ResponseMsg<Object> selectAdminUsers(Integer type) {
        try {
            if (null == type || type < 1) {
                return fail("管理员类别不能为空或小于1", false);
            }
 
            List<UserEntity> rs = userService.selectAdminUsers(type);
 
            return success(rs);
        } catch (Exception ex) {
            return fail(ex, false);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "根据用户ID查询角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping(value = "/selectRoleByUserId")
    public ResponseMsg<Object> selectRoleByUserId(Integer id) {
        try {
            if (null == id || id < 1) {
                return fail("用户ID不能为空或小于1", false);
            }
 
            List<RoleEntity> rs = userService.selectRoleByUserId(id);
 
            return success(rs);
        } catch (Exception ex) {
            return fail(ex, false);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "根据角色查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping(value = "/selectUserByRoleId")
    public ResponseMsg<Object> selectUserByRoleId(Integer id) {
        try {
            if (null == id || id < 1) {
                return fail("用户ID不能为空或小于1", false);
            }
 
            List<UserEntity> rs = userService.selectUserByRoleId(id);
 
            return success(rs);
        } catch (Exception ex) {
            return fail(ex, false);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "插入一条")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entity", value = "实体类", dataType = "UserEntity", paramType = "body", example = "")
    })
    @PostMapping(value = "/insertUser", produces = "application/json; charset=UTF-8")
    public ResponseMsg<Integer> insertUser(@RequestBody UserEntity entity, HttpServletRequest req) {
        try {
            String str = userService.validateNewPwd(entity);
            if (str != null) {
                return fail(str, -1);
            }
 
            UserEntity ue = tokenService.getCurrentUser(req);
            if (ue != null) {
                entity.setCreateUser(ue.getId());
            }
 
            int count = userService.insertUser(entity);
 
            return success(count);
        } catch (Exception ex) {
            return fail(ex, -1);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "插入多条")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "list", value = "实体类集合", dataType = "List<UserEntity>", paramType = "body", example = "")
    })
    @PostMapping(value = "/insertUsers", produces = "application/json; charset=UTF-8")
    @SuppressWarnings("AlibabaRemoveCommentedCode")
    public ResponseMsg<Integer> insertUsers(@RequestBody List<UserEntity> list, HttpServletRequest req) {
        try {
            if (list == null || list.isEmpty()) {
                return fail("实体类集合为空", -1);
            }
 
            UserEntity ue = tokenService.getCurrentUser(req);
            for (UserEntity entity : list) {
                /*String str = userService.validateNewPwd(entity);
                if (str != null) {
                    return fail(str, -1);
                }*/
                if (ue != null) {
                    entity.setCreateUser(ue.getId());
                }
            }
 
            int count = userService.insertUsers(list);
 
            return success(count);
        } catch (Exception ex) {
            return fail(ex, -1);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "删除一条")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping(value = "/deleteUser")
    public ResponseMsg<Integer> deleteUser(int id) {
        try {
            int count = userService.deleteUser(id);
 
            return success(count);
        } catch (Exception ex) {
            return fail(ex, -1);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "删除多条")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID数组", dataType = "List<Integer>", paramType = "query", example = "1,2")
    })
    @GetMapping(value = "/deleteUsers")
    public ResponseMsg<Integer> deleteUsers(@RequestParam List<Integer> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return fail("id数组不能为空", -1);
            }
 
            int count = userService.deleteUsers(ids);
 
            return success(count);
        } catch (Exception ex) {
            return fail(ex, -1);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "更新一条")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entity", value = "实体类", dataType = "UserEntity", paramType = "body", example = "")
    })
    @ResponseBody
    @PostMapping(value = "/updateUser", produces = "application/json; charset=UTF-8")
    @SuppressWarnings("AlibabaRemoveCommentedCode")
    public ResponseMsg<Integer> updateUser(@RequestBody UserEntity entity, HttpServletRequest req) {
        try {
            /*String str = userService.validateOldPwd(entity);
            if (str != null) {
                return fail(str, -1);
            }*/
 
            UserEntity ue = tokenService.getCurrentUser(req);
            if (ue != null) {
                entity.setUpdateUser(ue.getId());
            }
 
            int count = userService.updateUser(entity);
 
            return success(count);
        } catch (Exception ex) {
            return fail(ex, -1);
        }
    }
 
    @SysLog()
    @ApiOperation(value = "更新多个用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminPwd", value = "管理员密码", dataType = "String", paramType = "body", example = ""),
            @ApiImplicitParam(name = "newPwd", value = "新密码", dataType = "String", paramType = "body", example = ""),
            @ApiImplicitParam(name = "ids", value = "用户ID集合", dataType = "List<Integer>", paramType = "body", example = "")
    })
    @PostMapping(value = "/updateUsersPwd", produces = "application/json; charset=UTF-8")
    public ResponseMsg<Boolean> updateUsersPwd(@RequestBody UserUpdateEntity uue, HttpServletRequest req) {
        try {
            if (uue == null || uue.getIds() == null || uue.getIds().isEmpty()) {
                return fail("没有找到数据", false);
            }
 
            UserEntity ue = tokenService.getCurrentUser(req);
            String str = userService.validateAdminPwd(ue, uue.getAdminPwd());
            if (str != null) {
                return fail(str, false);
            }
 
            Integer rows = userService.selectForIsAdmin(ue.getId());
            if (rows < 1) {
                return fail("只允许管理员操作", false);
            }
 
            str = userService.validateNewPwd(ue, uue.getNewPwd());
            if (str != null) {
                return fail(str, false);
            }
 
            rows = userService.updateUsersPwd(ue.getId(), ue.getSalt(), uue.getIds());
 
            return success(rows > 0 ? "更新成功" : "更新失败", rows > 0);
        } catch (Exception ex) {
            return fail(ex, false);
        }
    }
}