diff --git a/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/controller/ParamsReportDetailEOController.java b/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/controller/ParamsReportDetailEOController.java index a3cd6327c..010580e3e 100644 --- a/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/controller/ParamsReportDetailEOController.java +++ b/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/controller/ParamsReportDetailEOController.java @@ -1,15 +1,21 @@ package com.jero.modules.cert.report.controller; +import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.jero.common.api.vo.Result; import com.jero.common.aspect.annotation.AutoLog; +import com.jero.common.exception.JeroBootException; import com.jero.common.system.base.controller.JeroController; +import com.jero.modules.cert.report.entity.ParamsExportTemplateEO; import com.jero.modules.cert.report.entity.ParamsReportConfigEO; import com.jero.modules.cert.report.entity.ParamsReportDetailEO; +import com.jero.modules.cert.report.service.IParamsExportTemplateEOService; import com.jero.modules.cert.report.service.IParamsReportConfigEOService; import com.jero.modules.cert.report.service.IParamsReportDetailEOService; import com.jero.modules.cert.report.vo.ParamsReportDetailVO; +import com.jero.modules.oss.entity.OSSFile; +import com.jero.modules.oss.service.IOSSFileService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; @@ -43,6 +49,13 @@ public class ParamsReportDetailEOController extends JeroController ossFiles = ossFileService.getFileInfosByConnectId(paramsExportTemplateEO.getFileConnectId()); + if (CollectionUtil.isNotEmpty(ossFiles)) { + String fileName = ossFiles.get(0).getFileName(); + String templateFileType = fileName.substring(fileName.lastIndexOf(".")); + if (".doc,.DOC,.docx,.DOCX".contains(templateFileType)) { + paramsReportDetailEOService.exportCustomWord(paramsReportDetailVO, response, request); -// @ApiOperation(value = "上报库参数项-自定义导出excel") -// @GetMapping(value = "/exportCustomExcel") -//// @RequiresPermissions("report:detail:export:custom") -// public void exportCustomExcel(ParamsReportDetailVO paramsReportDetailVO, -// HttpServletResponse response, -// HttpServletRequest request) { -// paramsReportDetailEOService.exportCustomExcel(paramsReportDetailVO, response, request); -// } + } else if (".xls, .XLS,.xlsx,.XLSX".contains(templateFileType)) { + paramsReportDetailEOService.exportCustomExcel(paramsReportDetailVO, response, request); + + } + } else { + throw new JeroBootException("没有找到导出模板!"); + + } + } /** * 列表查询 diff --git a/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/IParamsReportDetailEOService.java b/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/IParamsReportDetailEOService.java index 3ac662c11..925a271be 100644 --- a/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/IParamsReportDetailEOService.java +++ b/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/IParamsReportDetailEOService.java @@ -50,6 +50,9 @@ public interface IParamsReportDetailEOService extends IService> getTemplateLabelList(); - void exportCustom(ParamsReportDetailVO paramsReportDetailVO, HttpServletResponse response, HttpServletRequest request); + // 自定义导出-word模板 + void exportCustomWord(ParamsReportDetailVO paramsReportDetailVO, HttpServletResponse response, HttpServletRequest request); + + void exportCustomExcel(ParamsReportDetailVO paramsReportDetailVO, HttpServletResponse response, HttpServletRequest request); } diff --git a/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/impl/ParamsReportDetailEOServiceImpl.java b/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/impl/ParamsReportDetailEOServiceImpl.java index 1881e36a6..54b443582 100644 --- a/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/impl/ParamsReportDetailEOServiceImpl.java +++ b/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/impl/ParamsReportDetailEOServiceImpl.java @@ -1,5 +1,7 @@ package com.jero.modules.cert.report.service.impl; +import cn.afterturn.easypoi.excel.ExcelExportUtil; +import cn.afterturn.easypoi.excel.entity.TemplateExportParams; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ZipUtil; @@ -23,23 +25,25 @@ import com.jero.modules.cert.report.entity.*; import com.jero.modules.cert.report.enums.ExportTypeEnum; import com.jero.modules.cert.report.mapper.ParamsReportDetailEOMapper; import com.jero.modules.cert.report.service.*; -import com.jero.modules.cert.report.util.Docx4jUtil; import com.jero.modules.cert.report.vo.ParamsReportDetailVO; import com.jero.modules.cert.template.enums.ControlTypeEnum; import com.jero.modules.ocr.util.LineHumpUtil; import com.jero.modules.oss.entity.OSSFile; import com.jero.modules.oss.service.IOSSFileService; +import com.jero.modules.project.util.WordUtil; import com.jero.modules.split.common.ReadExcel; import com.jero.modules.system.entity.SysDictItem; import com.jero.modules.system.service.ISysDictItemService; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.http.entity.ContentType; import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.usermodel.*; import org.aspectj.util.FileUtil; +import org.docx4j.Docx4J; import org.docx4j.openpackaging.exceptions.Docx4JException; +import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mock.web.MockMultipartFile; @@ -504,7 +508,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl allParamsInfoList = queryForExportCustom(paramsReportDetailVO); Map params = allParamsInfoList.get(0); + List> imgListAll = (List>) allParamsInfoList.get(1).get("imgListAll"); String templateUrl = ""; ParamsExportTemplateEO paramsExportTemplateEO = paramsExportTemplateEOService.getById(paramsReportDetailVO.getExportTemplateId()); @@ -702,7 +707,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl allRelevFileList = (List) allParamsInfoList.get(1).get("fileListAll"); + List allRelevFileList = (List) allParamsInfoList.get(2).get("fileListAll"); if (allRelevFileList != null && !allRelevFileList.isEmpty()) { allRelevFileList = allRelevFileList.stream().distinct().collect(Collectors.toList()); downLoadFileList(allRelevFileList,fileNowPath + File.separator + "导出文件"); @@ -713,23 +718,15 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl allParamsInfoList = queryForExportCustom(paramsReportDetailVO); + Map params = allParamsInfoList.get(0); + + String templateUrl = ""; + ParamsExportTemplateEO paramsExportTemplateEO = paramsExportTemplateEOService.getById(paramsReportDetailVO.getExportTemplateId()); + List ossFiles = ossFileService.getFileInfosByConnectId(paramsExportTemplateEO.getFileConnectId()); + if (CollectionUtil.isNotEmpty(ossFiles)) { + templateUrl = ossFiles.get(0).getUrl(); + } + + // 替换模板中的占位符 + try { + + // 拿取模板 + String repFileName = fileName.replaceAll("/","_"); + wordOS = new FileOutputStream(fileNowPath + File.separator + repFileName); + if (CosBootUtil.doesObjectExist(templateUrl)) { + InputStream wordTemplate = CosBootUtil.download(templateUrl); + com.qcloud.cos.utils.IOUtils.copy(wordTemplate, wordOS); + } + + // 替换占位符内容 + TemplateExportParams templateExportParams = new TemplateExportParams(); + templateExportParams.setTemplateUrl(fileNowPath + File.separator + repFileName); + Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, params); + + OutputStream word = new FileOutputStream(fileNowPath + File.separator + repFileName); + workbook.write(word); + wordOS.flush(); + wordOS.close(); + word.flush(); + word.close(); + + // 打包导出压缩包 + response.setHeader("Content-Disposition", + "attachment; filename=\""+ ReadExcel.encodeFileName(fileOriName+".zip", request) +"\""); + response.setContentType("application/force-download"); + response.flushBuffer(); + os = response.getOutputStream(); + ZipUtil.zip(fileNowPath,fileNowPath+".zip"); + FileInputStream fis = new FileInputStream(fileNowPath+".zip"); + int len2 = 0; + while ((len2 = fis.read()) != -1) { + os.write(len2); + } + + os.flush(); + os.close(); // 后开先关 + fis.close(); // 先开后关 + + } catch (Exception e) { + log.error(e.getMessage(), e); + throw new JeroBootException("下载文件失败,请重试"); + + } finally { + IOUtils.closeQuietly(os); + IOUtils.closeQuietly(wordOS); + File tempZipFile = new File(uploadpath + "/tempZip"); + FileUtil.deleteContents(tempZipFile); + } + + } + private void downLoadFileList(List allRelevFileList,String fileNowPath) { File nowFile = new File(fileNowPath); if (nowFile.exists()){ @@ -852,23 +934,23 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl ossFileList = ossFileService.getFileInfosByConnectId(paramsConfigDataEO.getFileConnectId()); if (CollectionUtil.isNotEmpty(ossFileList)) { - configDataBuilder.append(ossFileList.get(0).getFileName()).append("&"); + configDataBuilder.append(ossFileList.get(0).getFileName()).append("#"); fileList.addAll(ossFileList); } } String configData = configDataBuilder.toString(); - if (configData.contains("&")) { - configData = configData.substring(0, configData.lastIndexOf("&")); + if (configData.contains("#")) { + configData = configData.substring(0, configData.lastIndexOf("#")); } record1.put(paramsConfigEO.getId(), configData); @@ -902,17 +984,17 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl queryForExportCustom(ParamsReportDetailVO paramsReportDetailVO) { - paramsReportDetailVO.setSeparator("&"); // 设置分隔符 + paramsReportDetailVO.setSeparator("*"); // 设置分隔符 List idList = new ArrayList<>(); if (StringUtils.isNotEmpty(paramsReportDetailVO.getIds())) { idList = Arrays.asList(paramsReportDetailVO.getIds().split(",")); } - List configIdList = Arrays.asList(paramsReportDetailVO.getConfigIds().split(",")); - List> dataList = paramsReportDetailEOMapper.listInfoForExport(idList, paramsReportDetailVO); + List configIdList = Arrays.asList(paramsReportDetailVO.getConfigIds().split(",")); // 需要导出的配置列 + List> dataList = paramsReportDetailEOMapper.listInfoForExport(idList, paramsReportDetailVO); // 查询需要导出的数据 List paramsConfigEOList = paramsReportConfigEOService.queryList(paramsReportDetailVO.getParamsManifestId()); // 查询所有配置列 List resultList = new ArrayList<>(); Map resultMap = new HashMap<>(); - List fileListAll = new ArrayList<>(); + + List> imgListAll = new ArrayList<>(); // 存放图片信息 + List fileListAll = new ArrayList<>(); // 存放文件 + String fileTypeStr = ".png,.PNG,.jfif,.JFIF,.pjpeg,.PJPEG,.jpeg,.JPEG,.pjp,.PJP,.jpg,.JPG"; // 查询配置列数据 for (Map record1 : dataList) { String paramsCollectManifestId = (String) record1.get("id"); String nioNumber = (String) record1.get("nio_number"); + String controlType = (String) record1.get("control_type"); if (CollectionUtil.isNotEmpty(paramsConfigEOList)) { List fileList = new ArrayList<>(); + List imgFileList = new ArrayList<>(); // 合并配置数据 String configData = ""; List paramsReportConfigDataEOS = paramsReportConfigDataEOService.queryByConfigIdListAndCollectManifestId(configIdList, paramsCollectManifestId); String textDatas = paramsReportConfigDataEOS.stream().filter(e->StringUtils.isNotEmpty(e.getTextData())) - .map(ParamsReportConfigDataEO::getTextData).distinct().collect(Collectors.joining(paramsReportDetailVO.getSeparator())); + .map(ParamsReportConfigDataEO::getTextData).distinct().collect(Collectors.joining(paramsReportDetailVO.getSeparator())); // 整合文本数据 String pullDatas = paramsReportConfigDataEOS.stream().filter(e->StringUtils.isNotEmpty(e.getPullData())) - .map(ParamsReportConfigDataEO::getPullData).distinct().collect(Collectors.joining(paramsReportDetailVO.getSeparator())); + .map(ParamsReportConfigDataEO::getPullData).distinct().collect(Collectors.joining(paramsReportDetailVO.getSeparator())); // 整合下拉数据 List fileNameList = new ArrayList<>(); - paramsReportConfigDataEOS.forEach(paramsReportConfigDataEO -> { + for (ParamsReportConfigDataEO paramsReportConfigDataEO : paramsReportConfigDataEOS) { // 获取”导出文件夹“下的文件 if (StringUtils.isNotEmpty(paramsReportConfigDataEO.getFileConnectId())) { List ossFileList = ossFileService.getFileInfosByConnectId(paramsReportConfigDataEO.getFileConnectId()); if (CollectionUtil.isNotEmpty(ossFileList)) { - fileNameList.add(ossFileList.get(0).getFileName()); + String fileName = ossFileList.get(0).getFileName(); + if (!fileTypeStr.contains(fileName.substring(fileName.lastIndexOf(".")))) { // 图片不放入”导出文件夹“下,直接插入正文 + fileNameList.add(ossFileList.get(0).getFileName()); - fileList.addAll(ossFileList); + fileList.addAll(ossFileList); + } else { + imgFileList.addAll(ossFileList); + } } } - }); + } + fileListAll.addAll(fileList); StringBuilder configDataBuilder = new StringBuilder(); // 重新组合配置数据 - if (StringUtils.isNotEmpty(textDatas)) { - configDataBuilder.append(textDatas).append("&"); - } - if (StringUtils.isNotEmpty(pullDatas)) { - configDataBuilder.append(pullDatas).append("&"); - } - if (CollectionUtil.isNotEmpty(fileNameList)) { - configDataBuilder.append(StringUtils.join(fileNameList, ",")).append("&"); - } - configData = configDataBuilder.toString(); - if (configData.contains("&")) { - configData = configData.substring(0, configData.lastIndexOf("&")); - } - resultMap.put(nioNumber, configData); + if ((ControlTypeEnum.FILE.getValue().equals(controlType) + || ControlTypeEnum.TEXT_FILE.getValue().equals(controlType) + || ControlTypeEnum.PULL_SINGLE_FILE.getValue().equals(controlType) + || ControlTypeEnum.PULL_MORE_FILE.getValue().equals(controlType) + || ControlTypeEnum.TEXT_PULL_SINGLE_FILE.getValue().equals(controlType)) && CollectionUtil.isNotEmpty(imgFileList)) { + // 获取图片字节 + // 附件为图片类型的配置数据,直接插入正文 + Map imgMap = new HashMap<>(); + List bytesList =new ArrayList<>(); - fileListAll.addAll(fileList); + int lengthTotal = 0; // 总长度 + for (OSSFile ossFile : imgFileList){ + if (CosBootUtil.doesObjectExist(ossFile.getUrl())) { + InputStream is = CosBootUtil.download(ossFile.getUrl()); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + try { + IOUtils.copy(is, os); + } catch (IOException e) { + log.error(e.getMessage(), e); + throw new JeroBootException(e.getMessage()); + } + byte[] bytes = os.toByteArray(); + lengthTotal += bytes.length; + bytesList.add(bytes); + } + } + + // 合并多个bytes + byte[] totalByte = new byte[lengthTotal]; + int begin = 0; + for (byte[] bytes : bytesList){ + System.arraycopy(bytes, 0, totalByte, begin, bytes.length); + begin += bytes.length; + } + + if (StringUtils.isNotEmpty(textDatas)) { + configDataBuilder.append(textDatas).append("#"); // + } + if (StringUtils.isNotEmpty(pullDatas)) { + configDataBuilder.append(pullDatas).append("#"); // + } + configData = configDataBuilder.toString(); + if (configData.contains("#")) { // + configData = configData.substring(0, configData.lastIndexOf("#")); + } + + imgMap.put("key", nioNumber); + imgMap.put("text", configData); + imgMap.put("bytes", totalByte); + + imgListAll.add(imgMap); + + } else { + + if (StringUtils.isNotEmpty(textDatas)) { + configDataBuilder.append(textDatas).append("#"); + } + if (StringUtils.isNotEmpty(pullDatas)) { + configDataBuilder.append(pullDatas).append("#"); + } + if (CollectionUtil.isNotEmpty(fileNameList)) { + configDataBuilder.append(StringUtils.join(fileNameList, ",")).append("#"); + } + configData = configDataBuilder.toString(); + if (configData.contains("#")) { + configData = configData.substring(0, configData.lastIndexOf("#")); + } + + resultMap.put(nioNumber, configData); + } } } + Map imgMap = new HashMap<>(); + imgMap.put("imgListAll", imgListAll); Map fileMap = new HashMap<>(); fileMap.put("fileListAll", fileListAll); resultList.add(resultMap); + resultList.add(imgMap); resultList.add(fileMap); return resultList; }