mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 00:23:53 +08:00
添加编程事务,保证数据的统一,提取公共方法
--AgentVoicePrintServiceImpl.java 1.为保存,修复,删除声纹添加事务 2.提取发送注销声纹的请求为公共方法
This commit is contained in:
+92
-37
@@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import org.springframework.core.io.ByteArrayResource;
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@@ -39,6 +40,8 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
|
|||||||
private final AgentChatAudioService agentChatAudioService;
|
private final AgentChatAudioService agentChatAudioService;
|
||||||
private final RestTemplate restTemplate;
|
private final RestTemplate restTemplate;
|
||||||
private final SysParamsService sysParamsService;
|
private final SysParamsService sysParamsService;
|
||||||
|
// Springboot提供的编程事务类
|
||||||
|
private final TransactionTemplate transactionTemplate;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -48,44 +51,49 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
|
|||||||
String audioId = dto.getAudioId();
|
String audioId = dto.getAudioId();
|
||||||
ByteArrayResource resource = getVoicePrintAudioWAV(audioId);
|
ByteArrayResource resource = getVoicePrintAudioWAV(audioId);
|
||||||
|
|
||||||
// 保存声纹信息
|
|
||||||
AgentVoicePrintEntity entity = ConvertUtils.sourceToTarget(dto, AgentVoicePrintEntity.class);
|
AgentVoicePrintEntity entity = ConvertUtils.sourceToTarget(dto, AgentVoicePrintEntity.class);
|
||||||
int insert = baseMapper.insert(entity);
|
// 开启事务
|
||||||
if(insert != 1){
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
return false;
|
try {
|
||||||
}
|
// 保存声纹信息
|
||||||
registerVoicePrint(entity.getId(), resource);
|
int row = baseMapper.insert(entity);
|
||||||
return true;
|
// 插入一条数据,影响的数据不等于1说明出现了,保存问题回滚
|
||||||
|
if (row != 1) {
|
||||||
|
status.setRollbackOnly(); // 标记事务回滚
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 发送注册声纹请求
|
||||||
|
registerVoicePrint(entity.getId(), resource);
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
status.setRollbackOnly(); // 标记事务回滚
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean delete(String voicePrintId) {
|
public boolean delete(String voicePrintId) {
|
||||||
int insert = baseMapper.deleteById(voicePrintId);
|
// 开启事务
|
||||||
if(insert != 1){
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
throw new RenException("声纹删除失败");
|
try {
|
||||||
}
|
// 删除声纹
|
||||||
URI uri = getVoicePrintURI();
|
int row = baseMapper.deleteById(voicePrintId);
|
||||||
String baseUrl = getBaseUrl(uri);
|
if(row != 1){
|
||||||
String requestUrl = baseUrl + "/voiceprint/" + voicePrintId;
|
status.setRollbackOnly(); // 标记事务回滚
|
||||||
// 创建请求头
|
return false;
|
||||||
HttpHeaders headers = new HttpHeaders();
|
}
|
||||||
headers.set("Authorization", getAuthorization(uri));
|
cancelVoicePrint(voicePrintId);
|
||||||
// 创建请求体
|
return true;
|
||||||
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(headers);
|
} catch (Exception e) {
|
||||||
|
status.setRollbackOnly(); // 标记事务回滚
|
||||||
// 发送 POST 请求
|
throw e;
|
||||||
ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.DELETE, requestEntity, String.class);
|
}
|
||||||
if (response.getStatusCode() != HttpStatus.OK) {
|
}));
|
||||||
throw new RenException("声纹保存失败");
|
|
||||||
}
|
|
||||||
// 检查响应内容
|
|
||||||
String responseBody = response.getBody();
|
|
||||||
if(responseBody == null || !responseBody.contains("true")){
|
|
||||||
throw new RenException("声纹保存失败");
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<AgentVoicePrintVO> list(String agentId) {
|
public List<AgentVoicePrintVO> list(String agentId) {
|
||||||
List<AgentVoicePrintEntity> list = baseMapper.selectList(new LambdaQueryWrapper<AgentVoicePrintEntity>()
|
List<AgentVoicePrintEntity> list = baseMapper.selectList(new LambdaQueryWrapper<AgentVoicePrintEntity>()
|
||||||
@@ -101,14 +109,35 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
|
|||||||
public boolean update(AgentVoicePrintUpdateDTO dto) {
|
public boolean update(AgentVoicePrintUpdateDTO dto) {
|
||||||
// 获取音频Id
|
// 获取音频Id
|
||||||
String audioId = dto.getAudioId();
|
String audioId = dto.getAudioId();
|
||||||
// 如果有新的音频,则注册新的声纹
|
// 如果有新的音频
|
||||||
|
ByteArrayResource resource;
|
||||||
if (!StringUtils.isEmpty(audioId)) {
|
if (!StringUtils.isEmpty(audioId)) {
|
||||||
ByteArrayResource resource = getVoicePrintAudioWAV(audioId);
|
resource = getVoicePrintAudioWAV(audioId);
|
||||||
registerVoicePrint(dto.getId(),resource);
|
} else {
|
||||||
|
resource = null;
|
||||||
}
|
}
|
||||||
AgentVoicePrintEntity entity = ConvertUtils.sourceToTarget(dto, AgentVoicePrintEntity.class);
|
// 开启事务
|
||||||
baseMapper.updateById(entity);
|
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
|
||||||
return true;
|
try {
|
||||||
|
AgentVoicePrintEntity entity = ConvertUtils.sourceToTarget(dto, AgentVoicePrintEntity.class);
|
||||||
|
int row = baseMapper.updateById(entity);
|
||||||
|
if (row != 1){
|
||||||
|
status.setRollbackOnly(); // 标记事务回滚
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(resource != null){
|
||||||
|
String id = entity.getId();
|
||||||
|
// 先注销之前这个声纹id上的声纹向量
|
||||||
|
cancelVoicePrint(id);
|
||||||
|
// 发送注册声纹请求
|
||||||
|
registerVoicePrint(id, resource);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
status.setRollbackOnly(); // 标记事务回滚
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -207,4 +236,30 @@ public class AgentVoicePrintServiceImpl extends ServiceImpl<AgentVoicePrintDao,
|
|||||||
throw new RenException("声纹保存失败");
|
throw new RenException("声纹保存失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送注销声纹的请求
|
||||||
|
* @param voicePrintId 声纹id
|
||||||
|
*/
|
||||||
|
private void cancelVoicePrint(String voicePrintId) {
|
||||||
|
URI uri = getVoicePrintURI();
|
||||||
|
String baseUrl = getBaseUrl(uri);
|
||||||
|
String requestUrl = baseUrl + "/voiceprint/" + voicePrintId;
|
||||||
|
// 创建请求头
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.set("Authorization", getAuthorization(uri));
|
||||||
|
// 创建请求体
|
||||||
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(headers);
|
||||||
|
|
||||||
|
// 发送 POST 请求
|
||||||
|
ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.DELETE, requestEntity, String.class);
|
||||||
|
if (response.getStatusCode() != HttpStatus.OK) {
|
||||||
|
throw new RenException("声纹保存失败");
|
||||||
|
}
|
||||||
|
// 检查响应内容
|
||||||
|
String responseBody = response.getBody();
|
||||||
|
if(responseBody == null || !responseBody.contains("true")){
|
||||||
|
throw new RenException("声纹保存失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user