Commit 189aeac5 authored by tianbo's avatar tianbo

refactor(jg): 优化电梯数据保存逻辑与错误处理

- 增加对设备是否已登记的判断逻辑,避免重复历史登记 - 异常处理中增加堆栈信息记录,便于问题定位 - 保存失败时记录 traceInfo 信息到错误日志中 - 在异常信息中追加 record ID,方便追踪具体出错数据 - 修复历史登记判断逻辑,确保只对未登记设备执行历史登记操作
parent 48ff192d
...@@ -775,11 +775,11 @@ public class XiAnDataDockServiceImpl { ...@@ -775,11 +775,11 @@ public class XiAnDataDockServiceImpl {
* *
* @param elevatorExcelDtos * @param elevatorExcelDtos
*/ */
public Map<String, String> batchSaveElevatorData(List<XiAnElevatorExcelDto> elevatorExcelDtos) { public Map<String, Object> batchSaveElevatorData(List<XiAnElevatorExcelDto> elevatorExcelDtos) {
log.info("解析成功,准备上传数据,条数:{}", elevatorExcelDtos.size()); log.info("解析成功,准备上传数据,条数:{}", elevatorExcelDtos.size());
int batchSize = 1000; int batchSize = 1000;
int totalSize = elevatorExcelDtos.size(); int totalSize = elevatorExcelDtos.size();
Map<String, String> rMap = new HashMap<>(); Map<String, Object> rMap = new HashMap<>();
// 主线程中获取登录信息传递到异步线程中 // 主线程中获取登录信息传递到异步线程中
RequestContextWrapper contextWrapper = RequestContextWrapper.capture(); RequestContextWrapper contextWrapper = RequestContextWrapper.capture();
Set<String> importResult = ConcurrentHashMap.newKeySet(); // 使用线程安全的Set Set<String> importResult = ConcurrentHashMap.newKeySet(); // 使用线程安全的Set
...@@ -817,8 +817,8 @@ public class XiAnDataDockServiceImpl { ...@@ -817,8 +817,8 @@ public class XiAnDataDockServiceImpl {
// 等待所有异步任务完成 // 等待所有异步任务完成
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
if (!ValidationUtil.isEmpty(importResult)) { if (!ValidationUtil.isEmpty(importResult)) {
rMap.put("message", "设备已做过后续业务或被编辑,更新失败:[" + String.join(",", importResult) + "]"); rMap.put("message", importResult);
log.info("设备已做过后续业务或被编辑,更新失败:[{}", String.join(",", importResult) + "]"); log.info("更新失败:{}", importResult);
return rMap; return rMap;
} }
log.info("设备保存成功"); log.info("设备保存成功");
......
...@@ -26,4 +26,7 @@ public class ExcelImportErrorLogDto { ...@@ -26,4 +26,7 @@ public class ExcelImportErrorLogDto {
@Field(type = FieldType.Text) @Field(type = FieldType.Text)
private String errorInfo; private String errorInfo;
@Field(type = FieldType.Text)
private String traceInfo;
} }
...@@ -16,6 +16,7 @@ import com.alibaba.fastjson.JSONObject; ...@@ -16,6 +16,7 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.yeejoin.amos.boot.biz.common.bo.CompanyBo; import com.yeejoin.amos.boot.biz.common.bo.CompanyBo;
import com.yeejoin.amos.boot.biz.common.bo.ReginParams; import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
...@@ -514,11 +515,11 @@ public class DataDockServiceImpl { ...@@ -514,11 +515,11 @@ public class DataDockServiceImpl {
* @param record * @param record
*/ */
private void saveSupervisionInfo(Map<String, Object> equ, String record) { private void saveSupervisionInfo(Map<String, Object> equ, String record) {
log.error("数据===>{}", toJSONString(equ)); log.info("数据===>{}", toJSONString(equ));
IdxBizJgSupervisionInfo oldSupervisionInfo = idxBizJgSupervisionInfoService.lambdaQuery() IdxBizJgSupervisionInfo oldSupervisionInfo = idxBizJgSupervisionInfoService.lambdaQuery()
.eq(IdxBizJgSupervisionInfo::getRecord, record).one(); .eq(IdxBizJgSupervisionInfo::getRecord, record).one();
IdxBizJgSupervisionInfo supervisionInfo = JSON.parseObject(toJSONString(equ), IdxBizJgSupervisionInfo.class); IdxBizJgSupervisionInfo supervisionInfo = JSON.parseObject(toJSONString(equ), IdxBizJgSupervisionInfo.class);
log.error("数据===hou>{}", toJSONString(supervisionInfo)); log.info("数据===hou>{}", toJSONString(supervisionInfo));
if (!ValidationUtil.isEmpty(supervisionInfo)) { if (!ValidationUtil.isEmpty(supervisionInfo)) {
if (!ValidationUtil.isEmpty(oldSupervisionInfo)) { if (!ValidationUtil.isEmpty(oldSupervisionInfo)) {
supervisionInfo.setSequenceNbr(oldSupervisionInfo.getSequenceNbr()); supervisionInfo.setSequenceNbr(oldSupervisionInfo.getSequenceNbr());
...@@ -2622,17 +2623,19 @@ public class DataDockServiceImpl { ...@@ -2622,17 +2623,19 @@ public class DataDockServiceImpl {
@GlobalTransactional(rollbackFor = Exception.class) @GlobalTransactional(rollbackFor = Exception.class)
public Set<String> saveElevatorData(List<Map<String, Object>> equLists) { public Set<String> saveElevatorData(List<Map<String, Object>> equLists) {
RequestContextWrapper contextWrapper = RequestContextWrapper.capture(); RequestContextWrapper contextWrapper = RequestContextWrapper.capture();
Set<String> recordSet = new HashSet<>(); Set<String> recordSet = Sets.newConcurrentHashSet();
Set<String> inUseRecordSet = new HashSet<>(); Set<String> inUseRecordSet = Sets.newConcurrentHashSet();
try { try {
CompletableFuture.allOf( CompletableFuture.allOf(
equLists.parallelStream().map(equ -> CompletableFuture.runAsync(() -> { equLists.parallelStream().map(equ -> CompletableFuture.runAsync(() -> {
contextWrapper.apply(); contextWrapper.apply();
String record = saveElevatorDataInTransaction(equ, "jg_his_xa", null); Object resultObj = saveElevatorDataInTransaction(equ, "jg_his_xa", null);
if (inUseError.equals(record)) { if (resultObj instanceof String) {
inUseRecordSet.add(equ.get("record").toString()); recordSet.add(resultObj.toString());
} else { }
recordSet.add(record); if (resultObj instanceof Map) {
Map<String, Object> result = (Map<String, Object>) resultObj;
inUseRecordSet.add(JSONObject.toJSONString(result));
} }
}, executorService)).toArray(CompletableFuture[]::new) }, executorService)).toArray(CompletableFuture[]::new)
).join(); ).join();
...@@ -2650,7 +2653,8 @@ public class DataDockServiceImpl { ...@@ -2650,7 +2653,8 @@ public class DataDockServiceImpl {
} }
@GlobalTransactional(rollbackFor = Exception.class) @GlobalTransactional(rollbackFor = Exception.class)
public String saveElevatorDataInTransaction(Map<String, Object> equ, String dataSource, String remark) { public Object saveElevatorDataInTransaction(Map<String, Object> equ, String dataSource, String remark) {
Map<String, String> errorResult = Maps.newConcurrentMap();
String record = Optional.ofNullable(equ.get("record")).map(String::valueOf).orElse(UUID.randomUUID().toString()); String record = Optional.ofNullable(equ.get("record")).map(String::valueOf).orElse(UUID.randomUUID().toString());
String equList = Optional.ofNullable(equ.get("equList")).map(String::valueOf).orElse(""); String equList = Optional.ofNullable(equ.get("equList")).map(String::valueOf).orElse("");
String businessId = Optional.ofNullable(equ.get("businessId")).map(String::valueOf).orElse(""); String businessId = Optional.ofNullable(equ.get("businessId")).map(String::valueOf).orElse("");
...@@ -2661,9 +2665,17 @@ public class DataDockServiceImpl { ...@@ -2661,9 +2665,17 @@ public class DataDockServiceImpl {
// 判断设备是否已经做过除历史登记外的其他业务,如果做了其他业务则不能更新并记录反馈 // 判断设备是否已经做过除历史登记外的其他业务,如果做了其他业务则不能更新并记录反馈
Integer useCount = commonMapper.countEquipInUseTimesForXaElevator(record); Integer useCount = commonMapper.countEquipInUseTimesForXaElevator(record);
if (useCount > 0) { if (useCount > 0) {
return inUseError; errorResult.put("id", record);
errorResult.put("type", "inUse");
errorResult.put("msg", inUseError);
return errorResult;
}
// 判断是否做过历史登记
boolean isRegistered = true;
Map<String, Object> useRegistrationMap = jgUseRegistrationServiceImpl.getJgUseRegistrationMapper().getUseRegistrationDetail(record);
if (ValidationUtil.isEmpty(useRegistrationMap) || ValidationUtil.isEmpty(useRegistrationMap.get("UseRegistratSequenceNbr"))) {
isRegistered = false;
} }
IdxBizJgUseInfo oldUseInfo = idxBizJgUseInfoService.getOne(new QueryWrapper<IdxBizJgUseInfo>().eq("record", record));
IdxBizJgRegisterInfo oldRegisterInfo = idxBizJgRegisterInfoService.getOne(new QueryWrapper<IdxBizJgRegisterInfo>().eq("record", record)); IdxBizJgRegisterInfo oldRegisterInfo = idxBizJgRegisterInfoService.getOne(new QueryWrapper<IdxBizJgRegisterInfo>().eq("record", record));
IdxBizJgUseInfo useInfo = saveUseInfo(equ, record, dataSource, remark, null); IdxBizJgUseInfo useInfo = saveUseInfo(equ, record, dataSource, remark, null);
saveDesignInfo(equ, record); saveDesignInfo(equ, record);
...@@ -2678,12 +2690,12 @@ public class DataDockServiceImpl { ...@@ -2678,12 +2690,12 @@ public class DataDockServiceImpl {
saveEquInfoToEs(record, isCompleteXa); saveEquInfoToEs(record, isCompleteXa);
this.saveInstallInfo(equ);//安装信息 this.saveInstallInfo(equ);//安装信息
this.historyEquUpdateMaintenanceInfo(equ);//维保信息 this.historyEquUpdateMaintenanceInfo(equ);//维保信息
// oldUseInfo为空表示是新设备,新设备如果有使用登记证号则做历史登记,否则只进行上面的设备信息更新 // isRegistered=false是未做过登记设备,且有使用登记证号则做历史登记
if (!useRegistrationCode.isEmpty() && ValidationUtil.isEmpty(oldUseInfo)) { if (!ValidationUtil.isEmpty(useRegistrationCode) && !isRegistered) {
this.handleHistoryEquip(equ);//历史登记 this.handleHistoryEquip(equ);//历史登记
} }
// oldUseInfo不为空表示是平台已有设备,已有历史设备则更新历史单据信息。 // isRegistered=true表示已做过历史平台登记,则需更新历史单据等信息。
if (!ValidationUtil.isEmpty(oldUseInfo)) { if (isRegistered) {
updateHistoryInfo(equ, registerInfo, otherInfo, oldRegisterInfo, useInfo); updateHistoryInfo(equ, registerInfo, otherInfo, oldRegisterInfo, useInfo);
} }
} else if (!businessId.isEmpty()) { } else if (!businessId.isEmpty()) {
...@@ -2699,14 +2711,24 @@ public class DataDockServiceImpl { ...@@ -2699,14 +2711,24 @@ public class DataDockServiceImpl {
} }
return record; return record;
} catch (Exception e) { } catch (Exception e) {
// esEquipmentCategory.deleteById(record); String traceInfo = Arrays.stream(e.getStackTrace()).filter(x-> x.getClassName().contains("com.yeejoin")).map(x -> x.getClassName() + "."+ x.getMethodName() + ":" + x.getLineNumber()).collect(Collectors.toList()).toString();
esEquipmentCategory.deleteById(record);
// superviseInfoMapper.deleteDataAll(Collections.singletonList(record)); // superviseInfoMapper.deleteDataAll(Collections.singletonList(record));
ExcelImportErrorLogDto errorLogDto = JSON.parseObject(toJSONString(equ), ExcelImportErrorLogDto.class); ExcelImportErrorLogDto errorLogDto = JSON.parseObject(toJSONString(equ), ExcelImportErrorLogDto.class);
errorLogDto.setErrorInfo(e.getMessage()); errorLogDto.setErrorInfo(e.getMessage());
errorLogDto.setTraceInfo(traceInfo);
excelImportErrorLogDao.save(errorLogDto); excelImportErrorLogDao.save(errorLogDto);
String errorMessage = e.getMessage();
if (errorMessage == null) {
errorMessage = "Unknown error occurred: " + e.getClass().getName();
}
log.error("{}数据:保存时出现异常,对应数据:{}", dataSource, toJSONString(equ), e); log.error("{}数据:保存时出现异常,对应数据:{}", dataSource, toJSONString(equ), e);
log.error("==========================西安电梯数据:保存时出现异常,对应数据:{}==========================",record);
throw new RuntimeException("保存电梯数据失败:" + e.getMessage()); errorResult.put("id", record);
errorResult.put("type", "error");
errorResult.put("msg", errorMessage);
errorResult.put("traceInfo", traceInfo);
return errorResult;
} }
} }
...@@ -3046,7 +3068,7 @@ public class DataDockServiceImpl { ...@@ -3046,7 +3068,7 @@ public class DataDockServiceImpl {
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
this.rollBackForDelRedisData(); this.rollBackForDelRedisData();
throw new BadRequest(e.getMessage()); throw new BadRequest(e.getMessage() + "[" + map.get("record") + "]");
} finally { } finally {
FlowingEquipRedisContext.clean(); FlowingEquipRedisContext.clean();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment