diff --git a/laws-modules/src/main/java/com/jero/modules/assessmentLibrary/entity/LawsEvaluationNotMetLibrary.java b/laws-modules/src/main/java/com/jero/modules/assessmentLibrary/entity/LawsEvaluationNotMetLibrary.java index ddcf2b36..a6499cdf 100644 --- a/laws-modules/src/main/java/com/jero/modules/assessmentLibrary/entity/LawsEvaluationNotMetLibrary.java +++ b/laws-modules/src/main/java/com/jero/modules/assessmentLibrary/entity/LawsEvaluationNotMetLibrary.java @@ -5,13 +5,17 @@ import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.io.Serializable; +import java.util.Collections; import java.util.Date; +import java.util.List; import com.fasterxml.jackson.annotation.JsonFormat; import com.jero.common.aspect.annotation.Dict; +import com.jero.modules.assessmentLibrary.entity.vo.MaterialFile; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import org.jeecgframework.poi.excel.annotation.Excel; +import org.jeecgframework.poi.excel.annotation.ExcelCollection; import org.springframework.format.annotation.DateTimeFormat; /** @@ -209,6 +213,13 @@ public class LawsEvaluationNotMetLibrary implements Serializable { @Dict(dictTable = "oss_file", dicCode = "id", dicText = "file_name") private String file; + /** + * 附件 + */ + @ApiModelProperty(value = "附件") + @ExcelCollection(name = "") + private List materialFileList = Collections.emptyList(); + /** * 标准ID */ diff --git a/laws-modules/src/main/java/com/jero/modules/assessmentLibrary/service/impl/LawsEvaluationNotMetLibraryServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/assessmentLibrary/service/impl/LawsEvaluationNotMetLibraryServiceImpl.java index 5208a404..9f155dc0 100644 --- a/laws-modules/src/main/java/com/jero/modules/assessmentLibrary/service/impl/LawsEvaluationNotMetLibraryServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/assessmentLibrary/service/impl/LawsEvaluationNotMetLibraryServiceImpl.java @@ -4,28 +4,45 @@ import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jero.common.exception.JeroBootException; import com.jero.common.system.query.QueryGenerator; +import com.jero.common.util.FileUtils; +import com.jero.common.util.MinioUtil; +import com.jero.common.util.ZipUtil; +import com.jero.modules.assessmentLibrary.entity.LawsConformityAssessmentLibrary; +import com.jero.modules.assessmentLibrary.entity.LawsConformityAssessmentLibraryDetail; import com.jero.modules.assessmentLibrary.entity.LawsEvaluationNotMetLibrary; +import com.jero.modules.assessmentLibrary.entity.vo.LawsConformityAssessmentLibraryExportVO; +import com.jero.modules.assessmentLibrary.entity.vo.MaterialFile; import com.jero.modules.assessmentLibrary.mapper.LawsEvaluationNotMetLibraryMapper; import com.jero.modules.assessmentLibrary.service.ILawsEvaluationNotMetLibraryService; +import com.jero.modules.laws.enterprise.util.BeanCopyUtils; +import com.jero.modules.oss.entity.OSSFile; +import com.jero.modules.oss.service.IOSSFileService; import com.jero.modules.projectLibrary.common.AssessCommon; +import com.jero.modules.system.util.SysWaterMarkUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.*; import org.jeecgframework.poi.excel.ExcelExportUtil; import org.jeecgframework.poi.excel.entity.ExportParams; +import org.jeecgframework.poi.excel.entity.enmus.ExcelType; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.List; +import java.util.*; /** * @author ThinkBook @@ -37,9 +54,17 @@ import java.util.List; public class LawsEvaluationNotMetLibraryServiceImpl extends ServiceImpl implements ILawsEvaluationNotMetLibraryService { + @Resource + private IOSSFileService ossFileService; + @Resource + private SysWaterMarkUtil sysWaterMarkUtil; @Resource private LawsEvaluationNotMetLibraryMapper notMetMapper; + @Value("${jero.path.exportExcelTempPath}") + private String exportExcelTempPath; + private static final String EXPORT_EXCEL_TEMP = "exportExcelTemp"; + @Override public IPage queryPage(LawsEvaluationNotMetLibrary lawsEvaluationNotMet, Integer pageNo, Integer pageSize, HttpServletRequest req) { QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(lawsEvaluationNotMet, req.getParameterMap()); @@ -84,29 +109,116 @@ public class LawsEvaluationNotMetLibraryServiceImpl extends ServiceImpl lawsEvaluationNotMets = this.queryPage(lawsEvaluationNotMet, 1, Integer.MAX_VALUE, req); List records = lawsEvaluationNotMets.getRecords(); - // 设置导出参数 - ExportParams exportParams = new ExportParams(null, sheetName); // 可以为标题添加更多的配置 + this.settleDocuments(url, records); + + // 生成excel + ExportParams exportParams = new ExportParams(); + exportParams.setType(ExcelType.HSSF); + // 复制一份数据 + List newRecordsExport = new ArrayList<>(records); Workbook workbook = ExcelExportUtil.exportExcel(exportParams, LawsEvaluationNotMetLibrary.class, records); - - // 设置响应头和内容类型 - response.setContentType("application/vnd.ms-excel;charset=UTF-8"); // 显式设置字符编码 - response.setCharacterEncoding("UTF-8"); // 这行确保响应流使用UTF-8编码 - - // 处理文件名乱码问题 - String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()).replace("+", "%20"); - response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName + ".xls"); - - // 写出到响应流 - workbook.write(response.getOutputStream()); - workbook.close(); + // 生成超链接 + generateHyperlink(newRecordsExport, workbook); + // 导出excel + workbook.write(fos); + // 生成并导出zip + ZipUtil.toZip(url, response.getOutputStream(), response, true); } catch (Exception e) { log.error("导出excel异常", e); throw new JeroBootException("导出excel异常", e); + } finally { + // 删除文件 + if (new File(url).exists()) { + FileUtils.deleteFolders(url); + } + } + + } + + + private static void generateHyperlink(List vos, Workbook workbook) { + Sheet sheet = workbook.getSheetAt(0); + // 设置字体颜色、下划线 + CellStyle cellStyle = workbook.createCellStyle(); + Font font = workbook.createFont(); + font.setColor(IndexedColors.BLUE.getIndex()); + font.setUnderline(Font.U_SINGLE); + cellStyle.setFont(font); + cellStyle.setWrapText(true); + int i = 1; + for (LawsEvaluationNotMetLibrary vo : vos) { + if (!Objects.isNull(vo.getMaterial()) && !vo.getMaterialFileList().isEmpty()) { + for (MaterialFile materialFile : vo.getMaterialFileList()) { + Row row = sheet.getRow(i); + Cell cell = row.getCell(18); + cell.setCellFormula("HYPERLINK(\"" + materialFile.getFileName() + "\", \"" + materialFile.getFileName() + "\")"); + cell.setCellStyle(cellStyle); + i += 1; + } + } else { + i += 1; + } + } + } + + private void settleDocuments(String url, List excelVos) throws + IOException { + for (LawsEvaluationNotMetLibrary vo : excelVos) { + // 查询文件 + if (StringUtils.isNotBlank(vo.getMaterial())) { + List fileIdList = Arrays.asList(vo.getMaterial().split(",")); + List ossFileList = ossFileService.listByIds(fileIdList); + if (!ossFileList.isEmpty()) { + // 赋值 + List materialFileList = com.jero.common.util.BeanCopyUtils.copyBeanList(ossFileList, MaterialFile.class); + vo.setMaterialFileList(materialFileList); + // 写出文件 + for (OSSFile ossFile : ossFileList) { + InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), ossFile.getUrl()); + // pdf加水印 + if (StringUtils.endsWithIgnoreCase(ossFile.getFileName(), ".pdf")) { + inputStream = sysWaterMarkUtil.addWatermarkToPdf(inputStream); + } + // 导出文件 + String filePath = url + File.separator + ossFile.getFileName(); + writeToLocal(filePath, inputStream); + } + } + } + } + } + + /** + * 将InputStream写入本地文件 + * + * @param filePath + * @param input + * @throws IOException + */ + public void writeToLocal(String filePath, InputStream input) + throws IOException { + int index; + byte[] bytes = new byte[1024]; + try (FileOutputStream downloadFile = new FileOutputStream(filePath)) { + while ((index = input.read(bytes)) != -1) { + downloadFile.write(bytes, 0, index); + downloadFile.flush(); + } + } catch (Exception e) { + log.error("将InputStream写入本地失败:{}", e.getMessage()); + } finally { + input.close(); } } }