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 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 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 chatQuestionEntityList = new ArrayList<>(); List 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>> datas = ExcelUtil.getBankListByExcelSheet(Files.newInputStream(Paths.get(targetFile.getAbsolutePath())), targetFile.getName()); List> questionList = datas.get(0); List> 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 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 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 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 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 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 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 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 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 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 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; } }