package com.CUST.brain.web.controller.system;

import com.CUST.brain.common.api.CommonResult;
import com.CUST.brain.common.dto.system.PermissionTreeVO;
import com.CUST.brain.common.dto.system.UpdateRolePermissionsRequestDTO;
import com.CUST.brain.dao.domain.system.SysPermission;
import com.CUST.brain.service.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/permission")
@Api(tags = "系统权限管理")
public class SysPermissionController {

    @Resource
    private SysPermissionService permissionService;

    @GetMapping("/listForRoleTree")
    @ApiOperation("根据角色获取权限树")
    public CommonResult<List<PermissionTreeVO>> listForRoleTree(@RequestParam String roleId) {
        List<PermissionTreeVO> permissions = permissionService.listForRoleTree(roleId);
        return CommonResult.success(permissions);
    }

    @GetMapping("/role/{roleId}/permissionIds")
    @ApiOperation("获取角色已分配的权限ID列表")
    public CommonResult<List<SysPermission>> getPermissionIdsByRole(@PathVariable String roleId) {
        List<SysPermission> permissions = permissionService.getPermissionIdsByRoleId(roleId);
        return CommonResult.success(permissions);
    }

    @PostMapping("/updateRolePermissions")
    @ApiOperation("更新角色权限")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> updateRolePermissions(
            @RequestBody @Valid UpdateRolePermissionsRequestDTO requestDTO) {
        permissionService.updateRolePermissions(requestDTO);
        return CommonResult.success(true, "更新成功");
    }
} 