123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package com.hb.proj.auth.controller;
- import java.util.List;
- import java.util.Map;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.validation.annotation.Validated;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import com.hb.proj.allconfig.AccessToken;
- import com.hb.proj.auth.service.MenuService;
- import com.hb.proj.model.MenuPO;
- import com.hb.proj.model.MenuVO;
- import com.hb.proj.utils.RespVO;
- import com.hb.proj.utils.RespVOBuilder;
- import jakarta.validation.constraints.NotBlank;
- @RestController
- @RequestMapping("/menu")
- @Validated
- public class MenuController {
- @Autowired
- private MenuService service;
-
-
- /**
- * 获得单个菜单
- * @param menuId 菜单记录id
- * @return
- */
- @GetMapping("/get")
- public RespVO<MenuVO> get(@NotBlank(message = "菜单编号不能为空") String menuId){
-
- return RespVOBuilder.ok(service.getMenu(menuId));
- }
-
-
- /**
- * 增加新菜单
- * @param menu 菜单实体
- * @param token 登录人信息,自动从请求获取
- * @return
- */
- @PostMapping("/add")
- public RespVO<String> add(@Validated MenuPO menu,AccessToken token){
-
- if(StringUtils.isBlank(menu.getFatherMenuId())){
- menu.setFatherMenuId("0");
- }
- String newAssistCode = service.generateLeveledCode(menu.getFatherMenuId());
- if(newAssistCode==null){
- return RespVOBuilder.error("指定的父菜单不存在");
- }
- menu.setAssistCode(newAssistCode);
- menu.setModifyBy(token!=null?token.getTokenId():"unknow");
- return RespVOBuilder.ok(service.addMenu(menu));
- }
-
- /**
- * 更新菜单
- * @param menu 菜单实体
- * @param token 登录人信息,自动从请求获取
- * @return
- */
- @PostMapping("/update")
- public RespVO<Object> update(@Validated MenuPO menu,AccessToken token){
- if(StringUtils.isEmpty(menu.getFatherMenuId())){
- menu.setFatherMenuId("0");
- }
- menu.setModifyBy(token!=null?token.getTokenId():"unknow");
-
- MenuVO dbMenu=service.getMenu(menu.getMenuId());
-
- //非更新数据保留原值
- menu.setAssistCode(dbMenu.getAssistCode());
- menu.setCreateBy(dbMenu.getCreateBy());
- menu.setCreateTime(dbMenu.getCreateTime());
- menu.setDelIf(dbMenu.getDelIf());
-
- String fatherMenuId=menu.getFatherMenuId();
- if (!fatherMenuId.equals(dbMenu.getFatherMenuId())) { // 隶属关系变动后,修正assistCode
- String newAssistCode =service.generateLeveledCode(fatherMenuId);
- if(newAssistCode==null){
- return RespVOBuilder.error("指定的父菜单不存在");
- }
- service.updateMenu(menu,newAssistCode);
- }
- else{
- service.updateMenu(menu,null);
- }
- return RespVOBuilder.ok();
- }
-
-
- /**
- * 删除单个菜单
- * @param menuId 被删除菜单id
- * @return
- */
- @RequestMapping("/delete")
- public RespVO<Object> delete(@NotBlank(message = "菜单编号不能为空") String menuId){
- service.deleteMenu(menuId);
- return RespVOBuilder.ok();
- }
-
- /**
- * 获得菜单子节点
- * @param superId
- * @param targetDepth
- * @param response
- */
- @PostMapping("/treeNode")
- public RespVO<Object> treeNode(String superId){
- List<Map<String,Object>> nodes=service.queryTreeNodeBySuperId(StringUtils.isEmpty(superId)?"0":superId);
- return RespVOBuilder.ok(nodes);
- }
-
- }
|