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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package foundation.icall.callout;
 
import java.sql.ResultSet;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import foundation.action.ActionContext;
import foundation.dao.OrderBy;
import foundation.dao.Page;
import foundation.dao.bizlogic.IActionProvider;
import foundation.data.entity.EntitySet;
import foundation.data.getter.EntitySetGetter;
import foundation.data.mapping.Mappings;
import foundation.data.meta.field.FieldsRuntime;
import foundation.data.meta.field.TableFieldLoader;
import foundation.data.object.DataObject;
import foundation.icall.ICall;
import foundation.icall.OutboundResult;
import foundation.icall.RemoteContext;
import foundation.icall.RunType;
import foundation.icall.log.ICallLogger;
import foundation.icall.log.LogRecord;
import foundation.icall.stamp.Stamp;
import foundation.io.IOContext;
import foundation.io.define.DataIOContainer;
import foundation.io.define.IOTask;
import foundation.io.define.IOWorkStep;
import foundation.io.define.IOWorkflow;
import foundation.io.engine.IOEngine;
import foundation.persist.IStepLoadable;
import foundation.persist.NamedSQL;
import foundation.persist.SQLRunner;
import foundation.persist.source.NamedDataSource;
import foundation.util.Util;
 
public class RemoteDBCallProvider implements IActionProvider, IStepLoadable {
 
    protected static Logger logger;
    protected static int Page_Size = 5000;
    protected ActionContext context;
    protected RemoteContext remoteContext;
    protected ICall iCall;
    protected NamedDataSource dataSource;
    protected DataObject mirrorDataObject;
    protected DataObject historyDataObject;
    protected FieldsRuntime remoteFields;
    protected Stamp stamp;
    protected NamedSQL getDataSQL;
    protected Page page;
    protected int totalLoaded;
    protected int lastLoaded;
    protected int workingCount;
    
    static {
        logger = LogManager.getLogger(RemoteDBCallProvider.class);
    }
    
    public RemoteDBCallProvider() {
        stamp = new Stamp();
    }
    
    @Override
    public void exec(ActionContext context, String method) throws Exception {
        doExec(context);
    }
    
    @Override
    public void test(ActionContext context, String method) throws Exception {
        logger.info("icall {} test", iCall.getName());
    }
    
    public void doExec(ActionContext context) throws Exception {
        this.context = context;
        this.remoteContext = context.getContext(RemoteContext.class);
        this.iCall = remoteContext.getICall();
        String runType = context.getParam("run_type", RunType.Normal.name());
        
        //1. 初始化
        dataSource = remoteContext.getRemoteSource();
        mirrorDataObject = DataObject.getInstance(iCall.getDataName());
        historyDataObject = DataObject.getInstance(iCall.getHistoryTableName());
        
        OutboundResult result = remoteContext.getOutboundResult();
        LogRecord record = result.getRecord();
        record.setUrl(iCall.getURL());
        record.setRequestBody(iCall.getName());
        ICallLogger.beginAction(record, runType);
        
        stamp.init(iCall, dataSource);
        
        //2. 清空表/镜像表
        mirrorDataObject.emptyTable();
        
        //3. 获取时间戳,如果是全量获取,需要清空历史表里面的数据
        stamp.load();
        
        if (stamp.isNeedClearHistory()) {
            historyDataObject.emptyTable();
        }
            
        //4. 分批次获取数据
        loadRemoteDataToLocalMirror();
        
        //5. 删除重复数据(时间戳一样,ID一样)
        deleteDoubleMirrorData();
        
        //6. 保存本次数据到业务表
        moveLocalMirrorToTarget();
 
        //7. 保存本次数据到历史表
        saveLocalMirrorToHistory();
        
        //8. 保存时间戳
        stamp.saveLocal();
        
        //9. 输出总条数
        String response = "从[" + iCall.getURL() + "]共获取 " + Math.max(totalLoaded, 0) + "条记录,排重生效" + workingCount + "条";
        logger.debug(response);
 
        result.setResponse(response);
        remoteContext.setOutboundResult(result);
        
    }
    
    protected void loadRemoteDataToLocalMirror() throws Exception {
        //1. 循环取数前初始化分页和继续加载标识
        totalLoaded = 0;
        lastLoaded = -1;
        workingCount = 0;
        page = new Page(dataSource.getDBaseType());
        page.setPageSize(Page_Size);
        
        //2. 根据是否有时间戳来获取取数SQL
        String remoteTable = iCall.getURL();
        String remoteFilter = iCall.getRemoteFilter();
        String stampField = iCall.getStampField();
        String orderByField = iCall.getOrderByField();
        
        if (!stamp.isActive()) {
            getDataSQL = NamedSQL.getInstance(dataSource.getDBaseType(), "getRemoteDataNoStep");
        }
        else {
            if ("oa-3-dmsorder".equalsIgnoreCase(iCall.getId())) {
                getDataSQL = NamedSQL.getInstance(dataSource.getDBaseType(), "getRemoteDataByStep_OA");
                getDataSQL.setParam("timeStamp", stamp.getLocalValue(), false);
            }
            else {
                getDataSQL = NamedSQL.getInstance(dataSource.getDBaseType(), "getRemoteDataByStep");
                getDataSQL.setParam("timeStamp", stamp.getLocalValueString());
            }
            
            getDataSQL.setParam("fieldName", stampField);
            
        }
        
        //3. 设置初始参数
        getDataSQL.setTableName(remoteTable);
        getDataSQL.setFilter(remoteFilter);
        getDataSQL.setOrderBy(new OrderBy(orderByField, " asc"));
        
        if (iCall.isPageActive()) {
            getDataSQL.setPage(page);
        }
        else {
            getDataSQL.clearPage();
        }
        
        //4. 循环(分页)取数
        SQLRunner.getData(dataSource, this);
    }
 
