package com.zd.common.swagger.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;


/**
 * @Author: zhoupan
 * @Date: 2021/09/30/11:28
 * @Description:
 */
@Configuration
@EnableKnife4j
@EnableSwagger2
public class Knife4jConfiguration {
    /**
     * 作者
     */
    public static class Author {

        public static final String CYL = "柴云龙";
        public static final String GS = "高升";
        public static final String ZP = "周攀";
    }

    /**
     * 借用一下这个类进行配置
     */
    @Autowired
    SwaggerProperties swaggerProperties;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(Arrays.asList(securitySchemes()));
    }

    private SecurityScheme securitySchemes() {
        return new ApiKey("授权信息", "Authorization", "header");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .description(swaggerProperties.getDescription())
                .contact(new Contact("ZD", "", ""))
                .version(swaggerProperties.getVersion())
                .title(swaggerProperties.getTitle())
                .build();
    }
}
