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
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
package com.product.data.sync.util;
 
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.product.common.collect.ListUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.data.sync.config.CmnConst;
import com.product.util.BaseUtil;
import com.product.util.CallBack;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
 
public class DataSaveSynchronization extends AbstractBaseService implements Runnable {
    @Autowired
    public ExceptionLog exceptionLog;
 
    public ExceptionLog getExceptionLog() {
        return exceptionLog;
    }
 
    public void setExceptionLog(ExceptionLog exceptionLog) {
        this.exceptionLog = exceptionLog;
    }
 
    @Autowired
    public BaseDao baseDao;
 
    @Override
    public BaseDao getBaseDao() {
        return baseDao;
    }
 
    @Override
    public void setBaseDao(BaseDao baseDao) {
        this.baseDao = baseDao;
    }
 
    private ResultSet resultSet;
    private Map<String, String> map;
    private Map<String, String> syncMap;
    private Map<String, String> sileMap;
    private CallBack callBack;
 
    public DataSaveSynchronization(ResultSet resultSet, Map<String, String> map, Map<String, String> sileMap, Map<String, String> syncMap) {
        this.resultSet = resultSet;
        this.map = map;
        this.syncMap = syncMap;
        this.sileMap = sileMap;
    }
 
    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
 
    @Override
    public void run() {
        //系统表名
        String tableName = map.get("tableName");
        //日志uuid
        String logUuid = map.get("logUuid");
        //事件前调用
        String savePreEvent = map.get("savePreEvent");
        //事件后调用
        String postSaveEvent = map.get("postSaveEvent");
        //同步类型
        String syncType = map.get(CmnConst.SYNC_TYPE);
        //是否有uuid
        Boolean is_uuid = Boolean.valueOf(map.get("is_uuid"));
        //迭代数量
        Integer resultRow = 0;
        //新增数量
        Integer addNum = 0;
        //修改数量
        Integer upNum = 0;
        //错误数量
        Integer errorNum = 0;
        List<String> list = ListUtils.newArrayList();
        List<String> addUuid = Lists.newArrayList();
        List<String> updateUuid = Lists.newArrayList();
        while (true) {
            try {
                if (!resultSet.next()) break;
            } catch (SQLException e) {
                exceptionLog.upExceptionLog(logUuid, e);
                break;
            }
            try {
                FieldSetEntity fieldSet = new FieldSetEntity();
                fieldSet.setTableName(tableName);
                resultRow++;
                StringBuffer condition = new StringBuffer();
                for (String key : syncMap.keySet()) {
                    fieldSet.setValue(syncMap.get(key), resultSet.getString(key));
                }
                list.clear();
                for (String key : sileMap.keySet()) {
                    String value = resultSet.getString(key);
                    String fieldName = sileMap.get(key);
                    fieldSet.setValue(fieldName, value);
                    condition.append(fieldName).append(" = ? AND ");
                    list.add(value);
                }
                //拼接修改语句  如果未修改成功记录 就把数据放入新增的dataTableEntity
                boolean is_update = false;
 
                //调用保存前方法
                if (!BaseUtil.strIsNull(savePreEvent) && savePreEvent.indexOf(".") != -1) {
                    DataManipulationUtils.codeCalls(savePreEvent, fieldSet);
                }
 
                if ("1".equals(syncType)) {
                    String term = condition.substring(0, condition.length() - 4);
                    FieldSetEntity fieldSetEntityByFilter = baseDao.getFieldSetEntityByFilter(tableName, term, list.toArray(new String[]{}), false);
                    //含有uuid
                    if (is_uuid) {
                        is_update = baseDao.update(fieldSet);
                        updateUuid.add(fieldSet.getUUID());
                        //如果查出了系统原数据
                    } else if (fieldSetEntityByFilter != null) {
                        fieldSet.setValue("uuid", fieldSetEntityByFilter.getString("uuid"));
                        is_update = baseDao.update(fieldSet);
                        updateUuid.add(fieldSet.getUUID());
                        // 没有uuid   并且没有源数据就新增
                    } else {
                        is_update = false;
                    }
                }
                //修改不成功
                if (!is_update) {
                    baseDao.add(fieldSet);
                    addNum++;
                } else {
                    //修改成功计数加一
                    upNum++;
                }
                //调用保存后方法
                if (!BaseUtil.strIsNull(postSaveEvent) && postSaveEvent.indexOf(".") != -1) {
                    DataManipulationUtils.codeCalls(postSaveEvent, fieldSet);
                }
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | BaseException | SQLException e) {
                errorNum++;
                exceptionLog.addSubExceptionLog(logUuid, list, e);
                continue;
            }
        }
        Map<String, Object> map = Maps.newHashMap();
        //迭代数量
        map.put("resultRow", (resultRow));
        //新增数量
        map.put("addNum", (addNum));
        //修改数量
        map.put("upNum", (upNum));
        //错误数量
        map.put("errorNum", (errorNum));
        map.put("changeDataKeys", Lists.newArrayList(addUuid, updateUuid));
        //回调函数
        if (this.callBack != null) {
            callBack.method(map);
        }
    }
}