|
|
@@ -13,6 +13,7 @@ import com.jeeplus.common.excel.annotation.ExportMode;
|
|
|
import com.jeeplus.common.redis.RedisUtils;
|
|
|
import com.jeeplus.common.utils.ResponseUtil;
|
|
|
import com.jeeplus.finance.invoice.domain.CwFinanceInvoice;
|
|
|
+import com.jeeplus.finance.invoice.service.CwFinanceInvoiceDownloadService;
|
|
|
import com.jeeplus.finance.invoice.service.CwFinanceInvoiceService;
|
|
|
import com.jeeplus.finance.invoice.service.OMS.*;
|
|
|
import com.jeeplus.finance.invoice.service.dto.*;
|
|
|
@@ -80,6 +81,9 @@ public class CwFinanceInvoiceController {
|
|
|
@Resource
|
|
|
private RedInvoiceRetryScheduledService redInvoiceRetryScheduledService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private CwFinanceInvoiceDownloadService cwFinanceInvoiceDownloadService;
|
|
|
+
|
|
|
/**
|
|
|
* 查询发票列表
|
|
|
* @param cwFinanceInvoiceDTO
|
|
|
@@ -94,7 +98,12 @@ public class CwFinanceInvoiceController {
|
|
|
IPage<CwFinanceInvoiceDTO> result = new Page<CwFinanceInvoiceDTO>();
|
|
|
result = cwFinanceInvoiceService.findList (page, cwFinanceInvoiceDTO,"");
|
|
|
System.out.println("当前时间1:"+System.currentTimeMillis());
|
|
|
+ // 查询当前用户已下载的发票ID集合
|
|
|
+ List<String> downloadedIds = cwFinanceInvoiceDownloadService.getDownloadedInvoiceIds();
|
|
|
+ Set<String> downloadedSet = new HashSet<>(downloadedIds);
|
|
|
result.getRecords().stream().forEach(i -> {
|
|
|
+ // 设置下载状态
|
|
|
+ i.setDownloadStatus(downloadedSet.contains(i.getId()) ? "1" : "0");
|
|
|
// 发票申请
|
|
|
if (StringUtils.isNotBlank(i.getTaskId()) && StringUtils.isNotBlank(i.getStatus())) {
|
|
|
if ("2".equals(i.getStatus())) { // “审核中”的数据要获取数据审核人
|
|
|
@@ -928,7 +937,7 @@ public class CwFinanceInvoiceController {
|
|
|
* 下载附件(文件没有时间戳前缀)
|
|
|
*/
|
|
|
@GetMapping("/downLoadOMSInvoiceAttachzip")
|
|
|
- public void downLoadOMSInvoiceAttachzip(@RequestParam("fileUrl")String fileUrl,@RequestParam("invoiceNo")String invoiceNo, HttpServletResponse response) {
|
|
|
+ public void downLoadOMSInvoiceAttachzip(@RequestParam("fileUrl")String fileUrl, @RequestParam("invoiceNo")String invoiceNo, @RequestParam(value = "invoiceId", required = false)String invoiceId, HttpServletResponse response) {
|
|
|
long timestamp = System.currentTimeMillis();
|
|
|
UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
|
|
|
String filePath = null;
|
|
|
@@ -1011,6 +1020,15 @@ public class CwFinanceInvoiceController {
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
+
|
|
|
+ // 记录单条下载行为
|
|
|
+ if (StringUtils.isNotBlank(invoiceId)) {
|
|
|
+ try {
|
|
|
+ cwFinanceInvoiceDownloadService.saveDownloadRecords(Collections.singletonList(invoiceId), "single");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("记录发票下载行为异常,invoiceId={}", invoiceId, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public static void deleteFile(File folder) {
|
|
|
@@ -1403,4 +1421,113 @@ public class CwFinanceInvoiceController {
|
|
|
Files.copy(file.toPath(), response.getOutputStream());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询当前用户已下载的发票ID列表
|
|
|
+ */
|
|
|
+ @ApiOperation(value = "查询已下载发票ID列表")
|
|
|
+ @GetMapping("/downloadedInvoiceIds")
|
|
|
+ public ResponseEntity<List<String>> getDownloadedInvoiceIds() {
|
|
|
+ List<String> ids = cwFinanceInvoiceDownloadService.getDownloadedInvoiceIds();
|
|
|
+ return ResponseEntity.ok(ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量下载数电票
|
|
|
+ * 将多张发票的OMS附件打包为ZIP返回,并记录下载行为
|
|
|
+ */
|
|
|
+ @ApiOperation(value = "批量下载数电票")
|
|
|
+ @PostMapping("/batchDownloadInvoice")
|
|
|
+ public void batchDownloadInvoice(@RequestBody CwFinanceInvoiceBatchDownloadDTO dto, HttpServletResponse response) {
|
|
|
+ List<String> invoiceIds = dto.getInvoiceIds();
|
|
|
+ if (invoiceIds == null || invoiceIds.isEmpty()) {
|
|
|
+ try {
|
|
|
+ response.setContentType("application/json;charset=utf-8");
|
|
|
+ response.getWriter().write("{\"code\":500,\"msg\":\"请选择需要下载的发票\"}");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (invoiceIds.size() > 25) {
|
|
|
+ try {
|
|
|
+ response.setContentType("application/json;charset=utf-8");
|
|
|
+ response.getWriter().write("{\"code\":500,\"msg\":\"最多只能选择25条发票进行批量下载\"}");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录下载行为
|
|
|
+ cwFinanceInvoiceDownloadService.saveDownloadRecords(invoiceIds, "batch");
|
|
|
+
|
|
|
+ // 查询发票数据获取omsAttachmentUrl
|
|
|
+ List<CwFinanceInvoiceDTO> invoices = cwFinanceInvoiceService.queryByIds(invoiceIds);
|
|
|
+ List<String> failureMessages = new ArrayList<>();
|
|
|
+ String baseFilePath;
|
|
|
+ if (System.getProperty("os.name").toLowerCase().contains("win")) {
|
|
|
+ baseFilePath = "D:/attachment-file/batch_invoice_" + System.currentTimeMillis();
|
|
|
+ } else {
|
|
|
+ baseFilePath = "/attachment-file/batch_invoice_" + System.currentTimeMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ File baseFolder = new File(baseFilePath);
|
|
|
+ if (!baseFolder.exists()) {
|
|
|
+ baseFolder.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 逐个下载OMS附件到本地临时目录
|
|
|
+ for (CwFinanceInvoiceDTO invoice : invoices) {
|
|
|
+ if (org.apache.commons.lang3.StringUtils.isBlank(invoice.getOmsAttachmentUrl())) {
|
|
|
+ failureMessages.add("发票[" + invoice.getNo() + "]无OMS附件");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String invoicePath = baseFilePath + "/" + invoice.getNo();
|
|
|
+ File invoiceFolder = new File(invoicePath);
|
|
|
+ if (!invoiceFolder.exists()) {
|
|
|
+ invoiceFolder.mkdirs();
|
|
|
+ }
|
|
|
+ SpringUtil.getBean(IWorkAttachmentApi.class).downloadFolderFromOss(invoice.getOmsAttachmentUrl(), invoicePath + "/");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("批量下载-发票[{}]附件下载失败", invoice.getNo(), e);
|
|
|
+ failureMessages.add("发票[" + invoice.getNo() + "]下载失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 打包为ZIP
|
|
|
+ FileUtil.zipFolder(baseFilePath, baseFilePath + ".zip");
|
|
|
+ File zipFile = new File(baseFilePath + ".zip");
|
|
|
+
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFile.getName(), "UTF-8"));
|
|
|
+ response.setContentType("application/octet-stream;charset=utf-8");
|
|
|
+
|
|
|
+ try (InputStream inputStream = new FileInputStream(zipFile);
|
|
|
+ BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
|
|
|
+ OutputStream outputStream = response.getOutputStream()) {
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = bufferedInputStream.read(bytes)) != -1) {
|
|
|
+ outputStream.write(bytes, 0, len);
|
|
|
+ }
|
|
|
+ outputStream.flush();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将失败信息写入response header
|
|
|
+ if (!failureMessages.isEmpty()) {
|
|
|
+ response.setHeader("X-Download-Failures", URLEncoder.encode(String.join(";", failureMessages), "UTF-8"));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("批量下载发票异常", e);
|
|
|
+ } finally {
|
|
|
+ // 清理临时文件
|
|
|
+ deleteFile(baseFolder);
|
|
|
+ File zipFile = new File(baseFilePath + ".zip");
|
|
|
+ if (zipFile.exists()) {
|
|
|
+ zipFile.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|