P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
package foundation.data.rule;
 
import foundation.data.entity.Entity;
import foundation.data.meta.field.Field;
import foundation.handler.IMessageReporter;
import foundation.util.Util;
 
public class FieldValidateRule {
 
    public static String ErrorCode_Empty = "Error_Empty";
    public static String ErrorCode_Regex = "Error_Regex";
    private Field field;
    private boolean notEmpty;
    private String validateRegex;
    
    
    public FieldValidateRule(Field field) {
        this.field = field;
        this.notEmpty = !field.isNullable();
        this.validateRegex = field.getValidateRegex();
    }
 
    public static FieldValidateRule getInstance(Field field) {
        FieldValidateRule rule = null;
        
        if (!field.isNullable() || field.hasValidateRegex()) {
            rule = new FieldValidateRule(field);
            
        }
 
        return rule;
    }
    
    public boolean exec(Entity entity, IMessageReporter reporter) {
        boolean success = true;
        
        int idx = field.getIndexNo();
        boolean empty = entity.isEmptyValue(idx);
                
        //1. 非空校验
        if (notEmpty && empty) {
            success = false;
            
            if (reporter != null) {
                reporter.reportOneMessage("数据校验", field.getLabelChinese() + "不能为空");
            }
        }
        
        if (Util.isEmpty(validateRegex) || empty) {
            return success;
        }
        
        String value = entity.getString(idx);
        boolean match = value.matches(validateRegex);
        
        if (!match) {
            success = false;
            
            if (reporter != null) {
                reporter.reportOneMessage("数据校验", field.getLabelChinese() + "格式不对");
            }
        }
        
        return success;
    }
    
}