content: package com.company.project.controller;
  
  import cn.dev33.satoken.annotation.SaCheckPermission;
  import cn.dev33.satoken.annotation.SaMode;
  import com.company.project.common.aop.annotation.LogAnnotation;
  import com.company.project.common.exception.BusinessException;
  import com.company.project.common.exception.code.BaseResponseCode;
  import com.company.project.entity.SysPermission;
  import com.company.project.service.PermissionService;
  import com.company.project.vo.resp.PermissionRespNode;
  import io.swagger.annotations.Api;
  import io.swagger.annotations.ApiOperation;
  import org.springframework.util.StringUtils;
  import org.springframework.web.bind.annotation.*;
  
  import javax.annotation.Resource;
  import javax.validation.Valid;
  import java.util.List;
  
  /**
   * 菜单权限管理
   *
   * @author wenbin
   * @version V1.0
   * @date 2020年3月18日
   */
  @RequestMapping("/sys")
  @RestController
  @Api(tags = "组织模块-菜单权限管理")
  public class PermissionController {
  
      @Resource
      private PermissionService permissionService;
  
      @PostMapping("/permission")
      @ApiOperation(value = "新增菜单权限接口")
      @LogAnnotation(title = "菜单权限管理", action = "新增菜单权限")
      @SaCheckPermission("sys:permission:add")
      public void addPermission(@RequestBody @Valid SysPermission vo) {
          verifyFormPid(vo);
          permissionService.save(vo);
      }
  
      @DeleteMapping("/permission/{id}")
      @ApiOperation(value = "删除菜单权限接口")
      @LogAnnotation(title = "菜单权限管理", action = "删除菜单权限")
      @SaCheckPermission("sys:permission:deleted")
      public void deleted(@PathVariable("id") String id) {
          permissionService.deleted(id);
      }
  
      @PutMapping("/permission")
      @ApiOperation(value = "更新菜单权限接口")
      @LogAnnotation(title = "菜单权限管理", action = "更新菜单权限")
      @SaCheckPermission("sys:permission:update")
      public void updatePermission(@RequestBody @Valid SysPermission vo) {
          if (StringUtils.isEmpty(vo.getId())) {
              throw new BusinessException("id不能为空");
          }
          SysPermission sysPermission = permissionService.getById(vo.getId());
          if (null == sysPermission) {
              throw new BusinessException(BaseResponseCode.DATA_ERROR);
          }
          // 只有类型变更或者所属菜单变更
          if (sysPermission.getType().equals(vo.getType()) || !sysPermission.getPid().equals(vo.getPid())) {
              verifyFormPid(vo);
          }
          permissionService.updatePermission(vo);
      }
  
      @GetMapping("/permission/{id}")
      @ApiOperation(value = "查询菜单权限接口")
      @LogAnnotation(title = "菜单权限管理", action = "查询菜单权限")
      @SaCheckPermission("sys:permission:detail")
      public SysPermission detailInfo(@PathVariable("id") String id) {
          return permissionService.getById(id);
  
      }
  
      @GetMapping("/permissions")
      @ApiOperation(value = "获取所有菜单权限接口")
      @LogAnnotation(title = "菜单权限管理", action = "获取所有菜单权限")
      @SaCheckPermission("sys:permission:list")
      public List<SysPermission> getAllMenusPermission() {
          return permissionService.selectAll(null);
      }
  
      @GetMapping("/permission/tree")
      @ApiOperation(value = "获取所有目录菜单树接口")
      @LogAnnotation(title = "菜单权限管理", action = "获取所有目录菜单树")
      @SaCheckPermission(value = {"sys:permission:update", "sys:permission:add"}, mode = SaMode.OR)
      public List<PermissionRespNode> getAllMenusPermissionTree(@RequestParam(required = false) String permissionId) {
          return permissionService.selectAllMenuByTree(permissionId);
      }
  
      @GetMapping("/permission/tree/all")
      @ApiOperation(value = "获取所有目录菜单树接口")
      @LogAnnotation(title = "菜单权限管理", action = "获取所有目录菜单树")
      @SaCheckPermission(value = {"sys:role:update", "sys:role:add"}, mode = SaMode.OR)
      public List<PermissionRespNode> getAllPermissionTree() {
          return permissionService.selectAllByTree(1);
      }
  
      /**
       * 操作后的菜单类型是目录的时候 父级必须为目录
       * 操作后的菜单类型是菜单的时候，父类必须为目录类型
       * 操作后的菜单类型是按钮的时候 父类必须为菜单类型
       */
      private void verifyFormPid(SysPermission sysPermission) {
          SysPermission parent;
          parent = permissionService.getById(sysPermission.getPid());
          switch (sysPermission.getType()) {
              case 1:
                  if (parent != null) {
                      if (parent.getType() != 1) {
                          throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_CATALOG_ERROR);
                      }
                  } else if (!"0".equals(sysPermission.getPid())) {
                      throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_CATALOG_ERROR);
                  }
                  break;
              case 2:
                  if (parent == null || parent.getType() != 1) {
                      throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_MENU_ERROR);
                  }
                  if (StringUtils.isEmpty(sysPermission.getUrl())) {
                      throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_URL_NOT_NULL);
                  }
  
                  break;
              case 3:
                  if (parent == null || parent.getType() != 2) {
                      throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_BTN_ERROR);
                  }
                  if (StringUtils.isEmpty(sysPermission.getPerms())) {
                      throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_URL_PERMS_NULL);
                  }
                  if (StringUtils.isEmpty(sysPermission.getUrl())) {
                      throw new BusinessException(BaseResponseCode.OPERATION_MENU_PERMISSION_URL_NOT_NULL);
                  }
                  break;
              default:
          }
      }
  }
  