[update] 标签管理 使用状态从启用变为停用时检查应用中心是否使用该标签

This commit is contained in:
Liuyang 2025-01-16 10:12:06 +08:00
parent 8e54e83794
commit e9687b4f81
3 changed files with 86 additions and 0 deletions

View File

@ -67,4 +67,12 @@ public interface ApplicationService {
* @return Map<Long, String>
*/
Map<Long, String> getApplicatioMap ();
/**
* 根据 标签id 获取 应用
*
* @param labelId 标签id
* @return 应用
*/
Map<Long,String> getApplicationByLabelId (Long labelId);
}

View File

@ -229,4 +229,25 @@ public class ApplicationServiceImpl implements ApplicationService {
.collect(Collectors.toMap(ApplicationDO::getId, ApplicationDO::getAppName));
}
/**
* 根据 标签id 获取 应用
*
* @param labelId 标签id
* @return 应用
*/
@Override
public Map<Long,String> getApplicationByLabelId (Long labelId) {
if (labelId == null){
return Collections.emptyMap();
}
List<ApplicationDO> applications = this.applicationMapper.selectList(new LambdaQueryWrapper<ApplicationDO>()
.select(ApplicationDO::getId, ApplicationDO::getAppName)
.eq(ApplicationDO::getAppLabel, labelId));
if (CollectionUtils.isEmpty(applications)){
return Collections.emptyMap();
}
return applications.stream().collect(Collectors.toMap(ApplicationDO::getId, ApplicationDO::getAppName));
}
}

View File

@ -1,13 +1,16 @@
package cn.iocoder.yudao.module.llm.service.label;
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.module.llm.controller.admin.label.vo.LabelPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.label.vo.LabelSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.label.LabelDO;
import cn.iocoder.yudao.module.llm.dal.mysql.label.LabelMapper;
import cn.iocoder.yudao.module.llm.service.application.ApplicationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -34,6 +37,9 @@ public class LabelServiceImpl implements LabelService {
@Resource
private LabelMapper labelMapper;
@Resource
private ApplicationService applicationService;
@Override
public Long createLabel (LabelSaveReqVO createReqVO) {
validateLabelNameExists(createReqVO);
@ -49,11 +55,62 @@ public class LabelServiceImpl implements LabelService {
// 校验存在
validateLabelExists(updateReqVO.getId());
validateLabelNameExists(updateReqVO);
// 校验是否启用状态从启用变为停用
checkIfLabelEnabledToDisabled(updateReqVO);
// 更新
LabelDO updateObj = BeanUtils.toBean(updateReqVO, LabelDO.class);
labelMapper.updateById(updateObj);
}
/**
* 检查是否启用状态从启用变为停用
*
* @param updateRequestVO 标签修改
*/
private void checkIfLabelEnabledToDisabled (LabelSaveReqVO updateRequestVO) {
Long id = updateRequestVO.getId();
// 获取标签信息
LabelDO labelDO = labelMapper.selectById(id);
// 检查标签状态是否从启用变为停用
if (labelDO.getStatus() == 0) {
if (updateRequestVO.getStatus() == 1) {
// 验证标签使用情况
validateLabelUse(labelDO);
}
}
}
/**
* 校验标签使用情况
*
* @param label 标签对象
*/
private void validateLabelUse (LabelDO label) {
Long id = label.getId();
String labelName = label.getLabelName();
// 检查标签是否在 应用中心 有使用
validateLabelUsesInApplicationCenter(id, labelName);
// 检查标签是否在 Prompt 模版中有使用
// validateLabelUsesInPromptTemplate(id);
// 检查标签是否在 Prompt 模版备份中有使用
// validateLabelUsesInPromptTemplateBackup(id);
}
/**
* 检查标签是否在 应用中心 有使用
*
* @param id 标签id
* @param name 标签名称
*/
private void validateLabelUsesInApplicationCenter (Long id, String name) {
Map<Long, String> application = applicationService.getApplicationByLabelId(id);
if (CollectionUtils.isNotEmpty(application)) {
String msg = String.format("标签【%s】在应用中心 %s 有使用,请先删除应用中心中的标签", name, application.values());
throw exception(new ErrorCode(10004_001, msg));
}
}
@Override
public void deleteLabel (Long id) {
// 校验存在