2 Commits 48f065e030 ... 3b3e72a7b6

Autor SHA1 Mensagem Data
  王苗苗 3b3e72a7b6 语音文件的url调整 1 semana atrás
  王苗苗 57d847cc96 新增通过通过记录ID导出语音 和 通过筛选条件导出语音接口 1 semana atrás

+ 126 - 3
slibra-admin/src/main/java/com/slibra/web/controller/business/TCallRecordController.java

@@ -1,13 +1,15 @@
 package com.slibra.web.controller.business;
 
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import javax.servlet.http.HttpServletResponse;
 
 import com.slibra.common.core.domain.R;
+import com.slibra.common.exception.ServiceException;
+import com.slibra.common.utils.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.CollectionUtils;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PutMapping;
@@ -25,6 +27,21 @@ import com.slibra.business.service.ITCallRecordService;
 import com.slibra.common.utils.poi.ExcelUtil;
 import com.slibra.common.core.page.TableDataInfo;
 
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.core.io.Resource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
 /**
  * 通话记录Controller
  * 
@@ -38,6 +55,12 @@ public class TCallRecordController extends BaseController
     @Autowired
     private ITCallRecordService tCallRecordService;
 
+    @Value("${DOMAIN}")
+    private String domain;
+
+    @Value("${URL_DISK_PREFIX}")
+    private String urlDiskPrefix;
+
     /**
      * 查询通话记录列表
      */
@@ -147,4 +170,104 @@ public class TCallRecordController extends BaseController
     {
         return R.ok(tCallRecordService.updateFile2TextByIdOneLine(id));
     }
+
+    //目录
+    private static final String FILE_STORAGE_LOCATION = "/Users/wangmiaomiao/Downloads/mlvrwj.20250217/";
+
+    /**
+     * 通过通话记录的ID,下载单条通话语音
+     * @param id
+     * @return
+     * @throws IOException
+     */
+    @GetMapping("/downloadById")
+    public ResponseEntity<InputStreamResource> downloadFile(@RequestParam Long id) throws IOException {
+        TCallRecord tCallRecord = this.tCallRecordService.selectTCallRecordById(id);
+        if(Objects.isNull(tCallRecord))
+            throw new ServiceException("未查询到录音");
+        String url = tCallRecord.getUrl();
+        if(StringUtils.isBlank(url))
+            throw new ServiceException("未查询到录音");
+        //查询服务器上的文件
+        url = url.replace(domain, urlDiskPrefix);
+        // 构造文件的完整路径
+        File file = new File(url);
+
+        // 检查文件是否存在
+        if (!file.exists()) {
+            throw new RuntimeException("File not found ");
+        }
+
+        // 读取文件内容到字节数组
+        byte[] fileData = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
+
+        // 设置HTTP响应头
+        HttpHeaders header = new HttpHeaders();
+        header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"");
+        header.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
+
+        // 返回文件内容作为响应体
+        return ResponseEntity.ok()
+                .headers(header)
+                .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
+    }
+
+
+    /**
+     * 根据筛选条件,批量下载语音
+     * @param tCallRecord
+     * @return
+     * @throws IOException
+     */
+    @PostMapping("/downloadBatchByCondition")
+    public ResponseEntity<Resource> downloadBatchFiles(TCallRecord tCallRecord) throws IOException {
+        //查询数据
+        List<TCallRecord> tCallRecords = this.tCallRecordService.selectTCallRecordList(tCallRecord);
+        if(CollectionUtils.isEmpty(tCallRecords))
+            throw new ServiceException("未查询到数据");
+        List<String> fileNames = new ArrayList<>(tCallRecords.size());
+        for (TCallRecord callRecord : tCallRecords) {
+            String url = callRecord.getUrl();
+            if(StringUtils.isNotBlank(url))
+                fileNames.add(url.replace(domain, urlDiskPrefix));
+        }
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        try (ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) {
+            for (String fileName : fileNames) {
+//                File file = new File(FILE_STORAGE_LOCATION + fileName);
+                File file = new File(fileName);
+                if (!file.exists()) {
+                    logger.error("File not found: " + fileName);
+                    continue;//查询其他的
+//                    throw new RuntimeException("File not found: " + fileName);
+                }
+                addFileToZip(file, file.getName(), zipOutputStream);
+            }
+        }
+
+        byte[] zipContent = byteArrayOutputStream.toByteArray();
+        ByteArrayResource resource = new ByteArrayResource(zipContent);
+
+        HttpHeaders headers = new HttpHeaders();
+        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"batch_download.zip\"");
+        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
+
+        return ResponseEntity.ok()
+                .headers(headers)
+                .contentLength(zipContent.length)
+                .body(resource);
+    }
+
+    private void addFileToZip(File file, String entryName, ZipOutputStream zipOut) throws IOException {
+        try (FileInputStream fis = new FileInputStream(file)) {
+            ZipEntry zipEntry = new ZipEntry(entryName);
+            zipOut.putNextEntry(zipEntry);
+
+            byte[] bytes = new byte[1024];
+            int length;
+            while ((length = fis.read(bytes)) >= 0) {
+                zipOut.write(bytes, 0, length);
+            }
+        }
+    }
 }