xyc
5 天以前 664cae654867b528ea339f187ebe290cb04d6fcf
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
package com.trafficaudit.system.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.trafficaudit.common.Result;
import com.trafficaudit.system.entity.User;
import com.trafficaudit.system.mapper.UserMapper;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/** 用户管理(含负责报表模块配置:freight/passenger/invest/energy/cityPassenger) */
@RestController
@RequestMapping("/api/user")
public class UserController {
 
    @Resource
    private UserMapper userMapper;
    @Resource
    private PasswordEncoder passwordEncoder;
 
    @GetMapping("/list")
    public Result<List<Map<String, Object>>> list(@RequestParam(value = "keyword", required = false) String keyword) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        if (keyword != null && !keyword.trim().isEmpty()) {
            String k = keyword.trim();
            qw.and(w -> w.like(User::getUsername, k).or().like(User::getRealName, k));
        }
        qw.orderByAsc(User::getId);
        List<Map<String, Object>> data = new ArrayList<>();
        for (User u : userMapper.selectList(qw)) {
            Map<String, Object> m = new HashMap<>();
            m.put("id", u.getId());
            m.put("username", u.getUsername());
            m.put("realName", u.getRealName());
            m.put("phone", u.getPhone());
            m.put("status", u.getStatus());
            m.put("modules", u.getModules());
            m.put("createdAt", u.getCreatedAt() == null ? null : u.getCreatedAt().toString());
            data.add(m);
        }
        return Result.ok(data);
    }
 
    /** 新增/编辑用户;编辑时 password 留空则不修改密码;modules 为模块 key 数组 */
    @PostMapping("/save")
    public Result<Void> save(@RequestBody Map<String, Object> params) {
        Object idObj = params.get("id");
        Long id = idObj == null ? null : Long.valueOf(idObj.toString());
        String username = str(params.get("username"));
        if (username == null || username.trim().isEmpty()) return Result.error(400, "用户名不能为空");
        username = username.trim();
        String password = str(params.get("password"));
        String realName = str(params.get("realName"));
        String phone = str(params.get("phone"));
        Integer status = params.get("status") == null ? 1 : Integer.valueOf(params.get("status").toString());
        List<String> moduleKeys = new ArrayList<>();
        Object modulesObj = params.get("modules");
        if (modulesObj instanceof List) {
            for (Object o : (List<?>) modulesObj) if (o != null) moduleKeys.add(o.toString());
        }
        String modules = String.join(",", moduleKeys);
 
        LambdaQueryWrapper<User> dup = new LambdaQueryWrapper<User>().eq(User::getUsername, username);
        if (id != null) dup.ne(User::getId, id);
        if (userMapper.selectCount(dup) != null && userMapper.selectCount(dup) > 0) return Result.error(400, "用户名已存在");
 
        if (id == null) {
            if (password == null || password.isEmpty()) return Result.error(400, "新增用户必须设置密码");
            User u = new User();
            u.setUsername(username);
            u.setRealName(realName);
            u.setPhone(phone);
            u.setStatus(status);
            u.setModules(modules);
            u.setPassword(passwordEncoder.encode(password));
            userMapper.insert(u);
        } else {
            User u = userMapper.selectById(id);
            if (u == null) return Result.error(404, "用户不存在");
            u.setRealName(realName);
            u.setPhone(phone);
            u.setStatus(status);
            u.setModules(modules);
            if (password != null && !password.isEmpty()) u.setPassword(passwordEncoder.encode(password));
            userMapper.updateById(u);
        }
        return Result.ok(null);
    }
 
    @PostMapping("/delete")
    public Result<Void> delete(@RequestParam("id") Long id) {
        User u = userMapper.selectById(id);
        if (u == null) return Result.error(404, "用户不存在");
        if ("admin".equals(u.getUsername())) return Result.error(400, "admin 不允许删除");
        userMapper.deleteById(id);
        return Result.ok(null);
    }
 
    private String str(Object o) {
        return o == null ? null : o.toString();
    }
}