1821349743@qq.com
2023-02-20 a387ff8978bd0e738e83e0277cfa38d7c3ab3080
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
package com.product.data.sync.util;
 
import com.product.common.lang.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.FieldSetEntity;
import com.product.core.service.support.AbstractBaseService;
import com.product.data.sync.config.CmnConst;
import com.product.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
 
@Service
public class ExceptionLog extends AbstractBaseService {
    @Autowired
    public BaseDao baseDao;
 
    @Override
    public BaseDao getBaseDao() {
        return baseDao;
    }
 
    @Override
    public void setBaseDao(BaseDao baseDao) {
        this.baseDao = baseDao;
    }
 
    /**
     * 新增修改失败修改日志,添加错误详情子表
     * @param sync_config_log_uuid 日志主表uuid
     * @param list 错误数据唯一标识集合
     * @param e 异常
     */
    public void addSubExceptionLog(String sync_config_log_uuid, List<String> list, Exception e) {
        FieldSetEntity subException = new FieldSetEntity();
        subException.setTableName(CmnConst.PRODUCT_SYS_DATABASE_SYNC_CONFIG_LOG_SUB);
        subException.setValue(CmnConst.SYNC_CONFIG_LOG_UUID, sync_config_log_uuid);
        subException.setValue(CmnConst.ERROR_INFO, e.getMessage());
        //直接开集合并去掉括号
        subException.setValue(CmnConst.WRONG_DATA_KEY, StringUtils.strip(list.toString(), "[]"));
        baseDao.add(subException);
    }
 
    /**
     * 新增修改失败修改日志,添加错误详情子表
     * @param sync_config_log_uuid 日志主表uuid
     * @param e 异常
     * @param currentPage 含错误数据的页数
     */
    public void addSubExceptionLog(String sync_config_log_uuid, Exception e, Integer currentPage) {
        FieldSetEntity subException = new FieldSetEntity();
        subException.setTableName(CmnConst.PRODUCT_SYS_DATABASE_SYNC_CONFIG_LOG_SUB);
        subException.setValue(CmnConst.SYNC_CONFIG_LOG_UUID, sync_config_log_uuid);
        subException.setValue(CmnConst.ERROR_INFO, e.getMessage());
        Integer startIndex = (currentPage - 1) * 1000;
        Integer endIndex = currentPage * 1000;
        //直接开集合并去掉括号
        subException.setValue(CmnConst.WRONG_DATA_KEY, "[" + startIndex + "-" + endIndex + "]");
        //添加时间
        subException.setValue(CmnConst.CREATED_UTC_DATETIME, new Date());
        baseDao.add(subException);
    }
 
    /**
     * 启动数据同步默认创建日志
     * @param configUuid 同步配置uuid
     * @param data_origin_name 数据源表名
     * @param system_table_name 系统表名
     * @return 日志uuid
     */
    public String addExceptionLog(String configUuid, String data_origin_name, String system_table_name) {
        FieldSetEntity exceptionLog = new FieldSetEntity();
        exceptionLog.setTableName(CmnConst.PRODUCT_SYS_DATABASE_SYNC_CONFIG_LOG);
        exceptionLog.setValue(CmnConst.SYNC_CONFIG_UUID, configUuid);
        exceptionLog.setValue(CmnConst.DATA_ORIGIN_NAME, data_origin_name);
        exceptionLog.setValue(CmnConst.SYNC_TIME, new Date());
        exceptionLog.setValue(CmnConst.SYSTEM_TABLE_NAME, system_table_name);
        return baseDao.add(exceptionLog);
    }
 
    /**
     * 查询成功或中途失败修改同步日志
     * @param conn 数据源连接
     * @param uuid 日志uuid
     * @param e 查询时异常
     * @param addNum 新增条数
     * @param upNum 修改条数
     * @param errorNum 错误条数
     * @param totalNumber 总条数
     */
    public void upExceptionLog(Connection conn, String uuid, Exception e, Integer addNum, Integer upNum, Integer deleteNumber, Integer errorNum, Integer totalNumber) {
        StringBuffer exception = new StringBuffer();
        if (e != null) {
            exception.append(e.getMessage());
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
                exception.append("\n");
                exception.append(ex.getMessage());
            }
        }
        FieldSetEntity exceptionLog = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_DATABASE_SYNC_CONFIG_LOG, uuid, false);
        String errInfo = exceptionLog.getString(CmnConst.ERR_INFO);
        if(!BaseUtil.strIsNull(errInfo)){
            exception.append("\n").append(errInfo);
        }
        exceptionLog.setValue(CmnConst.ERR_INFO, exception.toString());
        exceptionLog.setValue(CmnConst.SYNC_TIME, new Date());
        exceptionLog.setValue(CmnConst.ADD_NUM, addNum);
        exceptionLog.setValue(CmnConst.UPDATE_NUM, upNum);
        exceptionLog.setValue(CmnConst.DELETE_NUM, deleteNumber);
        exceptionLog.setValue(CmnConst.FAIL_NUM, errorNum);
        exceptionLog.setValue(CmnConst.TOTAL_NUMBER, totalNumber);
        baseDao.update(exceptionLog);
    }
 
    /**
     * 修改日志
     * @param uuid 日志uuid
     * @param e 查询时异常
     */
    public void upExceptionLog(String uuid, Exception e) {
        StringBuffer exception = new StringBuffer();
        if (e != null) {
            exception.append(e.getMessage());
        }
        FieldSetEntity exceptionLog = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_DATABASE_SYNC_CONFIG_LOG, uuid, false);
        String errInfo = exceptionLog.getString(CmnConst.ERR_INFO);
        if(!BaseUtil.strIsNull(errInfo)){
            exception.append("\n").append(errInfo);
        }
        exceptionLog.setValue(CmnConst.ERR_INFO, exception.toString());
        baseDao.update(exceptionLog);
    }
}