package com.ruoyi.framework.aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;

import org.aspectj.lang.reflect.MethodSignature;

@Aspect
@Component
public class SqlLogAspect {

    @Around("execution(* com.ruoyi.system.mapper.*.*(..))") // 匹配所有Mapper接口的方法
    public Object logSql(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取目标对象、方法和参数
        Object target = joinPoint.getTarget();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Class<?> returnType = signature.getReturnType();

        // 执行目标方法
        Object result = joinPoint.proceed();

        // 判断结果集类型
        if (!(result instanceof List)) {
            return Class.forName(returnType.toString().split("class ")[1]).newInstance();
        }

        return result;
    }
}
