소스 검색

FAQ导入

zzf 1 년 전
부모
커밋
15b107358d

+ 35 - 0
src/main/java/com/rf/kjb/chat/dao/domain/ChatAnswerEntity.java

@@ -0,0 +1,35 @@
+package com.rf.kjb.chat.dao.domain;
+
+import com.rf.kjb.base.model.BaseEntry;
+import lombok.*;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Table;
+
+/**
+ * @Author:zzf
+ * @Date:2022/10/21:00:30
+ * @Description:
+ */
+@Entity
+@Data
+@Getter
+@Setter
+@NoArgsConstructor
+@AllArgsConstructor
+@Table(name = "t_chat_answer")
+@org.hibernate.annotations.Table(appliesTo = "t_chat_answer", comment = "对话答案表")
+public class ChatAnswerEntity extends BaseEntry {
+
+    @Column(name = "question_no",columnDefinition = "varchar(50) not null comment '问题编号'")
+    private String questionNo;
+
+    @Column(name = "next_question_no",columnDefinition = "varchar(50) not null comment '下一个问题编号'")
+    private String nextQuestionNo;
+
+    @Column(name = "answer",columnDefinition = "varchar(200) not null comment '答案信息'")
+    private String answer;
+
+    @Column(name = "label",columnDefinition = "varchar(2) not null comment '分类:1-焦虑;2-抑郁;3-失眠;4-压力'")
+    private String label;
+}

+ 40 - 0
src/main/java/com/rf/kjb/chat/dao/domain/ChatQuestionEntity.java

@@ -0,0 +1,40 @@
+package com.rf.kjb.chat.dao.domain;
+
+import com.rf.kjb.base.model.BaseEntry;
+import lombok.*;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Table;
+
+/**
+ * @Author:zzf
+ * @Date:2022/10/21:00:30
+ * @Description:
+ */
+@Entity
+
+@Data
+@Getter
+@Setter
+@NoArgsConstructor
+@AllArgsConstructor
+@Table(name = "t_chat_question")
+@org.hibernate.annotations.Table(appliesTo = "t_chat_question", comment = "对话问题表")
+public class ChatQuestionEntity extends BaseEntry {
+
+    @Column(name = "question",columnDefinition = "varchar(50) not null comment '问题'")
+    private String question;
+
+    @Column(name = "next_question_no",columnDefinition = "varchar(50) not null comment '下一个问题编号'")
+    private String nextQuestionNo;
+
+    @Column(name = "question_type",columnDefinition = "varchar(200) not null comment '答案信息'")
+    private String questionType;
+
+    @Column(name = "label",columnDefinition = "varchar(2) not null comment '分类:1-焦虑;2-抑郁;3-失眠;4-压力'")
+    private String label;
+
+    @Column(name = "scale_flag",columnDefinition = "varchar(50) comment '量表标志,如果是量表的题目信息,此处为量表flag'")
+    private String scaleFlag;
+}

+ 16 - 0
src/main/java/com/rf/kjb/chat/dao/repository/ChatAnswerRepository.java

@@ -0,0 +1,16 @@
+package com.rf.kjb.chat.dao.repository;
+
+
+import com.rf.kjb.base.repository.BaseRepository;
+import com.rf.kjb.chat.dao.domain.ChatAnswerEntity;
+import java.util.List;
+
+public interface ChatAnswerRepository extends BaseRepository<ChatAnswerEntity,String> {
+    List<ChatAnswerEntity> findByQuestionNo(String questionNo);
+
+    List<ChatAnswerEntity> findByQuestionNoAndLabel(String questionNo, String label);
+
+    void deleteByLabel(String label);
+
+    void saveBatch(List<ChatAnswerEntity> chatAnswerEntityList);
+}

+ 10 - 0
src/main/java/com/rf/kjb/chat/dao/repository/ChatQuestionRepository.java

