|
|
@@ -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; // 注入线程池
|
|
|
@@ -327,18 +330,32 @@ 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() + ":" + "不存在相应记录");
|
|
|
- }
|
|
|
+ public CompletableFuture<String> saveSigment(Long userId, Long assignmentId, MultipartFile file, Double duration) {
|
|
|
+ return CompletableFuture.supplyAsync(() -> {
|
|
|
+ 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) {
|
|
|
+ throw new MyException(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase() + ":" + "不存在相应记录");
|
|
|
+ }
|
|
|
+ // 如果文档存在,更新 audioSigment 列表
|
|
|
+ Update update = new Update().push("audioSigment", url).push("audioDuration", duration);
|
|
|
+ mongoTemplate.updateFirst(query, update, SpeakingAIDialogue.class);
|
|
|
+ System.out.println(dialogue);
|
|
|
+ return url;
|
|
|
+ }catch (IOException e){
|
|
|
+ throw new MyException(500 ,e.getMessage()+ " 文件上传失败");
|
|
|
+ }
|
|
|
+ }).completeOnTimeout(
|
|
|
+ "操作超时,请稍后再试",
|
|
|
+ 500, // 超时时间(秒)
|
|
|
+ TimeUnit.SECONDS
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
@Override
|