package com.fufan.mtsaas.tools;

import lombok.Data;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/*
    引用包
   <!-- SpEL表达式 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>5.3.18</version>
    </dependency>
 */

public class SpelEvaluator {

//    public static boolean evaluate(Object obj, String expression) {
//        ExpressionParser parser = new SpelExpressionParser();
//        StandardEvaluationContext context = new StandardEvaluationContext(obj);
//        Expression exp = parser.parseExpression(expression);
//        return exp.getValue(context, Boolean.class);
//    }
//
//    public static void main(String[] args) {
//        Order order = new Order(1500, "NEW");
//
//        // 使用SpEL表达式动态判断
//        boolean isLargeOrder = evaluate(order, "amount > 1000");
//        boolean isNewOrder = evaluate(order, "status.equals('NEW')");
//
//        System.out.println("Is large order: " + isLargeOrder); // true
//        System.out.println("Is new order: " + isNewOrder);     // true
//    }
}

@Data
class Order {
    private double amount;
    private String status;

    public Order(double amount, String status) {
        this.amount = amount;
        this.status = status;
    }

    // 构造方法和getter/setter省略...
}