package com.chy.borrow.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;


@Aspect
@Component
@Slf4j
/**
 * @Author: zhangkai
 */
@Configuration
public class LogForController {
    @Pointcut("execution(public * com.chy.borrow.controller.*.*(..))")
    public void webLog() {
    }

    @Before("webLog()")
    public void before(JoinPoint joinPoint)throws Throwable{
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        // 如果有session则返回session如果没有则返回null(避免创建过多的session浪费内存)
//        HttpSession session = request.getSession(false);
        // 获取请求头
        Enumeration<String> enumeration = request.getHeaderNames();
        StringBuffer headers = new StringBuffer();
        while (enumeration.hasMoreElements()) {
            String name = enumeration.nextElement();
            String value = request.getHeader(name);
            headers.append(name + ":" + value).append(",");
        }
       StringBuilder stringBuilder=new StringBuilder();
        if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) {

            for (int i = 0; i < joinPoint.getArgs().length; i++) {
                //这里出现参数不能转换问题，想了半天最后发现里面有request对象不能被序列化
                //最后把这些对象过滤了。
//                params += JSON.toJSON(joinPoint.getArgs()[i]) + ";";

                stringBuilder.append(joinPoint.getArgs()[i]);
            }
        }
        log.info("===   请求前操作：Class-Method-访问类和方法 : {} ; request-请求入参：{}",
                joinPoint.getSignature().toShortString(),
                JSONObject.toJSONString(stringBuilder));
        log.info("===   请求前操作：request-请求head：{}",
                JSONObject.toJSONString(headers));
    }

    @AfterReturning(returning = "ret", pointcut = "webLog()")

    public void doAfterReturning(Object ret) throws Throwable {

        log.info("===   请求后返回值 : " + JSON.toJSONString(ret));

    }


//    @Around("webLog()")
//    public Object around(ProceedingJoinPoint joinPoint)throws Throwable{
//        Object proceed = joinPoint.proceed();
//        log.info("请求成功后内容：Class-Method-访问类和方法 : {} ; request-请求入参：{} ; response-返回出参： {}",
//                joinPoint.getSignature().toShortString(),
//                JSONObject.toJSONString(joinPoint.getArgs()),
//                JSONObject.toJSONString(proceed.toString()));
//        return proceed;
//    }
}