refactor(llm): 优化应用与模板关联逻辑

- 提取增加和减少模板使用量的方法,避免代码重复
- 在更新应用时,处理关联模板的使用量变化
- 删除应用时,减少关联模板的使用量
-移除未使用的 LambdaUpdateWrapper
This commit is contained in:
sunxiqing 2025-08-12 13:40:16 +08:00
parent f460ddc38b
commit 4c0ad24006

View File

@ -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<ApplicationDO> wrapper = new LambdaUpdateWrapper<>();
// LambdaUpdateWrapper<ApplicationDO> 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);
}
/**