@@ -0,0 +1,10 @@
+package com.rf.kjb.chat.dao.repository;
+
+import com.rf.kjb.base.repository.BaseRepository;
+import com.rf.kjb.chat.dao.domain.ChatQuestionEntity;
+
+public interface ChatQuestionRepository extends BaseRepository<ChatQuestionEntity,String> {
+    ChatQuestionEntity findByIdAndLabel(String id, String label);
+
+    void deleteByLabel(String label);
+}

+ 115 - 0
src/main/java/com/rf/kjb/chat/rest/ChatController.java

@@ -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();
+
+    }
+
+
+}

+ 15 - 0
src/main/java/com/rf/kjb/chat/service/ChatAnswerService.java

@@ -0,0 +1,15 @@
+package com.rf.kjb.chat.service;
+
+import com.rf.kjb.chat.dao.domain.ChatAnswerEntity;
+
+import java.util.List;
+
+public interface ChatAnswerService {
+    List<ChatAnswerEntity> findByQuestionNo(String questionNo);
+
+    List<ChatAnswerEntity> findByQuestionNoAndLabel(String questionNo, String label);
+
+    void deleteByLabel(String label);
+
+    void saveBatch(List<ChatAnswerEntity> chatAnswerEntityList);
+}

+ 21 - 0
src/main/java/com/rf/kjb/chat/service/ChatQuestionService.java

@@ -0,0 +1,21 @@
+package com.rf.kjb.chat.service;
+
+
+import com.rf.kjb.chat.dao.domain.ChatQuestionEntity;
+
+import java.util.List;
+
+/**
+ * @Author:zzf
+ * @Date:2022/10/21:01:04
+ * @Description:
+ */
+public interface ChatQuestionService {
+    ChatQuestionEntity findById(String id);
+
+    ChatQuestionEntity findByIdAndLabel(String id, String label);
+
+    void deleteByLabel(String label);
+
+    void saveBatch(List<ChatQuestionEntity> chatQuestionEntityList);
+}

+ 40 - 0
src/main/java/com/rf/kjb/chat/service/impl/ChatAnswerServiceImpl.java

@@ -0,0 +1,40 @@
+package com.rf.kjb.chat.service.impl;
+
+import com.rf.kjb.chat.dao.domain.ChatAnswerEntity;
+import com.rf.kjb.chat.dao.repository.ChatAnswerRepository;
+import com.rf.kjb.chat.service.ChatAnswerService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * @Author:zzf
+ * @Date:2022/10/21:01:06
+ * @Description:
+ */
+@Service
+public class ChatAnswerServiceImpl implements ChatAnswerService {
+
+    @Autowired
+    private ChatAnswerRepository answerRepository;
+    @Override
+    public List<ChatAnswerEntity> findByQuestionNo(String questionNo) {
+        return this.answerRepository.findByQuestionNo(questionNo);
+    }
+
+    @Override
+    public List<ChatAnswerEntity> findByQuestionNoAndLabel(String questionNo, String label) {
+        return this.answerRepository.findByQuestionNoAndLabel( questionNo,  label);
+    }
+
+    @Override
+    public void deleteByLabel(String label) {
+        this.answerRepository.deleteByLabel(label);
+    }
+
+    @Override
+    public void saveBatch(List<ChatAnswerEntity> chatAnswerEntityList) {
+        this.answerRepository.saveAll(chatAnswerEntityList);
+    }
+}

+ 40 - 0
src/main/java/com/rf/kjb/chat/service/impl/ChatQuestionServiceImpl.java

