陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
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
package com.mzl.flower.web.system;
 
import com.mzl.flower.base.BaseController;
import com.mzl.flower.base.R;
import com.mzl.flower.base.ReturnDataDTO;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.dto.request.system.CreateMenuDTO;
import com.mzl.flower.dto.request.system.UpdateMenuDTO;
import com.mzl.flower.dto.response.system.MenuDTO;
import com.mzl.flower.dto.response.system.MenuTreeDTO;
import com.mzl.flower.entity.system.Menu;
import com.mzl.flower.service.system.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
 
@RestController
@RequestMapping("/api/menu")
@Api(value = "菜单管理", tags = "菜单管理")
@Validated
public class MenuController extends BaseController {
    @Autowired
    private MenuService menuService;
 
    @GetMapping("/list")
    @ApiOperation(value = "查询菜单列表")
    public ResponseEntity<ReturnDataDTO<List<MenuTreeDTO>>> searchMenuList() {
        return returnData(R.SUCCESS.getCode(), menuService.searchOperationMenu());
    }
 
    @PostMapping("/list/new")
    @ApiOperation(value = "新增菜单")
    public ResponseEntity<ReturnDataDTO> addTenantMenu(@RequestBody @Validated CreateMenuDTO dto) {
        if (StringUtils.isNotBlank(dto.getParentId()) && !"-1".equals(dto.getParentId())) {
            Menu menu = menuService.getMenu(dto.getParentId());
            if (menu == null) {
                throw new ValidationException("父菜单不存在");
            }
        }
        menuService.addMenu(dto);
        return returnData(R.SUCCESS.getCode(), null);
    }
 
    @GetMapping("/list/view")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "查询菜单详情")
    public ResponseEntity<ReturnDataDTO<MenuDTO>> getMenu(String id) {
        return returnData(R.SUCCESS.getCode(), menuService.getMenuDetail(id));
    }
 
    @PostMapping("/list/edit")
    @ApiOperation(value = "编辑菜单")
    public ResponseEntity<ReturnDataDTO> updateTenantMenu(@RequestBody @Validated UpdateMenuDTO dto) {
        if (StringUtils.isNotBlank(dto.getParentId()) && !"-1".equals(dto.getParentId())) {
            Menu menu = menuService.getMenu(dto.getParentId());
            if (menu == null) {
                throw new ValidationException("父菜单不存在");
            }
        }
        menuService.updateMenu(dto);
        return returnData(R.SUCCESS.getCode(), null);
    }
 
    @GetMapping("/list/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单ID", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "删除菜单")
    public ResponseEntity<ReturnDataDTO> deleteTenantMenu(String id) {
        menuService.deleteMenu(id);
        return returnData(R.SUCCESS.getCode(), null);
    }
}