package cn.cslg.report.common.core.annotation;

import cn.cslg.report.common.model.DataSource;
import cn.cslg.report.common.model.vo.PersonnelVO;
import cn.cslg.report.common.utils.CacheUtils;
import cn.cslg.report.common.utils.JsonUtils;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.report.common.utils.auth.TreeUtils;
import cn.cslg.report.common.utils.auth.checkAuth;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

@Order(3)
@Aspect
@Component
@Slf4j
public class LoggerAspect {


    /**
     * 定义切点
     */
    @Pointcut("execution( * cn.cslg.report.controller..*.*(..))")
    public void annotationPointcut() {

    }

    /**
     * @param joinPoint 当前执行的方法
     */
    @Around("annotationPointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {

        //判断通过
        return joinPoint.proceed();
    }

    @AfterThrowing(pointcut = "annotationPointcut()",throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Throwable e){

        //获取用户请求方法的参数并序列化为JSON格式字符串
        String params = "";
        if (joinPoint.getArgs()!=null&&joinPoint.getArgs().length>0){
            for (int i = 0; i < joinPoint.getArgs().length; i++) {
                params+= JsonUtils.objectToJson(joinPoint.getArgs()[i])+";";
            }
        }
        try{
            String str1 ="异常代码:" + e.getClass().getName();
            String str2 ="异常信息:" + e.getMessage();
            String str3="异常方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()");
            String str4="请求参数:" + params;
   log.error(str1+"\r"+str2+"\r"+str3+"\r"+str4);
        }catch (Exception ex){
            //记录本地异常日志
            log.error("==异常通知异常==");
            log.error("异常信息:{}", ex.getMessage());
        }
    }

}

