|
@@ -0,0 +1,380 @@
|
|
|
+package com.rf.kjb.intelligentDialogue.rest;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.rf.kjb.base.rest.BaseController;
|
|
|
+import com.rf.kjb.chat.rest.ChatController;
|
|
|
+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.scale.util.DateUtil;
|
|
|
+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.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 javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+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("/intelligent")
|
|
|
+@Api(tags = "对话")
|
|
|
+public class IntelligentDialogueController extends BaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IntelligentDialogueService intelligentDialogueService;
|
|
|
+
|
|
|
+
|
|
|
+ @PostMapping("/save")
|
|
|
+ @ApiOperation(value = "保存对话", notes = "identifier:用户编号;userName:用户名;label:对话分类:0-问;1-答;content:对话内容")
|
|
|
+ public Result save(@RequestBody String json) throws IOException {
|
|
|
+ IntelligentDialogueEntity intelligentDialogue = JSONObject.parseObject(json, IntelligentDialogueEntity.class);
|
|
|
+ //保存记录
|
|
|
+ intelligentDialogue.setCreateTime(new Date());
|
|
|
+ this.intelligentDialogueService.save(intelligentDialogue);
|
|
|
+ return success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/dialogueList")
|
|
|
+ @ApiOperation(value = "智能对话列表", notes = "identifier:编号;beginDate:查询起始日期;endDate:查询结束日期;pageNum:页数;pageSize:每页记录数")
|
|
|
+ public Result getChatList(String identifier, int pageNum, int pageSize) {
|
|
|
+ Page<IntelligentDialogueEntity> intelligentDialogueEntities = this.intelligentDialogueService.find(identifier, pageNum, pageSize);
|
|
|
+ return success(intelligentDialogueEntities);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @ApiOperation(value = "对话记录下载")
|
|
|
+ @GetMapping("/dialogueRecord/download")
|
|
|
+ public Result chatRecordDownload(String identifier, HttpServletResponse response) throws Exception {
|
|
|
+ if (identifier != null || !identifier.equals("")) {
|
|
|
+ List<IntelligentDialogueEntity> intelligentDialogueEntityList = this.intelligentDialogueService.findByIdentifier(identifier);
|
|
|
+ if (intelligentDialogueEntityList != null && intelligentDialogueEntityList.size() > 0) {
|
|
|
+ String filePath = "./对话记录/" + identifier + "-智能对话记录-" + 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 < intelligentDialogueEntityList.size(); i++) {
|
|
|
+ XSSFRow row = sheet.createRow(i + 1);
|
|
|
+ XSSFCell cellOrder1 = row.createCell(0);
|
|
|
+ if (intelligentDialogueEntityList.get(i).getLabel().equals("0")) {
|
|
|
+ cellOrder1.setCellValue("智能助手:");
|
|
|
+ } else {
|
|
|
+ cellOrder1.setCellValue("用户:");
|
|
|
+ }
|
|
|
+ XSSFCell cellQuestion1 = row.createCell(1);
|
|
|
+ cellQuestion1.setCellValue(intelligentDialogueEntityList.get(i).getContent());
|
|
|
+ }
|
|
|
+ OutputStream outputStream = Files.newOutputStream(file.toPath());
|
|
|
+ workbook.write(outputStream);
|
|
|
+ outputStream.close();
|
|
|
+
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //删除生成的文件
|
|
|
+ Files.delete(Paths.get(filePath));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return fail(ErrorCode.FAILED);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return fail(ErrorCode.FAILED);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ @ApiOperation(value = "智能对话记录批量下载接口")
|
|
|
+ @GetMapping("/dialogueRecord/batchDownload")
|
|
|
+ public Result chatRecordBatchDownload(@RequestParam(value = "identifiers", required = false) List<String> identifiers, HttpServletResponse response) {
|
|
|
+ //原始记录文件
|
|
|
+ File file = null;
|
|
|
+ //原始记录文件路径
|
|
|
+ String primitiveFilePath = "./对话记录/";
|
|
|
+ //原始记录复制后文件
|
|
|
+ File destFile = null;
|
|
|
+ //所有记录汇总后文件夹,用来压缩
|
|
|
+ String filePath ="./对话记录/记录下载";
|
|
|
+ //原始记录文件名
|
|
|
+ String fileName = null;
|
|
|
+ for (int i = 0; i < identifiers.size(); i++) {
|
|
|
+ List<IntelligentDialogueEntity> intelligentDialogueEntityList = this.intelligentDialogueService.findByIdentifier(identifiers.get(i));
|
|
|
+ if (intelligentDialogueEntityList != null && intelligentDialogueEntityList.size() > 0) {
|
|
|
+ fileName = identifiers.get(i) + "-智能对话记录-" + DateUtil.getNowTime_CN() + ".xlsx";
|
|
|
+ file = new File(primitiveFilePath + fileName);
|
|
|
+ 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 y = 0; y < intelligentDialogueEntityList.size(); y++) {
|
|
|
+ XSSFRow row = sheet.createRow(y + 1);
|
|
|
+ XSSFCell cellOrder1 = row.createCell(0);
|
|
|
+ if (intelligentDialogueEntityList.get(y).getLabel().equals("0")) {
|
|
|
+ cellOrder1.setCellValue("智能助手:");
|
|
|
+ } else {
|
|
|
+ cellOrder1.setCellValue("用户:");
|
|
|
+ }
|
|
|
+ XSSFCell cellQuestion1 = row.createCell(1);
|
|
|
+ cellQuestion1.setCellValue(intelligentDialogueEntityList.get(y).getContent());
|
|
|
+ }
|
|
|
+ OutputStream outputStream = Files.newOutputStream(file.toPath());
|
|
|
+ workbook.write(outputStream);
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+ if (file.exists()){
|
|
|
+ destFile = new File(filePath+"/"+fileName);
|
|
|
+ //将原始文件进行复制
|
|
|
+ org.apache.commons.io.FileUtils.copyFile(file, destFile);
|
|
|
+ }
|
|
|
+ //删除生成的文件
|
|
|
+ Files.delete(Paths.get(primitiveFilePath + fileName));
|
|
|
+ }
|
|
|
+ //将复制后的整个文件夹打包
|
|
|
+ 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("/dialogueRecord/AllDownload")
|
|
|
+ public Result chatRecordAllDownload(String identifier, HttpServletResponse response) {
|
|
|
+ List<String> identifiers = new ArrayList<>();
|
|
|
+ if (identifier != null && !identifier.equals("")) {
|
|
|
+ identifiers.add(identifier);
|
|
|
+ }else {
|
|
|
+ List<IntelligentDialogueEntity> intelligentDialogueEntitys = this.intelligentDialogueService.findAllIdentifier();
|
|
|
+ for (IntelligentDialogueEntity intelligentDialogueEntity : intelligentDialogueEntitys) {
|
|
|
+ identifiers.add(intelligentDialogueEntity.getIdentifier());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //原始记录文件
|
|
|
+ File file = null;
|
|
|
+ //原始记录文件路径
|
|
|
+ String primitiveFilePath = "./对话记录/";
|
|
|
+ //原始记录复制后文件
|
|
|
+ File destFile = null;
|
|
|
+ //所有记录汇总后文件夹,用来压缩
|
|
|
+ String filePath ="./对话记录/记录下载";
|
|
|
+ //原始记录文件名
|
|
|
+ String fileName = null;
|
|
|
+ for (int i = 0; i < identifiers.size(); i++) {
|
|
|
+ List<IntelligentDialogueEntity> intelligentDialogueEntityList = this.intelligentDialogueService.findByIdentifier(identifiers.get(i));
|
|
|
+ if (intelligentDialogueEntityList != null && intelligentDialogueEntityList.size() > 0) {
|
|
|
+ fileName = identifiers.get(i) + "-智能对话记录-" + DateUtil.getNowTime_CN() + ".xlsx";
|
|
|
+ file = new File(primitiveFilePath + fileName);
|
|
|
+ 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 y = 0; y < intelligentDialogueEntityList.size(); y++) {
|
|
|
+ XSSFRow row = sheet.createRow(y + 1);
|
|
|
+ XSSFCell cellOrder1 = row.createCell(0);
|
|
|
+ if (intelligentDialogueEntityList.get(y).getLabel().equals("0")) {
|
|
|
+ cellOrder1.setCellValue("智能助手:");
|
|
|
+ } else {
|
|
|
+ cellOrder1.setCellValue("用户:");
|
|
|
+ }
|
|
|
+ XSSFCell cellQuestion1 = row.createCell(1);
|
|
|
+ cellQuestion1.setCellValue(intelligentDialogueEntityList.get(y).getContent());
|
|
|
+ }
|
|
|
+ OutputStream outputStream = Files.newOutputStream(file.toPath());
|
|
|
+ workbook.write(outputStream);
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+ if (file.exists()){
|
|
|
+ destFile = new File(filePath+"/"+fileName);
|
|
|
+ //将原始文件进行复制
|
|
|
+ org.apache.commons.io.FileUtils.copyFile(file, destFile);
|
|
|
+ }
|
|
|
+ //删除生成的文件
|
|
|
+ Files.delete(Paths.get(primitiveFilePath + fileName));
|
|
|
+ }
|
|
|
+ //将复制后的整个文件夹打包
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|