JinPengYong
2021-03-21 8a66d1972d2e9245e2999d06d49d2c864492ab17
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
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.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.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.util.PageResult;
import com.moral.util.WebUtils;
 
@Slf4j
@Api(tags = {"用户"})
@RestController
@RequestMapping("/user")
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @ApiOperation(value = "添加账户", notes = "添加账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户,长度6-10", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码,长度6-20", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "账户名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱,格式123456@qq.com", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号,1开头11为数字", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "wechat", value = "微信", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "addUser", method = RequestMethod.POST)
    public ResultMessage addUser(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());
        }
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "删除账户", notes = "删除账户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "deleteUser", method = RequestMethod.POST)
    public ResultMessage deleteUser(String userId) {
        if (userId == null) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);
        User user = userService.getOne(queryWrapper);
        if (user == null) {
            return ResultMessage.fail(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode(), ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg());
        }
        userService.deleteUser(user);
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "修改账户信息", notes = "修改账户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户,长度6-10", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码,长度6-20", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "账户名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱,格式123456@qq.com", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号,1开头11位数字", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "wechat", value = "微信", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "updateUser", method = RequestMethod.POST)
    public ResultMessage updateUser(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.updateUser(user, token);
        if (!result.isEmpty()) {
            return ResultMessage.fail((int) result.get("code"), result.get("msg").toString());
        }
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "分页查询账户列表", notes = "分页查询账户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "String"),
            @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 = "header", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号模糊查询", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱模糊查询", required = false, paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "wechat", value = "微信模糊查询", required = false, paramType = "header", dataType = "String"),
 
    })
    @RequestMapping(value = "selectUsers", method = RequestMethod.POST)
    public ResultMessage selectUsers(HttpServletRequest request) {
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        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);
    }
 
}