package com.gree.mall.manager;

/**
 *
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


/**
 * API文档设置
 * com.qichuan.sugar.minaapi.SwaggerConfig
 */
@Profile({"dev","test"})
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket statisticsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .groupName("运营后台API")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gree.mall.manager.controller"))
                .paths(PathSelectors.regex("/.*"))
                .build()
                .apiInfo(apiBInfo())
                .globalOperationParameters(setHeaderToken());
    }

    private ApiInfo apiBInfo() {

        return new ApiInfoBuilder()
                .title("家盛茂运营后台API")
                .description("家盛茂运营后台API")
                .contact(new Contact("格匠", "http://gejiang", "gejiang@gejiang.com"))
                .version("1.0")
                .build();
    }

    /**
     * 设置公共header参数
     * @return
     */
    private List<Parameter> setHeaderToken() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("x-token").description("校验TOKEN").modelRef(new ModelRef("string")).defaultValue("1111").parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }
}