|
|
@@ -1,15 +1,13 @@
|
|
|
package com.njuzr.eaibackend.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
-import com.njuzr.eaibackend.config.DoubaoConfig;
|
|
|
-import com.njuzr.eaibackend.config.IseClientFactory;
|
|
|
-import com.njuzr.eaibackend.config.OssConfig;
|
|
|
import com.njuzr.eaibackend.exception.MyException;
|
|
|
import com.njuzr.eaibackend.mapper.SpeakingAIDialogueMapper;
|
|
|
import com.njuzr.eaibackend.po.AIEntry;
|
|
|
import com.njuzr.eaibackend.po.SpeakingAIDialogue;
|
|
|
import com.njuzr.eaibackend.service.AIRequestService;
|
|
|
import com.njuzr.eaibackend.service.DoubaoService;
|
|
|
+import com.njuzr.eaibackend.utils.OssUtil;
|
|
|
import com.njuzr.eaibackend.vo.AIReplyVo;
|
|
|
import com.njuzr.eaibackend.vo.RestrictDataVo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -24,6 +22,7 @@ import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
@@ -53,6 +52,10 @@ public class DoubaoServiceImpl implements DoubaoService {
|
|
|
@Autowired
|
|
|
AIRequestService aiRequestService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ OssUtil ossUtil;
|
|
|
+
|
|
|
+
|
|
|
@Autowired
|
|
|
@Qualifier("doubaoTaskExecutor") // 指定 Bean 名称
|
|
|
private ThreadPoolTaskExecutor doubaoTaskExecutor; // 注入线程池
|
|
|
@@ -317,6 +320,42 @@ public class DoubaoServiceImpl implements DoubaoService {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
+ public String saveSigment(Long userId, Long assignmentId, MultipartFile file, Double duration) {
|
|
|
+ // 1. 立即返回响应
|
|
|
+ String immediateResponse = "请求已接收,系统正在处理您的文件";
|
|
|
+ // 2. 在后台异步执行上传和保存操作
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ try {
|
|
|
+ // 上传文件
|
|
|
+ String filePath = "uploads/" + file.getOriginalFilename();
|
|
|
+ String url = ossUtil.uploadFile(file.getInputStream(), filePath);
|
|
|
+ log.info("文件上传成功,URL: {}", url);
|
|
|
+
|
|
|
+ // 保存到数据库
|
|
|
+ Query query = new Query(Criteria.where("userId").is(userId)
|
|
|
+ .and("assignmentId").is(assignmentId));
|
|
|
+ SpeakingAIDialogue dialogue = mongoTemplate.findOne(query, SpeakingAIDialogue.class);
|
|
|
+
|
|
|
+ if (dialogue == null) {
|
|
|
+ log.error("不存在相应记录 - userId: {}, assignmentId: {}", userId, assignmentId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Update update = new Update()
|
|
|
+ .push("audioSigment", url)
|
|
|
+ .push("audioDuration", duration);
|
|
|
+ mongoTemplate.updateFirst(query, update, SpeakingAIDialogue.class);
|
|
|
+
|
|
|
+ log.info("后台处理完成 - userId: {}, assignmentId: {}, URL: {}", userId, assignmentId, url);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("后台处理失败 - userId: {}, assignmentId: {}", userId, assignmentId, e);
|
|
|
+ }
|
|
|
+ }).exceptionally(ex -> {
|
|
|
+ log.error("后台任务执行异常", ex);
|
|
|
+ return null;
|
|
|
+ });
|
|
|
+ return immediateResponse;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<SpeakingAIDialogue.DialogueEntry> deleteChatHistory(Long userId, Long assignmentId, String userQuestion, Integer voiceCode) {
|
|
|
// 构建查询条件,根据 userId 和 assignmentId 查找对话记录
|
|
|
@@ -327,19 +366,7 @@ public class DoubaoServiceImpl implements DoubaoService {
|
|
|
return startChat(userId, assignmentId, userQuestion, voiceCode);
|
|
|
}
|
|
|
|
|
|
- public List<String> saveSigment(Long userId, Long assignmentId, String sigmentUrl, Double duration) {
|
|
|
- Query query = new Query(Criteria.where("userId").is(userId).and("assignmentId").is(assignmentId));
|
|
|
- // 查询是否存在对应的文档
|
|
|
- SpeakingAIDialogue dialogue = mongoTemplate.findOne(query, SpeakingAIDialogue.class);
|
|
|
- if (dialogue != null) {
|
|
|
- // 如果文档存在,更新 audioSigment 列表
|
|
|
- Update update = new Update().push("audioSigment", sigmentUrl).push("audioDuration",duration);
|
|
|
- mongoTemplate.updateFirst(query, update, SpeakingAIDialogue.class);
|
|
|
- return dialogue.getAudioSigment();
|
|
|
- } else {
|
|
|
- throw new MyException(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase() + ":" + "不存在相应记录");
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public List<String> getSigment(Long userId, Long assignmentId) {
|