| | |
| | | import cn.iocoder.yudao.module.ai.dal.mysql.model.AiApiKeyMapper; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.List; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.API_KEY_DISABLE; |
| | | import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.API_KEY_NOT_EXISTS; |
| | | import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * AI API 密钥 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | */ |
| | | @Service |
| | | @Validated |
| | | public class AiApiKeyServiceImpl implements AiApiKeyService { |
| | | |
| | | @Resource |
| | | private AiApiKeyMapper apiKeyMapper; |
| | | private AiApiKeyMapper mapper; |
| | | |
| | | @Override |
| | | public Long createApiKey(AiApiKeySaveReqVO createReqVO) { |
| | | // 插入 |
| | | AiApiKeyDO apiKey = BeanUtils.toBean(createReqVO, AiApiKeyDO.class); |
| | | apiKeyMapper.insert(apiKey); |
| | | // 返回 |
| | | return apiKey.getId(); |
| | | AiApiKeyDO key = BeanUtils.toBean(createReqVO, AiApiKeyDO.class); |
| | | mapper.insert(key); |
| | | return key.getId(); |
| | | } |
| | | |
| | | @Override |
| | | public void updateApiKey(AiApiKeySaveReqVO updateReqVO) { |
| | | // 校验存在 |
| | | validateApiKeyExists(updateReqVO.getId()); |
| | | // 更新 |
| | | AiApiKeyDO updateObj = BeanUtils.toBean(updateReqVO, AiApiKeyDO.class); |
| | | apiKeyMapper.updateById(updateObj); |
| | | mapper.updateById(BeanUtils.toBean(updateReqVO, AiApiKeyDO.class)); |
| | | } |
| | | |
| | | @Override |
| | | public void deleteApiKey(Long id) { |
| | | // 校验存在 |
| | | validateApiKeyExists(id); |
| | | // 删除 |
| | | apiKeyMapper.deleteById(id); |
| | | } |
| | | |
| | | private AiApiKeyDO validateApiKeyExists(Long id) { |
| | | AiApiKeyDO apiKey = apiKeyMapper.selectById(id); |
| | | if (apiKey == null) { |
| | | throw exception(API_KEY_NOT_EXISTS); |
| | | } |
| | | return apiKey; |
| | | mapper.deleteById(id); |
| | | } |
| | | |
| | | @Override |
| | | public AiApiKeyDO getApiKey(Long id) { |
| | | return apiKeyMapper.selectById(id); |
| | | return mapper.selectById(id); |
| | | } |
| | | |
| | | @Override |
| | | public AiApiKeyDO validateApiKey(Long id) { |
| | | AiApiKeyDO apiKey = validateApiKeyExists(id); |
| | | if (CommonStatusEnum.isDisable(apiKey.getStatus())) { |
| | | throw exception(API_KEY_DISABLE); |
| | | } |
| | | return apiKey; |
| | | } |
| | | |
| | | @Override |
| | | public PageResult<AiApiKeyDO> getApiKeyPage(AiApiKeyPageReqVO pageReqVO) { |
| | | return apiKeyMapper.selectPage(pageReqVO); |
| | | } |
| | | |
| | | @Override |
| | | public List<AiApiKeyDO> getApiKeyList() { |
| | | return apiKeyMapper.selectList(); |
| | | AiApiKeyDO key = mapper.selectById(id); |
| | | if (key == null) throw exception(API_KEY_NOT_EXISTS); |
| | | if (CommonStatusEnum.isDisable(key.getStatus())) throw exception(API_KEY_DISABLE); |
| | | return key; |
| | | } |
| | | |
| | | @Override |
| | | public AiApiKeyDO getRequiredDefaultApiKey(String platform, Integer status) { |
| | | AiApiKeyDO apiKey = apiKeyMapper.selectFirstByPlatformAndStatus(platform, status); |
| | | if (apiKey == null) { |
| | | throw exception(API_KEY_NOT_EXISTS); |
| | | return mapper.selectFirstByPlatformAndStatus(platform, status); |
| | | } |
| | | return apiKey; |
| | | |
| | | @Override |
| | | public PageResult<AiApiKeyDO> getApiKeyPage(AiApiKeyPageReqVO pageReqVO) { |
| | | return mapper.selectPage(pageReqVO); |
| | | } |
| | | |
| | | private void validateApiKeyExists(Long id) { |
| | | if (mapper.selectById(id) == null) throw exception(API_KEY_NOT_EXISTS); |
| | | } |
| | | |
| | | } |