diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/model/ModelsController.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/model/ModelsController.java index c754c673..210aefc4 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/model/ModelsController.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/model/ModelsController.java @@ -123,7 +123,7 @@ public class ModelsController extends BaseController { */ @PostMapping @ApiOperation("添加模型") - public GenericsAjaxResult add(@RequestBody Models models) { + public GenericsAjaxResult add(@RequestBody Models models) throws Exception { return genericsSuccess(this.modelsService.insert(models)); } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/Models.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/Models.java index 87eadc37..f00f189d 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/Models.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/Models.java @@ -2,6 +2,7 @@ package com.ruoyi.platform.domain; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.annotation.JsonNaming; +import com.ruoyi.platform.annotations.CheckDuplicate; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -22,6 +23,7 @@ public class Models implements Serializable { private Integer id; @ApiModelProperty(value = "模型名称") + @CheckDuplicate private String name; @ApiModelProperty(value = "模型描述") diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/DatasetVersionDao.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/DatasetVersionDao.java index 19e21651..841f671b 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/DatasetVersionDao.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/DatasetVersionDao.java @@ -84,7 +84,8 @@ public interface DatasetVersionDao { List queryByDatasetId(Integer datasetId); - DatasetVersion queryByDatasetVersion(@Param("datasetVersion") DatasetVersion datasetVersion); + DatasetVersion + queryByDatasetVersion(@Param("datasetVersion") DatasetVersion datasetVersion); List queryAllByDatasetVersion(@Param("datasetId") Integer datasetId, @Param("version") String version); diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/ModelsDao.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/ModelsDao.java index 8919b77a..c787e71a 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/ModelsDao.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/ModelsDao.java @@ -1,10 +1,12 @@ package com.ruoyi.platform.mapper; +import com.ruoyi.platform.domain.Dataset; import com.ruoyi.platform.domain.Models; import org.apache.ibatis.annotations.Param; import org.springframework.data.domain.Pageable; import java.util.List; +import java.util.Map; /** * (Models)表数据库访问层 @@ -22,6 +24,8 @@ public interface ModelsDao { */ Models queryById(Integer id); + Models findByName(@Param("name") String name); + /** * 查询指定行数据 * diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ModelsService.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ModelsService.java index c2522728..c4e5b920 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ModelsService.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ModelsService.java @@ -2,6 +2,7 @@ package com.ruoyi.platform.service; +import com.ruoyi.platform.domain.Dataset; import com.ruoyi.platform.domain.Models; import com.ruoyi.platform.domain.ModelsVersion; import com.ruoyi.platform.vo.ModelsVo; @@ -45,7 +46,7 @@ public interface ModelsService { * @param models 实例对象 * @return 实例对象 */ - Models insert(Models models); + Models insert(Models models) throws Exception; /** * 修改数据 @@ -81,5 +82,7 @@ public interface ModelsService { String readFileContent(Integer modelsId, String version) throws Exception; + public void checkDeclaredName(Models insert) throws Exception; + List> exportModels(String path, String uuid) throws Exception; } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java index 76d510f5..336776ea 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java @@ -221,7 +221,7 @@ public class ImageServiceImpl implements ImageService { if (imageVersionInsert == null) { throw new Exception("新增镜像版本失败"); } - // 使用CompletableFuture异步执行不同的镜像构建逻辑 + // 使用CompletableFuture异步执行不同的镜像构建逻辑,在构建镜像的同时,更新数据库中的镜像版本状态 CompletableFuture.supplyAsync(() -> { Map resultMap = new HashMap<>(); try { @@ -281,6 +281,7 @@ public class ImageServiceImpl implements ImageService { String pushCmd = "docker push " + imageUrl; String sizeCmd = "docker inspect --format='{{.Size}}' " + imageUrl; String s = k8sClientUtil.executeCommand(pod, tagCmd); + if (StringUtils.isNotEmpty(k8sClientUtil.executeCommand(pod, pushCmd))){ resultMap.put("url", imageUrl); //得到镜像文件大小 diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java index 14b1e470..b91fb890 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ModelsServiceImpl.java @@ -1,7 +1,9 @@ package com.ruoyi.platform.service.impl; import com.ruoyi.common.security.utils.SecurityUtils; +import com.ruoyi.platform.annotations.CheckDuplicate; import com.ruoyi.platform.domain.AssetIcon; +import com.ruoyi.platform.domain.Dataset; import com.ruoyi.platform.domain.Models; import com.ruoyi.platform.domain.ModelsVersion; import com.ruoyi.platform.mapper.ModelsDao; @@ -34,6 +36,7 @@ import javax.annotation.Resource; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; +import java.lang.reflect.Field; import java.net.URLEncoder; import java.util.*; import java.util.stream.Collectors; @@ -116,8 +119,9 @@ public class ModelsServiceImpl implements ModelsService { * @return 实例对象 */ @Override - public Models insert(Models models) { + public Models insert(Models models) throws Exception { LoginUser loginUser = SecurityUtils.getLoginUser(); + checkDeclaredName(models); models.setCreateBy(loginUser.getUsername()); models.setUpdateBy(loginUser.getUsername()); models.setUpdateTime(new Date()); @@ -173,8 +177,9 @@ public class ModelsServiceImpl implements ModelsService { if (!(StringUtils.equals(username,"admin") || StringUtils.equals(username,createdBy))){ throw new Exception("无权限删除该模型"); } - if (modelsVersionService.queryByModelsId(id).size()>0){ - throw new Exception("请先删除该镜像下的版本文件"); + //判断是否有版本文件 + if (!modelsVersionService.queryByModelsId(id).isEmpty()){ + throw new Exception("请先删除该模型下的版本文件"); } models.setState(0); return this.modelsDao.update(models)>0?"删除成功":"删除失败"; @@ -427,6 +432,31 @@ public class ModelsServiceImpl implements ModelsService { } + @Override + public void checkDeclaredName(Models insert) throws Exception { + Models existingModel = modelsDao.findByName(insert.getName()); + if (existingModel != null) { + // Check if the found models is not the same as the one being inserted + // This is important if you are using this method for both insert and update operations + // You may need an identifier check here, e.g., if 'insert' has an ID and it's the same as 'existingDataset' + if (insert.getId() != null && insert.getId().equals(existingModel.getId())) { + // This is the same dataset, no duplicate name issue for update operation + return; + } + + // Now we know there's another dataset with the same name + Field[] fields = Models.class.getDeclaredFields(); + for (Field field : fields) { + field.setAccessible(true); // Make private fields accessible + if ("name".equals(field.getName()) && field.isAnnotationPresent(CheckDuplicate.class)) { + // If the field is 'name' and is marked with CheckDuplicate annotation + CheckDuplicate annotation = field.getAnnotation(CheckDuplicate.class); + throw new Exception("重复的模型名称: " + insert.getName() + ". " + annotation.message()); + } + } + } + } + @Override public List> exportModels(String path, String uuid) throws Exception { List> results = new ArrayList<>(); diff --git a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/DatasetDaoMapper.xml b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/DatasetDaoMapper.xml index d4bae103..d97f1a3d 100644 --- a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/DatasetDaoMapper.xml +++ b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/DatasetDaoMapper.xml @@ -31,6 +31,7 @@ from dataset where name = #{name} and state = 1 limit 1 + + + +