jinpengyong
2021-03-25 25aeb531c8064ca768bb2d6f179ec7411b824b43
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
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.Group;
import com.moral.api.service.GroupService;
import com.moral.api.service.UserGroupService;
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("/group")
public class GroupController {
 
    @Autowired
    private GroupService groupService;
 
    @Autowired
    private UserGroupService userGroupService;
 
    @ApiOperation(value = "添加组", notes = "添加组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "addGroup", method = RequestMethod.POST)
    private ResultMessage addGroup(@RequestBody Group group, HttpServletRequest request) {
        if (group.getGroupName().isEmpty()) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        String token = request.getHeader("token");
        Map<String, Object> result = groupService.addGroup(group, token);
        if (!result.isEmpty()) {
            return ResultMessage.fail((int) result.get("code"), result.get("msg").toString());
        }
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "删除组", notes = "删除组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "deleteGroup", method = RequestMethod.POST)
    public ResultMessage deleteGroup(@RequestBody Map<String, Object> parameters) {
        if (!parameters.containsKey("groupId")) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        QueryWrapper<Group> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", parameters.get("groupId"));
        Group group = groupService.getOne(queryWrapper);
        if (group == null) {
            return ResultMessage.fail(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode(), ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg());
        }
        groupService.deleteGroup(group);
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "修改组信息", notes = "修改组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "updateGroup", method = RequestMethod.POST)
    public ResultMessage updateUser(@RequestBody Group group, HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String, Object> result = groupService.updateGroup(group, 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 = "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 = "groupName", value = "组名模糊查询", required = false, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "selectGroups", method = RequestMethod.POST)
    public ResultMessage selectGroups(@RequestBody Map<String, Object> parameters, HttpServletRequest request) {
        parameters.put("token", request.getHeader("token"));
        Page<Group> userPage = groupService.selectGroups(parameters);
        PageResult<Group> pageResult = new PageResult<>(
                userPage.getTotal(), userPage.getPages(), userPage.getRecords()
        );
        return ResultMessage.ok(pageResult);
    }
 
    @ApiOperation(value = "用户分配组", notes = "用户分配组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "allotGroups", method = RequestMethod.POST)
    public ResultMessage allotGroups(@RequestBody Map<String, Object> parameters, HttpServletRequest request) {
        if (!parameters.containsKey("userId")) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        String token = request.getHeader("token");
        userGroupService.allotGroups(parameters, token);
        return ResultMessage.ok();
    }
}