odoo增加报错重试机制

畅捷通报表
YXY 1 year ago
parent 7a30649616
commit 7902fcb255

@ -100,66 +100,49 @@ public abstract class OdooAbstrackob {
private void batchSync(OdooContext context) {
//分页对象
Map<String, Integer> pageMap = getPageMap();
//查询结果
List<Object> dateList = new ArrayList<>();
//错误信息
List<String> errorList = new ArrayList<>();
//构建飞书请求对象
LarkCompanyRelation companyRelation = context.getCompanyRelation();
LarkTableRelation tableRelation = context.getTableRelation();
LarkTableRequest larkRequest = new LarkTableRequest(companyRelation.getAppId(), companyRelation.getSecret(), tableRelation.getToAppToken(), tableRelation.getToTableId());
List<String> errorList = new ArrayList<>();
batchCreate(context, pageMap, larkRequest, errorList);
if (!CollectionUtils.isEmpty(errorList)) {
String errorKey = String.join(",", errorList);
throw new RuntimeException(String.format("存在同步失败的记录 %s", errorKey));
}
}
private void batchCreate(OdooContext context, Map<String, Integer> pageMap, LarkTableRequest larkRequest, List<String> errorList) {
//查询结果
List<Object> dateList = new ArrayList<>();
List<Integer> retryPageList = new ArrayList<>();
//待创建关联关系
List<ErpLarkRelation> waitCreateRelation = new ArrayList<>();
do {
List<ErpLarkRelation> waitCreateRelation = new ArrayList<>();
dateList = getDate(context, pageMap,context.getWarehouseId(),retryPageList);
dateList = getDate(context, pageMap, context.getWarehouseId(), retryPageList);
List<JSONObject> midDateList = dateList.stream().map(r -> {
return (JSONObject)JSONObject.toJSON(r);
return (JSONObject) JSONObject.toJSON(r);
}).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(midDateList)){
Map<String, String> relationMap = getLarkRelation(midDateList,context.getWarehouseId());
if (CollectionUtils.isNotEmpty(midDateList)) {
Map<String, String> relationMap = getLarkRelation(midDateList, context.getWarehouseId());
for (JSONObject jsonObject : midDateList) {
sync(jsonObject, errorList, relationMap, larkRequest,waitCreateRelation,context.getWarehouseId());
sync(jsonObject, errorList, relationMap, larkRequest, waitCreateRelation, context.getWarehouseId());
}
}
if (CollectionUtils.isNotEmpty(waitCreateRelation)){
if (CollectionUtils.isNotEmpty(waitCreateRelation)) {
erpLarkRelationMapper.batchInsert(waitCreateRelation);
}
pageMap.put(PAGE_OFFSET_KEY,pageMap.get(PAGE_OFFSET_KEY) + PAGE_SIZE);
pageMap.put(PAGE_OFFSET_KEY, pageMap.get(PAGE_OFFSET_KEY) + PAGE_SIZE);
} while (dateList == null || (CollectionUtils.isNotEmpty(dateList) && dateList.size() == PAGE_SIZE));
if (CollectionUtils.isNotEmpty(retryPageList)){
try {
for (Integer page : retryPageList) {
pageMap.put(PAGE_OFFSET_KEY,page + PAGE_SIZE);
List<ErpLarkRelation> waitCreateRelation = new ArrayList<>();
dateList = getDate(context, pageMap,context.getWarehouseId(),null);
List<JSONObject> midDateList = dateList.stream().map(r -> {
return (JSONObject)JSONObject.toJSON(r);
}).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(midDateList)){
Map<String, String> relationMap = getLarkRelation(midDateList,context.getWarehouseId());
for (JSONObject jsonObject : midDateList) {
sync(jsonObject, errorList, relationMap, larkRequest,waitCreateRelation,context.getWarehouseId());
}
}
if (CollectionUtils.isNotEmpty(waitCreateRelation)){
erpLarkRelationMapper.batchInsert(waitCreateRelation);
}
}
}catch (Exception e){
errorList.add(String.format("%s 获取第【%s】页数据失败 %s", this.getClassName(), pageMap.get(PAGE_OFFSET_KEY), e.getMessage()));
}
if (retryPageList.size() > 0) {
pageMap.put(PAGE_OFFSET_KEY, retryPageList.get(0) + PAGE_SIZE);
batchCreate(context, pageMap, larkRequest, errorList);
}
if (!CollectionUtils.isEmpty(errorList)) {
String errorKey = String.join(",", errorList);
throw new RuntimeException(String.format("存在同步失败的记录 %s", errorKey));
}
}
private void sync(JSONObject jsonObject, List<String> errorList
, Map<String, String> relationMap, LarkTableRequest larkRequest
, List<ErpLarkRelation> waitCreateRelation,Integer warehouseId) {
, List<ErpLarkRelation> waitCreateRelation, Integer warehouseId) {
String uniqueKey = null;
try {
Map<String, Object> larkBodyMap = this.buildLarkBodyMap(jsonObject);
@ -174,16 +157,16 @@ public abstract class OdooAbstrackob {
}
} else {
CreateAppTableRecordRespBody respBody = larkTableHelper.addTableRecord(larkRequest);
waitCreateRelation.add(buildErpLarkRelation(uniqueKey,respBody.getRecord().getRecordId(),warehouseId));
waitCreateRelation.add(buildErpLarkRelation(uniqueKey, respBody.getRecord().getRecordId(), warehouseId));
}
} catch (Exception e) {
log.error("{} addOrUpdate exception {}", this.getClassName(),uniqueKey, e);
errorList.add(String.format("【%s,%s】",uniqueKey,e.getMessage()));
log.error("{} addOrUpdate exception {}", this.getClassName(), uniqueKey, e);
errorList.add(String.format("【%s,%s】", uniqueKey, e.getMessage()));
}
}
//构建飞书行与cjt唯一键的对应关系对象
private ErpLarkRelation buildErpLarkRelation(String key, String larkKey,Integer warehouseId) {
private ErpLarkRelation buildErpLarkRelation(String key, String larkKey, Integer warehouseId) {
ErpLarkRelation erpLarkRelation = new ErpLarkRelation();
erpLarkRelation.setKey(key);
erpLarkRelation.setLarkKey(larkKey);
@ -201,7 +184,7 @@ public abstract class OdooAbstrackob {
return jsonObject.toJSONString();
}
private Map<String, String> getLarkRelation(List<JSONObject> dateList,Integer warehouseId) {
private Map<String, String> getLarkRelation(List<JSONObject> dateList, Integer warehouseId) {
List<String> uniqueKeyList = dateList.stream().map(r -> {
return buildUniqueKey(r);
}).distinct().collect(Collectors.toList());
@ -253,22 +236,22 @@ public abstract class OdooAbstrackob {
//获取待同步飞书表格信息
protected abstract TableRelationTypeEnum syncLarkType(Integer warehouseId);
protected JSONArray parseJsonArray(String value, int count){
protected JSONArray parseJsonArray(String value, int count) {
JSONArray jsonArray = new JSONArray();
try {
jsonArray = JSONArray.parseArray(value);
for (int i= jsonArray.size();i<count;i++){
for (int i = jsonArray.size(); i < count; i++) {
jsonArray.add(null);
}
}catch (Exception e){
for (int i= 0;i<count;i++){
} catch (Exception e) {
for (int i = 0; i < count; i++) {
jsonArray.add(null);
}
}
return jsonArray;
}
public List<Object> getDate(OdooContext odooContext, Map<String, Integer> pageMap,Integer warehouseId,List<Integer> retryPageList) {
public List<Object> getDate(OdooContext odooContext, Map<String, Integer> pageMap, Integer warehouseId, List<Integer> retryPageList) {
try {
XmlRpcClient models = odooContext.getModels();
Integer uid = getUserId();
@ -280,7 +263,7 @@ public abstract class OdooAbstrackob {
pageMap//分页
)));
return asList((Object[])models.execute(GET_DATA_P_METHOD_NAME, asList(
return asList((Object[]) models.execute(GET_DATA_P_METHOD_NAME, asList(
GET_DATA_DB, uid, GET_DATA_PASSWORD,
modelName, "read",
asList(ids),
@ -288,9 +271,7 @@ public abstract class OdooAbstrackob {
)));
} catch (Exception e) {
log.error("{} 获取数据失败", this.getClassName(), e);
if (retryPageList != null) {
retryPageList.add(pageMap.get(PAGE_OFFSET_KEY));
}
retryPageList.add(pageMap.get(PAGE_OFFSET_KEY));
}
return null;
}

Loading…
Cancel
Save