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
package foundation.schedule;
 
import java.util.Iterator;
 
import foundation.data.entity.Entity;
import foundation.json.IJSONProvider;
import foundation.json.IJSONWriter;
import foundation.util.ID;
import foundation.util.MapList;
import foundation.util.Util;
 
public class ScheduleBatch implements Iterable<ScheduleJob>, IJSONProvider {
 
    private String id;
    private String title;
    private String timeExpression;
    private int timeExpressionMinute;
    private String remark;
    private boolean active;
    private MapList<String, ScheduleJob> jobs;
    private int triggerTimes;
    private String instanceId;
    
    
    public ScheduleBatch() {
        jobs = new MapList<String, ScheduleJob>();
        triggerTimes = 0;
        instanceId = ID.newValue();
    }
    
    public void load(Entity entity) {
        id = entity.getString("id");
        title = entity.getString("title");
        timeExpression = entity.getString("time_expression");
        remark = entity.getString("remark");
        active = entity.getBoolean("is_active", true);
        
        String startMinutes = timeExpression.split(" ")[1];
        if (Util.isInteger(startMinutes)) {
            timeExpressionMinute = Integer.valueOf(startMinutes);
        }
        else {
            startMinutes = startMinutes.substring(0, startMinutes.indexOf("/"));
            
            if (Util.isInteger(startMinutes)) {
                timeExpressionMinute = Integer.valueOf(startMinutes);
            }
            else {
                timeExpressionMinute = 0;
            }
        }
    }
    
    public void loadOneJob(ScheduleJob job) {
        //1.
        timeExpressionMinute = job.setTimeExpression(timeExpressionMinute, timeExpression);
        
        job.setBatch(this);
        //2.
        jobs.add(job.getId(), job);
    }
    
    public String getId() {
        return id;
    }
 
    public boolean isActive() {
        return active;
    }
    
    public String getTitle() {
        return title;
    }
 
    public String getTimeExpression() {
        return timeExpression;
    }
 
    public String getRemark() {
        return remark;
    }
 
    public String getInstanceId() {
        return instanceId;
    }
    
    public String getInstanceId(int triggerTime) {
        if (this.triggerTimes < triggerTime) {
            instanceId = ID.newValue();
            this.triggerTimes = triggerTime;
        }
        
        return instanceId;
    }
    
    @Override
    public Iterator<ScheduleJob> iterator() {
        return jobs.iterator();
    }
 
    public void writeJSON(IJSONWriter writer) {
        writer.beginObject();
        
        writer.write("title", title);
        writer.write("time_expression", timeExpression);
        
        writer.beginArray("jobs");
        for (ScheduleJob job: jobs) {
            job.writeJSON(writer);
        }
        writer.endArray();
        
        writer.endObject();
    }
    
}