package tech.easyflow.admin.controller.system;

import com.mybatisflex.core.query.QueryWrapper;
import tech.easyflow.common.annotation.UsePermission;
import tech.easyflow.common.domain.Result;
import tech.easyflow.common.web.controller.BaseCurdController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.easyflow.system.entity.SysApiKeyResource;
import tech.easyflow.system.service.SysApiKeyResourceService;

import java.util.List;

/**
 * 请求接口表 控制层。
 *
 * @author 12076
 * @since 2025-12-01
 */
@RestController
@RequestMapping("/api/v1/sysApiKeyResourcePermission")
@UsePermission(moduleName = "/api/v1/sysApiKey")
public class SysApiKeyResourceController extends BaseCurdController<SysApiKeyResourceService, SysApiKeyResource> {
    public SysApiKeyResourceController(SysApiKeyResourceService service) {
        super(service);
    }

    /**
     * 查询普通 API Key 接口授权资源。
     *
     * <p>工作流 Public API 使用独立的全局授权开关，不进入普通接口授权列表，避免用户误以为勾选
     * 具体接口资源即可完成工作流调用授权。</p>
     *
     * @param entity 查询条件
     * @param asTree 是否树形返回
     * @param sortKey 排序字段
     * @param sortType 排序方向
     * @return 普通接口授权资源
     */
    @Override
    @GetMapping("list")
    public Result<List<SysApiKeyResource>> list(SysApiKeyResource entity, Boolean asTree, String sortKey, String sortType) {
        QueryWrapper queryWrapper = QueryWrapper.create(entity, buildOperators(entity));
        queryWrapper.notLike(SysApiKeyResource::getRequestInterface, "/public-api/workflow/");
        queryWrapper.notLike(SysApiKeyResource::getRequestInterface, "/public-api/knowledge-share/");
        queryWrapper.orderBy(buildOrderBy(sortKey, sortType, getDefaultOrderBy()));
        return Result.ok(service.list(queryWrapper));
    }
}
