From 4c0ad24006310accaa9c4f36409aabc40d73eb03 Mon Sep 17 00:00:00 2001 From: sunxiqing <2240398334@qq.com> Date: Tue, 12 Aug 2025 13:40:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(llm):=20=E4=BC=98=E5=8C=96=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E4=B8=8E=E6=A8=A1=E6=9D=BF=E5=85=B3=E8=81=94=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 提取增加和减少模板使用量的方法,避免代码重复 - 在更新应用时,处理关联模板的使用量变化 - 删除应用时,减少关联模板的使用量 -移除未使用的 LambdaUpdateWrapper --- .../application/ApplicationServiceImpl.java | 59 +++++++++++++++---- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/application/ApplicationServiceImpl.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/application/ApplicationServiceImpl.java index 804490bc8..4cfdf893b 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/application/ApplicationServiceImpl.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/application/ApplicationServiceImpl.java @@ -93,11 +93,7 @@ public class ApplicationServiceImpl implements ApplicationService { //prompt使用量+1 Long promptId = application.getPromptId(); if (promptId != null) { - PromptTemplatesRespVO promptTemplates = promptTemplatesService.getPromptTemplates(promptId); - PromptTemplatesDO promptTemplatesDO = new PromptTemplatesDO(); - promptTemplatesDO.setUseCount(promptTemplates.getUseCount() == null ? 1 : promptTemplates.getUseCount() + 1); - promptTemplatesDO.setId(promptTemplates.getId()); - promptTemplatesService.updatePromptTemplatesById(promptTemplatesDO); + increasedUseOfTemplates(promptId); } // 返回 return application.getId(); @@ -125,14 +121,52 @@ public class ApplicationServiceImpl implements ApplicationService { public void updateApplication (ApplicationSaveReqVO updateReqVO) { // 校验存在 validateApplicationExists(updateReqVO.getId()); + ApplicationDO applicationDO = applicationMapper.selectById(updateReqVO.getId()); + // 更新 ApplicationDO updateObj = BeanUtils.toBean(updateReqVO, ApplicationDO.class); if (!StringUtils.isBlank(updateReqVO.getSystemPrompt())){ updateObj.setPrompt(updateReqVO.getSystemPrompt()); } + if (updateObj.getPromptId() != applicationDO.getPromptId()){ + if (updateObj.getPromptId() != null) { + increasedUseOfTemplates(updateObj.getPromptId()); + } + if (applicationDO.getPromptId() != null) { + reducedTemplateUsage(applicationDO.getPromptId()); + } + } applicationMapper.updateById(updateObj); } + /** + * 模板使用量-1 + * @param promptId + */ + private void reducedTemplateUsage(Long promptId) { + if (promptId != null) { + PromptTemplatesRespVO promptTemplates = promptTemplatesService.getPromptTemplates(promptId); + PromptTemplatesDO promptTemplatesDO = new PromptTemplatesDO(); + promptTemplatesDO.setUseCount(promptTemplates.getUseCount() - 1); + promptTemplatesDO.setId(promptTemplates.getId()); + promptTemplatesService.updatePromptTemplatesById(promptTemplatesDO); + } + } + + /** + * 模板使用量+1 + * @param promptId + */ + private void increasedUseOfTemplates(Long promptId) { + if (promptId != null) { + PromptTemplatesRespVO promptTemplates = promptTemplatesService.getPromptTemplates(promptId); + PromptTemplatesDO promptTemplatesDO = new PromptTemplatesDO(); + promptTemplatesDO.setUseCount(promptTemplates.getUseCount() + 1); + promptTemplatesDO.setId(promptTemplates.getId()); + promptTemplatesService.updatePromptTemplatesById(promptTemplatesDO); + } + } + @Override public void deleteApplication (Long id) { // 校验存在 @@ -141,13 +175,18 @@ public class ApplicationServiceImpl implements ApplicationService { // 校验应用使用情况 validateApplicationUse(id); + ApplicationDO applicationDO = applicationMapper.selectById(id); + if (applicationDO.getPromptId() != null){ + reducedTemplateUsage(applicationDO.getPromptId()); + } + // 删除 - LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>(); +// LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>(); +// +// wrapper.eq(ApplicationDO::getId, id) +// .set(ApplicationDO::getDeleted, true); - wrapper.eq(ApplicationDO::getId, id) - .set(ApplicationDO::getDeleted, true); - - applicationMapper.update(null, wrapper); + applicationMapper.deleteById(id); } /**