package com.storlead.system.assemble;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.storlead.framework.mybatis.entity.SysBaseField;
import org.apache.poi.ss.formula.functions.T;

import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.function.Function;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-04-28 15:49
 */
public class LambdaUtils {

    public  static <W extends SysBaseField> SFunction<W, ?> getOrderByFunction(Class clazz,String fieldName) {
        try {
            // 这里我们假设字段名和getter方法名遵循Java Bean规范
            String getterName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            // 获取实体类的Class对象
            Class<T> entityClass = clazz;
            // 获取getter方法
            java.lang.reflect.Method getterMethod = entityClass.getMethod(getterName);

            // 根据getter方法构建SFunction
            return (SFunction<W, ?>) Proxy.newProxyInstance(
                    entityClass.getClassLoader(),
                    new Class<?>[]{SFunction.class},
                    (proxy, method, args) -> {
                        // 调用getter方法获取字段值
                        Object value = getterMethod.invoke(args[0]);
                        return value;
                    }
            );
        }catch (Exception e) {
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    public static <T, R> Function<T, R> property(T clazz, String fieldName) {
        return (Function<T, R>) lambdaProperty(clazz, fieldName);
    }

    @SuppressWarnings("unchecked")
    private static <T, R> Function<T, R> lambdaProperty(T clazz, String fieldName) {
        try {
            // 构建getter方法的名称，假设遵循Java Bean规范
            String getterName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
            Method method = clazz.getClass().getMethod(getterName);
            // 返回一个lambda表达式，该表达式调用getter方法并返回结果
            return (T o) -> {
                try {
                    return (R) method.invoke(o);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            };
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("No such getter method for field: " + fieldName, e);
        }
    }

    public static Class<?> getEntityClass(LambdaQueryWrapper<T> lambdaQueryWrapper) {
        // 获取 LambdaQueryWrapper 的类型
        Type type = lambdaQueryWrapper.getClass().getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            // 获取实际的类型参数
            Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
            if (actualTypeArguments.length > 0) {
                // 实体类通常是第一个类型参数
                return (Class<?>) actualTypeArguments[0];
            }
        }
        return null;
    }


}
