package com.chushang.gateway.filter;

import cn.hutool.core.collection.CollectionUtil;
import com.chushang.common.core.constant.CommonConstants;
import com.chushang.common.core.util.IPUtils;
import com.chushang.common.core.util.IdUtils;
import com.chushang.common.core.util.ServletUtils;
import com.chushang.common.core.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author zhangran
 * @date 2021/7/13
 * <p>
 * 全局拦截器，作用所有的微服务
 * <p>
 * 1. 对请求的API调用过滤，记录接口的请求时间，方便日志审计、告警、分析等运维操作 2. 后期可以扩展对接其他日志系统
 * <p>
 */
@Slf4j
@Component
public class ApiLoggingFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        setTraceId(exchange);
        exchange.getAttributes().put(CommonConstants.START_TIME, System.currentTimeMillis());
        return chain.filter(exchange.mutate().build()).then(Mono.fromRunnable(() -> {
            ServerHttpRequest request = exchange.getRequest();
            if (log.isInfoEnabled()) {
                String info = String.format("Method:{%s} Host:{%s} Path:{%s} Query:{%s}",
                        exchange.getRequest().getMethod().name(), exchange.getRequest().getURI().getHost(),
                        exchange.getRequest().getURI().getPath(), exchange.getRequest().getQueryParams());
                String token = request.getHeaders().getFirst(CommonConstants.HEAD_TOKEN_KEY);
                Long startTime = exchange.getAttribute(CommonConstants.START_TIME);
                if (startTime != null) {
                    Long executeTime = (System.currentTimeMillis() - startTime);
                    String ip = IPUtils.clientIp(request);
                    // 请求路径
                    String api = exchange.getRequest().getURI().getRawPath();
                    ServerHttpResponse response = exchange.getResponse();
                    HttpStatus statusCode = response.getStatusCode();

                    // 状态码
                    int code = statusCode != null ? statusCode.value() : 500;
                    // 此处 添加 到 plumelog 中, 进行数据统计
                    log.info("来自IP地址: {}的请求接口: {}, 请求信息: {},请求人: {} , 响应状态码: {}, 请求耗时: {}ms", ip, api, info, token, code, executeTime);
                }
            }
        }));
    }


    private void setTraceId(ServerWebExchange exchange){
        String traceId = exchange.getAttribute(CommonConstants.TRACE_ID);
        if (StringUtils.isEmpty(traceId)){
            traceId = IdUtils.getId(32);
            exchange.getAttributes().put(CommonConstants.TRACE_ID, ServletUtils.urlEncode(traceId));
        }
    }

    @Override
    public int getOrder() {
        return 0;
    }

}
