Commit 8fdc5512820247139762a0a091d4d9f3e8ef28c0
1 parent
436cb39c
添加用户操作的MQ通知功能,包括新增、更新和删除功能实现。
Showing
1 changed file
with
41 additions
and
4 deletions
itcast-auth/itcast-auth-server/src/main/java/com/itheima/authority/controller/auth/UserController.java
| 1 | 1 | package com.itheima.authority.controller.auth; |
| 2 | 2 | |
| 3 | +import cn.hutool.json.JSONObject; | |
| 3 | 4 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 5 | +import com.google.common.collect.Lists; | |
| 6 | +import com.itheima.authority.biz.mq.RabbitMessageSender; | |
| 4 | 7 | import com.itheima.authority.biz.service.auth.RoleService; |
| 5 | 8 | import com.itheima.authority.biz.service.auth.UserGroupService; |
| 6 | 9 | import com.itheima.authority.biz.service.auth.UserGroupUserService; |
| ... | ... | @@ -34,10 +37,7 @@ import org.springframework.validation.annotation.Validated; |
| 34 | 37 | import org.springframework.web.bind.annotation.*; |
| 35 | 38 | import org.springframework.web.multipart.MultipartFile; |
| 36 | 39 | |
| 37 | -import java.util.Collection; | |
| 38 | -import java.util.HashMap; | |
| 39 | -import java.util.List; | |
| 40 | -import java.util.Map; | |
| 40 | +import java.util.*; | |
| 41 | 41 | import java.util.stream.Collectors; |
| 42 | 42 | |
| 43 | 43 | import static com.itheima.tools.exception.code.ExceptionCode.BAD_REQUEST; |
| ... | ... | @@ -69,6 +69,8 @@ public class UserController extends BaseController { |
| 69 | 69 | private StationService stationService; |
| 70 | 70 | @Autowired |
| 71 | 71 | private DozerUtils dozer; |
| 72 | + @Autowired | |
| 73 | + private RabbitMessageSender rabbitMessageSender; | |
| 72 | 74 | |
| 73 | 75 | @ApiOperation(value = "分页查询用户", notes = "分页查询用户") |
| 74 | 76 | @ApiImplicitParams({ |
| ... | ... | @@ -247,6 +249,13 @@ public class UserController extends BaseController { |
| 247 | 249 | public R<User> save(@RequestBody @Validated UserSaveDTO data) { |
| 248 | 250 | User user = dozer.map(data, User.class); |
| 249 | 251 | userService.saveUser(user); |
| 252 | + | |
| 253 | + // 同步mq通知修改信息 | |
| 254 | + JSONObject jsonObject = new JSONObject(); | |
| 255 | + jsonObject.put("type", "USER"); | |
| 256 | + jsonObject.put("operation", "ADD"); | |
| 257 | + jsonObject.put("content", Lists.newArrayList(user)); | |
| 258 | + rabbitMessageSender.send("itcast-auth-user", "add", jsonObject.toString(), 3); | |
| 250 | 259 | return success(user); |
| 251 | 260 | } |
| 252 | 261 | |
| ... | ... | @@ -256,6 +265,12 @@ public class UserController extends BaseController { |
| 256 | 265 | public R<User> update(@RequestBody @Validated(SuperEntity.Update.class) UserUpdateDTO data) { |
| 257 | 266 | User user = dozer.map(data, User.class); |
| 258 | 267 | userService.updateUser(user); |
| 268 | + // 同步mq通知修改信息 | |
| 269 | + JSONObject jsonObject = new JSONObject(); | |
| 270 | + jsonObject.put("type", "USER"); | |
| 271 | + jsonObject.put("operation", "UPDATE"); | |
| 272 | + jsonObject.put("content", Lists.newArrayList(user)); | |
| 273 | + rabbitMessageSender.send("itcast-auth-user", "update", jsonObject.toString(), 3); | |
| 259 | 274 | return success(user); |
| 260 | 275 | } |
| 261 | 276 | |
| ... | ... | @@ -267,6 +282,15 @@ public class UserController extends BaseController { |
| 267 | 282 | user.setId(data.getId()); |
| 268 | 283 | user.setRoles(data.getRoles()); |
| 269 | 284 | userService.updateUserRole(user); |
| 285 | + | |
| 286 | + // 同步mq通知修改信息 | |
| 287 | + JSONObject jsonObject = new JSONObject(); | |
| 288 | + jsonObject.put("type", "USER"); | |
| 289 | + jsonObject.put("operation", "UPDATE"); | |
| 290 | + user = this.userService.getById(data.getId()); | |
| 291 | + user.setRoles(data.getRoles()); | |
| 292 | + jsonObject.put("content", Lists.newArrayList(user)); | |
| 293 | + rabbitMessageSender.send("itcast-auth-user", "update", jsonObject.toString(), 3); | |
| 270 | 294 | return success(user); |
| 271 | 295 | } |
| 272 | 296 | |
| ... | ... | @@ -289,7 +313,20 @@ public class UserController extends BaseController { |
| 289 | 313 | @DeleteMapping |
| 290 | 314 | @SysLog("删除用户") |
| 291 | 315 | public R<Boolean> delete(@RequestParam("ids[]") List<Long> ids) { |
| 316 | + // 查询待删除的组织 | |
| 317 | + List<User> delList = new ArrayList<>(); | |
| 318 | + for (Long id : ids) { | |
| 319 | + User user = this.userService.getById(id); | |
| 320 | + delList.add(user); | |
| 321 | + } | |
| 292 | 322 | userService.remove(ids); |
| 323 | + | |
| 324 | + // 同步mq通知修改信息 | |
| 325 | + JSONObject jsonObject = new JSONObject(); | |
| 326 | + jsonObject.put("type", "USER"); | |
| 327 | + jsonObject.put("operation", "DEL"); | |
| 328 | + jsonObject.put("content", delList); | |
| 329 | + rabbitMessageSender.send("itcast-auth-user", "del", jsonObject.toString(), 3); | |
| 293 | 330 | return success(true); |
| 294 | 331 | } |
| 295 | 332 | ... | ... |