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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package foundation.icall.stamp;
 
import java.util.Date;
 
import foundation.dao.Filter;
import foundation.data.entity.Entity;
import foundation.data.object.DataObject;
import foundation.data.object.EntitySaver;
import foundation.icall.ICall;
import foundation.persist.NamedSQL;
import foundation.persist.SQLRunner;
import foundation.persist.source.NamedDataSource;
import foundation.util.Util;
 
public class Stamp {
 
    private ICall iCall;
    private NamedDataSource remoteSource;
    private Date localValue;
    private Date remoteValue;
    private boolean active;
    private boolean needClearHistory;
    
    public Stamp() {
        needClearHistory = false;
    }
    
    public void init(ICall iCall, NamedDataSource remoteSource) {
        this.iCall = iCall;
        this.remoteSource = remoteSource;
        active = false;
        localValue = null;
        remoteValue = null;
    }
 
    public void load() throws Exception {
        //1. 如果没有配置时间戳字段,不需要加载
        String stampField = iCall.getStampField();
        
        if (Util.isEmpty(stampField)) {
            active = false;
            needClearHistory = true;
            return;
        }
        
        //2. 加载本地时间戳
        loadLocal();
        
        //3. 加载远程时间戳
        loadRemote();
        
        //4.
        active = (localValue != null) && (remoteValue != null);
    }
    
    public void loadLocal() throws Exception {
        String callId = iCall.getId();
        
        DataObject dataObject = DataObject.getInstance("sys_interface_stamp");
        Entity entity = dataObject.getTableEntity(new Filter("interface_id", callId));
        
        if (iCall.isPermitEmptyStamp() && entity == null) {
            return;
        }
        
        Date value = new Date(0);
        
        if (entity == null) {
            EntitySaver saver = dataObject.createEntitySaver();
            saver.set("id", callId);
            saver.set("interface_id", callId);
            saver.set("stamp_time", value);
            saver.insert();
            
            needClearHistory = true;
        }
        else {
            value = entity.getDate("stamp_time", null);
            
            if (value == null) {
                value = new Date(0);
                needClearHistory = true;
            }
        }
        
        localValue = value;
    }
    
    public void saveLocal() throws Exception {
        if (!active) {
            return;
        }
        
        DataObject dataObject = DataObject.getInstance("sys_interface_stamp");
        EntitySaver saver = dataObject.createEntitySaver();
        
        String iCallId = iCall.getId();
        
        saver.set("id", iCallId);
        saver.set("interface_id", iCallId);
        saver.set("stamp_time", remoteValue);
        saver.update();
    }    
    
    public void loadRemote() throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance(remoteSource.getDBaseType(), "getMaxRemoteStamp");
        namedSQL.setParam("tableName", iCall.getURL());
        namedSQL.setParam("fieldName", iCall.getStampField());
        remoteValue = SQLRunner.getDate(remoteSource, namedSQL);
    }
    
    public boolean isNeedClearHistory() {
        return needClearHistory;
    }
 
    public boolean isActive() {
        return active;
    }
 
    public Date getLocalValue() {
        return localValue;
    }
    
    public String getLocalValueString() {
        return Util.DataTimeToString(localValue);
    }
    
    public Date getRemoteValue() {
        return remoteValue;
    }
 
}