jinpengyong
2021-04-02 c90b6e135f2f609ba187e243e37ae37d3da6ddee
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
package com.moral.api.controller;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
 
import lombok.extern.slf4j.Slf4j;
 
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.User;
import com.moral.api.service.UserService;
import com.moral.api.utils.OperationLogUtils;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.util.PageResult;
 
@Slf4j
@Api(tags = {"用户"})
@RestController
@RequestMapping("/user")
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @ApiOperation(value = "添加账户", notes = "添加账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "addUser", method = RequestMethod.POST)
    public ResultMessage addUser(@RequestBody User user, HttpServletRequest request) {
        if (user.getAccount().isEmpty() || user.getPassword().isEmpty()) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        String token = request.getHeader("token");
        Map<String, Object> result = userService.addUser(user, token);
        if (!result.isEmpty()) {
            return ResultMessage.fail((int) result.get("code"), result.get("msg").toString());
        }
 
        //日志
        String content = "添加了账户:" + user.getAccount();
        OperationLogUtils.insertLog(request, content);
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "删除账户", notes = "删除账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "deleteUser", method = RequestMethod.POST)
    public ResultMessage deleteUser(@RequestBody User user, HttpServletRequest request) {
        if (user.getId() == null) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", user.getId());
        user = userService.getOne(queryWrapper);
        if (user == null) {
            return ResultMessage.fail(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode(),
                    ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg());
        }
        userService.deleteUser(user);
 
        //日志
        String content = "删除了账户:" + user.getAccount();
        OperationLogUtils.insertLog(request, content);
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "修改账户信息", notes = "修改账户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "updateUser", method = RequestMethod.POST)
    public ResultMessage updateUser(@RequestBody User user, HttpServletRequest request) {
        if (user.getId() == null) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        String token = request.getHeader("token");
        Map<String, Object> result = userService.updateUser(user, token);
        if (!result.isEmpty()) {
            return ResultMessage.fail((int) result.get("code"), result.get("msg").toString());
        }
 
        //日志
        String content = "修改了账户:" + user.getAccount();
        OperationLogUtils.insertLog(request, content);
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "分页查询账户列表", notes = "分页查询账户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "order", value = "排序字段", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "account", value = "账号模糊查询", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "用户名模糊查询", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号模糊查询", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱模糊查询", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "wechat", value = "微信模糊查询", required = false, paramType = "query", dataType = "String"),
 
    })
    @RequestMapping(value = "selectUsers", method = RequestMethod.POST)
    public ResultMessage selectUsers(@RequestBody Map<String, Object> parameters, HttpServletRequest request) {
        parameters.put("token", request.getHeader("token"));
        Page<User> userPage = userService.selectUsers(parameters);
        PageResult<User> pageResult = new PageResult<>(
                userPage.getTotal(), userPage.getPages(), userPage.getRecords()
        );
        return ResultMessage.ok(pageResult);
    }
 
}