|
|
@@ -19,11 +19,12 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeansException;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
-import org.springframework.jdbc.UncategorizedSQLException;
|
|
|
import org.springframework.lang.Nullable;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -77,16 +78,43 @@ public class TransferVehicleManage implements ApplicationContextAware {
|
|
|
/**
|
|
|
* 插入车辆数据
|
|
|
*/
|
|
|
-// @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public void batchSaveVehicle(List<VehicleDataSaveRequest> vehicleDataList) {
|
|
|
+ //1. 校验,筛选出需要新增的车辆
|
|
|
List<VehicleData> newVehicles = checkVehicle(vehicleDataList);
|
|
|
- // 批量保存到关系库
|
|
|
+
|
|
|
+ // 2. 记录时序库插入的唯一标识(用于补偿)
|
|
|
+ List<String> taosUniqueKeys = Collections.emptyList();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 3. 先批量保存到时序数据库
|
|
|
+ taosUniqueKeys = taosService.batchInsertVehicleData(vehicleDataList);
|
|
|
+
|
|
|
+ // 4、再批量保存到关系库
|
|
|
+ saveBatchVehicle(newVehicles);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 5. 补偿删除时序库数据
|
|
|
+ try {
|
|
|
+ if (!taosUniqueKeys.isEmpty()) {
|
|
|
+ taosService.batchDelete(taosUniqueKeys);
|
|
|
+ log.error("已补偿删除时序库数据,唯一标识:{}", taosUniqueKeys);
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error("补偿删除失败,需人工处理:", ex.getMessage());
|
|
|
+ }
|
|
|
+ throw new IotException(ErrorCodeEnum.DATA_SAVE_FAIL, "数据保存失败,已触发补偿", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关系库插入
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void saveBatchVehicle(List<VehicleData> newVehicles) {
|
|
|
if (CollectionUtils.isNotEmpty(newVehicles)) {
|
|
|
vehicleDataService.saveBatch(newVehicles);
|
|
|
}
|
|
|
-
|
|
|
- // 批量保存到时序数据库
|
|
|
- taosService.batchInsertVehicleData(vehicleDataList);
|
|
|
}
|
|
|
|
|
|
/**
|