[update] 应用中心 删除应用时检查Prompt模版是否有使用

This commit is contained in:
Liuyang 2025-01-20 10:24:21 +08:00
parent 5685dd0faf
commit 4d1eab17a0
5 changed files with 88 additions and 0 deletions

View File

@ -38,4 +38,12 @@ public interface PromptTemplatesMapper extends BaseMapperX<PromptTemplatesDO> {
* @return 模版
*/
List<PromptTemplatesDO> getPromptTemplateByLabelId (@Param("labelId") Long labelId);
/**
* 根据 应用id 获取 模版
*
* @param applicationId 应用id
* @return 模版
*/
List<PromptTemplatesDO> getPromptTemplateByAppId (@Param("applicationId") Long applicationId);
}

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.llm.service.application;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
@ -120,13 +121,48 @@ public class ApplicationServiceImpl implements ApplicationService {
public void deleteApplication(Long id) {
// 校验存在
validateApplicationExists(id);
// 校验应用使用情况
validateApplicationUse(id);
// 删除
LambdaUpdateWrapper<ApplicationDO> wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(ApplicationDO::getId, id)
.set(ApplicationDO::getDeleted,true);
applicationMapper.update(null, wrapper);
}
/**
* 校验应用使用情况
* @param id 应用id
*/
private void validateApplicationUse (Long id) {
// 获取应用信息用来返回错误信息
ApplicationDO applicationDO = applicationMapper.selectById(id);
String appName = applicationDO.getAppName();
// 检查标签是否在 Prompt 模版中有使用
validateAppUsesInPromptTemplate(id, appName);
}
/**
* 检查标签是否在 Prompt 模版 有使用
*
* @param id 应用id
* @param name 应用名称
*/
private void validateAppUsesInPromptTemplate (Long id, String name) {
Map<Long, String> template = promptTemplatesService.getPromptTemplateByAppId(id);
if (CollectionUtils.isNotEmpty(template)) {
String msg = String.format("应用【%s】在 Prompt模版 %s 有使用请先删除Prompt模版中的应用", name, template.values());
throw exception(new ErrorCode(10006_001, msg));
}
}
private void validateApplicationExists(Long id) {
if (applicationMapper.selectById(id) == null) {
throw exception(APPLICATION_NOT_EXISTS);

View File

@ -46,4 +46,12 @@ public interface PromptTemplatesService {
* @return 模版
*/
Map<Long,String> getPromptTemplateByLabelId (Long labelId);
/**
* 根据 应用id 获取 模版
*
* @param applicationId 应用id
* @return 模版
*/
Map<Long,String> getPromptTemplateByAppId (Long applicationId);
}

View File

@ -450,6 +450,7 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
if (labelId == null) {
return Collections.emptyMap();
}
List<PromptTemplatesDO> templatesDO = this.promptTemplatesMapper.getPromptTemplateByLabelId(labelId);
if (CollectionUtils.isEmpty(templatesDO)) {
return Collections.emptyMap();
@ -457,4 +458,24 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
return templatesDO.stream().collect(Collectors.toMap(PromptTemplatesDO::getId, PromptTemplatesDO::getName));
}
/**
* 根据 应用id 获取 模版
*
* @param applicationId 应用id
* @return 模版
*/
@Override
public Map<Long, String> getPromptTemplateByAppId (Long applicationId) {
if (applicationId == null) {
return Collections.emptyMap();
}
List<PromptTemplatesDO> templatesDO = this.promptTemplatesMapper.getPromptTemplateByAppId(applicationId);
if (CollectionUtils.isEmpty(templatesDO)) {
return Collections.emptyMap();
}
return templatesDO.stream().collect(Collectors.toMap(PromptTemplatesDO::getId, PromptTemplatesDO::getName));
}
}

View File

@ -20,4 +20,19 @@
AND lpt.deleted = FALSE
AND lptt.deleted = FALSE
</select>
<!-- 根据 应用id 获取 模版 -->
<select id="getPromptTemplateByAppId"
resultType="cn.iocoder.yudao.module.llm.dal.dataobject.prompttemplates.PromptTemplatesDO">
SELECT
lpt.id AS id,
lpt.name AS name
FROM
llm_prompt_templates lpt
LEFT JOIN llm_prompt_templates_applications lpta ON lpt.id = lpta.prompt_template_id
WHERE
lpta.application_id = #{applicationId}
AND lpt.deleted = FALSE
AND lpta.deleted = FALSE
</select>
</mapper>