package {{packageName}}.client.{{lowerCase apiEntity.api.codeName}}.feign;

import {{packageName}}.client.{{lowerCase apiEntity.api.codeName}}.feign.helper.IgnorePrivInterceptor;
import {{packageName}}.client.{{lowerCase apiEntity.api.codeName}}.feign.helper.OutsideAccessorUtils;
import {{packageName}}.client.{{lowerCase apiEntity.api.codeName}}.feign.helper.SuperLoginClient;
import feign.Feign;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * {{apiEntity.entity.logicName}}
 *
 */
@Configuration
public class {{apiEntity.codeName}}FeignClientConfiguration {

    @Value("${ibiz.ref.service.ibizrt4ebsx-rt4ebsx.name:ibizrt4ebsx-rt4ebsx}")
    String serviceValue;

    @Value("${ibiz.ref.service.ibizrt4ebsx-rt4ebsx.system:f7ad7e05-9031-11eb-b882-00163e06e68c}")
    String serviceSystem;

    @Value("${ibiz.ref.service.ibizrt4ebsx-rt4ebsx.super:false}")
    boolean superapi;

    @Value("${ibiz.ref.service.uaa:ibizrt4ebsx-rt4ebsx}")
    String uaaservice;

    @Value("${ibiz.ref.service.ibizrt4ebsx-rt4ebsx.login:}")
    String login;

    @Value("${ibiz.ref.service.ibizrt4ebsx-rt4ebsx.password:}")
    String password;

    @Autowired
    Feign.Builder builder;

    @Autowired
    OutsideAccessorUtils outsideAccessorUtils;

    @Autowired
    List<RequestInterceptor> defaultInterceptors;

    @Bean
    public {{apiEntity.codeName}}FeignClient {{camelCase apiEntity.codeName}}FeignClient() {
        List<RequestInterceptor> requestInterceptors = new ArrayList<>();
        requestInterceptors.addAll(defaultInterceptors);
        RequestInterceptor systemInterceptor = new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                requestTemplate.header("srfsystemid", Collections.emptyList());
                requestTemplate.header("srfsystemid", serviceSystem);
            }
        };
        requestInterceptors.add(systemInterceptor);

        //忽略权限
        if (superapi) {
            SuperLoginClient superLoginClient = outsideAccessorUtils.buildAccessor(SuperLoginClient.class, null, uaaservice, Arrays.asList(new RequestInterceptor() {
                @Override
                public void apply(RequestTemplate requestTemplate) {
                    requestTemplate.header("srfsystemid", Collections.emptyList());
                    requestTemplate.header("srforgid", Collections.emptyList());
                    requestTemplate.header("Authorization", Collections.emptyList());
                }
            }));
            RequestInterceptor ignorePrivInterceptor = new IgnorePrivInterceptor(superLoginClient, login, password);
            requestInterceptors.add(ignorePrivInterceptor);
        }
        return outsideAccessorUtils.buildAccessor({{apiEntity.codeName}}FeignClient.class, new {{apiEntity.codeName}}Fallback(), serviceValue, requestInterceptors);
    }

}