cjl
2023-11-15 d117d3af15dddc4f07baffa8b5bf6b727c05de7c
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
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.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
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.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 = "insert", method = RequestMethod.POST)
    private ResultMessage insert(@RequestBody Group group) {
        if (group.getGroupName().isEmpty()) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        Map<String, Object> result = groupService.addGroup(group);
        if (!result.isEmpty()) {
            return ResultMessage.fail((int) result.get("code"), result.get("msg").toString());
        }
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "删除组", notes = "删除组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "组id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public ResultMessage delete(Integer id) {
        if (id == null) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        groupService.deleteGroup(id);
        return ResultMessage.ok();
    }
 
    @ApiOperation(value = "修改组信息", notes = "修改组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public ResultMessage update(@RequestBody Group group) {
        Map<String, Object> result = groupService.updateGroup(group);
        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 = "orderType", value = "排序类型,升序:0,降序:1", defaultValue = "0", 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 = "select", method = RequestMethod.GET)
    public ResultMessage select(HttpServletRequest request) {
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null);
        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 = "userId", value = "用户id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "getGroup", method = RequestMethod.GET)
    public ResultMessage getGroup(Integer userId) {
        if (ObjectUtils.isEmpty(userId)) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        Map<String, Object> group = groupService.getGroup(userId);
        return ResultMessage.ok(group);
    }
 
    @ApiOperation(value = "用户分配组", notes = "用户分配组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping(value = "allot", method = RequestMethod.POST)
    public ResultMessage allot(@RequestBody Map<String, Object> parameters) {
        if (!parameters.containsKey("userId")) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        userGroupService.allotGroups(parameters);
        return ResultMessage.ok();
    }
}