wujian
2024-02-22 268beb4ebc1e5b8d4ad715b71cd64a0944073a87
提交 | 用户 | 时间
268beb 1 package com.jcdm.web.controller.tool;
W 2
3 import java.util.ArrayList;
4 import java.util.LinkedHashMap;
5 import java.util.List;
6 import java.util.Map;
7 import org.springframework.web.bind.annotation.DeleteMapping;
8 import org.springframework.web.bind.annotation.GetMapping;
9 import org.springframework.web.bind.annotation.PathVariable;
10 import org.springframework.web.bind.annotation.PostMapping;
11 import org.springframework.web.bind.annotation.PutMapping;
12 import org.springframework.web.bind.annotation.RequestBody;
13 import org.springframework.web.bind.annotation.RequestMapping;
14 import org.springframework.web.bind.annotation.RestController;
15 import com.jcdm.common.core.controller.BaseController;
16 import com.jcdm.common.core.domain.R;
17 import com.jcdm.common.utils.StringUtils;
18 import io.swagger.annotations.Api;
19 import io.swagger.annotations.ApiImplicitParam;
20 import io.swagger.annotations.ApiImplicitParams;
21 import io.swagger.annotations.ApiModel;
22 import io.swagger.annotations.ApiModelProperty;
23 import io.swagger.annotations.ApiOperation;
24
25 /**
26  * swagger 用户测试方法
27  * 
28  * @author jc
29  */
30 @Api("用户信息管理")
31 @RestController
32 @RequestMapping("/test/user")
33 public class TestController extends BaseController
34 {
35     private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
36     {
37         users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
38         users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
39     }
40
41     @ApiOperation("获取用户列表")
42     @GetMapping("/list")
43     public R<List<UserEntity>> userList()
44     {
45         List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
46         return R.ok(userList);
47     }
48
49     @ApiOperation("获取用户详细")
50     @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
51     @GetMapping("/{userId}")
52     public R<UserEntity> getUser(@PathVariable Integer userId)
53     {
54         if (!users.isEmpty() && users.containsKey(userId))
55         {
56             return R.ok(users.get(userId));
57         }
58         else
59         {
60             return R.fail("用户不存在");
61         }
62     }
63
64     @ApiOperation("新增用户")
65     @ApiImplicitParams({
66         @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", dataTypeClass = Integer.class),
67         @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", dataTypeClass = String.class),
68         @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String", dataTypeClass = String.class),
69         @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String", dataTypeClass = String.class)
70     })
71     @PostMapping("/save")
72     public R<String> save(UserEntity user)
73     {
74         if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
75         {
76             return R.fail("用户ID不能为空");
77         }
78         users.put(user.getUserId(), user);
79         return R.ok();
80     }
81
82     @ApiOperation("更新用户")
83     @PutMapping("/update")
84     public R<String> update(@RequestBody UserEntity user)
85     {
86         if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
87         {
88             return R.fail("用户ID不能为空");
89         }
90         if (users.isEmpty() || !users.containsKey(user.getUserId()))
91         {
92             return R.fail("用户不存在");
93         }
94         users.remove(user.getUserId());
95         users.put(user.getUserId(), user);
96         return R.ok();
97     }
98
99     @ApiOperation("删除用户信息")
100     @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
101     @DeleteMapping("/{userId}")
102     public R<String> delete(@PathVariable Integer userId)
103     {
104         if (!users.isEmpty() && users.containsKey(userId))
105         {
106             users.remove(userId);
107             return R.ok();
108         }
109         else
110         {
111             return R.fail("用户不存在");
112         }
113     }
114 }
115
116 @ApiModel(value = "UserEntity", description = "用户实体")
117 class UserEntity
118 {
119     @ApiModelProperty("用户ID")
120     private Integer userId;
121
122     @ApiModelProperty("用户名称")
123     private String username;
124
125     @ApiModelProperty("用户密码")
126     private String password;
127
128     @ApiModelProperty("用户手机")
129     private String mobile;
130
131     public UserEntity()
132     {
133
134     }
135
136     public UserEntity(Integer userId, String username, String password, String mobile)
137     {
138         this.userId = userId;
139         this.username = username;
140         this.password = password;
141         this.mobile = mobile;
142     }
143
144     public Integer getUserId()
145     {
146         return userId;
147     }
148
149     public void setUserId(Integer userId)
150     {
151         this.userId = userId;
152     }
153
154     public String getUsername()
155     {
156         return username;
157     }
158
159     public void setUsername(String username)
160     {
161         this.username = username;
162     }
163
164     public String getPassword()
165     {
166         return password;
167     }
168
169     public void setPassword(String password)
170     {
171         this.password = password;
172     }
173
174     public String getMobile()
175     {
176         return mobile;
177     }
178
179     public void setMobile(String mobile)
180     {
181         this.mobile = mobile;
182     }
183 }