package com.product.data.sync.util;
|
|
import com.beust.jcommander.internal.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.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.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
|
public class DataAddSynchronization 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 DataAddSynchronization(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");
|
//迭代数量
|
Integer resultRow = 0;
|
//新增数量
|
Integer addNum = 0;
|
List<String> addDataRecord = new ArrayList<>();
|
//错误数量
|
Integer errorNum = 0;
|
List<String> list = ListUtils.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);
|
}
|
//调用保存前方法
|
if (!BaseUtil.strIsNull(savePreEvent) && savePreEvent.indexOf(".") != -1) {
|
DataManipulationUtils.codeCalls(savePreEvent, fieldSet);
|
}
|
String term = condition.substring(0, condition.length() - 4);
|
FieldSetEntity fieldSetEntityByFilter = baseDao.getFieldSetEntityByFilter(tableName, term, list.toArray(new String[]{}), false);
|
//如果未查到数据就新增
|
if (fieldSetEntityByFilter == null) {
|
baseDao.add(fieldSet);
|
addNum++;
|
} else {
|
errorNum++;
|
}
|
//调用保存后方法
|
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("errorNum", (errorNum));
|
map.put("changeDataKeys", Lists.newArrayList(addDataRecord));
|
//回调函数
|
if (this.callBack != null) {
|
callBack.method(map);
|
}
|
}
|
}
|