package com.shanghaichengdi.ghjgitem.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
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.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @Author: zhangkai
 */
@Aspect
@Slf4j
@Component
public class LogForControllerConf {

  @Pointcut("execution(public * com.shanghaichengdi.ghjgitem.controller..*.*(..))")
  public void webLog() {
  }

//    @Pointcut("execution(public * com.shcd.ghjg.controller.*.*.*(..))")
//    private void inTrading() {}

  @Before("webLog()")
  public void before(JoinPoint joinPoint) throws Throwable {
    try {
      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(",");
        headers.append(name + ":" + value).append(",");
      }
      Subject subject = SecurityUtils.getSubject();

      if (subject.getPrincipal() != null) {
      } else {
        log.info(
            "===   请求前操作：Class-Method-访问类和方法 : {} ; request-请求入参：{} ;request-请求head：{}",
            joinPoint.getSignature().toShortString(),
            JSONObject.toJSONString(joinPoint.getArgs().length > 0 ? joinPoint.getArgs()[0] : ""),
            JSONObject.toJSONString(headers));
      }
    } catch (Exception e) {
//            log.info("参数打印错误 ===> "+e.getMessage());
    }
  }

  //    @AfterThrowing(pointcut = "webLog()",throwing = "e")
//    public void doAfterThrowing(JoinPoint joinPoint,RuntimeException e)  {
//        log.info("===   "+joinPoint.getTarget()+",发生异常 : " + e.getMessage());
//
//    }
  @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;
//    }
}
