diff --git a/jianshui-admin/src/main/java/com/jianshui/web/controller/platform/InvoiceDownloadController.java b/jianshui-admin/src/main/java/com/jianshui/web/controller/platform/InvoiceDownloadController.java index 82ddd8e..80f82da 100644 --- a/jianshui-admin/src/main/java/com/jianshui/web/controller/platform/InvoiceDownloadController.java +++ b/jianshui-admin/src/main/java/com/jianshui/web/controller/platform/InvoiceDownloadController.java @@ -25,7 +25,7 @@ public class InvoiceDownloadController { @Autowired private InvoiceDownloadService invoiceDownloadService; @ApiOperation("发票批量下载") - @GetMapping("/dowmLoadPdf/{ids}/{status}") + @GetMapping("/dowmLoad/{ids}/{status}") public Object downLoad(@PathVariable Long[] ids,@PathVariable String status,HttpServletResponse response){ if (InvoiceCommonConstants.PDFSTATUS.equals(status)){ return invoiceDownloadService.dowmLoadPdf(ids,response); diff --git a/jianshui-common/src/main/java/com/jianshui/common/constant/InvoiceCommonConstants.java b/jianshui-common/src/main/java/com/jianshui/common/constant/InvoiceCommonConstants.java index 06afb5a..cbc6c0e 100644 --- a/jianshui-common/src/main/java/com/jianshui/common/constant/InvoiceCommonConstants.java +++ b/jianshui-common/src/main/java/com/jianshui/common/constant/InvoiceCommonConstants.java @@ -93,4 +93,13 @@ public class InvoiceCommonConstants { */ public final static String OFDSTATUS = "1"; + /** + * 未查询到企业信息 + */ + public final static String NOTCOMPANYINFO = "未查询到企业相关信息!"; + + /** + * 未查询到发票信息 + */ + public final static String NOTINVOICEINFO = "未查询到发票信息!"; } diff --git a/jianshui-platform/src/main/java/com/jianshui/platform/dto/DownLoadDTO.java b/jianshui-platform/src/main/java/com/jianshui/platform/dto/DownLoadDTO.java new file mode 100644 index 0000000..36388bb --- /dev/null +++ b/jianshui-platform/src/main/java/com/jianshui/platform/dto/DownLoadDTO.java @@ -0,0 +1,25 @@ +package com.jianshui.platform.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotEmpty; + +/** + * @Author: kane + * @Description: 发票下载信息 + * @CreateTime: 2023-06-11 12:54 + * @Version: 1.0 + **/ +@ApiModel("发票下载") +@Data +public class DownLoadDTO { + + @ApiModelProperty(value = "发票号码") + private String fphm; + + @NotEmpty + @ApiModelProperty(value = "发票下载地址") + private String url; +} diff --git a/jianshui-platform/src/main/java/com/jianshui/platform/service/impl/InvoiceDownloadServiceImpl.java b/jianshui-platform/src/main/java/com/jianshui/platform/service/impl/InvoiceDownloadServiceImpl.java index 754d610..f8dddd8 100644 --- a/jianshui-platform/src/main/java/com/jianshui/platform/service/impl/InvoiceDownloadServiceImpl.java +++ b/jianshui-platform/src/main/java/com/jianshui/platform/service/impl/InvoiceDownloadServiceImpl.java @@ -15,9 +15,11 @@ import com.jianshui.invoice.domain.Invoice; import com.jianshui.invoice.domain.dto.HXResponse; import com.jianshui.invoice.domain.dto.api.jcsk.FileAcquisitionTwoDTO; import com.jianshui.invoice.mapper.InvoiceMapper; +import com.jianshui.platform.dto.DownLoadDTO; import com.jianshui.platform.service.InvoiceDownloadService; import com.jianshui.platform.utils.BatchDownloadUtils; import com.jianshui.platform.utils.InvoiceAllYhdjUtils; +import com.jianshui.platform.vo.DownLoadErrorVO; import com.jianshui.system.domain.InvoiceAllYhdj; import com.jianshui.system.mapper.CompanyserviceMapper; import com.jianshui.system.mapper.SysUserMapper; @@ -64,6 +66,9 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService { SysUser sysUser = sysUserMapper.selectUserById(userId); //根据企业id获取企业信息 Companyservice companyservice = companyServiceMapper.selectCompanyserviceByCompanyid(sysUser.getCompanyId()); + if (companyservice == null){ + return AjaxResult.error(InvoiceCommonConstants.NOTCOMPANYINFO); + } //获取登记用户信息 InvoiceAllYhdjUtils invoiceAllYhdjUtils = new InvoiceAllYhdjUtils(); InvoiceAllYhdj userInfo = invoiceAllYhdjUtils.getUserInfo(companyservice); @@ -71,12 +76,20 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService { return AjaxResult.error(InvoiceCommonConstants.NOINFORMATIONISFOUND); } //pfd文件集合 - List pdfList = new ArrayList<>(); + List pdfDownLoad = new ArrayList<>(); + //下载失败集合 + List errorList = new ArrayList<>(); for (Long id : ids) { + DownLoadDTO downLoadDTO = new DownLoadDTO(); Invoice invoice = invoiceMapper.selectInvoiceById(id); + if (invoice == null){ + return AjaxResult.error(InvoiceCommonConstants.NOTINVOICEINFO); + } String invoicePdfUrl = invoice.getInvoicePdfUrl(); if (invoicePdfUrl != null) { - pdfList.add(invoicePdfUrl); + downLoadDTO.setUrl(invoicePdfUrl); + downLoadDTO.setFphm(invoice.getFphm()); + pdfDownLoad.add(downLoadDTO); } else { //获取pfd下载地址 //封装请求实体类 @@ -94,6 +107,8 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService { String kprqFormat = simpleDateFormat.format(kprq); map.put(InvoiceCommonConstants.KPRQ, kprqFormat); fileAcquisitionDTO.setJsonData(map); + //下载失败信息类 + DownLoadErrorVO downLoadErrorVO = new DownLoadErrorVO(); cn.hutool.json.JSONObject result = null; try { result = ApiHttp.request(WebServiceConstant.FPEWMXZ, WebServiceConstant.URL, fileAcquisitionDTO, companyservice); @@ -103,20 +118,34 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService { if (InvoiceCommonConstants.SUCCESSCODE.equals(code) && StrUtil.isNotEmpty(data)) { cn.hutool.json.JSONObject json = JSONUtil.parseObj(data); String pdfUrl = json.get(InvoiceCommonConstants.PDFURL) != null ? json.get(InvoiceCommonConstants.PDFURL).toString() : ""; - pdfList.add(pdfUrl); + downLoadDTO.setFphm(invoice.getFphm()); + downLoadDTO.setUrl(pdfUrl); + pdfDownLoad.add(downLoadDTO); } else { - return AjaxResult.error(msg); + //设置错误信息 + downLoadErrorVO.setFphm(invoice.getFphm()); + downLoadErrorVO.setMsg(msg); + errorList.add(downLoadErrorVO); } } catch (Exception e) { e.printStackTrace(); log.error("【金四服务类】【金财数科】【获取发票下载地址】API请求异常,外部报文返回code非0000。错误信息:{}", e.getMessage()); - return AjaxResult.error(InvoiceCommonConstants.DOWNLOADADDRESSERROR); + //设置错误信息 + downLoadErrorVO.setFphm(invoice.getFphm()); + downLoadErrorVO.setMsg(e.getMessage()); + errorList.add(downLoadErrorVO); } } } - //下载pfd文件 - BatchDownloadUtils.downloadAndZipFiles(pdfList,InvoiceCommonConstants.PDFSTATUS,response); - return AjaxResult.success(InvoiceCommonConstants.DOWNLOADSUCCESSCODE); + if (errorList.size() > 0 || errorList != null){ + //下载pdf文件集合 + BatchDownloadUtils.downloadAndZipFiles(pdfDownLoad,InvoiceCommonConstants.PDFSTATUS,response); + return AjaxResult.error(errorList); + }else { + //下载pdf文件集合; + BatchDownloadUtils.downloadAndZipFiles(pdfDownLoad,InvoiceCommonConstants.PDFSTATUS,response); + return AjaxResult.success(InvoiceCommonConstants.DOWNLOADSUCCESSCODE); + } } /** @@ -137,6 +166,9 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService { SysUser sysUser = sysUserMapper.selectUserById(userId); //根据企业id获取企业信息 Companyservice companyservice = companyServiceMapper.selectCompanyserviceByCompanyid(sysUser.getCompanyId()); + if (companyservice == null){ + return AjaxResult.error(InvoiceCommonConstants.NOTCOMPANYINFO); + } //获取登记用户信息 InvoiceAllYhdjUtils invoiceAllYhdjUtils = new InvoiceAllYhdjUtils(); InvoiceAllYhdj userInfo = invoiceAllYhdjUtils.getUserInfo(companyservice); @@ -144,12 +176,20 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService { return new HXResponse(InvoiceCommonConstants.NOINFORMATIONISFOUND); } //ofd文件集合 - List ofdList = new ArrayList<>(); + List ofdDownLoad = new ArrayList<>(); + //下载失败集合 + List errorList = new ArrayList<>(); for (Long id : ids) { Invoice invoice = invoiceMapper.selectInvoiceById(id); + DownLoadDTO downLoadDTO = new DownLoadDTO(); + if (invoice == null){ + return AjaxResult.error(InvoiceCommonConstants.NOTINVOICEINFO); + } String invoiceOfdUrl = invoice.getcOfdUrl(); if (invoiceOfdUrl != null) { - ofdList.add(invoiceOfdUrl); + downLoadDTO.setUrl(invoiceOfdUrl); + downLoadDTO.setFphm(invoice.getFphm()); + ofdDownLoad.add(downLoadDTO); } else { //获取ofd下载地址 //封装请求实体类 @@ -167,6 +207,8 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService { String kprqFormat = simpleDateFormat.format(kprq); map.put(InvoiceCommonConstants.KPRQ, kprqFormat); fileAcquisitionDTO.setJsonData(map); + //下载失败信息类 + DownLoadErrorVO downLoadErrorVO = new DownLoadErrorVO(); cn.hutool.json.JSONObject result = null; try { result = ApiHttp.request(WebServiceConstant.FPEWMXZ, WebServiceConstant.URL, fileAcquisitionDTO, companyservice); @@ -176,20 +218,34 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService { if (InvoiceCommonConstants.SUCCESSCODE.equals(code) && StrUtil.isNotEmpty(data)) { cn.hutool.json.JSONObject json = JSONUtil.parseObj(data); String ofdUrl = json.get(InvoiceCommonConstants.OFDURL) != null ? json.get(InvoiceCommonConstants.OFDURL).toString() : ""; - ofdList.add(ofdUrl); + downLoadDTO.setFphm(invoice.getFphm()); + downLoadDTO.setUrl(ofdUrl); + ofdDownLoad.add(downLoadDTO); } else { - return AjaxResult.error(msg); + //设置错误信息 + downLoadErrorVO.setFphm(invoice.getFphm()); + downLoadErrorVO.setMsg(msg); + errorList.add(downLoadErrorVO); } } catch (Exception e) { e.printStackTrace(); log.error("【金四服务类】【金财数科】【获取发票下载地址】API请求异常,外部报文返回code非0000。错误信息:{}", e.getMessage()); - return AjaxResult.error(InvoiceCommonConstants.DOWNLOADADDRESSERROR); + //设置错误信息 + downLoadErrorVO.setFphm(invoice.getFphm()); + downLoadErrorVO.setMsg(e.getMessage()); + errorList.add(downLoadErrorVO); } } } - //下载ofd文件集合 - BatchDownloadUtils.downloadAndZipFiles(ofdList,InvoiceCommonConstants.OFDSTATUS,response); - return AjaxResult.success(InvoiceCommonConstants.DOWNLOADSUCCESSCODE); + if (errorList.size() > 0 || errorList != null){ + //下载ofd文件集合 + BatchDownloadUtils.downloadAndZipFiles(ofdDownLoad,InvoiceCommonConstants.OFDSTATUS,response); + return AjaxResult.error(errorList); + }else { + //下载ofd文件集合 + BatchDownloadUtils.downloadAndZipFiles(ofdDownLoad,InvoiceCommonConstants.OFDSTATUS,response); + return AjaxResult.success(InvoiceCommonConstants.DOWNLOADSUCCESSCODE); + } } diff --git a/jianshui-platform/src/main/java/com/jianshui/platform/utils/BatchDownloadUtils.java b/jianshui-platform/src/main/java/com/jianshui/platform/utils/BatchDownloadUtils.java index f4b4ef4..691281d 100644 --- a/jianshui-platform/src/main/java/com/jianshui/platform/utils/BatchDownloadUtils.java +++ b/jianshui-platform/src/main/java/com/jianshui/platform/utils/BatchDownloadUtils.java @@ -1,7 +1,12 @@ package com.jianshui.platform.utils; import com.jianshui.common.constant.InvoiceCommonConstants; +import com.jianshui.invoice.domain.Invoice; +import com.jianshui.invoice.mapper.InvoiceMapper; +import com.jianshui.platform.dto.DownLoadDTO; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; + import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; @@ -9,8 +14,10 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.UUID; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -21,13 +28,15 @@ import java.util.zip.ZipOutputStream; * @Version: 1.0 **/ public class BatchDownloadUtils { + /** * 功能描述: 批量下载 + * * @param status 文件类型0-pfd 1-ofd - * @param urls + * @param downLoad * @return : void */ - public static void downloadAndZipFiles(List urls, String status, HttpServletResponse response) { + public static void downloadAndZipFiles(List downLoad, String status, HttpServletResponse response) { Date now = new Date(); SimpleDateFormat format = new SimpleDateFormat(InvoiceCommonConstants.DATEFORMAT); String name = format.format(now); @@ -40,13 +49,15 @@ public class BatchDownloadUtils { response.setCharacterEncoding("utf-8"); ops = response.getOutputStream(); zos = new ZipOutputStream(ops); - for (String url : urls) { + String fileName = ""; + for (DownLoadDTO downLoadDTO : downLoad) { + String url = downLoadDTO.getUrl(); if (StringUtils.hasText(url)) { URL downloadUrl = new URL(url); HttpURLConnection httpConn = (HttpURLConnection) downloadUrl.openConnection(); int responseCode = httpConn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { - String fileName = getFileName(httpConn, status); + fileName = getFileName(httpConn, status,downLoadDTO.getFphm()); inputStream = httpConn.getInputStream(); ZipEntry zipEntry = new ZipEntry(fileName); zos.putNextEntry(zipEntry); @@ -61,22 +72,22 @@ public class BatchDownloadUtils { } } catch (Exception ex) { ex.printStackTrace(); - }finally { - if (inputStream != null){ + } finally { + if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } - if (zos != null){ + if (zos != null) { try { zos.close(); } catch (IOException e) { e.printStackTrace(); } } - if (ops != null){ + if (ops != null) { try { ops.close(); } catch (IOException e) { @@ -89,35 +100,30 @@ public class BatchDownloadUtils { /** * 功能描述: 获取下载后的文件名 + * * @param httpConn * @return : java.lang.String */ - private static String getFileName(HttpURLConnection httpConn, String status) { + private static String getFileName(HttpURLConnection httpConn, String status,String fphm) { String fileName = ""; String disposition = httpConn.getHeaderField("Content-Disposition"); - Date date = new Date(); - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(InvoiceCommonConstants.DATEFORMAT); if (disposition != null) { int index = disposition.indexOf("filename="); if (index > 0) { fileName = disposition.substring(index + 10, disposition.length() - 1); - if ("0".equals(status)) { - String name = simpleDateFormat.format(date); - fileName = name + ".pdf"; + if (InvoiceCommonConstants.PDFSTATUS.equals(status)) { + fileName = fphm + ".pdf"; } else { - String name = simpleDateFormat.format(date); - fileName = name + ".ofd"; + fileName = fphm + ".ofd"; } } } if (StringUtils.isEmpty(fileName)) { fileName = httpConn.getURL().getFile().substring(httpConn.getURL().getFile().lastIndexOf("/") + 1); - if ("0".equals(status)) { - String name = simpleDateFormat.format(date); - fileName = name + ".pdf"; + if (InvoiceCommonConstants.PDFSTATUS.equals(status)) { + fileName = fphm + ".pdf"; } else { - String name = simpleDateFormat.format(date); - fileName = name + ".ofd"; + fileName = fphm + ".ofd"; } } return fileName; diff --git a/jianshui-platform/src/main/java/com/jianshui/platform/vo/DownLoadErrorVO.java b/jianshui-platform/src/main/java/com/jianshui/platform/vo/DownLoadErrorVO.java new file mode 100644 index 0000000..259ff22 --- /dev/null +++ b/jianshui-platform/src/main/java/com/jianshui/platform/vo/DownLoadErrorVO.java @@ -0,0 +1,21 @@ +package com.jianshui.platform.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +/** + * @Author: kane + * @Description: 下载失败实体类 + * @CreateTime: 2023-06-11 09:27 + * @Version: 1.0 + **/ +@ApiModel("下载失败") +@Data +public class DownLoadErrorVO { + + @ApiModelProperty("发票号码") + private String fphm; + + @ApiModelProperty("错误信息") + private String msg; +}