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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package foundation.alert;
 
import foundation.data.entity.Entity;
import foundation.data.meta.field.FieldsRuntime;
import foundation.data.object.DataObject;
import foundation.persist.NamedSQL;
import foundation.util.MapList;
import foundation.util.Util;
 
public class AlertRule {
    
    private String id;
    private String code;
    private String name;
    private String dataName;
    private String docIdField;
    private String sqlName;
    private String titleTemplate;
    private String message;
    private String url;
    private boolean isUpdateDocument;
    private MapList<String, AlterNotifier> notifiers;
 
    final static boolean clearCheckError = false;
    final static boolean addCheckError = true;
    
    public void load(Entity entity) throws Exception {
        id = entity.getString("id");
        code = entity.getString("code");
        name = entity.getString("name");
        dataName = entity.getString("data_name");
        docIdField = entity.getString("doc_id_field", "id");
        sqlName = entity.getString("sql_name");
        titleTemplate = entity.getString("title_template");
        message = entity.getString("message");
        url = entity.getString("url");
        isUpdateDocument = entity.getBoolean("is_update_document", false);
        notifiers = new MapList<String, AlterNotifier>();
    }
    
 
    public void loadOneNotifier(Entity entity) {
        AlterNotifier notifier = new AlterNotifier();
        notifier.load(entity);
        notifiers.add(notifier.getId(), notifier);
    }
 
    public void executeOne(String id) throws Exception {
        //1. 获取目标表的结构,计算出 title
        String title = calculateTitle();
        String accountId = getAccountId(dataName);
        String positionId = getPositionId(dataName);
        
        //2. 插入 alert表
        String filterSegment = docIdField + "=" + Util.quotedStr(id);
        
        NamedSQL namedSQL = NamedSQL.getInstance("insertAlert");
        namedSQL.setParam("ruleId", this.id); 
        namedSQL.setParam("dataName", dataName); 
        namedSQL.setParam("accountId", accountId); 
        namedSQL.setParam("positionId", positionId); 
        namedSQL.setParam("title", title); 
        namedSQL.setParam("message", message); 
        namedSQL.setParam("filterSegment", filterSegment); 
        
        namedSQL.execute();
        
        //3. 根据 alert 给业务表增加标识
        updateDocument(addCheckError,isUpdateDocument);
        
    }
 
    public void executeBatch() throws Exception {
        //1. 获取目标表的结构,计算出 title
        String title = calculateTitle();
        String accountId = getAccountId(dataName);
        String positionId = getPositionId(dataName);
        String filterSegment = NamedSQL.getInstance(sqlName).toString();
        
        //2. 更新 alert表
        updateAlter(filterSegment, title);
        
        //3. 插入 alert表
        NamedSQL namedSQL = NamedSQL.getInstance("insertAlert");
        namedSQL.setParam("ruleId", id); 
        namedSQL.setParam("dataName", dataName); 
        namedSQL.setParam("accountId", accountId); 
        namedSQL.setParam("positionId", positionId);
        namedSQL.setParam("docId", docIdField);
        namedSQL.setParam("title", title); 
        namedSQL.setParam("message", message); 
        namedSQL.setParam("filterSegment", filterSegment); 
        
        namedSQL.execute();
        
        //4. 根据 alert 给业务表增加标识
        updateDocument(addCheckError, isUpdateDocument);
                
        //5. 清除 alert表
        clearAlter(filterSegment);
        
        //6. 根据 alert 清除业务表的标识
        updateDocument(clearCheckError, isUpdateDocument);
    }
    
    private void updateAlter(String filterSegment, String title) throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("updateAlert");
        namedSQL.setParam("ruleId", id); 
        namedSQL.setParam("dataName", dataName);
        namedSQL.setParam("docId", docIdField);
        namedSQL.setParam("title", title); 
        namedSQL.setParam("filterSegment",filterSegment); 
        
        namedSQL.execute();
    }
 
    private void clearAlter(String filterSegment) throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("deleteAlert");
        namedSQL.setParam("ruleId", id); 
        namedSQL.setParam("dataName", dataName);
        namedSQL.setParam("docId", docIdField);
        namedSQL.setParam("filterSegment",filterSegment); 
        
        namedSQL.execute();
    }
    
    private void updateDocument(boolean isExistError, boolean isUpdateDocument) throws Exception {
        if (!isUpdateDocument) {
            return;
        }
        updateDocument(isExistError);
    }
 
    private void updateDocument(boolean isExistError) throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("updateDocumentCheckError");
        namedSQL.setParam("ruleId", id); 
        namedSQL.setParam("dataName", dataName);
        namedSQL.setParam("docId", docIdField);
        
        if(isExistError) {
            namedSQL.setParam("checkError", "T"); 
            namedSQL.setParam("isExist", "");     
        }else {
            namedSQL.setParam("checkError", ""); 
            namedSQL.setParam("isExist", "not"); 
        }
            
        namedSQL.execute();
    }
 
    public String calculateTitle() {
        String title = titleTemplate;
        if(Util.isEmpty(title)) {
            return "";
        }
        title = " concat('" + title +"') ";
        title = title.replace("@{", "',").replace("}", ",'");
        return title;
    }
 
    private String getPositionId(String dataName) throws Exception {
        if("md_position".equalsIgnoreCase(dataName)) {
            return "id";
        }
        
        DataObject dataOject = DataObject.getInstance(dataName);
        FieldsRuntime fieldRuntime = dataOject.getTableFieldMetas();
        
        if (fieldRuntime.contains("position_id")) {
            return "position_id";
        }
        else {
            return "null";
        }
    }
 
    private String getAccountId(String dataName) throws Exception {
        if("md_org_account".equalsIgnoreCase(dataName)) {
            return "id";
        }
        
        DataObject dataOject = DataObject.getInstance(dataName);
        FieldsRuntime fieldRuntime = dataOject.getTableFieldMetas();
        
        if (fieldRuntime.contains("account_id")) {
            return "account_id";
        }
        else if(fieldRuntime.contains("customer_id")) {
            return "customer_id";
        }
        else if(fieldRuntime.contains("org_id")) {
            return "org_id";
        }
        else {
            return "null";
        }
    }
    
    public String getId() {
        return id;
    }
 
    public String getCode() {
        return code;
    }
 
    public String getName() {
        return name;
    }
 
    public String getDataName() {
        return dataName;
    }
 
    public String getDocIdField() {
        return docIdField;
    }
 
    public String getSqlName() {
        return sqlName;
    }
 
    public String getTitleTemplate() {
        return titleTemplate;
    }
 
    public String getUrl() {
        return url;
    }
 
    public MapList<String, AlterNotifier> getNotifiers() {
        return notifiers;
    }
}