mitao
2024-03-27 789b5b823440d174a198a35fd033ca975cb54f4a
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.ruoyi.system.dto;
 
import com.ruoyi.common.enums.CalculateTypeEnum;
import com.ruoyi.common.utils.CalculateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
 
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
 
/**
 * @author mitao
 * @date 2024/3/18
 */
@Data
@ApiModel(value = "基础数据配置数据传输对象")
public class BasicDataConfigDTO {
 
    @ApiModelProperty(value = "类型名称")
    @NotBlank(message = "类型名称不能为空")
    private String typeName;
 
    @ApiModelProperty(value = "基础数据分类id")
    @NotNull(message = "基础数据分类id不能为空")
    private Integer basicDataCategoryId;
 
    @ApiModelProperty(value = "字段说明")
    @NotBlank(message = "字段说明不能为空")
    private String fieldDescription;
 
    @ApiModelProperty(value = "计算类型(数字计算 文本统计 百分比统计)")
    @NotNull(message = "计算类型不能为空")
    private CalculateTypeEnum calculateType;
 
    @ApiModelProperty(value = "字段id ",notes = "多个id使用 ',' 拼接")
    @NotBlank(message = "字段id不能为空")
    private String fieldIdStr;
 
    @ApiModelProperty(value = "计算公式",notes = "计算类型为数字计算,配置内容使用该字段接收;公式中字段的格式为:field_字段id,e.g:field_1,field_2等。")
    private String numberCalculateFormula;
 
    @ApiModelProperty(value = "文本和百分比配置信息",notes = "当计算方式为 文本统计/百分比统计 " +
            "配置内容使用这个字段接收;key为键,value为得分;百分比统计 将百分比区间使用'_'拼接作为key,e.g: 10_20,20_30")
    private List<CalculateDTO> dtoList;
 
    public static void main(String[] args) {
    /*    String rule = "(field1_13+field2_50) × 5 ÷ field3_63×10";
 
        // 正则表达式模式,匹配形如 "fieldName:value" 的字符串
        Pattern pattern = Pattern.compile("\\b(\\w+)_(\\d+)\\b");
        Matcher matcher = pattern.matcher(rule);
 
        Map<String, Integer> map = new HashMap<>();
        // 循环匹配并输出字段名和值
        while (matcher.find()) {
            String fieldName = matcher.group(1);
            int value = Integer.parseInt(matcher.group(2));
            System.out.println("Field: " + fieldName + ", Value: " + value);
        }
 
        // 假设用户上传的字段值
        int field1_13 = 500;
        int field2_50 = 30;
        int field3_63 = 1000;
 
        // 计算表达式
        String expression = "(field1_13 + field2_50) * 5 / field3_63 * 10";
 
        // 创建 JEXL 引擎
        JexlEngine jexl = new JexlBuilder().create();
 
        // 创建表达式对象
        JexlExpression exp = jexl.createExpression(expression);
 
        // 创建上下文
        JexlContext context = new MapContext();
        context.set("field1_13", field1);
        context.set("field2_50", field2);
        context.set("field3_63", field3);
 
        // 执行计算
        Object result = exp.evaluate(context);
 
        // 输出结果
        System.out.println("Result: " + result);*/
 
        String expression = "(field_13 + field_50) × 5 ÷ field_63 × 10-1";
        expression = CalculateUtil.formatExpression(expression);
        System.err.println(expression);
        Map<String, Integer> fieldsAndValue = CalculateUtil.getFieldsAndValue(expression);
        System.out.println(fieldsAndValue);
      /*  double calculate = CalculateUtil.calculate(expression, fieldsAndValue);
        System.out.println(calculate);*/
    }
 }