@@ -0,0 +1,40 @@
+package com.rf.kjb.chat.service.impl;
+
+import com.rf.kjb.chat.dao.domain.ChatQuestionEntity;
+import com.rf.kjb.chat.dao.repository.ChatQuestionRepository;
+import com.rf.kjb.chat.service.ChatQuestionService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * @Author:zzf
+ * @Date:2022/10/21:01:04
+ * @Description:
+ */
+@Service
+public class ChatQuestionServiceImpl implements ChatQuestionService {
+
+    @Autowired
+    private ChatQuestionRepository questionRepository;
+    @Override
+    public ChatQuestionEntity findById(String id) {
+        return this.questionRepository.findById(id).orElse(null);
+    }
+
+    @Override
+    public ChatQuestionEntity findByIdAndLabel(String id, String label) {
+        return this.questionRepository.findByIdAndLabel( id,  label);
+    }
+
+    @Override
+    public void deleteByLabel(String label) {
+        this.questionRepository.deleteByLabel(label);
+    }
+
+    @Override
+    public void saveBatch(List<ChatQuestionEntity> chatQuestionEntityList) {
+        this.questionRepository.saveAll(chatQuestionEntityList);
+    }
+}

+ 5 - 5
src/main/java/com/rf/kjb/excel/ExcelUtil.java

@@ -94,13 +94,13 @@ public class ExcelUtil {
         if (null == work) {
             throw new Exception("创建Excel工作薄为空!");
         }
-        Sheet sheet = null;
-        Row row = null;
-        Cell cell = null;
-        list = new ArrayList<List<List<Object>>>();
+        Sheet sheet;
+        Row row;
+        Cell cell;
+        list = new ArrayList<>();
         //遍历Excel中所有的sheet
         for (int i = 0; i < work.getNumberOfSheets() - 1; i++) {
-            List<List<Object>> sheets = new ArrayList<List<Object>>();
+            List<List<Object>> sheets = new ArrayList<>();
             sheet = work.getSheetAt(i);
             if (sheet == null) {
                 continue;

+ 3 - 0
src/main/java/com/rf/kjb/filter/JWTInterceptorConfig.java

@@ -27,12 +27,14 @@ public class JWTInterceptorConfig implements WebMvcConfigurer {
         String[] swaggerExcludes = new String[]{"/swagger*/**", "/v3/api-docs", "/webjars/**","/doc.html"};
         String[] user = new String[]{"/v1/user/login", "/v1/user/register","/v1/user/**/checkUser","/v1/user/hello"};
         String[] record = new String[]{"/v1/record/download/**","/v1/recordPhy/download","/v1/recordPhy/export/all/pulse/target"};
+        String[] faqImport = new String[]{"/chat/import/**/faq"};
         registry.addInterceptor(jwtInterceptor)
                 .addPathPatterns("/**")
                 .excludePathPatterns("/", "/index**", "/error")
                 .excludePathPatterns(user)
                 .excludePathPatterns(swaggerExcludes)
                 .excludePathPatterns(record)
+                .excludePathPatterns(faqImport)
                 .excludePathPatterns("/favicon.ico")
                 .excludePathPatterns("/druid/**")
                 .excludePathPatterns("/static/**");//排除静态资源
@@ -42,6 +44,7 @@ public class JWTInterceptorConfig implements WebMvcConfigurer {
                 .excludePathPatterns(user)
                 .excludePathPatterns(swaggerExcludes)
                 .excludePathPatterns(record)
+                .excludePathPatterns(faqImport)
                 .excludePathPatterns("/favicon.ico")
                 .excludePathPatterns("/druid/**")
                 .excludePathPatterns("/static/**");//排除静态资源

+ 0 - 12
src/main/java/com/rf/kjb/test/Controller.java

@@ -1,12 +0,0 @@
-package com.rf.kjb.test;
-
-import com.rf.kjb.base.rest.BaseController;
-
-/**
- * @Author:zzf
- * @Date:2023/6/27:16:32
- * @Description:
- */
-
-public class Controller extends BaseController {
-}

+ 0 - 12
src/main/java/com/rf/kjb/test/TestEntity.java

@@ -1,12 +0,0 @@
-package com.rf.kjb.test;
-
-import com.rf.kjb.base.model.BaseEntry;
-
-/**
- * @Author:zzf
- * @Date:2023/6/27:16:32
- * @Description:
- */
-public class TestEntity extends BaseEntry {
-
-}