package com.ruoyi.web.core.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * @auther 周志雄
 * @date 2024/6/26 14:52
 */

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("无人机管理API")
                .description("无人机管理相关接口")
                .version("1.0")
                .build();
    }

    private Predicate<RequestHandler> customRequestHandlerSelector(String... basePackages) {
        List<Predicate<RequestHandler>> predicates = new ArrayList<>();
        for (String basePackage : basePackages) {
            predicates.add(RequestHandlerSelectors.basePackage(basePackage.trim()));
        }
        return predicates.stream().reduce(Predicate::or).orElse(x -> false);
    }
}
