MenuController.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.hb.proj.auth.controller;
  2. import java.util.List;
  3. import java.util.Map;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.validation.annotation.Validated;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PostMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import com.hb.proj.allconfig.AccessToken;
  12. import com.hb.proj.auth.service.MenuService;
  13. import com.hb.proj.model.MenuPO;
  14. import com.hb.proj.model.MenuVO;
  15. import com.hb.proj.utils.RespVO;
  16. import com.hb.proj.utils.RespVOBuilder;
  17. import jakarta.validation.constraints.NotBlank;
  18. @RestController
  19. @RequestMapping("/menu")
  20. @Validated
  21. public class MenuController {
  22. @Autowired
  23. private MenuService service;
  24. /**
  25. * 获得单个菜单
  26. * @param menuId 菜单记录id
  27. * @return
  28. */
  29. @GetMapping("/get")
  30. public RespVO<MenuVO> get(@NotBlank(message = "菜单编号不能为空") String menuId){
  31. return RespVOBuilder.ok(service.getMenu(menuId));
  32. }
  33. /**
  34. * 增加新菜单
  35. * @param menu 菜单实体
  36. * @param token 登录人信息,自动从请求获取
  37. * @return
  38. */
  39. @PostMapping("/add")
  40. public RespVO<String> add(@Validated MenuPO menu,AccessToken token){
  41. if(StringUtils.isBlank(menu.getFatherMenuId())){
  42. menu.setFatherMenuId("0");
  43. }
  44. String newAssistCode = service.generateLeveledCode(menu.getFatherMenuId());
  45. if(newAssistCode==null){
  46. return RespVOBuilder.error("指定的父菜单不存在");
  47. }
  48. menu.setAssistCode(newAssistCode);
  49. menu.setModifyBy(token!=null?token.getTokenId():"unknow");
  50. return RespVOBuilder.ok(service.addMenu(menu));
  51. }
  52. /**
  53. * 更新菜单
  54. * @param menu 菜单实体
  55. * @param token 登录人信息,自动从请求获取
  56. * @return
  57. */
  58. @PostMapping("/update")
  59. public RespVO<Object> update(@Validated MenuPO menu,AccessToken token){
  60. if(StringUtils.isEmpty(menu.getFatherMenuId())){
  61. menu.setFatherMenuId("0");
  62. }
  63. menu.setModifyBy(token!=null?token.getTokenId():"unknow");
  64. MenuVO dbMenu=service.getMenu(menu.getMenuId());
  65. //非更新数据保留原值
  66. menu.setAssistCode(dbMenu.getAssistCode());
  67. menu.setCreateBy(dbMenu.getCreateBy());
  68. menu.setCreateTime(dbMenu.getCreateTime());
  69. menu.setDelIf(dbMenu.getDelIf());
  70. String fatherMenuId=menu.getFatherMenuId();
  71. if (!fatherMenuId.equals(dbMenu.getFatherMenuId())) { // 隶属关系变动后,修正assistCode
  72. String newAssistCode =service.generateLeveledCode(fatherMenuId);
  73. if(newAssistCode==null){
  74. return RespVOBuilder.error("指定的父菜单不存在");
  75. }
  76. service.updateMenu(menu,newAssistCode);
  77. }
  78. else{
  79. service.updateMenu(menu,null);
  80. }
  81. return RespVOBuilder.ok();
  82. }
  83. /**
  84. * 删除单个菜单
  85. * @param menuId 被删除菜单id
  86. * @return
  87. */
  88. @RequestMapping("/delete")
  89. public RespVO<Object> delete(@NotBlank(message = "菜单编号不能为空") String menuId){
  90. service.deleteMenu(menuId);
  91. return RespVOBuilder.ok();
  92. }
  93. /**
  94. * 获得菜单子节点
  95. * @param superId
  96. * @param targetDepth
  97. * @param response
  98. */
  99. @PostMapping("/treeNode")
  100. public RespVO<Object> treeNode(String superId){
  101. List<Map<String,Object>> nodes=service.queryTreeNodeBySuperId(StringUtils.isEmpty(superId)?"0":superId);
  102. return RespVOBuilder.ok(nodes);
  103. }
  104. }