|
@@ -0,0 +1,115 @@
|
|
|
+package com.rf.kjb.chat.rest;
|
|
|
+
|
|
|
+import com.rf.kjb.base.rest.BaseController;
|
|
|
+import com.rf.kjb.chat.dao.domain.ChatAnswerEntity;
|
|
|
+import com.rf.kjb.chat.dao.domain.ChatQuestionEntity;
|
|
|
+import com.rf.kjb.chat.service.ChatAnswerService;
|
|
|
+import com.rf.kjb.chat.service.ChatQuestionService;
|
|
|
+import com.rf.kjb.excel.ExcelUtil;
|
|
|
+import com.rf.kjb.utils.Result;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/chat")
|
|
|
+@Api(tags = "智能对话")
|
|
|
+public class ChatController extends BaseController {
|
|
|
+ @Autowired
|
|
|
+ private ChatQuestionService questionService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ChatAnswerService answerService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询会话问题
|
|
|
+ */
|
|
|
+ @GetMapping("/getQuestion")
|
|
|
+ @ApiOperation("查询问题信息:首次查询时,id传空字符串即可查询出第一道题目;questionType=0表示选择题,questionType=1表示填空题;当改问题的nextQuestionNo 不为空是,则表示改题为陈述,没有答案,直接在显示此问题之后再次请求该接口显示questionNo对应的题目信息即可;label:分类:1-焦虑;2-抑郁;3-失眠;4-压力")
|
|
|
+ public Result getQuestion(String id ,String label){
|
|
|
+
|
|
|
+ if(StringUtils.isBlank(id)){
|
|
|
+ id = "1";
|
|
|
+ }
|
|
|
+ if(StringUtils.isBlank(label)){
|
|
|
+ id = "1";
|
|
|
+ }
|
|
|
+
|
|
|
+ ChatQuestionEntity questionEntity = this.questionService.findByIdAndLabel(id,label);
|
|
|
+ return success(questionEntity);
|
|
|
+ }
|
|
|
+ @GetMapping("/getAnswer/{questionNo}/{label}")
|
|
|
+ @ApiOperation("查询答案列表,questionNo为问题的id字段,nextQuestionNo 为此答案被选中后的下一个应该呈现的问题")
|
|
|
+ public Result getAnswer(@PathVariable String questionNo, @PathVariable String label){
|
|
|
+ List<ChatAnswerEntity> answerEntities = this.answerService.findByQuestionNoAndLabel(questionNo,label);
|
|
|
+ return success(answerEntities);
|
|
|
+ }
|
|
|
+ @PostMapping("/import/{label}/faq")
|
|
|
+ @ApiOperation("智能问答导入")
|
|
|
+ public Result importQuestionAndAnswer( @RequestPart("file") MultipartFile file, @PathVariable String label){
|
|
|
+// FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
+ String [] fileNames = Objects.requireNonNull(file.getOriginalFilename()).split("\\.");
|
|
|
+ File targetFile = null;
|
|
|
+ List<ChatQuestionEntity> chatQuestionEntityList = new ArrayList<>();
|
|
|
+ List<ChatAnswerEntity> chatAnswerEntityList = new ArrayList<>();
|
|
|
+ try {
|
|
|
+// targetFile = new File("./"+fileNames[0]+ UUID.randomUUID()+"."+fileNames[1]);
|
|
|
+// FileUtils.copyInputStreamToFile(file.getInputStream(),targetFile);
|
|
|
+ targetFile = File.createTempFile(fileNames[0],"."+fileNames[1]);
|
|
|
+ file.transferTo(targetFile);
|
|
|
+ List<List<List<Object>>> datas = ExcelUtil.getBankListByExcelSheet(Files.newInputStream(Paths.get(targetFile.getAbsolutePath())), targetFile.getName());
|
|
|
+ List<List<Object>> questionList = datas.get(0);
|
|
|
+ List<List<Object>> answerList = datas.get(1);
|
|
|
+ questionList.forEach(item ->{
|
|
|
+ ChatQuestionEntity chatQuestionEntity = new ChatQuestionEntity();
|
|
|
+ chatQuestionEntity.setId((String) item.get(0));
|
|
|
+ chatQuestionEntity.setQuestion((String) item.get(1));
|
|
|
+ chatQuestionEntity.setNextQuestionNo((String) item.get(2));
|
|
|
+ chatQuestionEntity.setQuestionType((String) item.get(3));
|
|
|
+ chatQuestionEntity.setScaleFlag((String) item.get(4));
|
|
|
+ chatQuestionEntity.setLabel(label);
|
|
|
+ chatQuestionEntityList.add(chatQuestionEntity);
|
|
|
+ });
|
|
|
+ answerList.forEach(item ->{
|
|
|
+ ChatAnswerEntity chatAnswerEntity = new ChatAnswerEntity();
|
|
|
+ chatAnswerEntity.setQuestionNo((String) item.get(0));
|
|
|
+ chatAnswerEntity.setNextQuestionNo((String) item.get(1));
|
|
|
+ chatAnswerEntity.setAnswer((String) item.get(2));
|
|
|
+ chatAnswerEntity.setLabel(label);
|
|
|
+ chatAnswerEntityList.add(chatAnswerEntity);
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(chatQuestionEntityList.size()>0 && chatAnswerEntityList.size()>0){
|
|
|
+ this.questionService.deleteByLabel(label);
|
|
|
+ this.answerService.deleteByLabel(label);
|
|
|
+
|
|
|
+ this.questionService.saveBatch(chatQuestionEntityList);
|
|
|
+ this.answerService.saveBatch(chatAnswerEntityList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return success();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|