许鹏程
2024-01-15 1be3406d2c07ec7c1376044f77a3bbd2576bf256
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
package com.product.data.center.service;
 
import com.alibaba.fastjson.JSONObject;
import com.product.common.utils.StringUtils;
import com.product.common.utils.spring.SpringUtils;
import com.product.core.service.support.QueryFilterService;
import io.logz.sender.com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.service.support.AbstractBaseService;
import com.product.data.center.config.CmnConst;
import com.product.data.center.entity.JournalEntity;
import com.product.util.BaseUtil;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;
 
/**
 * 操作日志
 *
 * @author
 */
@Component
public class JournalManagerService extends AbstractBaseService {
 
    @Autowired
    BaseDao baseDao;
 
    @Autowired
    QueryFilterService queryFilterService;
 
    @Resource
    MesExternalService mesExternalService;
 
 
    public void timingClearDataCenterLog(){
//        select uuid from product_sys_data_center_log a
//        where created_utc_datetime<=date(date_add(now(),interval -7 day)) and (result=1 or (deal_flag=1 and deal_result=1)) and type>1
//        union all
//        select a.uuid from product_sys_data_center_log a
//        join product_sys_data_center_log b on a.uuid=b.pre_step_uuid
//        where a.type=1 and b.type=2 and (b.result=1 or (b.deal_flag=1 and b.deal_result=1))
//        and b.created_utc_datetime<=date(date_add(now(),interval -7 day))
    }
 
