From 799174406e1f4ae494795e0a7b51ed5fe2def899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=AD=90=E9=BB=98?= <925456043@qq.com> Date: Tue, 24 Mar 2026 18:38:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E5=90=8E=E7=9A=84=E5=88=86=E7=B1=BB=E5=85=B3?= =?UTF-8?q?=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 插件保存接口返回实体以便前端拿到真实插件 ID - 分类关联更新改为按差异增删并补充事务保护 - 新建插件后缺失 ID 时明确抛出错误 --- .../admin/controller/ai/PluginController.java | 2 +- .../service/PluginCategoryMappingService.java | 3 +- .../easyflow/ai/service/PluginService.java | 2 +- .../PluginCategoryMappingServiceImpl.java | 74 ++++++++++--------- .../ai/service/impl/PluginServiceImpl.java | 4 +- .../src/views/ai/plugin/AddPluginModal.vue | 10 +-- 6 files changed, 49 insertions(+), 46 deletions(-) diff --git a/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/ai/PluginController.java b/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/ai/PluginController.java index 3f39cc4..f81617e 100644 --- a/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/ai/PluginController.java +++ b/easyflow-api/easyflow-api-admin/src/main/java/tech/easyflow/admin/controller/ai/PluginController.java @@ -40,7 +40,7 @@ public class PluginController extends BaseCurdController @PostMapping("/plugin/save") @SaCheckPermission("/api/v1/plugin/save") - public Result savePlugin(@JsonBody Plugin plugin){ + public Result savePlugin(@JsonBody Plugin plugin){ return Result.ok(pluginService.savePlugin(plugin)); } diff --git a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/PluginCategoryMappingService.java b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/PluginCategoryMappingService.java index da83456..01d7841 100644 --- a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/PluginCategoryMappingService.java +++ b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/PluginCategoryMappingService.java @@ -5,7 +5,6 @@ import tech.easyflow.ai.entity.PluginCategory; import tech.easyflow.ai.entity.PluginCategoryMapping; import java.math.BigInteger; -import java.util.ArrayList; import java.util.List; /** @@ -16,7 +15,7 @@ import java.util.List; */ public interface PluginCategoryMappingService extends IService { - boolean updateRelation(BigInteger pluginId, ArrayList categoryIds); + boolean updateRelation(BigInteger pluginId, List categoryIds); List getPluginCategories(BigInteger pluginId); } diff --git a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/PluginService.java b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/PluginService.java index 0a5bc13..5442e58 100644 --- a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/PluginService.java +++ b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/PluginService.java @@ -14,7 +14,7 @@ import java.util.List; */ public interface PluginService extends IService { - boolean savePlugin(Plugin plugin); + Plugin savePlugin(Plugin plugin); boolean removePlugin(String id); diff --git a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/PluginCategoryMappingServiceImpl.java b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/PluginCategoryMappingServiceImpl.java index 5c949ef..0aeb31a 100644 --- a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/PluginCategoryMappingServiceImpl.java +++ b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/PluginCategoryMappingServiceImpl.java @@ -2,18 +2,23 @@ package tech.easyflow.ai.service.impl; import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.spring.service.impl.ServiceImpl; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import tech.easyflow.ai.entity.PluginCategory; import tech.easyflow.ai.entity.PluginCategoryMapping; import tech.easyflow.ai.mapper.PluginCategoryMapper; import tech.easyflow.ai.mapper.PluginCategoryMappingMapper; import tech.easyflow.ai.service.PluginCategoryMappingService; -import org.springframework.stereotype.Service; import tech.easyflow.common.web.exceptions.BusinessException; import javax.annotation.Resource; import java.math.BigInteger; import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; /** * 服务层实现。 @@ -31,45 +36,46 @@ public class PluginCategoryMappingServiceImpl extends ServiceImpl categoryIds) { - if (categoryIds == null){ - QueryWrapper queryWrapper = QueryWrapper.create().select("*") - .from("tb_plugin_category_mapping") - .where("plugin_id = ?", pluginId); - int delete = relationMapper.deleteByQuery(queryWrapper); - if (delete <= 0){ - throw new BusinessException("删除失败"); - } - return true; + @Transactional + public boolean updateRelation(BigInteger pluginId, List categoryIds) { + if (pluginId == null) { + throw new BusinessException("插件ID不能为空"); } - for (BigInteger categoryId : categoryIds) { - QueryWrapper queryWrapper = QueryWrapper.create().select("*") + + List targetCategoryIds = categoryIds == null + ? Collections.emptyList() + : categoryIds.stream() + .filter(java.util.Objects::nonNull) + .distinct() + .collect(Collectors.toList()); + + QueryWrapper currentRelationQuery = QueryWrapper.create().select("category_id") + .from("tb_plugin_category_mapping") + .where("plugin_id = ?", pluginId); + List currentCategoryIds = relationMapper.selectListByQueryAs(currentRelationQuery, BigInteger.class); + Set currentCategoryIdSet = new LinkedHashSet<>(currentCategoryIds); + Set targetCategoryIdSet = new LinkedHashSet<>(targetCategoryIds); + + Set categoryIdsToDelete = new LinkedHashSet<>(currentCategoryIdSet); + categoryIdsToDelete.removeAll(targetCategoryIdSet); + if (!categoryIdsToDelete.isEmpty()) { + QueryWrapper deleteQuery = QueryWrapper.create() .from("tb_plugin_category_mapping") - .where("plugin_id = ?", pluginId) - .where("category_id = ?", categoryId); - PluginCategoryMapping selectedOneByQuery = relationMapper.selectOneByQuery(queryWrapper); + .where("plugin_id = ?", pluginId) + .in("category_id", new ArrayList<>(categoryIdsToDelete)); + relationMapper.deleteByQuery(deleteQuery); + } + + Set categoryIdsToInsert = new LinkedHashSet<>(targetCategoryIdSet); + categoryIdsToInsert.removeAll(currentCategoryIdSet); + for (BigInteger categoryId : categoryIdsToInsert) { PluginCategoryMapping pluginCategoryMapping = new PluginCategoryMapping(); pluginCategoryMapping.setCategoryId(categoryId); pluginCategoryMapping.setPluginId(pluginId); - if (selectedOneByQuery == null) { - int insert = relationMapper.insert(pluginCategoryMapping); - if (insert <= 0) { - throw new BusinessException("新增失败"); - } - } else { - QueryWrapper queryWrapperUpdate = QueryWrapper.create().select("*") - .from("tb_plugin_category_mapping") - .where("plugin_id = ?", pluginId); - PluginCategoryMapping selectedOne = relationMapper.selectOneByQuery(queryWrapper); - if (selectedOne != null){ - continue; - } - int update = relationMapper.updateByQuery(pluginCategoryMapping, queryWrapperUpdate); - if (update <= 0){ - throw new BusinessException("更新失败"); - } + int insert = relationMapper.insert(pluginCategoryMapping); + if (insert <= 0) { + throw new BusinessException("新增分类关联失败"); } - } return true; } diff --git a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/PluginServiceImpl.java b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/PluginServiceImpl.java index cc705f2..faa8aaa 100644 --- a/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/PluginServiceImpl.java +++ b/easyflow-modules/easyflow-module-ai/src/main/java/tech/easyflow/ai/service/impl/PluginServiceImpl.java @@ -48,13 +48,13 @@ public class PluginServiceImpl extends ServiceImpl impleme private PluginItemService pluginItemService; @Override - public boolean savePlugin(Plugin plugin) { + public Plugin savePlugin(Plugin plugin) { plugin.setCreated(new Date()); int insert = pluginMapper.insert(plugin); if (insert <= 0) { throw new BusinessException("保存失败"); } - return true; + return plugin; } @Override diff --git a/easyflow-ui-admin/app/src/views/ai/plugin/AddPluginModal.vue b/easyflow-ui-admin/app/src/views/ai/plugin/AddPluginModal.vue index 22cde51..e205f2b 100644 --- a/easyflow-ui-admin/app/src/views/ai/plugin/AddPluginModal.vue +++ b/easyflow-ui-admin/app/src/views/ai/plugin/AddPluginModal.vue @@ -145,12 +145,10 @@ function save() { }) .then(async (res) => { if (res.errorCode === 0) { - const pluginId = - res.data?.id || - res.data || - plainEntity.id || - entity.value.id || - ''; + const pluginId = res.data?.id || plainEntity.id || entity.value.id; + if (!pluginId) { + throw new Error('插件保存成功,但未返回插件ID'); + } await syncPluginCategories(pluginId, categoryIds); dialogVisible.value = false; ElMessage.success($t('message.saveOkMessage'));