    private void deleteDoubleMirrorData() throws Exception {
        //1. 如果没有时间戳,就不需要排重
        if (!stamp.isActive()) {
            workingCount = totalLoaded;
            logger.debug("时间戳没有启用,不需要排重");
            return;
        }
        
        //2. 获取ID字段(可能是多个字段)
        String mirrorTable = mirrorDataObject.getTableName();
        String historyTable = iCall.getHistoryTableName();
        
        Fields idFields = iCall.getIdFields();
        String idFieldsString = idFields.getFieldPairsString(mirrorTable, historyTable);
        
        //3. 删除重复数据
        NamedSQL namedSQL = NamedSQL.getInstance("deleteDoubleMirror");
        namedSQL.setParam("mirrorTableName" , mirrorTable);
        namedSQL.setParam("historyTableName" , historyTable);
        namedSQL.setParam("idFieldPairs" , idFieldsString);
        namedSQL.setParam("stampField" , iCall.getStampField());
        
        int cnt = namedSQL.execute();
        logger.debug("delete double mirror data {}", cnt);
        
        //3. 获取剩余数据条数
        workingCount = mirrorDataObject.getCount();
    }
    
    protected void moveLocalMirrorToTarget() throws Exception {
        String taskName = iCall.getIOTaskName();
        
        //1. 如果没有定义 搬运
        if (Util.isEmpty(taskName)) {
            logger.debug("没有配置搬运程序,将mirror数据搬运到业务表跳过");
            return;
        }
        
        //2.运行IO Task -- 运行数据搬运
        IOWorkflow ioWorkflow = IOWorkflow.getInstance(taskName);
        
        if (ioWorkflow != null) {
            for (IOWorkStep step: ioWorkflow) {
                step.exec();
            }
            
            return;
        }
 
        //3.运行IO Task -- 运行数据搬运
        DataIOContainer ioContainer = DataIOContainer.getInstance();
        IOTask ioTask = ioContainer.getIOTask(taskName);
        
        if (ioTask != null) {
            IOEngine ioEngine = new IOEngine();
            ioEngine.init(context.getDataReader(), context.getDataWriter(), ioTask, new IOContext());
            
            ioEngine.runTableToTables();
        }
    }
    
    private void saveLocalMirrorToHistory() throws Exception {
        //1. 根据表结构,获取字段列表
        FieldsRuntime historyFields = historyDataObject.getTableFieldMetas();
        FieldsRuntime mirrorFields = mirrorDataObject.getTableFieldMetas();
        Mappings mappings = Mappings.getInstance(mirrorFields, historyFields);
        String fieldNames = mappings.getFromFieldsString();
        
        //2. 搬运数据
        NamedSQL namedSQL = NamedSQL.getInstance("moveMirrorToHistory");
        namedSQL.setParam("historyTableName", historyDataObject.getTableName());
        namedSQL.setParam("mirrorTableName", mirrorDataObject.getTableName());
        namedSQL.setParam("fieldNames", fieldNames);
        
        int cnt = namedSQL.execute();
        logger.debug("move mirror to history {}", cnt);        
    }
 
    @Override
    public boolean hasNextForLoad() {
        return (lastLoaded < 0) || ((lastLoaded >= Page_Size) && iCall.isPageActive());
    }
 
    @Override
    public NamedSQL getStepSQL() {
        if (iCall.isPageActive()) {
            getDataSQL.setPage(page);
        }
        else {
            getDataSQL.clearPage();
        }
        
        return getDataSQL;
    }
    
    @Override
    public void loadResultSet(ResultSet rslt, Object... args) throws Exception {
        try {
            //1. 获取远程数据库的表字段
            if (remoteFields == null) {
                String rometeTable = iCall.getURL();
                TableFieldLoader fieldsLoader = new TableFieldLoader(dataSource, rometeTable);
                remoteFields = fieldsLoader.getFieldsByTableResult();
            }
 
            //2. 加载数据到 EntitySet
            EntitySetGetter getter = new EntitySetGetter(remoteFields);
            getter.load(rslt);
            
            EntitySet entitySet = getter.getDataSet();
            
            //3. 记录插入数量
            lastLoaded = entitySet.size();
            totalLoaded = totalLoaded + lastLoaded;
                    
            //4. 将本次数据保存到表中
            mirrorDataObject.batchInsertEntitySet(entitySet);
        }
        finally {
            page.next();
        }
    }
 
}