package com.gree.mall.miniapp;

/**
 *
 */

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.miniapp.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();
        tokenPar.name("x-token").description("校验TOKEN").modelRef(new ModelRef("string")).defaultValue("1111").parameterType("header").required(false).build();

        ParameterBuilder tokenPar2 = new ParameterBuilder();
        tokenPar2.name("APPID").description("APPID").modelRef(new ModelRef("string")).defaultValue("1").parameterType("header").required(false).build();

        List<Parameter> pars = new ArrayList<>();
        pars.add(tokenPar.build());
        pars.add(tokenPar2.build());
        return pars;
    }
}