/* * Copyright 2019-2020 Zheng Jie * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package easydo.technology.system.rest; import com.alibaba.fastjson.JSONObject; import easydo.technology.exception.BadRequestException; import easydo.technology.system.domain.Menu; import easydo.technology.system.dto.MenuDto; import easydo.technology.system.dto.MenuQueryParam; import easydo.technology.system.service.MenuService; import easydo.technology.utils.PageUtil; import easydo.technology.utils.SecurityUtils; import lombok.Delegate; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.util.*; import java.util.stream.Collectors; /** * @author Zheng Jie * @date 2018-12-03 */ @RestController @RequiredArgsConstructor @RequestMapping("/menus") public class MenuController { private final MenuService menuService; // private final MenuMapper menuMapper; private static final String ENTITY_NAME = "menu"; @GetMapping @PreAuthorize("@el.check('menu:list')") public ResponseEntity query(MenuQueryParam criteria) throws Exception { List menuDtoList = menuService.queryAll(criteria, true); return new ResponseEntity<>(PageUtil.toPage(menuDtoList, menuDtoList.size()), HttpStatus.OK); } @GetMapping(value = "/lazy") @PreAuthorize("@el.check('menu:list','roles:list')") public ResponseEntity query(@RequestParam Long pid, @RequestParam String platformType) { return new ResponseEntity<>(menuService.getMenus(pid, platformType), HttpStatus.OK); } @GetMapping(value = "/build") public ResponseEntity buildMenus() { List menuDtoList = menuService.findByUser(SecurityUtils.getCurrentUserId()); List menuDtos = menuService.buildTree(menuDtoList); return new ResponseEntity<>(menuService.buildMenus(menuDtos), HttpStatus.OK); } // @RequestMapping(value = "/appletsMenus") // @ApiOperation("获取前端所需小程序菜单") // public ResponseEntity appletsMenus() { // List menuDtoList = menuService.findByUser(SecurityUtils.getCurrentUserId()); // menuDtoList = menuDtoList.stream().filter(model -> model.getPlatformType().equals("easydo-mini-iot")).collect(Collectors.toList()); // return new ResponseEntity<>(menuService.buildMenus(menuDtoList), HttpStatus.OK); // } // @GetMapping(value = "/buildTransfer") // public ResponseEntity buildTransfer() { // @ApiOperation("获取前端所需菜单") // List menuDtoList = menuService.findByUser(SecurityUtils.getCurrentUserId(), "easydo-transfer"); // menuDtoList = menuDtoList.stream().filter(model -> model.getType() < 2).collect(Collectors.toList()); // List menuDtos = menuService.buildTree(menuDtoList); // return new ResponseEntity<>(menuService.buildMenus(menuDtos), HttpStatus.OK); // } @PostMapping("/superior") @PreAuthorize("@el.check('menu:list')") public ResponseEntity getSuperior(@RequestBody List ids) throws Exception { Set menuDtos = new LinkedHashSet<>(); if (ids != null && !ids.isEmpty()) { for (Long id : ids) { MenuDto menuDto = menuService.findById(id); menuDtos.addAll(menuService.getSuperior(menuDto, new ArrayList<>())); } return new ResponseEntity<>(menuService.buildTree(new ArrayList<>(menuDtos)), HttpStatus.OK); } throw new Exception("参数缺失"); } @PostMapping @PreAuthorize("@el.check('menu:add')") public ResponseEntity create(@Validated @RequestBody Menu resources) { if (resources.getId() != null) { throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID"); } menuService.save(resources); return new ResponseEntity<>(resources, HttpStatus.OK); } @PutMapping @PreAuthorize("@el.check('menu:edit')") public ResponseEntity update(@Validated(Menu.Update.class) @RequestBody Menu resources) { menuService.updateById(resources); return new ResponseEntity<>(resources, HttpStatus.OK); } @DeleteMapping @PreAuthorize("@el.check('menu:del')") public ResponseEntity delete(@RequestBody Set ids) { Set menuSet = new HashSet<>(); List menuList = new ArrayList<>(); for (Long id : ids) { Menu menu = menuService.getById(id); menuList.add(menu); } menuSet = menuService.getDeleteMenus(menuList, menuSet); ids = menuSet.stream().map(Menu::getId).collect(Collectors.toSet()); menuService.removeByIds(ids); return new ResponseEntity<>(HttpStatus.OK); } @GetMapping(value = "/download") @PreAuthorize("@el.check('menu:list')") public void download(HttpServletResponse response, MenuQueryParam criteria) throws Exception { menuService.download(menuService.queryAll(criteria, false), response); } }