package com.ruoyi.common.core.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;

public final class ExpressUtil {
    public static <T> List<Map<String, Object>> hitExpress(T obj, List<Map<String, Object>> indexRangeList) {
        if (CollectionUtils.isEmpty(indexRangeList)) {
            return null;
        }
        SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
        List<Map<String, Object>> result = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        BeanUtil.beanToMap(obj, map, false, false);
        String express;
        String indexName;
        Expression expression;
        Boolean eval;
        for (Map<String, Object> stringObjectMap : indexRangeList) {
            indexName = MapUtil.getStr(stringObjectMap, "indexName");
            express = String.format("%s < ['indexUpperLimit'] && %s >= ['indexLowerLimit']", map.get(indexName), map.get(indexName));
            expression = spelExpressionParser.parseExpression(express);
            eval = expression.getValue(stringObjectMap, Boolean.class);
            if (Boolean.FALSE.equals(eval)) {
                result.add(stringObjectMap);
            }
        }
        return result;
    }
}
