123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113 |
- package com.rf.kjb.chat.rest;
- import com.alibaba.fastjson.JSONObject;
- import com.auth0.jwt.interfaces.DecodedJWT;
- import com.rf.kjb.base.rest.BaseController;
- import com.rf.kjb.chat.dao.domain.*;
- import com.rf.kjb.chat.service.*;
- import com.rf.kjb.excel.ExcelUtil;
- import com.rf.kjb.exception.ErrorCode;
- import com.rf.kjb.intelligentDialogue.dao.domain.IntelligentDialogueEntity;
- import com.rf.kjb.intelligentDialogue.service.IntelligentDialogueService;
- import com.rf.kjb.opLog.dao.model.SysLogEntity;
- import com.rf.kjb.scale.util.DateUtil;
- import com.rf.kjb.user.dao.model.UserEntry;
- import com.rf.kjb.user.service.UserService;
- import com.rf.kjb.utils.FileUtils;
- import com.rf.kjb.utils.JWTUtil;
- import com.rf.kjb.utils.Result;
- import com.rf.kjb.utils.ZipUtils;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import lombok.SneakyThrows;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.poi.xssf.usermodel.XSSFCell;
- import org.apache.poi.xssf.usermodel.XSSFRow;
- import org.apache.poi.xssf.usermodel.XSSFSheet;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.net.URLEncoder;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.*;
- import java.util.stream.Stream;
- @Slf4j
- @RestController
- @RequestMapping("/chat")
- @Api(tags = "对话")
- public class ChatController extends BaseController {
- @Autowired
- private ChatQuestionService questionService;
- @Autowired
- private ChatAnswerService answerService;
- @Autowired
- private ResultQuestionService resultQuestionService;
- @Autowired
- private ChatRecordService chatRecordService;
- @Autowired
- private UserService userService;
- @Autowired
- private IntelligentDialogueService intelligentDialogueService;
- @Autowired
- private QuestionSkipService questionSkipService;
- /**
- * 查询会话问题
- */
- @GetMapping("/getQuestion/{label}")
- @ApiOperation("查询问题信息:首次查询时,id传空字符串即可查询出第一道题目;questionType=0表示选择题,questionType=1表示填空题;当改问题的nextQuestionNo 不为空是,则表示改题为陈述,没有答案,直接在显示此问题之后再次请求该接口显示questionNo对应的题目信息即可")
- public Result getQuestion(String id, String num, @PathVariable String label, HttpServletRequest request) {
- if (StringUtils.isBlank(id)) {
- id = "1";
- }
- ChatQuestionEntity questionEntity = this.questionService.findByIdAndLabelAndNum(id, label, num);
- String token = request.getHeader("Authorization");
- token = token.split(" ")[1];//以空格划分Bearer token,获取token
- //从请求头中获取token
- DecodedJWT verify = JWTUtil.verify(token);
- String identifier = verify.getClaim("identifier").asString();
- UserEntry userEntry = this.userService.findByIdentifier(identifier);
- if (questionEntity.getLabel().equals("5") && questionEntity.getId().equals("3")){
- questionEntity.setQuestion(userEntry.getUserName()+questionEntity.getQuestion());
- }
- //情绪打分跳转
- List<QuestionSkipEntity> questionSkipEntities = this.questionSkipService.findByLabelAndNumAndType(label,num,"5");
- for (QuestionSkipEntity questionSkipEntity : questionSkipEntities) {
- if (id.equals(questionSkipEntity.getQuestionNo())){
- int a = emotionScore(identifier,label,num,questionSkipEntity.getSkipQuestionNo());
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum(id, label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum(String.valueOf(Integer.valueOf(id)+2), label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum(String.valueOf(Integer.valueOf(id)+4), label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum(String.valueOf(Integer.valueOf(id)+6), label, num);
- break;
- default:
- break;
- }
- }
- }
- //情绪二次打分跳转
- if (id.equals("4")){
- if (num.equals("2") || num.equals("3") || num.equals("4") || num.equals("5")){
- String lastNum = String.valueOf(Integer.valueOf(num) - 1);
- IntelligentDialogueEntity intelligentDialogue = this.intelligentDialogueService.findLastScoreByIdentifierByLabelByNumByQuestionNo(identifier,label,lastNum,"5");
- int lastScore = 0;
- if (intelligentDialogue != null){
- lastScore = Integer.valueOf(intelligentDialogue.getContent());
- }
- IntelligentDialogueEntity intelligentDialogueEntity = this.intelligentDialogueService.findScoreByIdentifierByLabelByNumByQuestionNo(identifier,label,num);
- int score = Integer.valueOf(intelligentDialogueEntity.getContent()) - lastScore;
- if (score <= -80){
- questionEntity = this.questionService.findByIdAndLabelAndNum("13", label, num);
- }else if (score <= -60 && score > -80){
- questionEntity = this.questionService.findByIdAndLabelAndNum("11", label, num);
- }else if (score <= -40 && score > -60){
- questionEntity = this.questionService.findByIdAndLabelAndNum("9", label, num);
- }else if (score <= -20 && score > -40){
- questionEntity = this.questionService.findByIdAndLabelAndNum("7", label, num);
- }else if (score <= -1 && score > -20){
- questionEntity = this.questionService.findByIdAndLabelAndNum("5", label, num);
- }else if (score <= 20 && score > -1){
- questionEntity = this.questionService.findByIdAndLabelAndNum("4", label, num);
- }else if (score <= 40 && score > 20){
- questionEntity = this.questionService.findByIdAndLabelAndNum("6", label, num);
- }else if (score <= 60 && score > 40){
- questionEntity = this.questionService.findByIdAndLabelAndNum("8", label, num);
- }else if (score <= 80 && score > 60){
- questionEntity = this.questionService.findByIdAndLabelAndNum("10", label, num);
- }else {
- questionEntity = this.questionService.findByIdAndLabelAndNum("12", label, num);
- }
- }
- }
- /*if (label.equals("2")){
- if (num.equals("1")){
- if (id.equals("41")){
- questionEntity = emotionScoreSkip(identifier,label,num,"40");
- }
- if (id.equals("83")){
- questionEntity = emotionScoreSkip(identifier,label,num,"82");
- }
- if (id.equals("121")){
- questionEntity = emotionScoreSkip(identifier,label,num,"120");
- }
- if (id.equals("169")){
- questionEntity = emotionScoreSkip(identifier,label,num,"168");
- }
- if (id.equals("210")){
- questionEntity = emotionScoreSkip(identifier,label,num,"209");
- }
- if (id.equals("249")){
- questionEntity = emotionScoreSkip(identifier,label,num,"246");
- }
- if (id.equals("288")){
- questionEntity = emotionScoreSkip(identifier,label,num,"285");
- }
- if (id.equals("336")){
- questionEntity = emotionScoreSkip(identifier,label,num,"333");
- }
- if (id.equals("377")){
- questionEntity = emotionScoreSkip(identifier,label,num,"374");
- }
- if (id.equals("414")){
- questionEntity = emotionScoreSkip(identifier,label,num,"411");
- }
- }
- if (num.equals("2") || num.equals("3") || num.equals("4") || num.equals("5")){
- if (id.equals("57")){
- questionEntity = emotionScoreSkip(identifier,label,num,"56");
- }
- if (id.equals("99")){
- questionEntity = emotionScoreSkip(identifier,label,num,"98");
- }
- if (id.equals("137")){
- questionEntity = emotionScoreSkip(identifier,label,num,"136");
- }
- if (id.equals("185")){
- questionEntity = emotionScoreSkip(identifier,label,num,"184");
- }
- if (id.equals("226")){
- questionEntity = emotionScoreSkip(identifier,label,num,"225");
- }
- if (id.equals("265")){
- questionEntity = emotionScoreSkip(identifier,label,num,"262");
- }
- if (id.equals("304")){
- questionEntity = emotionScoreSkip(identifier,label,num,"301");
- }
- if (id.equals("352")){
- questionEntity = emotionScoreSkip(identifier,label,num,"349");
- }
- if (id.equals("393")){
- questionEntity = emotionScoreSkip(identifier,label,num,"390");
- }
- if (id.equals("430")){
- questionEntity = emotionScoreSkip(identifier,label,num,"427");
- }
- //情绪二次打分跳转
- if (id.equals("4")){
- String lastNum = String.valueOf(Integer.valueOf(num) - 1);
- IntelligentDialogueEntity intelligentDialogue = this.intelligentDialogueService.findLastScoreByIdentifierByLabelByNumByQuestionNo(identifier,label,lastNum,"4");
- int lastScore = 0;
- if (intelligentDialogue != null){
- lastScore = Integer.valueOf(intelligentDialogue.getContent());
- }
- IntelligentDialogueEntity intelligentDialogueEntity = this.intelligentDialogueService.findScoreByIdentifierByLabelByNumByQuestionNo(identifier,label,num);
- int score = Integer.valueOf(intelligentDialogueEntity.getContent()) - lastScore;
- if (score <= -80){
- questionEntity = this.questionService.findByIdAndLabelAndNum("13", label, num);
- }else if (score <= -60 && score > -80){
- questionEntity = this.questionService.findByIdAndLabelAndNum("11", label, num);
- }else if (score <= -40 && score > -60){
- questionEntity = this.questionService.findByIdAndLabelAndNum("9", label, num);
- }else if (score <= -20 && score > -40){
- questionEntity = this.questionService.findByIdAndLabelAndNum("7", label, num);
- }else if (score <= 0 && score > -20){
- questionEntity = this.questionService.findByIdAndLabelAndNum("5", label, num);
- }else if (score <= 20 && score > 0){
- questionEntity = this.questionService.findByIdAndLabelAndNum("4", label, num);
- }else if (score <= 40 && score > 20){
- questionEntity = this.questionService.findByIdAndLabelAndNum("6", label, num);
- }else if (score <= 60 && score > 40){
- questionEntity = this.questionService.findByIdAndLabelAndNum("8", label, num);
- }else if (score <= 80 && score > 60){
- questionEntity = this.questionService.findByIdAndLabelAndNum("10", label, num);
- }else {
- questionEntity = this.questionService.findByIdAndLabelAndNum("12", label, num);
- }
- }
- }
- }*/
- //替换用户名
- if (questionEntity.getQuestion().contains("userName")){
- questionEntity.setQuestion(questionEntity.getQuestion().replace("userName",userEntry.getUserName()));
- }
- //替换使用时长
- if (questionEntity.getQuestion().contains("utilityTime")){
- IntelligentDialogueEntity intelligentDialogueEntity = this.intelligentDialogueService.findByIdentifierByLabelByNum(identifier,label,num);
- Date beginTime = intelligentDialogueEntity.getCreateTime();
- //当前时间
- Date endTime = new Date();
- //计算时间差
- int diff = DateUtil.getDistanceByUnit(beginTime, endTime, 2);
- if (diff <= 60){
- questionEntity.setQuestion(questionEntity.getQuestion().replace("utilityTime",diff+"分钟"));
- }else {
- int MINUTE = diff%60;
- int HOUR = diff/60;
- if (HOUR <= 24){
- questionEntity.setQuestion(questionEntity.getQuestion().replace("utilityTime",HOUR + "小时" + MINUTE + "分钟"));
- }else {
- int DAY = HOUR/24;
- HOUR = HOUR%24;
- questionEntity.setQuestion(questionEntity.getQuestion().replace("utilityTime",DAY + "天" + HOUR + "小时" + MINUTE + "分钟"));
- }
- }
- }
- //替换天数问候
- if (questionEntity.getQuestion().contains("days")){
- String lastNum = String.valueOf(Integer.valueOf(num) - 1);
- IntelligentDialogueEntity intelligentDialogueEntity = this.intelligentDialogueService.findDateByIdentifierByLabelByNum(identifier,label,lastNum);
- Date beginTime = intelligentDialogueEntity.getCreateTime();
- //当前时间
- Date endTime = new Date();
- //计算时间差
- int diff = DateUtil.getDistanceByUnit(beginTime, endTime, 4);
- questionEntity.setQuestion(questionEntity.getQuestion().replace("days",String.valueOf(diff)));
- }
- //替换评测结果
- if (questionEntity.getQuestion().contains("SDSEvaluationResult")){
- IntelligentDialogueEntity intelligentDialogueEntity = this.intelligentDialogueService.findByIdentifierByLabelByNumByQuestionNo(identifier,label,num,"1");
- questionEntity.setQuestion(questionEntity.getQuestion().replace("SDSEvaluationResult",intelligentDialogueEntity.getContent()));
- }
- //替换治疗成绩
- if (questionEntity.getQuestion().contains("therapeuticAchievement")){
- List<IntelligentDialogueEntity> intelligentDialogueEntityList = this.intelligentDialogueService.findGradeByIdentifierByLabelByNumByQuestionNo(identifier,label,num,"2");
- int therapeuticAchievement = Integer.valueOf(intelligentDialogueEntityList.get(1).getContent()) - Integer.valueOf(intelligentDialogueEntityList.get(0).getContent());
- questionEntity.setQuestion(questionEntity.getQuestion().replace("therapeuticAchievement",String.valueOf(therapeuticAchievement)));
- }
- //替换上次训练内容
- if (questionEntity.getQuestion().contains("LastTrainingContent")){
- String lastNum = String.valueOf(Integer.valueOf(num) - 1);
- IntelligentDialogueEntity intelligentDialogueEntity = this.intelligentDialogueService.findJobByIdentifierByLabelByNumByQuestionNo(identifier,label,lastNum,"3");
- questionEntity.setQuestion(questionEntity.getQuestion().replace("LastTrainingContent",intelligentDialogueEntity.getContent()));
- }
- return success(questionEntity);
- }
- @PostMapping("/import/{label}/{num}/faq")
- @ApiOperation("智能问答导入")
- public Result importQuestionAndAnswer(@RequestPart("file") MultipartFile file, @PathVariable String label, @PathVariable String num) {
- // 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));
- String nextQuestionNo = String.valueOf(item.get(2)).replace("aaa","");
- chatQuestionEntity.setNextQuestionNo(nextQuestionNo);
- String questionType = (String) item.get(3);
- if (questionType.equals("单选")) {
- chatQuestionEntity.setQuestionType("0");
- } else if (questionType.equals("填空")) {
- chatQuestionEntity.setQuestionType("1");
- } else if (questionType.equals("多选")) {
- chatQuestionEntity.setQuestionType("2");
- } else if (questionType.equals("整数")) {
- chatQuestionEntity.setQuestionType("3");
- } else {
- chatQuestionEntity.setQuestionType("0");
- }
- //chatQuestionEntity.setScaleFlag((String) item.get(4));
- chatQuestionEntity.setLabel(label);
- chatQuestionEntity.setNum(num);
- 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);
- chatAnswerEntity.setQuestionType("0");
- chatAnswerEntity.setNum(num);
- 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();
- }
- @GetMapping("/getAnswer/{questionNo}/{label}/{num}")
- @ApiOperation("查询答案信息列表,questionNo为问题的id字段,nextQuestionNo 为此答案被选中后的下一个应该呈现的问题")
- public Result getAnswer(@PathVariable String questionNo, @PathVariable String label, @PathVariable String num) {
- List<ChatAnswerEntity> answerEntities = this.answerService.findByQuestionNoAndLabelAndNum(questionNo, label, num);
- return success(answerEntities);
- }
- @GetMapping("/getNextQuestionByScaleResult")
- @ApiOperation("根据量表测试结果查询下一问题编号")
- public Result getNextQuestionByScaleResult(String label, String result, String num) {
- ResultQuestionEntity resultQuestionEntity = this.resultQuestionService.findByLabelAndResultAndNum(label, result, num);
- //ResultQuestionEntity resultQuestionEntity = this.resultQuestionService.findByLabelAndNum(label, num);
- if (resultQuestionEntity == null) {
- return fail(ErrorCode.NEXT_QUESTION_NO_NOT_FOUND);
- }
- ChatQuestionEntity byIdAndLabel = this.questionService.findByIdAndLabelAndNum(resultQuestionEntity.getNextQuestionNo(), label, resultQuestionEntity.getNum());
- if (byIdAndLabel == null) {
- return fail(ErrorCode.NEXT_QUESTION_NO_ERROR);
- }
- return success(resultQuestionEntity);
- }
- @PostMapping("/complete/chat")
- @ApiOperation(value = "结束会话,保存会话记录", notes = "identifier:用户编号;userName:用户名;label:智能对话分类 1-焦虑;2-抑郁;3-失眠;4-压力;content:会话详情")
- public Result completeChat(@RequestBody String json) throws IOException {
- ChatRecordEntity chatRecordEntity = JSONObject.parseObject(json, ChatRecordEntity.class);
- //生成文件
- List<ChatEntity> chatEntityList = JSONObject.parseArray(chatRecordEntity.getContent(), ChatEntity.class);
- if (chatEntityList != null && chatEntityList.size() > 0) {
- String filePath = "./对话记录/" + chatRecordEntity.getIdentifier() + "-对话记录-" + DateUtil.getNowTime_CN() + ".xlsx";
- File file = new File(filePath);
- if (!file.exists()) {
- file.createNewFile();
- }
- XSSFWorkbook workbook = new XSSFWorkbook();
- ExcelUtil.createFont(workbook);
- XSSFSheet sheet = workbook.createSheet("对话记录");
- XSSFRow titleRow = sheet.createRow(0);
- XSSFCell cellOrder = titleRow.createCell(0);
- cellOrder.setCellValue("角色");
- XSSFCell cellQuestion = titleRow.createCell(1);
- cellQuestion.setCellValue("内容");
- for (int i = 0; i < chatEntityList.size(); i++) {
- XSSFRow row = sheet.createRow(i + 1);
- XSSFCell cellOrder1 = row.createCell(0);
- if (chatEntityList.get(i).getFrom().equals("1")) {
- cellOrder1.setCellValue("智能助手:");
- } else {
- cellOrder1.setCellValue("用户:");
- }
- XSSFCell cellQuestion1 = row.createCell(1);
- cellQuestion1.setCellValue(chatEntityList.get(i).getQuestion());
- }
- OutputStream outputStream = Files.newOutputStream(file.toPath());
- workbook.write(outputStream);
- outputStream.close();
- //保存记录
- chatRecordEntity.setFilePath(filePath);
- chatRecordEntity.setCreateTime(new Date());
- this.chatRecordService.save(chatRecordEntity);
- return success();
- } else {
- return fail(ErrorCode.CHAT_CONTENT_EMPTY);
- }
- }
- @GetMapping("/chat/list")
- @ApiOperation(value = "智能对话列表", notes = "identifier:编号;beginDate:查询起始日期;endDate:查询结束日期;pageNum:页数;pageSize:每页记录数")
- public Result getChatList(String identifier, String beginDate, String endDate, int pageNum, int pageSize) {
- Page<ChatRecordEntity> chatRecordEntities = this.chatRecordService.find(identifier, beginDate, endDate, pageNum, pageSize);
- return success(chatRecordEntities);
- }
- @GetMapping("/chat/download")
- @ApiOperation(value = "下载对话记录", notes = "")
- public Result downloadChatList(@RequestBody String json, HttpServletResponse response) throws IOException {
- List<String> list = JSONObject.parseArray(json, String.class);
- if (list.size() > 0) {
- for (String item : list) {
- File file = new File(item);
- if (file.exists()) {
- InputStream inputStream = null;
- OutputStream outputStream = null;
- try {
- inputStream = Files.newInputStream(file.toPath());
- outputStream = new FileOutputStream("./对话列表/" + item);
- IOUtils.copy(inputStream, outputStream);
- } catch (IOException e) {
- log.error("对话记录下载失败:" + e.getMessage());
- } finally {
- if (inputStream != null) {
- inputStream.close();
- }
- if (outputStream != null) {
- outputStream.close();
- }
- }
- }
- }
- File dir = new File("./对话列表");
- if (!dir.exists()) {
- return fail(ErrorCode.FAILED);
- } else {
- ZipUtils.createZip("./对话列表", "./对话列表" + DateUtil.getNowTime_CN() + ".zip", false);
- FileUtils.downloadFile(new File("./对话列表", "对话列表" + DateUtil.getNowTime_CN() + ".zip"), "", "对话列表" + DateUtil.getNowTime_CN() + ".zip", response);
- return success();
- }
- }
- return fail(ErrorCode.FAILED);
- }
- @ApiOperation(value = "对话记录下载")
- @GetMapping("/chatRecord/download")
- public Result chatRecordDownload(String id, HttpServletResponse response) throws Exception {
- if (id != null) {
- ChatRecordEntity chatRecordEntity = this.chatRecordService.findById(id);
- if (chatRecordEntity == null) {
- return fail(ErrorCode.FAILED);
- }
- File file = null;
- if (new File(chatRecordEntity.getFilePath()).exists()) {
- file = new File(chatRecordEntity.getFilePath());
- response.reset();
- response.setContentType("application/octet-stream");
- response.setCharacterEncoding("utf-8");
- response.setContentLength((int) file.length());
- response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
- response.setHeader("filename",java.net.URLEncoder.encode(file.getName(), "UTF-8"));
- byte[] buffer = new byte[1024];
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- try {
- fis = new FileInputStream(file);
- bis = new BufferedInputStream(fis);
- OutputStream os = response.getOutputStream();
- int i = bis.read(buffer);
- while (i != -1) {
- os.write(buffer, 0, i);
- i = bis.read(buffer);
- }
- return success();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (bis != null) {
- try {
- bis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- } else {
- return fail(ErrorCode.FAILED);
- }
- }
- return fail(ErrorCode.FAILED);
- }
- @SneakyThrows
- @ApiOperation(value = "对话记录批量下载接口")
- @GetMapping("/chatRecord/batchDownload")
- public Result chatRecordBatchDownload(@RequestParam(value = "ids", required = false) List<String> recordingIds, HttpServletResponse response) {
- //原始记录文件
- File file = null;
- //原始记录复制后文件
- File destFile = null;
- //所有记录汇总后文件夹,用来压缩
- String filePath ="./对话记录/记录下载";
- //原始记录文件名
- String fileName = null;
- for (int i = 0; i < recordingIds.size(); i++) {
- ChatRecordEntity chatRecordEntity = this.chatRecordService.findById(recordingIds.get(i));
- String[] split = chatRecordEntity.getFilePath().split("/");
- if (split.length > 1){
- fileName = split[split.length-1];
- }else {
- fileName = chatRecordEntity.getFilePath();
- }
- //设置文件路径
- file = new File(chatRecordEntity.getFilePath());
- if (file.exists()){
- destFile = new File(filePath+"/"+fileName+".xlsx");
- //将原始文件进行复制
- org.apache.commons.io.FileUtils.copyFile(file, destFile);
- }
- }
- //将复制后的整个文件夹打包
- ZipUtils.createZip(filePath, filePath + ".zip", true);
- File zipFile = new File(filePath + ".zip");
- //下载压缩后文件
- if (zipFile.exists()) {
- response.reset();
- response.setContentType("application/octet-stream");
- response.setCharacterEncoding("utf-8");
- response.setContentLength((int) zipFile.length());
- response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode("chatRecord.zip", "UTF-8"));
- response.setHeader("filename",java.net.URLEncoder.encode("chatRecord.zip", "UTF-8"));
- byte[] buffer = new byte[1024];
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- try {
- fis = new FileInputStream(zipFile);
- bis = new BufferedInputStream(fis);
- OutputStream os = response.getOutputStream();
- int i = bis.read(buffer);
- while (i != -1) {
- os.write(buffer, 0, i);
- i = bis.read(buffer);
- }
- return success();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (bis != null) {
- try {
- bis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //将复制后的整个文件夹删除
- Path path = Paths.get(filePath);
- try (Stream<Path> walk = Files.walk(path)) {
- walk.sorted(Comparator.reverseOrder())
- .forEach(ChatController::deleteDirectoryStream);
- }
- //删除生成的压缩包
- Files.delete(Paths.get(filePath + ".zip"));
- }
- }
- //删除生成的压缩包
- Files.delete(Paths.get(filePath + ".zip"));
- //将复制后的整个文件夹删除
- Path path = Paths.get(filePath);
- try (Stream<Path> walk = Files.walk(path)) {
- walk.sorted(Comparator.reverseOrder())
- .forEach(ChatController::deleteDirectoryStream);
- }
- return fail(ErrorCode.FAILED);
- }
- @SneakyThrows
- @ApiOperation(value = "对话记录全部下载接口")
- @GetMapping("/chatRecord/AllDownload")
- public Result chatRecordAllDownload(String identifier, String beginDate, String endDate, HttpServletResponse response) {
- List<ChatRecordEntity> chatRecordEntityList = this.chatRecordService.findAll(identifier, beginDate, endDate);
- //原始记录文件
- File file = null;
- //原始记录复制后文件
- File destFile = null;
- //所有记录汇总后文件夹,用来压缩
- String filePath ="./对话记录/记录下载";
- //原始记录文件名
- String fileName = null;
- for (int i = 0; i < chatRecordEntityList.size(); i++) {
- ChatRecordEntity chatRecordEntity = chatRecordEntityList.get(i);
- String[] split = chatRecordEntity.getFilePath().split("/");
- if (split.length > 1){
- fileName = split[split.length-1];
- }else {
- fileName = chatRecordEntity.getFilePath();
- }
- //设置文件路径
- file = new File(chatRecordEntity.getFilePath());
- if (file.exists()){
- destFile = new File(filePath+"/"+fileName+".xlsx");
- //将原始文件进行复制
- org.apache.commons.io.FileUtils.copyFile(file, destFile);
- }
- }
- //将复制后的整个文件夹打包
- ZipUtils.createZip(filePath, filePath + ".zip", true);
- File zipFile = new File(filePath + ".zip");
- //下载压缩后文件
- if (zipFile.exists()) {
- response.reset();
- response.setContentType("application/octet-stream");
- response.setCharacterEncoding("utf-8");
- response.setContentLength((int) zipFile.length());
- response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("chatRecord.zip", "UTF-8"));
- response.setHeader("filename",java.net.URLEncoder.encode("chatRecord.zip", "UTF-8"));
- byte[] buffer = new byte[1024];
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- try {
- fis = new FileInputStream(zipFile);
- bis = new BufferedInputStream(fis);
- OutputStream os = response.getOutputStream();
- int i = bis.read(buffer);
- while (i != -1) {
- os.write(buffer, 0, i);
- i = bis.read(buffer);
- }
- return success();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (bis != null) {
- try {
- bis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //将复制后的整个文件夹删除
- Path path = Paths.get(filePath);
- try (Stream<Path> walk = Files.walk(path)) {
- walk.sorted(Comparator.reverseOrder())
- .forEach(ChatController::deleteDirectoryStream);
- }
- //删除生成的压缩包
- Files.delete(Paths.get(filePath + ".zip"));
- }
- }
- //删除生成的压缩包
- Files.delete(Paths.get(filePath + ".zip"));
- //将复制后的整个文件夹删除
- Path path = Paths.get(filePath);
- try (Stream<Path> walk = Files.walk(path)) {
- walk.sorted(Comparator.reverseOrder())
- .forEach(ChatController::deleteDirectoryStream);
- }
- return fail(ErrorCode.FAILED);
- }
- public static void deleteDirectoryStream(Path path) {
- try {
- Files.delete(path);
- } catch (IOException e) {
- }
- }
- private ChatQuestionEntity emotionScoreSkip(String identifier, String label, String num, String questionNo) {
- int a = emotionScore(identifier,label,num,questionNo);
- ChatQuestionEntity questionEntity = new ChatQuestionEntity();
- if (label.equals("2")){
- if (num.equals("1")){
- if (questionNo.equals("40")){
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("41", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("43", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("45", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("47", label, num);
- break;
- default:
- break;
- }
- }
- if (questionNo.equals("82")){
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("83", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("85", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("87", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("89", label, num);
- break;
- default:
- break;
- }
- }
- if (questionNo.equals("120")){
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("121", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("123", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("125", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("157", label, num);
- break;
- default:
- break;
- }
- }
- if (questionNo.equals("168")){
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("169", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("171", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("173", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("175", label, num);
- break;
- default:
- break;
- }
- }
- if (questionNo.equals("209")){
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("210", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("212", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("214", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("216", label, num);
- break;
- default:
- break;
- }
- }
- if (questionNo.equals("246")){
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("249", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("251", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("253", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("255", label, num);
- break;
- default:
- break;
- }
- }
- if (questionNo.equals("285")){
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("288", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("290", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("292", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("294", label, num);
- break;
- default:
- break;
- }
- }
- if (questionNo.equals("333")){
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("336", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("338", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("340", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("342", label, num);
- break;
- default:
- break;
- }
- }
- if (questionNo.equals("374")){
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("377", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("379", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("381", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("383", label, num);
- break;
- default:
- break;
- }
- }
- if (questionNo.equals("411")){
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("414", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("416", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("418", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("420", label, num);
- break;
- default:
- break;
- }
- }
- }
- }
- if (label.equals("2")) {
- if (num.equals("2") || num.equals("3") || num.equals("4") || num.equals("5")){
- if (questionNo.equals("56")) {
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("57", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("59", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("61", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("63", label, num);
- break;
- default:
- break;
- }
- }
- if (questionNo.equals("98")) {
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("99", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("101", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("103", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("105", label, num);
- break;
- default:
- break;
- }
- }if (questionNo.equals("136")) {
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("137", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("139", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("141", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("143", label, num);
- break;
- default:
- break;
- }
- }if (questionNo.equals("184")) {
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("185", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("187", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("189", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("191", label, num);
- break;
- default:
- break;
- }
- }if (questionNo.equals("225")) {
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("226", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("228", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("230", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("232", label, num);
- break;
- default:
- break;
- }
- }if (questionNo.equals("262")) {
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("265", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("267", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("269", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("271", label, num);
- break;
- default:
- break;
- }
- }if (questionNo.equals("301")) {
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("304", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("306", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("308", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("310", label, num);
- break;
- default:
- break;
- }
- }if (questionNo.equals("349")) {
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("352", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("354", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("356", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("358", label, num);
- break;
- default:
- break;
- }
- }if (questionNo.equals("390")) {
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("393", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("395", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("397", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("399", label, num);
- break;
- default:
- break;
- }
- }if (questionNo.equals("427")) {
- switch (a) {
- case 1:
- questionEntity = this.questionService.findByIdAndLabelAndNum("430", label, num);
- break;
- case 2:
- questionEntity = this.questionService.findByIdAndLabelAndNum("432", label, num);
- break;
- case 3:
- questionEntity = this.questionService.findByIdAndLabelAndNum("434", label, num);
- break;
- case 4:
- questionEntity = this.questionService.findByIdAndLabelAndNum("436", label, num);
- break;
- default:
- break;
- }
- }
- }
- }
- return questionEntity;
- }
- private int emotionScore(String identifier, String label, String num, String questionNo) {
- IntelligentDialogueEntity intelligentDialogueEntity = this.intelligentDialogueService.findEmotionScoreByIdentifierByLabelByNumByQuestionNo(identifier,label,num,questionNo);
- int emotionScore = Integer.valueOf(intelligentDialogueEntity.getContent());
- if (emotionScore <= 25){
- emotionScore = 1;
- }else if (emotionScore <= 50 && emotionScore >= 26){
- emotionScore = 2;
- }else if (emotionScore <= 75 && emotionScore >= 51){
- emotionScore = 3;
- }else {
- emotionScore = 4;
- }
- return emotionScore;
- }
- }
|