feat:存储池同步接口修改
parent
1f3e63a8a8
commit
70f6ade5db
|
|
@ -12,6 +12,7 @@ import com.unisinsight.project.exception.BusinessException;
|
||||||
import com.unisinsight.project.feign.ExternalApiClient;
|
import com.unisinsight.project.feign.ExternalApiClient;
|
||||||
import com.unisinsight.project.mapper.StoragePoolMapper;
|
import com.unisinsight.project.mapper.StoragePoolMapper;
|
||||||
import com.unisinsight.project.service.StoragePoolService;
|
import com.unisinsight.project.service.StoragePoolService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
@ -21,10 +22,14 @@ import javax.annotation.Resource;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 存储池服务实现类
|
* 存储池服务实现类
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class StoragePoolServiceImpl extends ServiceImpl<StoragePoolMapper, StoragePool> implements StoragePoolService {
|
public class StoragePoolServiceImpl extends ServiceImpl<StoragePoolMapper, StoragePool> implements StoragePoolService {
|
||||||
|
|
||||||
|
|
@ -181,6 +186,7 @@ public class StoragePoolServiceImpl extends ServiceImpl<StoragePoolMapper, Stora
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void synchData() {
|
public void synchData() {
|
||||||
ApiResponse<StoragePoolData> response;
|
ApiResponse<StoragePoolData> response;
|
||||||
try {
|
try {
|
||||||
|
|
@ -192,22 +198,69 @@ public class StoragePoolServiceImpl extends ServiceImpl<StoragePoolMapper, Stora
|
||||||
throw new BusinessException("调用外部接口失败: " + e.getMessage());
|
throw new BusinessException("调用外部接口失败: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
List<VmStoragePool> vmStoragePoolList = response.getData().getItems();
|
List<VmStoragePool> remoteStoragePools = response.getData().getItems();
|
||||||
if (CollectionUtils.isEmpty(vmStoragePoolList)) {
|
|
||||||
return;
|
// 获取本地所有存储池数据
|
||||||
}
|
List<StoragePool> localStoragePools = this.list();
|
||||||
List<StoragePool> networkManageList = new ArrayList<>();
|
|
||||||
for (VmStoragePool vmStoragePool : vmStoragePoolList) {
|
// 创建映射便于查找
|
||||||
|
Map<String, VmStoragePool> remoteMap = remoteStoragePools.stream()
|
||||||
|
.collect(Collectors.toMap(VmStoragePool::getName, Function.identity()));
|
||||||
|
|
||||||
|
Map<String, StoragePool> localMap = localStoragePools.stream()
|
||||||
|
.collect(Collectors.toMap(StoragePool::getPoolName, Function.identity()));
|
||||||
|
|
||||||
|
// 分类处理数据
|
||||||
|
List<StoragePool> toAdd = new ArrayList<>();
|
||||||
|
List<StoragePool> toUpdate = new ArrayList<>();
|
||||||
|
|
||||||
|
// 处理远程数据:识别新增和修改项
|
||||||
|
for (VmStoragePool remotePool : remoteStoragePools) {
|
||||||
|
StoragePool localPool = localMap.get(remotePool.getName());
|
||||||
StoragePool storagePool = new StoragePool();
|
StoragePool storagePool = new StoragePool();
|
||||||
BeanUtils.copyProperties(vmStoragePool, storagePool);
|
BeanUtils.copyProperties(remotePool, storagePool);
|
||||||
storagePool.setPoolName(vmStoragePool.getName());
|
storagePool.setPoolName(remotePool.getName());
|
||||||
storagePool.setStatus("active".equals(vmStoragePool.getState()) ? 1 : 2);
|
storagePool.setStatus("active".equals(remotePool.getState()) ? 1 : 2);
|
||||||
networkManageList.add(storagePool);
|
|
||||||
}
|
|
||||||
// 清空数据并重新插入
|
|
||||||
this.remove(new QueryWrapper<>());
|
|
||||||
this.saveOrUpdateBatch(networkManageList);
|
|
||||||
|
|
||||||
|
if (localPool == null) {
|
||||||
|
// 新增项
|
||||||
|
storagePool.setCreateTime(new Date());
|
||||||
|
storagePool.setUpdateTime(new Date());
|
||||||
|
toAdd.add(storagePool);
|
||||||
|
} else {
|
||||||
|
// 修改项
|
||||||
|
storagePool.setId(localPool.getId());
|
||||||
|
storagePool.setCreateTime(localPool.getCreateTime());
|
||||||
|
storagePool.setUpdateTime(new Date());
|
||||||
|
toUpdate.add(storagePool);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 识别删除项
|
||||||
|
List<String> toRemoveNames = new ArrayList<>();
|
||||||
|
for (StoragePool localPool : localStoragePools) {
|
||||||
|
if (!remoteMap.containsKey(localPool.getPoolName())) {
|
||||||
|
toRemoveNames.add(localPool.getPoolName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 执行批量操作
|
||||||
|
if (!toAdd.isEmpty()) {
|
||||||
|
this.saveBatch(toAdd);
|
||||||
|
log.info("新增 {} 条存储池数据", toAdd.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!toUpdate.isEmpty()) {
|
||||||
|
this.updateBatchById(toUpdate);
|
||||||
|
log.info("更新 {} 条存储池数据", toUpdate.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!toRemoveNames.isEmpty()) {
|
||||||
|
QueryWrapper<StoragePool> deleteWrapper = new QueryWrapper<>();
|
||||||
|
deleteWrapper.in("pool_name", toRemoveNames);
|
||||||
|
this.remove(deleteWrapper);
|
||||||
|
log.info("删除 {} 条存储池数据", toRemoveNames.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue