13693261870
2025-07-02 6708810c4de34dfb9513061432d656f91d56ee3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.ruoyi.fuzhou.utils;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
 
import java.util.List;
 
/**
 * 数据清洗工具
 */
public class AnalyseUtils {
    //[{"param":"test","rule":"<","value":0.3}]
    public static <T> JSONArray listToJSONArray(List<T> list, String rule) {
        JSONArray ruleArray = JSONArray.parseArray(rule);
        JSONArray result = new JSONArray();
        JSONArray array = JSONArray.parseArray(JSONArray.toJSONString(list));
        if (ruleArray.size() == 0) {
            return array;
        }
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = array.getJSONObject(i);
            boolean success = true;
            for (int m = 0; m < ruleArray.size(); m++) {
                JSONObject jsonObject = ruleArray.getJSONObject(m);
                String key = jsonObject.getString("param");
                String sign = jsonObject.getString("rule");
                Double value = jsonObject.getDouble("value");
                if (key == null) {
                    throw new RuntimeException("参数不能为空!");
                }
                if (sign == null) {
                    throw new RuntimeException("符号不能为空!");
                }
                if (value == null) {
                    throw new RuntimeException("参数不能为空!");
                }
                //规则判断
                String objectValue = object.getString(key);
                if (StringUtils.isNotEmpty(objectValue)) {
                    try {
                        if ("<".equals(sign)) {
                            success = Double.valueOf(objectValue) < value;
                        } else if ("<=".equals(sign)) {
                            success = Double.valueOf(objectValue) <= value;
                        } else if (">=".equals(sign)) {
                            success = Double.valueOf(objectValue) >= value;
                        } else if (">".equals(sign)) {
                            success = Double.valueOf(objectValue) > value;
                        } else if ("!=".equals(sign)) {
                            success = !Double.valueOf(objectValue).equals(value);
                        } else if ("=".equals(sign)) {
                            success = Double.valueOf(objectValue).equals(value);
                        } else {
                            success = false;
                        }
                    }catch (Exception e){
                        success = false;
                        e.printStackTrace();
                    }
                } else {
                    success = false;
                }
                if (!success) {
                    break;
                }
            }
            if (success) {
                result.add(object);
            }
        }
        return result;
    }
}