From e2c223b22e6bb6c58152b302da6d224827651e96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=A4=E5=85=A8=E6=98=86?= Date: Thu, 28 Aug 2025 18:08:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AD=98=E5=82=A8=E6=B1=A0=E5=92=8C?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E4=BB=A3=E7=A0=81=E8=B0=83=E7=94=A8=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E6=96=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/controller/ClientController.java | 3 +- .../controller/NetworkManageController.java | 9 +- .../controller/StoragePoolController.java | 8 +- .../project/entity/dao/NetworkManage.java | 4 +- .../project/entity/dao/StoragePool.java | 4 +- .../project/entity/dto/Network.java | 150 ++++++++++++++++++ .../project/entity/dto/NetworkData.java | 24 +++ .../project/entity/dto/Pagination.java | 42 +++++ .../project/entity/dto/StoragePoolData.java | 52 ++++++ .../project/entity/dto/VmStoragePool.java | 105 ++++++++++++ .../project/entity/req/LoginReq.java | 5 + .../entity/req/NetworkManagePageReq.java | 2 +- .../project/entity/req/NetworkManageReq.java | 2 +- .../project/entity/req/StoragePoolReq.java | 2 +- .../project/feign/ExternalApiClient.java | 15 +- .../project/service/NetworkManageService.java | 2 + .../project/service/StoragePoolService.java | 2 + .../impl/NetworkManageServiceImpl.java | 75 ++++++--- .../service/impl/StoragePoolServiceImpl.java | 52 ++++-- 19 files changed, 510 insertions(+), 48 deletions(-) create mode 100644 nex-be/src/main/java/com/unisinsight/project/entity/dto/Network.java create mode 100644 nex-be/src/main/java/com/unisinsight/project/entity/dto/NetworkData.java create mode 100644 nex-be/src/main/java/com/unisinsight/project/entity/dto/Pagination.java create mode 100644 nex-be/src/main/java/com/unisinsight/project/entity/dto/StoragePoolData.java create mode 100644 nex-be/src/main/java/com/unisinsight/project/entity/dto/VmStoragePool.java diff --git a/nex-be/src/main/java/com/unisinsight/project/controller/ClientController.java b/nex-be/src/main/java/com/unisinsight/project/controller/ClientController.java index 9f9b772..4b89816 100644 --- a/nex-be/src/main/java/com/unisinsight/project/controller/ClientController.java +++ b/nex-be/src/main/java/com/unisinsight/project/controller/ClientController.java @@ -52,6 +52,7 @@ public class ClientController { deviceReq.setDeviceName("默认终端"); deviceReq.setDeviceGroupId(3L); deviceReq.setDeviceGroupName("默认终端分组"); + deviceReq.setMacAddr(loginReq.getMacAddr()); return deviceService.insert(deviceReq); } @@ -108,6 +109,4 @@ public class ClientController { hashMap.put("url", "https://intent-bathhouse.name"); return Result.successResult(hashMap); } - - } diff --git a/nex-be/src/main/java/com/unisinsight/project/controller/NetworkManageController.java b/nex-be/src/main/java/com/unisinsight/project/controller/NetworkManageController.java index 453ecb6..0d9e2c7 100644 --- a/nex-be/src/main/java/com/unisinsight/project/controller/NetworkManageController.java +++ b/nex-be/src/main/java/com/unisinsight/project/controller/NetworkManageController.java @@ -56,10 +56,11 @@ public class NetworkManageController { return Result.successResult(); } - @ApiOperation(value = "同步状态") - @GetMapping("/synchStatus") - public Result synchStatus() { - return null; + @ApiOperation(value = "同步数据和状态") + @GetMapping("/synchData") + public Result synchData() { + networkManageService.synchData(); + return Result.successResult(); } } diff --git a/nex-be/src/main/java/com/unisinsight/project/controller/StoragePoolController.java b/nex-be/src/main/java/com/unisinsight/project/controller/StoragePoolController.java index ac35de1..fe35d27 100644 --- a/nex-be/src/main/java/com/unisinsight/project/controller/StoragePoolController.java +++ b/nex-be/src/main/java/com/unisinsight/project/controller/StoragePoolController.java @@ -62,10 +62,10 @@ public class StoragePoolController { } - @ApiOperation(value = "同步状态") - @GetMapping("/synchStatus") - public Result synchStatus() { - + @ApiOperation(value = "同步数据") + @GetMapping("/synchData") + public Result synchData() { + storagePoolService.synchData(); return null; } diff --git a/nex-be/src/main/java/com/unisinsight/project/entity/dao/NetworkManage.java b/nex-be/src/main/java/com/unisinsight/project/entity/dao/NetworkManage.java index 1e9a975..5a3d190 100644 --- a/nex-be/src/main/java/com/unisinsight/project/entity/dao/NetworkManage.java +++ b/nex-be/src/main/java/com/unisinsight/project/entity/dao/NetworkManage.java @@ -23,8 +23,8 @@ public class NetworkManage { /** * 网络名称 */ - @TableField("name") - private String name; + @TableField("network_name") + private String networkName; /** * 网络类型 diff --git a/nex-be/src/main/java/com/unisinsight/project/entity/dao/StoragePool.java b/nex-be/src/main/java/com/unisinsight/project/entity/dao/StoragePool.java index ff88a16..3872265 100644 --- a/nex-be/src/main/java/com/unisinsight/project/entity/dao/StoragePool.java +++ b/nex-be/src/main/java/com/unisinsight/project/entity/dao/StoragePool.java @@ -31,9 +31,9 @@ public class StoragePool { /** * 存储池名称 */ - @TableField("name") + @TableField("pool_name") @ApiModelProperty(value = "存储池名称") - private String name; + private String poolName; /** * 存储池类型 diff --git a/nex-be/src/main/java/com/unisinsight/project/entity/dto/Network.java b/nex-be/src/main/java/com/unisinsight/project/entity/dto/Network.java new file mode 100644 index 0000000..9b3564c --- /dev/null +++ b/nex-be/src/main/java/com/unisinsight/project/entity/dto/Network.java @@ -0,0 +1,150 @@ +// Network.java - 网络信息对象 +package com.unisinsight.project.entity.dto; + +public class Network { + private String name; + private String uuid; + private String state; + private Integer autostart; + private Integer persistent; + private String description; + private String type; + private String bridge; + private String bridgeName; + private String ipAddress; + private String netmask; + private String gateway; + private String ipRange; + private Boolean dhcpEnabled; + private String dhcpStart; + private String dhcpEnd; + + // Getters and Setters + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public Integer getAutostart() { + return autostart; + } + + public void setAutostart(Integer autostart) { + this.autostart = autostart; + } + + public Integer getPersistent() { + return persistent; + } + + public void setPersistent(Integer persistent) { + this.persistent = persistent; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getBridge() { + return bridge; + } + + public void setBridge(String bridge) { + this.bridge = bridge; + } + + public String getBridgeName() { + return bridgeName; + } + + public void setBridgeName(String bridgeName) { + this.bridgeName = bridgeName; + } + + public String getIpAddress() { + return ipAddress; + } + + public void setIpAddress(String ipAddress) { + this.ipAddress = ipAddress; + } + + public String getNetmask() { + return netmask; + } + + public void setNetmask(String netmask) { + this.netmask = netmask; + } + + public String getGateway() { + return gateway; + } + + public void setGateway(String gateway) { + this.gateway = gateway; + } + + public String getIpRange() { + return ipRange; + } + + public void setIpRange(String ipRange) { + this.ipRange = ipRange; + } + + public Boolean getDhcpEnabled() { + return dhcpEnabled; + } + + public void setDhcpEnabled(Boolean dhcpEnabled) { + this.dhcpEnabled = dhcpEnabled; + } + + public String getDhcpStart() { + return dhcpStart; + } + + public void setDhcpStart(String dhcpStart) { + this.dhcpStart = dhcpStart; + } + + public String getDhcpEnd() { + return dhcpEnd; + } + + public void setDhcpEnd(String dhcpEnd) { + this.dhcpEnd = dhcpEnd; + } +} diff --git a/nex-be/src/main/java/com/unisinsight/project/entity/dto/NetworkData.java b/nex-be/src/main/java/com/unisinsight/project/entity/dto/NetworkData.java new file mode 100644 index 0000000..f6492ce --- /dev/null +++ b/nex-be/src/main/java/com/unisinsight/project/entity/dto/NetworkData.java @@ -0,0 +1,24 @@ +package com.unisinsight.project.entity.dto; +import java.util.List; + +public class NetworkData { + private List networks; + private Pagination pagination; + + // Getters and Setters + public List getNetworks() { + return networks; + } + + public void setNetworks(List networks) { + this.networks = networks; + } + + public Pagination getPagination() { + return pagination; + } + + public void setPagination(Pagination pagination) { + this.pagination = pagination; + } +} diff --git a/nex-be/src/main/java/com/unisinsight/project/entity/dto/Pagination.java b/nex-be/src/main/java/com/unisinsight/project/entity/dto/Pagination.java new file mode 100644 index 0000000..42ead4a --- /dev/null +++ b/nex-be/src/main/java/com/unisinsight/project/entity/dto/Pagination.java @@ -0,0 +1,42 @@ +// Pagination.java - 分页信息对象 +package com.unisinsight.project.entity.dto; + +public class Pagination { + private Integer currentPage; + private Integer pageSize; + private Integer total; + private Integer totalPages; + + // Getters and Setters + public Integer getCurrentPage() { + return currentPage; + } + + public void setCurrentPage(Integer currentPage) { + this.currentPage = currentPage; + } + + public Integer getPageSize() { + return pageSize; + } + + public void setPageSize(Integer pageSize) { + this.pageSize = pageSize; + } + + public Integer getTotal() { + return total; + } + + public void setTotal(Integer total) { + this.total = total; + } + + public Integer getTotalPages() { + return totalPages; + } + + public void setTotalPages(Integer totalPages) { + this.totalPages = totalPages; + } +} diff --git a/nex-be/src/main/java/com/unisinsight/project/entity/dto/StoragePoolData.java b/nex-be/src/main/java/com/unisinsight/project/entity/dto/StoragePoolData.java new file mode 100644 index 0000000..e22db25 --- /dev/null +++ b/nex-be/src/main/java/com/unisinsight/project/entity/dto/StoragePoolData.java @@ -0,0 +1,52 @@ +package com.unisinsight.project.entity.dto; + +import java.util.List; + +public class StoragePoolData { + private List items; + private Integer total; + private Integer page; + private Integer pageSize; + private Integer totalPages; + + // Getters and Setters + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } + + public Integer getTotal() { + return total; + } + + public void setTotal(Integer total) { + this.total = total; + } + + public Integer getPage() { + return page; + } + + public void setPage(Integer page) { + this.page = page; + } + + public Integer getPageSize() { + return pageSize; + } + + public void setPageSize(Integer pageSize) { + this.pageSize = pageSize; + } + + public Integer getTotalPages() { + return totalPages; + } + + public void setTotalPages(Integer totalPages) { + this.totalPages = totalPages; + } +} diff --git a/nex-be/src/main/java/com/unisinsight/project/entity/dto/VmStoragePool.java b/nex-be/src/main/java/com/unisinsight/project/entity/dto/VmStoragePool.java new file mode 100644 index 0000000..1709af9 --- /dev/null +++ b/nex-be/src/main/java/com/unisinsight/project/entity/dto/VmStoragePool.java @@ -0,0 +1,105 @@ +// StoragePool.java - 存储池信息对象 +package com.unisinsight.project.entity.dto; + +public class VmStoragePool { + private String name; + private String uuid; + private String state; + private Long capacity; + private Long allocation; + private Long available; + private Integer autostart; + private Integer persistent; + private Integer volumeCount; + private String type; + private String path; + + // Getters and Setters + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public Long getCapacity() { + return capacity; + } + + public void setCapacity(Long capacity) { + this.capacity = capacity; + } + + public Long getAllocation() { + return allocation; + } + + public void setAllocation(Long allocation) { + this.allocation = allocation; + } + + public Long getAvailable() { + return available; + } + + public void setAvailable(Long available) { + this.available = available; + } + + public Integer getAutostart() { + return autostart; + } + + public void setAutostart(Integer autostart) { + this.autostart = autostart; + } + + public Integer getPersistent() { + return persistent; + } + + public void setPersistent(Integer persistent) { + this.persistent = persistent; + } + + public Integer getVolumeCount() { + return volumeCount; + } + + public void setVolumeCount(Integer volumeCount) { + this.volumeCount = volumeCount; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } +} diff --git a/nex-be/src/main/java/com/unisinsight/project/entity/req/LoginReq.java b/nex-be/src/main/java/com/unisinsight/project/entity/req/LoginReq.java index 04ea9dc..5d81906 100644 --- a/nex-be/src/main/java/com/unisinsight/project/entity/req/LoginReq.java +++ b/nex-be/src/main/java/com/unisinsight/project/entity/req/LoginReq.java @@ -30,5 +30,10 @@ public class LoginReq { @JsonProperty("token") private String token; + @ApiModelProperty("mac地址") + private String macAddr; + + + } diff --git a/nex-be/src/main/java/com/unisinsight/project/entity/req/NetworkManagePageReq.java b/nex-be/src/main/java/com/unisinsight/project/entity/req/NetworkManagePageReq.java index d0d6948..d309ff0 100644 --- a/nex-be/src/main/java/com/unisinsight/project/entity/req/NetworkManagePageReq.java +++ b/nex-be/src/main/java/com/unisinsight/project/entity/req/NetworkManagePageReq.java @@ -17,7 +17,7 @@ public class NetworkManagePageReq { */ @Size(max = 64, message = "网络名称长度不能超过64个字符") @ApiModelProperty(value = "网络名称", required = true) - private String name; + private String networkName; /** * 网络类型 diff --git a/nex-be/src/main/java/com/unisinsight/project/entity/req/NetworkManageReq.java b/nex-be/src/main/java/com/unisinsight/project/entity/req/NetworkManageReq.java index b3ecb2f..0dc22da 100644 --- a/nex-be/src/main/java/com/unisinsight/project/entity/req/NetworkManageReq.java +++ b/nex-be/src/main/java/com/unisinsight/project/entity/req/NetworkManageReq.java @@ -22,7 +22,7 @@ public class NetworkManageReq { @NotBlank(message = "网络名称不能为空") @Size(max = 64, message = "网络名称长度不能超过64个字符") @ApiModelProperty(value = "网络名称", required = true) - private String name; + private String networkName; /** * 网络类型 diff --git a/nex-be/src/main/java/com/unisinsight/project/entity/req/StoragePoolReq.java b/nex-be/src/main/java/com/unisinsight/project/entity/req/StoragePoolReq.java index 3261428..9ce0af3 100644 --- a/nex-be/src/main/java/com/unisinsight/project/entity/req/StoragePoolReq.java +++ b/nex-be/src/main/java/com/unisinsight/project/entity/req/StoragePoolReq.java @@ -26,7 +26,7 @@ public class StoragePoolReq { @ApiModelProperty("名称") @NotBlank(message = "名称不能为空") @Size(max = 64, message = "名称长度不能超过64个字符") - private String name; + private String poolName; @ApiModelProperty("类型") @Size(max = 16, message = "类型长度不能超过16个字符") diff --git a/nex-be/src/main/java/com/unisinsight/project/feign/ExternalApiClient.java b/nex-be/src/main/java/com/unisinsight/project/feign/ExternalApiClient.java index 0a9b615..5ff4ed4 100644 --- a/nex-be/src/main/java/com/unisinsight/project/feign/ExternalApiClient.java +++ b/nex-be/src/main/java/com/unisinsight/project/feign/ExternalApiClient.java @@ -1,14 +1,16 @@ package com.unisinsight.project.feign; import com.unisinsight.project.config.FeignConfig; +import com.unisinsight.project.entity.dao.NetworkManage; import com.unisinsight.project.entity.dto.ApiResponse; +import com.unisinsight.project.entity.dto.NetworkData; +import com.unisinsight.project.entity.dto.StoragePoolData; import com.unisinsight.project.entity.req.NetworkManageReq; import com.unisinsight.project.entity.req.StoragePoolReq; import org.springframework.cloud.openfeign.FeignClient; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.*; + +import java.util.List; /** * 第三方API客户端 @@ -47,4 +49,9 @@ public interface ExternalApiClient { @DeleteMapping("/api/v1/storage/pools/{pool_name}") ApiResponse deleteStorage(@PathVariable("pool_name") String poolName); + @GetMapping("/api/v1/network/list") + ApiResponse list(@RequestParam("page")int page, @RequestParam("page_size")int pageSize); + + @GetMapping("/api/v1/storage/pools") + ApiResponse listStorage(@RequestParam("page")int page, @RequestParam("page_size")int pageSize); } diff --git a/nex-be/src/main/java/com/unisinsight/project/service/NetworkManageService.java b/nex-be/src/main/java/com/unisinsight/project/service/NetworkManageService.java index ad26a8e..5760d35 100644 --- a/nex-be/src/main/java/com/unisinsight/project/service/NetworkManageService.java +++ b/nex-be/src/main/java/com/unisinsight/project/service/NetworkManageService.java @@ -61,4 +61,6 @@ public interface NetworkManageService { * @return Page 分页结果 */ PageResult pageNetworkManages(NetworkManagePageReq networkManagePageReq); + + void synchData(); } diff --git a/nex-be/src/main/java/com/unisinsight/project/service/StoragePoolService.java b/nex-be/src/main/java/com/unisinsight/project/service/StoragePoolService.java index c767e2a..25841b8 100644 --- a/nex-be/src/main/java/com/unisinsight/project/service/StoragePoolService.java +++ b/nex-be/src/main/java/com/unisinsight/project/service/StoragePoolService.java @@ -50,4 +50,6 @@ public interface StoragePoolService { * @return Page 分页结果 */ PageResult pageStoragePools(StoragePoolReq storagePoolReq); + + void synchData(); } diff --git a/nex-be/src/main/java/com/unisinsight/project/service/impl/NetworkManageServiceImpl.java b/nex-be/src/main/java/com/unisinsight/project/service/impl/NetworkManageServiceImpl.java index d5658ef..4641f17 100644 --- a/nex-be/src/main/java/com/unisinsight/project/service/impl/NetworkManageServiceImpl.java +++ b/nex-be/src/main/java/com/unisinsight/project/service/impl/NetworkManageServiceImpl.java @@ -1,11 +1,12 @@ package com.unisinsight.project.service.impl; -import cn.hutool.core.exceptions.CheckedUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.unisinsight.project.entity.dao.NetworkManage; import com.unisinsight.project.entity.dto.ApiResponse; +import com.unisinsight.project.entity.dto.Network; +import com.unisinsight.project.entity.dto.NetworkData; import com.unisinsight.project.entity.req.NetworkManagePageReq; import com.unisinsight.project.entity.req.NetworkManageReq; import com.unisinsight.project.entity.res.PageResult; @@ -15,9 +16,10 @@ import com.unisinsight.project.mapper.NetworkManageMapper; import com.unisinsight.project.service.NetworkManageService; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -29,19 +31,20 @@ public class NetworkManageServiceImpl extends ServiceImpl queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("name", networkManageReq.getName()); + queryWrapper.eq("name", networkManageReq.getNetworkName()); if (this.count(queryWrapper) > 0) { - throw new BusinessException("网络名称'" + networkManageReq.getName() + "'已存在,请使用其他名称"); + throw new BusinessException("网络名称'" + networkManageReq.getNetworkName() + "'已存在,请使用其他名称"); } // 调用虚拟机创建存储池 @@ -68,7 +71,7 @@ public class NetworkManageServiceImpl extends ServiceImpl queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("name", networkManageReq.getName()); + queryWrapper.eq("name", networkManageReq.getNetworkName()); if (this.count(queryWrapper) > 0) { - throw new BusinessException("网络名称'" + networkManageReq.getName() + "'已存在,请使用其他名称"); + throw new BusinessException("网络名称'" + networkManageReq.getNetworkName() + "'已存在,请使用其他名称"); } - existing.setName(networkManageReq.getName()); + existing.setNetworkName(networkManageReq.getNetworkName()); } // 调用虚拟机创建存储池 @@ -103,14 +106,14 @@ public class NetworkManageServiceImpl extends ServiceImpl queryWrapper = new QueryWrapper<>(); // 添加查询条件 - if (pageReq.getName() != null && !pageReq.getName().isEmpty()) { - queryWrapper.like("name", pageReq.getName()); + if (pageReq.getNetworkName() != null && !pageReq.getNetworkName().isEmpty()) { + queryWrapper.like("network_name", pageReq.getNetworkName()); } if (pageReq.getType() != null && !pageReq.getType().isEmpty()) { queryWrapper.eq("type", pageReq.getType()); @@ -211,5 +214,41 @@ public class NetworkManageServiceImpl extends ServiceImpl response; + try { + response = client.list(1, 100); + if (!"200".equals(response.getCode())) { + throw new BusinessException("调用外部接口失败: " + response.getMessage()); + } + } catch (Exception e) { + throw new BusinessException("调用外部接口失败: " + e.getMessage()); + } + + List networkList = response.getData().getNetworks(); + if (CollectionUtils.isEmpty(networkList)) { + return; + } + List networkManageList = new ArrayList<>(); + for (Network network : networkList) { + NetworkManage networkManage = new NetworkManage(); + BeanUtils.copyProperties(network, networkManage); + networkManage.setNetworkName(network.getName()); + networkManage.setStatus("active".equals(network.getState()) ? 1 : 2); + networkManageList.add(networkManage); + } + // 清空数据并重新插入 + this.remove(new QueryWrapper<>()); + this.saveOrUpdateBatch(networkManageList); + + + + } } diff --git a/nex-be/src/main/java/com/unisinsight/project/service/impl/StoragePoolServiceImpl.java b/nex-be/src/main/java/com/unisinsight/project/service/impl/StoragePoolServiceImpl.java index f8790a6..f19dfc9 100644 --- a/nex-be/src/main/java/com/unisinsight/project/service/impl/StoragePoolServiceImpl.java +++ b/nex-be/src/main/java/com/unisinsight/project/service/impl/StoragePoolServiceImpl.java @@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.unisinsight.project.entity.dao.NetworkManage; import com.unisinsight.project.entity.dao.StoragePool; -import com.unisinsight.project.entity.dto.ApiResponse; +import com.unisinsight.project.entity.dto.*; import com.unisinsight.project.entity.req.StoragePoolReq; import com.unisinsight.project.entity.res.PageResult; import com.unisinsight.project.exception.BusinessException; @@ -15,9 +15,12 @@ import com.unisinsight.project.service.StoragePoolService; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.Date; +import java.util.List; /** * 存储池服务实现类 @@ -38,9 +41,9 @@ public class StoragePoolServiceImpl extends ServiceImpl queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("name", storagePoolReq.getName()); + queryWrapper.eq("name", storagePoolReq.getPoolName()); if (this.count(queryWrapper) > 0) { - throw new BusinessException("存储池名称'" + storagePoolReq.getName() + "'已存在,请使用其他名称"); + throw new BusinessException("存储池名称'" + storagePoolReq.getPoolName() + "'已存在,请使用其他名称"); } // 调用虚拟机创建存储池 @@ -93,12 +96,12 @@ public class StoragePoolServiceImpl extends ServiceImpl queryWrapper = new QueryWrapper<>(); // 添加查询条件 - if (storagePoolReq.getName() != null && !storagePoolReq.getName().isEmpty()) { - queryWrapper.like("name", storagePoolReq.getName()); + if (storagePoolReq.getPoolName() != null && !storagePoolReq.getPoolName().isEmpty()) { + queryWrapper.like("pool_name", storagePoolReq.getPoolName()); } if (storagePoolReq.getType() != null && !storagePoolReq.getType().isEmpty()) { queryWrapper.eq("type", storagePoolReq.getType()); @@ -176,4 +179,35 @@ public class StoragePoolServiceImpl extends ServiceImpl resultPage = this.page(page, queryWrapper); return PageResult.convertPage(resultPage, StoragePool.class); } + + @Override + public void synchData() { + ApiResponse response; + try { + response = client.listStorage(1, 100); + if (!"200".equals(response.getCode())) { + throw new BusinessException("调用外部接口失败: " + response.getMessage()); + } + } catch (Exception e) { + throw new BusinessException("调用外部接口失败: " + e.getMessage()); + } + + List vmStoragePoolList = response.getData().getItems(); + if (CollectionUtils.isEmpty(vmStoragePoolList)) { + return; + } + List networkManageList = new ArrayList<>(); + for (VmStoragePool vmStoragePool : vmStoragePoolList) { + StoragePool storagePool = new StoragePool(); + BeanUtils.copyProperties(vmStoragePool, storagePool); + storagePool.setPoolName(vmStoragePool.getName()); + storagePool.setStatus("active".equals(vmStoragePool.getState()) ? 1 : 2); + networkManageList.add(storagePool); + } + // 清空数据并重新插入 + this.remove(new QueryWrapper<>()); + this.saveOrUpdateBatch(networkManageList); + + + } }