    /**
     * 日志列表
     *
     * @param fse
     * @return
     */
    public DataTableEntity listJournal(FieldSetEntity fse, boolean getErrorLog) {
        String queryFilter1 = queryFilterService.getQueryFilter(fse);
 
        //获取配置信息和操作类型
        String config_uuid = fse.getString(CmnConst.CONFIG_UUID);
        String type = fse.getString(CmnConst.TYPE);
 
        //设置SQL拼接成分(查询列,排序列,过滤条件)
        Object[] showFields = new Object[]{CmnConst.TYPE, CmnConst.DETAIL, CmnConst.CREATED_UTC_DATETIME, CmnConst.RESULT, CmnConst.DEAL_FLAG, CmnConst.COUNT, CmnConst.UUID, CmnConst.CONFIG_UUID};
 
        DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG, getFilter(type, queryFilter1, true), new Object[]{type, config_uuid}, showFields, CmnConst.CREATED_UTC_DATETIME, fse.getInteger("pagesize"), fse.getInteger("cpage"));
        if (DataTableEntity.isEmpty(dt)) {
            dt = baseDao.listTable(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG, getFilter(type, queryFilter1, false), new Object[]{type, config_uuid}, showFields, CmnConst.CREATED_UTC_DATETIME, fse.getInteger("pagesize"), fse.getInteger("cpage"));
        }
        baseDao.loadPromptData(dt);
        return dt;
    }
 
    private String getFilter(String type, String queryFilter1, boolean getErrorLog) {
        StringBuilder queryFilter = new StringBuilder();
        queryFilter.append(" type=? AND config_uuid=?");
        //判断细项是否为空
        if (getErrorLog) {
            queryFilter.append(" AND result=0 and deal_flag=0");
        }
        //过滤归档日志
        if ("5".equals(type)) {
            queryFilter.append(" AND detail=4");
        }
        if (!StringUtils.isEmpty(queryFilter1)) {
            queryFilter.append(" and ").append(queryFilter1);
        }
        return queryFilter.toString();
    }
 
    /**
     * 日志列表 已弃用 2023年3月16日 15:47:29
     *
     * @param fse
     * @return
     */
    public DataTableEntity listJournal1(FieldSetEntity fse, boolean getErrorLog) {
        String queryFilter1 = queryFilterService.getQueryFilter(fse);
 
        //获取配置信息和操作类型
        String config_uuid = fse.getString(CmnConst.CONFIG_UUID);
        String type = fse.getString(CmnConst.TYPE);
 
        //设置SQL拼接成分(查询列,排序列,过滤条件)
        Object[] showFields = new Object[]{CmnConst.TYPE, CmnConst.DETAIL, CmnConst.CREATED_UTC_DATETIME, CmnConst.RESULT, CmnConst.DEAL_FLAG, CmnConst.COUNT, CmnConst.UUID, CmnConst.CONFIG_UUID};
 
        StringBuilder orderFields = new StringBuilder();
        orderFields.append(CmnConst.RESULT);
        orderFields.append(",");
        orderFields.append(CmnConst.DEAL_FLAG);
        orderFields.append(",");
        orderFields.append(CmnConst.CREATED_UTC_DATETIME);
 
        StringBuilder queryFilter = new StringBuilder();
        queryFilter.append("type=? AND config_uuid=?");
        //判断细项是否为空
        if (getErrorLog) {
            queryFilter.append(" AND result=0");
        }
        //过滤归档日志
        if ("5".equals(type)) {
            queryFilter.append(" AND detail=4");
        }
        if (!StringUtils.isEmpty(queryFilter1)) {
            queryFilter.append("and ").append(queryFilter1);
        }
        DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG, queryFilter.toString(), new Object[]{type, config_uuid}, showFields, orderFields.toString(), fse.getInteger("pagesize"), fse.getInteger("cpage"));
        baseDao.loadPromptData(dt);
        return dt;
    }
 
 
    /**
     * 日志详情
     *
     * @param fse
     * @return
     */
    public FieldSetEntity findJournal(FieldSetEntity fse) {
 
        //根据UUID获取日志信息
        FieldSetEntity fseInfo = baseDao.getFieldSet(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG, new String[]{CmnConst.TYPE, "detail", "created_utc_datetime", "result", "deal_flag", "error", "count", CmnConst.CONFIG_UUID}, fse.getUUID(), false);
 
        //判断是否为归档日志并确定是否删除了MES主子库,如删除,则返回相应的日志
        if ("5".equals(fseInfo.getString(CmnConst.TYPE)) && BaseUtil.strIsNull(fseInfo.getString(CmnConst.PRE_STEP_UUID))) {
 
            //查询删除主库日志
            FieldSetEntity fseDelMain = baseDao.getFieldSetByFilter(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG, "pre_step_uuid=? AND detail=6", new Object[]{fseInfo.getString(CmnConst.PRE_STEP_UUID)}, false);
            if (fseDelMain == null) {
                //查询删除子库日志
                FieldSetEntity fseDelSub = baseDao.getFieldSetByFilter(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG, "pre_step_uuid=? AND detail=5", new Object[]{fseInfo.getString(CmnConst.PRE_STEP_UUID)}, false);
                if (fseDelSub != null) {
                    //装载删除子库日志
                    DataTableEntity dtSub = new DataTableEntity();
                    fseDelSub.setTableName("delSub");
                    dtSub.addFieldSetEntity(fseDelSub);
                    fseInfo.addSubDataTable(dtSub);
                }
            } else {
                //装载删除主库日志
                DataTableEntity dtMain = new DataTableEntity();
                fseDelMain.setTableName("delMain");
                dtMain.addFieldSetEntity(fseDelMain);
                fseInfo.addSubDataTable(dtMain);
 
                //查询删除子库日志
                FieldSetEntity fseDelSub = baseDao.getFieldSetByFilter(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG, "pre_step_uuid=? AND detail=5", new Object[]{fseDelMain.getString(CmnConst.PRE_STEP_UUID)}, false);
                if (fseDelSub != null) {
                    DataTableEntity dtSub = new DataTableEntity();
                    fseDelSub.setTableName("delSub");
                    dtSub.addFieldSetEntity(fseDelSub);
                    fseInfo.addSubDataTable(dtSub);
                }
            }
        }
        return fseInfo;
    }
 
    /**
     * 模拟新增数据
     */
    public void insertData() {
        new Thread() {
            @Override
            public void run() {
                for (int i = 1; i <= 5; i++) {
                    System.out.println("i=" + i);
                    Random rd = new Random(); //创建一个Random类对象实例
                    int x = rd.nextInt(2);
                    int y = rd.nextInt(6) + 1;
 
                    FieldSetEntity fseEntity = new FieldSetEntity();
                    fseEntity.setTableName(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG);
                    fseEntity.setValue("count", "500");
                    fseEntity.setValue("result", x);
                    fseEntity.setValue("type", y);
                    fseEntity.setValue("deal_flag", "0");
                    fseEntity.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
                    baseDao.add(fseEntity);
                }
            }
        }.start();
    }
 
    /**
     * 日志监控首页(左侧监控分组)
     */
    public DataTableEntity journalServerView() {
        DataTableEntity dtTypeAndConfig = baseDao.listTable("SELECT type,data_source,target_data_source FROM product_sys_data_center_log_view GROUP BY type,data_source,target_data_source", new Object[]{});
        return dtTypeAndConfig;
    }
 
    /**
     * 日志监控视图(右侧监控数据) 弃用 2023年2月21日 14:49:39
     *
     * @return
     */
    public DataTableEntity groupCountErrorLog1(FieldSetEntity fse) {
        //获取类型(type:-1 左侧未选,加载所有  type:>0选中单个操作)
        int type = fse.getInteger(CmnConst.TYPE);
 
        //创建查询SQL
        StringBuilder sbSql = new StringBuilder();
        sbSql.append(" SELECT A.type,A.uuid config_uuid,A.name,A.source_table,B.time,IF(status=0 AND IFNULL(count,0)=0,'正常','异常')status,data_source,target_data_source ");
        sbSql.append(" FROM PRODUCT_SYS_DATA_CENTER_LOG_VIEW A ");
        sbSql.append(" LEFT JOIN ( ");
        sbSql.append(" SELECT config_uuid, ");
        sbSql.append(" DATE_FORMAT(Max(statistics_final_time),'%Y-%m-%d %H:%i:%s')time, ");
        sbSql.append(" SUM(CASE WHEN result=0 AND (deal_flag=0 OR deal_flag IS NULL) THEN 1 ELSE 0 END) count ");
        sbSql.append(" FROM product_sys_data_center_log ");
        if (type > 0) {
            sbSql.append(" WHERE type=? ");
        }
        sbSql.append(" GROUP BY config_uuid ");
        sbSql.append(" ) B ON A.uuid = B.config_uuid ");
        if (type > 0) {
            sbSql.append(" WHERE A.type=? AND data_source=? AND target_data_source=? ");
        }
        sbSql.append(" ORDER BY count DESC");
 
        //获取查询数据
        DataTableEntity dt = null;
        if (type < 0) {
            dt = baseDao.listTable(sbSql.toString(), new Object[]{});
        } else {
            String dataSource = fse.getString(CmnConst.DATA_SOURCE);
            String targetDataSource = fse.getString(CmnConst.TARGET_DATA_SOURCE);
            dt = baseDao.listTable(sbSql.toString(), new Object[]{type, type, dataSource, targetDataSource});
        }
 
        return dt;
    }
 
    /**
     * 日志监控视图(右侧监控数据)
     *
     * @return
     */
    public DataTableEntity groupCountErrorLog(FieldSetEntity fse) {
        //获取类型(type:-1 左侧未选,加载所有  type:>0选中单个操作)
        int type = fse.getInteger(CmnConst.TYPE);
 
        //创建查询SQL
        StringBuilder sbSql = new StringBuilder();
        sbSql.append(" SELECT A.type,A.uuid config_uuid,A.name,A.source_table,data_source,target_data_source,status");
        sbSql.append(" FROM PRODUCT_SYS_DATA_CENTER_LOG_VIEW A ");
        if (type > 0) {
            sbSql.append(" WHERE A.type=? AND data_source=? AND target_data_source=? ");
        }
 
        //获取查询数据
        DataTableEntity dt;
        if (type < 0) {
            dt = baseDao.listTable(sbSql.toString(), new Object[]{});
        } else {
            String dataSource = fse.getString(CmnConst.DATA_SOURCE);
            String targetDataSource = fse.getString(CmnConst.TARGET_DATA_SOURCE);
            dt = baseDao.listTable(sbSql.toString(), new Object[]{type, dataSource, targetDataSource});
        }
 
        Map<String, FieldSetEntity> resultMap = dt2Map(dt, CmnConst.CONFIG_UUID);
        StringBuilder sql = new StringBuilder(128);
        sql.append("\nselect t1.config_uuid,DATE_FORMAT(t1.statistics_final_time,'%Y-%m-%d %H:%i:%s') time,t2.count");
        sql.append("\nfrom (");
        sql.append("\n    select config_uuid,statistics_final_time");
        sql.append("\n    from product_sys_data_center_log l1");
        sql.append("\n    inner join (");
        sql.append("\n        SELECT Max(id) id");
        sql.append("\n        FROM product_sys_data_center_log  ");
        sql.append("\n        GROUP BY config_uuid");
        sql.append("\n    ) l2 on l1.id=l2.id");
        sql.append("\n) t1");
        sql.append("\nleft join (");
        sql.append("\n    select distinct config_uuid,1 count from product_sys_data_center_log where result=0 AND deal_flag=0");
        sql.append("\n) t2 on t1.config_uuid=t2.config_uuid");
        DataTableEntity errorDte = baseDao.listTable(sql.toString(), new Object[]{});
        Map<String, FieldSetEntity> errorMap = dt2Map(errorDte, CmnConst.CONFIG_UUID);
        DataTableEntity resultDte = new DataTableEntity();
        DataTableEntity commonDte = new DataTableEntity();
        errorMap.forEach((configUUID, tempFse) -> {
            FieldSetEntity resultFse = resultMap.get(configUUID);
            resultMap.remove(configUUID);
            if (FieldSetEntity.isEmpty(resultFse)) {
                return;
            }
            for (Object fieldNameObj : tempFse.getFields()) {
                if (fieldNameObj == null) {
                    continue;
                }
                String fieldName = fieldNameObj.toString();
                resultFse.setValue(fieldName, tempFse.getString(fieldName));
            }
            if (resultFse.getInteger("count") != null && resultFse.getInteger("count") > 0) {
                if ("0".equals(resultFse.getString("status"))) {
                    resultFse.setValue("status", "异常");
                }
                resultDte.addFieldSetEntity(resultFse);
            } else {
                if ("0".equals(resultFse.getString("status"))) {
                    resultFse.setValue("status", "正常");
                }
                commonDte.addFieldSetEntity(resultFse);
            }
        });
 
        resultDte.addFieldSetEntity(commonDte);
        resultMap.forEach((fieldName, tempFse) -> {
            tempFse.setValue("status", "正常");
            resultDte.addFieldSetEntity(tempFse);
        });
 
        return resultDte;
    }
 
    /**
     * dte转为map
     *
     * @param dte
     * @param fieldName
     * @return
     */
    private Map<String, FieldSetEntity> dt2Map(DataTableEntity dte, String fieldName) {
        Map<String, FieldSetEntity> map = Maps.newHashMap();
        FieldSetEntity fse;
        for (int i = 0; i < dte.getRows(); i++) {
            fse = dte.getFieldSetEntity(i);
            map.put(fse.getString(fieldName), fse);
        }
        return map;
    }
 
    /**
     * 日志二次处理
     *
     * @param uuids 日志uuid
     */
    public void secondOperation(String uuids) {
        String[] uuidArray = uuids.split(",");
        new Thread(() -> {
            for (String uuid : uuidArray) {
                FieldSetEntity fseJournal = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG, uuid, false);
                secondOperation(fseJournal);
            }
        }).start();
    }
 
    public void autoSecondOperation() {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * FROM product_sys_data_center_log a ");
        sql.append(" where type IN ( 1, 2 ) AND result = 0 AND deal_flag = 0 ");
        sql.append(" AND DATE_FORMAT( created_utc_datetime, '%Y-%m-%d' )>= DATE_FORMAT( now(), '%Y-%m-%d' ) ");
        sql.append(" AND (( type = 2  AND (");
        sql.append(" SELECT 1 FROM product_sys_data_center_log b where b.uuid = a.pre_step_uuid  AND ");
        sql.append(" ( b.source_uuid IS NULL OR b.source_uuid = '' ))) or (type =1 and (source_uuid IS NULL OR source_uuid = '')) ) ");
        DataTableEntity dataTableEntity = getBaseDao().listTable(sql.toString(), new Object[]{});
        if (DataTableEntity.isEmpty(dataTableEntity)) {
            return;
        }
        for (int i = 0; i < dataTableEntity.getRows(); i++) {
            secondOperation(dataTableEntity.getFieldSetEntity(i));
        }
    }
 
 
    private void secondOperation(FieldSetEntity fseJournal) {
        if (!FieldSetEntity.isEmpty(fseJournal)) {
            String uuid = fseJournal.getUUID();
            String type = fseJournal.getString(CmnConst.TYPE);//类型
            if (!BaseUtil.strIsNull(type)) {
                String dataSystemName = mesExternalService.getDataSystemName();
                if ("ch-kt".equals(dataSystemName) && mesExternalService.remoteRehandle(fseJournal)) {
                    return;
                }
                if ("1".equals(type)) {    //数据采集
                    DataCollectService dataCollectService = SpringUtils.getBean(DataCollectService.class);
                    mesExternalService.remoteSaveCollectLog(dataCollectService.reDeal(uuid));
                } else if ("2".equals(type)) {    //数据提取
                    DataCollectService dataCollectService = SpringUtils.getBean(DataCollectService.class);
                    mesExternalService.remoteSaveCollectLog(dataCollectService.reDeal(fseJournal.getString(CmnConst.PRE_STEP_UUID), fseJournal));
                } else if ("3".equals(type)) {    //数据同步
                    SpringUtils.getBean(DataSyncService.class).reDeal(uuid);
                } else if ("5".equals(type)) {    //数据归档不做操作
                    DataArchivingService dataArchivingService = SpringUtils.getBean(DataArchivingService.class);
                    dataArchivingService.reDeal(uuid);
                }
            }
        }
    }
 
 
    /**
     * 日志标识处理
     *
     * @param uuid 日志uuid
     */
    public void signOperation(String uuid) {
        //获取原日志数据
        FieldSetEntity fseJournal = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG, uuid, false);
        String result = fseJournal.getString(CmnConst.RESULT);
        String dealFlag = fseJournal.getString(CmnConst.DEAL_FLAG);
        //判断是否符合标识处理
        if ("0".equals(result) && (BaseUtil.isStrNum(dealFlag) || "0".equals(dealFlag))) {
            fseJournal.setValue(CmnConst.DEAL_FLAG, 1);
            fseJournal.setValue(CmnConst.DEAL_RESULT, 1);
            baseDao.update(fseJournal);
        }
    }
 
    /**
     * 定时任务自动创建日志
     *
     * @param journalEntity
     * @return
     */
    public FieldSetEntity autoCreateJournal(JournalEntity journalEntity) {
        FieldSetEntity fseLog = new FieldSetEntity();
        fseLog.setTableName(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG);
 
        //日志归属
        fseLog.setValue(CmnConst.TYPE, journalEntity.getType());        //日志类型
        fseLog.setValue(CmnConst.DETAIL, journalEntity.getDetail());    //细类型(两器,整机,电控)
 
        //操作结果1:成功  0:失败(error不能为空)
        if (journalEntity.getResult() == 0) {
            fseLog.setValue(CmnConst.ERROR, journalEntity.getError());    //错误日志
            fseLog.setValue(CmnConst.RESULT, 0);
        } else {
            fseLog.setValue(CmnConst.RESULT, 1);
        }
 
        fseLog.setValue(CmnConst.COUNT, journalEntity.getCount());    //执行条数
        fseLog.setValue(CmnConst.MIN_ID, journalEntity.getMin_id());    //操作数据的最小id
        fseLog.setValue(CmnConst.MAX_ID, journalEntity.getMax_id());    //操作数据的最大id
 
        //默认数据
        fseLog.setValue(CmnConst.DEAL_FLAG, 0);        //手动处理 1:是  0:否(手动触发就算处理)
        fseLog.setValue(CmnConst.DEAL_RESULT, 1);    //处理结果 1:成功  0:失败(会再生成一条错误日志)
//        fseLog.setValue(CmnConst.SOURCE_UUID, null);
        fseLog.setValue(CmnConst.SOURCE_UUID, journalEntity.getSource_uuid());//源日志UUID(deal_flag=1;deal_result=0的数据)
        fseLog.setValue(CmnConst.SINGLE_DURATION, journalEntity.getSingle_duration());
        fseLog.setValue(CmnConst.CONFIG_UUID, journalEntity.getConfigUuid());
        fseLog.setValue(CmnConst.STATISTICS_START_TIME, journalEntity.getStatistics_start_time());
        fseLog.setValue(CmnConst.STATISTICS_FINAL_TIME, journalEntity.getStatistics_final_time());
        fseLog.setValue(CmnConst.OTHER_INFO, journalEntity.getOther_info());
 
        fseLog.setValue(CmnConst.NEXT_STEP_STATUS, journalEntity.getNext_step_status());    //下一步处理状态 0:暂未操作  1:下一步处理成功  2:下一步执行失败
        fseLog.setValue(CmnConst.PRE_STEP_UUID, journalEntity.getPre_step_uuid());    //上一步UUID
        fseLog.setValue(CmnConst.CREATED_UTC_DATETIME, journalEntity.getCreated_utc_datetime() != null ? journalEntity.getCreated_utc_datetime() : new Date());
        baseDao.add(fseLog);
 
        return fseLog;
    }
 
    /**
     * 手动处理自动生成日志
     *
     * @param fseNewInfo 新数据
     * @param fsePreInfo 上一步数据
     * @return
     */
    public boolean autoCreateLog(FieldSetEntity fseNewInfo, FieldSetEntity fsePreInfo) {
        FieldSetEntity fseLog = new FieldSetEntity();
        fseLog.setTableName(CmnConst.PRODUCT_SYS_DATA_CENTER_LOG);
        //日志归属
        fseLog.setValue(CmnConst.TYPE, fsePreInfo.getString(CmnConst.TYPE));        //日志类型
        fseLog.setValue(CmnConst.DETAIL, fsePreInfo.getString(CmnConst.DETAIL));    //细类型(两器,整机,电控)
 
        //操作结果1:成功  0:失败(error不能为空)
        if ("0".equals(fseNewInfo.getString(CmnConst.RESULT))) {
            fsePreInfo.setValue(CmnConst.NEXT_STEP_STATUS, 2);    //下一步处理失败
            fseLog.setValue(CmnConst.ERROR, fseNewInfo.getString(CmnConst.ERROR));    //错误日志
        } else {
            fsePreInfo.setValue(CmnConst.NEXT_STEP_STATUS, 1);    //下一步处理成功
            fseLog.setValue(CmnConst.RESULT, 1);
        }
 
        fseLog.setValue(CmnConst.COUNT, fsePreInfo.getInteger(CmnConst.COUNT));    //执行条数
        fseLog.setValue(CmnConst.MIN_ID, fsePreInfo.getInteger(CmnConst.MIN_ID));    //操作数据的最小id
        fseLog.setValue(CmnConst.MAX_ID, fsePreInfo.getInteger(CmnConst.MAX_ID));    //操作数据的最大id
 
        //默认数据
        fseLog.setValue(CmnConst.DEAL_FLAG, 0);        //手动处理 1:是  0:否(手动触发就算处理)
        fseLog.setValue(CmnConst.DEAL_RESULT, 1);        //处理结果 1:成功  0:失败(会再生成一条错误日志)
        fseLog.setValue(CmnConst.SOURCE_UUID, null);    //源日志UUID(deal_flag=1;deal_result=0的数据)
 
        fseLog.setValue(CmnConst.NEXT_STEP_STATUS, 0);    //下一步处理状态 0:暂未操作  1:下一步处理成功  2:下一步执行失败
        fseLog.setValue(CmnConst.PRE_STEP_UUID, fsePreInfo.getUUID());    //上一步UUID
 
        //生成新日志
        baseDao.add(fseLog);
        //修改源日志
        baseDao.update(fsePreInfo);
 
        return true;
    }
 
    /**
     * 获取异常信息
     *
     * @param e
     * @return
     */
    public String getStackTrace(Throwable e) {
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
 
        } catch (Exception e2) {
            e2.printStackTrace();
        } finally {
            if (sw != null) {
                try {
                    sw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (pw != null) {
                pw.close();
            }
        }
        return sw.toString();
    }
 
    /**
     * 提取数据集中的首尾范围信息
     *
     * @param dte
     * @param timeField
     * @return
     */
    public JSONObject getRangeInfo(DataTableEntity dte, String timeField) {
        FieldSetEntity fse;
        JSONObject resultObj = new JSONObject();
        for (int i = 0; i < dte.getRows(); i++) {
            fse = dte.getFieldSetEntity(i);
            recordBorder(resultObj, fse, timeField, CmnConst.DATE);
        }
        return resultObj;
    }
 
    /**
     * 回写重新处理的结果到源日志
     *
     * @param sourceLogFse
     * @param resultFlag
     */
    public void writeBackReDealResult(FieldSetEntity sourceLogFse, boolean resultFlag) {
        sourceLogFse.setValue(CmnConst.DEAL_FLAG, 1);
        sourceLogFse.setValue(CmnConst.DEAL_RESULT, resultFlag ? 1 : 0);
        sourceLogFse.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date());
        baseDao.saveFieldSetEntity(sourceLogFse);
    }
 
    /**
     * 记录边界
     *
     * @param jsonObject 结果集
     * @param fse        数据记录
     * @param fieldName  字段名称
     * @param symbol     标识,date:记录统计开始时间,统计截止时间;int:记录最小ID,最大ID
     */
    private void recordBorder(JSONObject jsonObject, FieldSetEntity fse, String fieldName, String symbol) {
        if (fse.getValue(fieldName) == null) {
            return;
        }
        if (CmnConst.DATE.equals(symbol)) {
            if (jsonObject.get(CmnConst.STATISTICS_START_TIME) == null) {
                jsonObject.put(CmnConst.STATISTICS_START_TIME, fse.getDate(fieldName));
                jsonObject.put(CmnConst.STATISTICS_FINAL_TIME, fse.getDate(fieldName));
            } else {
                if (((Date) jsonObject.get(CmnConst.STATISTICS_START_TIME)).compareTo(fse.getDate(fieldName)) > 0) {
                    jsonObject.put(CmnConst.STATISTICS_START_TIME, fse.getDate(fieldName));
                } else if (((Date) jsonObject.get(CmnConst.STATISTICS_FINAL_TIME)).compareTo(fse.getDate(fieldName)) < 0) {
                    jsonObject.put(CmnConst.STATISTICS_FINAL_TIME, fse.getDate(fieldName));
                }
            }
        } else if (CmnConst.INT.equals(symbol)) {
            if (jsonObject.get(CmnConst.MIN_ID) == null) {
                jsonObject.put(CmnConst.MIN_ID, fse.getInteger(fieldName));
                jsonObject.put(CmnConst.MAX_ID, fse.getInteger(fieldName));
            } else {
                if (jsonObject.getInteger(CmnConst.MIN_ID) > fse.getInteger(fieldName)) {
                    jsonObject.put(CmnConst.MIN_ID, fse.getInteger(fieldName));
                } else if (jsonObject.getInteger(CmnConst.MAX_ID) < fse.getInteger(fieldName)) {
                    jsonObject.put(CmnConst.MAX_ID, fse.getInteger(fieldName));
                }
            }
        }
    }
 
    public static void main(String[] args) throws ParseException {
        Date d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-05-06 12:15:16");
        Date d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2022-05-06 12:15:16");
        System.out.println(d1.compareTo(d2));
 
        int minID = 46;
        int maxID = 98;
        System.out.println(minID > maxID);
    }
}