diff --git a/jianshui-admin/src/main/resources/application.yml b/jianshui-admin/src/main/resources/application.yml index aa11daa..971af7c 100644 --- a/jianshui-admin/src/main/resources/application.yml +++ b/jianshui-admin/src/main/resources/application.yml @@ -119,3 +119,10 @@ srm: # basic的账户密码 username: rendertopod pwd: renderpod!2 +invoice: + file: + save: + #本地存储时配置路径使用 + path: /invoice/file/upload + # 文件上传方式 0:数据库 1:oss 2:本地 + type: 1 diff --git a/jianshui-common/src/main/java/com/jianshui/common/constant/Constants.java b/jianshui-common/src/main/java/com/jianshui/common/constant/Constants.java index 0a3d7f9..16111d4 100644 --- a/jianshui-common/src/main/java/com/jianshui/common/constant/Constants.java +++ b/jianshui-common/src/main/java/com/jianshui/common/constant/Constants.java @@ -1,6 +1,9 @@ package com.jianshui.common.constant; import io.jsonwebtoken.Claims; +import org.apache.xmlbeans.impl.xb.xsdschema.Public; + +import javax.lang.model.element.NestingKind; /** * 通用常量信息 @@ -165,5 +168,28 @@ public class Constants { public static final String JSONArray = "JSONArray"; + public static final String STRING_0 = "0"; + public static final String STRING_1 = "1"; + public static final String STRING_2 = "2"; + public static final String PDF = "pdf"; + public static final String PDF_UPCASE = "PDF"; + public static final String OFD = "ofd"; + public static final String OFD_UPCASE = "OFD"; + + public static final String POINT = "."; + + public static final String LOCAL = "local"; + + public static final String DATA = "data"; + + public static final String FILE_URL = "file_url"; + + public static final String INVOICE_FILE_SAVE_PATH = "invoice.file.save.path"; + + public static final String INVOICE_FILE_SAVE_TYPE = "invoice.file.save.type"; + + public static final String ESCAPE = "/"; + + } \ No newline at end of file diff --git a/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java b/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java index 3af7b6b..6015d58 100644 --- a/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java +++ b/jianshui-invoice/src/main/java/com/jianshui/invoice/service/impl/api/ElephantInvoiceApiV6ServiceImpl.java @@ -80,6 +80,8 @@ import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletRequest; +import java.io.FileOutputStream; +import java.io.IOException; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.security.InvalidKeyException; @@ -612,70 +614,24 @@ public class ElephantInvoiceApiV6ServiceImpl implements IInvoiceApiService { if(ddfpxx.getPDFZJL() != null && StrUtil.isNotEmpty(ddfpxx.getPDFZJL())){ String fphm = ddfpxx.getFPHM(); if(StrUtil.isNotEmpty(fphm)){ - InvoiceFile invoiceFile = new InvoiceFile(); - invoiceFile.setFphm(ddfpxx.getFPHM()); - List invoiceFileList = invoiceFileMapper.selectInvoiceFileList(invoiceFile); - if(invoiceFileList != null && invoiceFileList.size() > 0){ - invoiceFile = invoiceFileList.get(0); - invoiceFile.setFileContent(ddfpxx.getPDFZJL()); - invoiceFile.setFileName(fileName); - invoiceFile.setCreateTime(new Date()); - invoiceFile.setIdentity(companyservice.getIdentity()); - invoiceFile.setTax(companyservice.getSellertax()); - fileId = invoiceFile.getFileId(); - fileName = invoiceFile.getFileName(); - invoiceFileMapper.updateInvoiceFile(invoiceFile); - }else{ - invoiceFile.setId(fileId); - invoiceFile.setFileContent(ddfpxx.getPDFZJL()); - invoiceFile.setFileId(fileId); - invoiceFile.setFileName(fileName); - invoiceFile.setCreateTime(new Date()); - invoiceFile.setIdentity(companyservice.getIdentity()); - invoiceFile.setTax(companyservice.getSellertax()); - invoiceFileMapper.insertInvoiceFile(invoiceFile); - } - - // 2023/11/16 kk:上传OSS转下载地址关闭,改为直接返回大象的文件流 - invoice.setInvoicePdfUrl(ELE_File_URL + fileId); - ICompanyservicePropService companyserviceProp = SpringUtils.getBean(ICompanyservicePropService.class); - CompanyserviceProp secretIdProp = companyserviceProp.selectPropByKey(companyservice.getCompanyid(), "elephant_invoice_file_dc"); - if (secretIdProp != null) { - invoice.setInvoicePdfUrl(secretIdProp.getValue() + fileId); + Environment environment = SpringUtils.getBean(Environment.class); + + String type = environment.getProperty(Constants.INVOICE_FILE_SAVE_TYPE); + + //配置文件存储类型0为数据库存储 + if (Constants.STRING_0.equals(type)){ + saveFileUseDb(invoice, fileId, fileName,companyservice, ddfpxx); + //配置文件存储类型1为oss存储 + }else if (Constants.STRING_1.equals(type)){ + saveFileUseOss(defaultStorage,invoice,ddfpxx); + //配置文件存储类型2为本地存储 + }else if (Constants.STRING_2.equals(type)){ + saveFileUseLocal(environment,invoice,ddfpxx,fileName); } } } // end - - - /*if (StringUtils.isNotEmpty(defaultStorage) && StringUtils.isEmpty(invoice.getInvoicePdfUrl())) { - try { - IStorageService service = storageFactory.getStorageInstance(defaultStorage); - String pdfzjl = ddfpxx.getPDFZJL(); - String zjllx = ddfpxx.getZJLLX(); - String pdfName = IdUtils.fastSimpleUUID() + "." + (StringUtils.equals(zjllx, "PDF") ? "pdf" : "ofd"); - if (StringUtils.isNotEmpty(pdfzjl)) { - byte[] pdfBytes = Base64.decode(pdfzjl); - String invoicePath = Constants.RESOURCE_PREFIX + "/invoice/" + pdfName; - AjaxResult uploadResult = service.upload(pdfBytes, invoicePath); - if (uploadResult.isSuccess()) { - StorageUrlDTO uploadResultDTO = (StorageUrlDTO) uploadResult.get("data"); - String fileUrl = configService.selectConfigByKey("file_url"); - if (StringUtils.isNotEmpty(fileUrl)) { - uploadResultDTO.setContentType(zjllx.toLowerCase()); - String pdfUrl = fileUrl + "/" + Base64.encode(JSONObject.toJSONString(uploadResultDTO)); - invoice.setInvoicePdfUrl(pdfUrl); - invoice.setInvoiceFileStorage(uploadResultDTO.getServiceProvider()); - if (StringUtils.equals(zjllx, "OFD")) { - invoice.setcOfdUrl(pdfUrl); - } - } - } - } - } catch (JianshiSystemErrorException e) { - e.printStackTrace(); - } - }*/ + debugLog("断点13", uuid, timestamp); if (invoice.getId() != null) { @@ -768,6 +724,117 @@ public class ElephantInvoiceApiV6ServiceImpl implements IInvoiceApiService { return response; } + private void saveFileUseLocal(Environment environment,Invoice invoice,DDFPXX ddfpxx,String fileName) { + try { + byte[] pdfBytes = Base64.decode(ddfpxx.getPDFZJL()); + //文件名称 + fileName = fileName+ Constants.POINT + (StringUtils.equals(ddfpxx.getZJLLX(), Constants.PDF_UPCASE) ? Constants.PDF : Constants.OFD); + //文件写入路径 + String targetFilePath = environment.getProperty(Constants.INVOICE_FILE_SAVE_PATH) + fileName; + // 创建一个输出流对象,用于将数据写入到本地文件,如果文件存在则会覆盖 + FileOutputStream fileOutputStream = new FileOutputStream(targetFilePath, false); + + // 直接将字节数组写入到输出流(即本地文件) + fileOutputStream.write(pdfBytes); + + // 完成写入后关闭流 + fileOutputStream.close(); + //拼接url + + IStorageService service = storageFactory.getStorageInstance(Constants.LOCAL); + + AjaxResult upload = service.upload(targetFilePath, ""); + + StorageUrlDTO uploadResultDTO = (StorageUrlDTO) upload.get(Constants.DATA); + + String fileUrl = configService.selectConfigByKey(Constants.FILE_URL); + +// uploadResultDTO.setStoragePath(targetFilePath); + uploadResultDTO.setContentType(ddfpxx.getZJLLX().toLowerCase()); + + Base64.encode(JSONObject.toJSONString(uploadResultDTO)); + + String pdfUrl = fileUrl + Constants.ESCAPE + Base64.encode(JSONObject.toJSONString(uploadResultDTO)); + invoice.setInvoicePdfUrl(pdfUrl); + invoice.setInvoiceFileStorage(uploadResultDTO.getServiceProvider()); + if (StringUtils.equals(ddfpxx.getZJLLX(), Constants.OFD_UPCASE)) { + invoice.setcOfdUrl(pdfUrl); + } + }catch (UnsupportedEncodingException e){ + log.error("编码转换异常",e); + }catch (IOException e){ + log.error("文件写入异常",e); + }catch (Exception e){ + log.error("文件保存异常",e); + } + + } + + private void saveFileUseOss(String defaultStorage,Invoice invoice,DDFPXX ddfpxx) { + if (StringUtils.isNotEmpty(defaultStorage) && StringUtils.isEmpty(invoice.getInvoicePdfUrl())) { + try { + IStorageService service = storageFactory.getStorageInstance(defaultStorage); + String pdfzjl = ddfpxx.getPDFZJL(); + String zjllx = ddfpxx.getZJLLX(); + String pdfName = IdUtils.fastSimpleUUID() + "." + (StringUtils.equals(zjllx, "PDF") ? "pdf" : "ofd"); + if (StringUtils.isNotEmpty(pdfzjl)) { + byte[] pdfBytes = Base64.decode(pdfzjl); + String invoicePath = Constants.RESOURCE_PREFIX + "/invoice/" + pdfName; + AjaxResult uploadResult = service.upload(pdfBytes, invoicePath); + if (uploadResult.isSuccess()) { + StorageUrlDTO uploadResultDTO = (StorageUrlDTO) uploadResult.get("data"); + String fileUrl = configService.selectConfigByKey("file_url"); + if (StringUtils.isNotEmpty(fileUrl)) { + uploadResultDTO.setContentType(zjllx.toLowerCase()); + String pdfUrl = fileUrl + "/" + Base64.encode(JSONObject.toJSONString(uploadResultDTO)); + invoice.setInvoicePdfUrl(pdfUrl); + invoice.setInvoiceFileStorage(uploadResultDTO.getServiceProvider()); + if (StringUtils.equals(zjllx, "OFD")) { + invoice.setcOfdUrl(pdfUrl); + } + } + } + } + } catch (JianshiSystemErrorException e) { + e.printStackTrace(); + } + } + } + + private void saveFileUseDb(Invoice invoice,String fileId,String fileName,Companyservice companyservice,DDFPXX ddfpxx) { + InvoiceFile invoiceFile = new InvoiceFile(); + invoiceFile.setFphm(ddfpxx.getFPHM()); + List invoiceFileList = invoiceFileMapper.selectInvoiceFileList(invoiceFile); + if(invoiceFileList != null && invoiceFileList.size() > 0){ + invoiceFile = invoiceFileList.get(0); + invoiceFile.setFileContent(ddfpxx.getPDFZJL()); + invoiceFile.setFileName(fileName); + invoiceFile.setCreateTime(new Date()); + invoiceFile.setIdentity(companyservice.getIdentity()); + invoiceFile.setTax(companyservice.getSellertax()); + fileId = invoiceFile.getFileId(); + fileName = invoiceFile.getFileName(); + invoiceFileMapper.updateInvoiceFile(invoiceFile); + }else{ + invoiceFile.setId(fileId); + invoiceFile.setFileContent(ddfpxx.getPDFZJL()); + invoiceFile.setFileId(fileId); + invoiceFile.setFileName(fileName); + invoiceFile.setCreateTime(new Date()); + invoiceFile.setIdentity(companyservice.getIdentity()); + invoiceFile.setTax(companyservice.getSellertax()); + invoiceFileMapper.insertInvoiceFile(invoiceFile); + } + + // 2023/11/16 kk:上传OSS转下载地址关闭,改为直接返回大象的文件流 + invoice.setInvoicePdfUrl(ELE_File_URL + fileId); + ICompanyservicePropService companyserviceProp = SpringUtils.getBean(ICompanyservicePropService.class); + CompanyserviceProp secretIdProp = companyserviceProp.selectPropByKey(companyservice.getCompanyid(), "elephant_invoice_file_dc"); + if (secretIdProp != null) { + invoice.setInvoicePdfUrl(secretIdProp.getValue() + fileId); + } + } + /** * 批量发票查询接口 *