package com.changfa.aop;

import com.alibaba.fastjson.JSON;
import com.changfa.constants.LoginConstant;
import com.changfa.entity.TUser;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.SneakyThrows;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * @author wwz
 */
@Aspect
public class AopLogConfig {


    /**
     * 进入方法时间戳
     */
    private Long startTime;
    /**
     * 方法结束时间戳(计时)
     */
    private Long endTime;

    @Pointcut("execution(* com.changfa.controller..*(..))")
    public void pointcut() {

    }

    /**
     * 前置通知：
     * 1. 在执行目标方法之前执行，比如请求接口之前的登录验证;
     * 2. 在前置通知中设置请求日志信息，如开始时间，请求参数，注解内容等
     *
     * @param joinPoint
     * @throws Throwable
     */
    @Before("pointcut()")
    @SneakyThrows
    public void doBefore(JoinPoint joinPoint) {
        // 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return;
        }
        HttpServletRequest request = attributes.getRequest();
        Logger log = LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringType());
        //获取请求头中的User-Agent
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        //打印请求的内容
        startTime = System.currentTimeMillis();
        log.info("请求开始时间：{}", LocalDateTime.now());
        log.info("调用方法：{}", ((MethodSignature) joinPoint.getSignature()).getMethod().getName());
        log.info("请求Url : {}", request.getRequestURL().toString());
        log.info("请求方式 : {}", request.getMethod());
        log.info("请求ip : {}", request.getRemoteAddr());
        log.info("请求参数 : {}", Arrays.toString(joinPoint.getArgs()));
        // 系统信息
        log.info("浏览器：{}", userAgent.getBrowser().toString());
        log.info("浏览器版本：{}", userAgent.getBrowserVersion());
        log.info("操作系统: {}", userAgent.getOperatingSystem().toString());
        try {
            if (!request.getRequestURI().contains("user/login")) {
                TUser user = LoginConstant.currentUser();
                if (user != null) {
                    log.info("当前用户: {}", user.getAccount());
                }

            }
        } catch (Exception e) {

        }
    }

    /**
     * 返回通知：
     * 1. 在目标方法正常结束之后执行
     * 1. 在返回通知中补充请求日志信息，如返回时间，方法耗时，返回值，并且保存日志信息
     *
     * @param ret
     * @throws Throwable
     */
    @AfterReturning(returning = "ret", pointcut = "pointcut()")
    @SneakyThrows
    public void doAfterReturning(JoinPoint joinPoint, Object ret) throws Throwable {
        endTime = System.currentTimeMillis();
        Logger log = LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringType());
        log.info("请求结束时间：{}", LocalDateTime.now());
        log.info("请求耗时：{}ms", (endTime - startTime));
        // 处理完请求，返回内容
        log.info("请求返回 : {}", JSON.toJSONString(ret));
    }

    /**
     * 异常通知：
     * 1. 在目标方法非正常结束，发生异常或者抛出异常时执行
     * 1. 在异常通知中设置异常信息，并将其保存
     *
     * @param throwable
     */
    @AfterThrowing(value = "pointcut()", throwing = "throwable")
    public void doAfterThrowing(JoinPoint joinPoint, Throwable throwable) {
        // 保存异常日志记录
        Logger log = LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringType());
        log.error("发生异常时间：{}", LocalDateTime.now());
        log.error("抛出异常：{}", throwable.getMessage());
    }
}
