package middleware

import (
	"bytes"
	"gadmin/config"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"io"
	"net/http"
	"time"
)

// Logger 访问日志
func Logger(c *gin.Context) {

	var (
		start = time.Now() // 开始时间
		body  []byte
		err   error
	)
	if c.Request.Method != http.MethodGet {
		body, err = io.ReadAll(c.Request.Body)
		if err != nil {
			logrus.Warnf("read body from request error:%v", err)
		} else {
			c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
		}
	}

	writer := responseBodyWriter{
		ResponseWriter: c.Writer,
		body:           &bytes.Buffer{},
	}
	c.Writer = writer

	// 处理请求
	c.Next()
	// 结束时间

	var (
		getMap    = make(map[string]interface{})
		headerMap = make(map[string]interface{})
		end       = time.Now()
	)

	for k, v := range c.Request.URL.Query() {
		getMap[k] = v
	}

	for k, v := range c.Request.Header {
		headerMap[k] = v
	}

	//remark := ""
	//if c.Request.Method == http.MethodPost {
	//	remark = config.GetMenuName(c.Request.URL.Path)
	//}

	//
	//logrus.Warnf("models:%#v", models)

	// 日志文件
	//config.AccessLog.WithFields(utility.JSONMethod(models)).Infoln()
	data := logrus.Fields{
		"clientIp": c.ClientIP(),     // 访问IP
		"method":   c.Request.Method, // 访问方式
		"get":      c.Request.URL.Query().Encode(),
		"latency":  end.Sub(start),     // 执行事件
		"code":     c.Writer.Status(),  // 状态码
		"path":     c.Request.URL.Path, // 访问路由
		"ua":       c.Request.UserAgent(),
	}
	config.AccessLog.WithFields(data).Infoln()
}

type responseBodyWriter struct {
	gin.ResponseWriter
	body *bytes.Buffer
}

func (r responseBodyWriter) Write(b []byte) (int, error) {
	r.body.Write(b)
	return r.ResponseWriter.Write(b)
}
