/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
 *
 */
package net.northking.cctp.common.log;

import net.northking.cctp.common.security.SecurityHttpConstants;
import net.northking.cctp.common.util.UUIDUtil;
import net.northking.cctp.common.util.WebFluxNetUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * http日志对象
 *
 * @author : xiusong.xie
 * @version : 1.0
 */
public class HttpLog implements Serializable
{
  private String spanId;
  private String traceId;
  private String parentId;

  private String appName;
  private String host;
  private int port;
  private String className;
  private String classMethod;

  private long startTime;
  private long endTime;
  private long elapsedTime;

  private String uri;
  private String httpMethod;
  private String queryString;
  private int status;
  private String remoteAddress;

  private Map<String, Object> requestHeaders = new HashMap<>();
  private Map<String, Object> responseHeaders = new HashMap<>();
  private String requestPayload;
  private Map<String, Object> responseBody = new HashMap<>();

  private long requestLength;
  private long responseLength;

  private String operator;
  private String tenantId;
  private String domains;

  private String error;

  public HttpLog()
  {
  }

  public HttpLog(ServerHttpRequest request, ServerHttpResponse response)
  {
    this.spanId = UUIDUtil.create32UUID();
    this.traceId = request.getHeaders().getFirst(SecurityHttpConstants.ATTRIBUTE_TRACE);
    this.parentId = request.getHeaders().getFirst(SecurityHttpConstants.ATTRIBUTE_SPAN);

    this.remoteAddress = WebFluxNetUtils.getClientIPAddress(request);
    this.httpMethod = request.getMethodValue();
    this.uri = request.getURI().getRawPath();

    StringBuilder qs = new StringBuilder();
    request.getQueryParams().forEach((key, value) ->
    {
      qs.append("&").append(key).append("=").append(value);
    });
    this.queryString = qs.toString();

    request.getHeaders().forEach((key, value) ->
    {
      this.requestHeaders.put(key, value);
    });

    this.status = response.getStatusCode().value();

    response.getHeaders().forEach((key, value) ->
    {
      this.responseHeaders.put(key, value);
    });

  }


  public String getSpanId()
  {
    return spanId;
  }

  public void setSpanId(String spanId)
  {
    this.spanId = spanId;
  }

  public String getTraceId()
  {
    return traceId;
  }

  public void setTraceId(String traceId)
  {
    this.traceId = traceId;
  }

  public String getParentId()
  {
    return parentId;
  }

  public void setParentId(String parentId)
  {
    this.parentId = parentId;
  }

  public long getStartTime()
  {
    return startTime;
  }

  public void setStartTime(long startTime)
  {
    this.startTime = startTime;
  }

  public long getEndTime()
  {
    return endTime;
  }

  public void setEndTime(long endTime)
  {
    this.endTime = endTime;
  }

  public String getUri()
  {
    return uri;
  }

  public void setUri(String uri)
  {
    this.uri = uri;
  }

  public String getClassName()
  {
    return className;
  }

  public void setClassName(String className)
  {
    this.className = className;
  }

  public String getClassMethod()
  {
    return classMethod;
  }

  public void setClassMethod(String classMethod)
  {
    this.classMethod = classMethod;
  }

  public String getHttpMethod()
  {
    return httpMethod;
  }

  public void setHttpMethod(String httpMethod)
  {
    this.httpMethod = httpMethod;
  }

  public int getStatus()
  {
    return status;
  }

  public void setStatus(int status)
  {
    this.status = status;
  }

  public String getRemoteAddress()
  {
    return remoteAddress;
  }

  public void setRemoteAddress(String remoteAddress)
  {
    this.remoteAddress = remoteAddress;
  }

  public Map<String, Object> getRequestHeaders()
  {
    return requestHeaders;
  }

  public void setRequestHeaders(Map<String, Object> requestHeaders)
  {
    this.requestHeaders = requestHeaders;
  }

  public Map<String, Object> getResponseHeaders()
  {
    return responseHeaders;
  }

  public void setResponseHeaders(Map<String, Object> responseHeaders)
  {
    this.responseHeaders = responseHeaders;
  }

  public String getRequestPayload()
  {
    return requestPayload;
  }

  public void setRequestPayload(String requestPayload)
  {
    this.requestPayload = requestPayload;
  }

  public Map<String, Object> getResponseBody()
  {
    return responseBody;
  }

  public void setResponseBody(Map<String, Object> responseBody)
  {
    this.responseBody = responseBody;
  }

  public String getAppName()
  {
    return appName;
  }

  public void setAppName(String appName)
  {
    this.appName = appName;
  }

  public String getHost()
  {
    return host;
  }

  public void setHost(String host)
  {
    this.host = host;
  }

  public int getPort()
  {
    return port;
  }

  public void setPort(int port)
  {
    this.port = port;
  }

  public long getElapsedTime()
  {
    return elapsedTime;
  }

  public void setElapsedTime(long elapsedTime)
  {
    this.elapsedTime = elapsedTime;
  }

  public String getOperator()
  {
    return operator;
  }

  public void setOperator(String operator)
  {
    this.operator = operator;
  }

  public String getTenantId()
  {
    return tenantId;
  }

  public void setTenantId(String tenantId)
  {
    this.tenantId = tenantId;
  }

  public String getQueryString()
  {
    return queryString;
  }

  public void setQueryString(String queryString)
  {
    this.queryString = queryString;
  }

  public String getDomains()
  {
    return domains;
  }

  public void setDomains(String domains)
  {
    this.domains = domains;
  }

  public String getError()
  {
    return error;
  }

  public void setError(String error)
  {
    this.error = error;
  }

  public long getRequestLength()
  {
    return requestLength;
  }

  public void setRequestLength(long requestLength)
  {
    this.requestLength = requestLength;
  }

  public long getResponseLength()
  {
    return responseLength;
  }

  public void setResponseLength(long responseLength)
  {
    this.responseLength = responseLength;
  }
}
