package com.purui.prims.headless.server.rest;

import com.purui.prims.headless.api.pojo.request.BatchRoleAuthReq;
import com.purui.prims.headless.api.pojo.request.RoleAuthReq;
import com.purui.prims.headless.api.pojo.response.RoleAuthResp;
import com.purui.prims.headless.server.service.RoleAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色授权API
 *
 * @author charlie
 * @date 2024/7/5
 */
@RestController
@RequestMapping("/api/semantic/roleAuth")
@Api(tags = "角色授权管理")
public class RoleAuthController {

    private final RoleAuthService roleAuthService;

    public RoleAuthController(RoleAuthService roleAuthService) {
        this.roleAuthService = roleAuthService;
    }

    // 授权
    @ApiOperation(value = "授权")
    @PostMapping("/grant")
    public boolean grant(@RequestBody RoleAuthReq roleAuthReq) {
        return roleAuthService.grant(roleAuthReq);
    }

    @ApiOperation(value = "批量授权")
    @PostMapping("/grant/batch")
    public boolean grant(@RequestBody BatchRoleAuthReq roleAuthReq) {
        return roleAuthService.batchGrant(roleAuthReq);
    }

    // 删除授权
    @ApiOperation(value = "删除授权")
    @DeleteMapping("/delete/{id}")
    public boolean delete(@PathVariable Integer id) {
        return roleAuthService.removeById(id);
    }

    // 根据角色查询授权列表
    @ApiOperation(value = "查询授权列表")
    @GetMapping("/getAuthList")
    public List<RoleAuthResp> getRoleAuthList(String dataType, Integer roleId, Long dataId) {
        return roleAuthService.getRoleAuthList(dataType, roleId, dataId);
    }

    // 根据角色查询授权列表
    @ApiOperation(value = "查询授权列表")
    @GetMapping("/getDomainRoleAuthList")
    public List<RoleAuthResp> getDomainRoleAuthList(Integer roleId) {
        return roleAuthService.getDomainRoleAuthList(roleId);
    }

}
