From eca5251c6652f6fd73b7983aa9349eacfb492dde Mon Sep 17 00:00:00 2001 From: sunzheng Date: Thu, 19 Sep 2024 13:59:03 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BF=AE=E6=94=B9=E5=8F=82=E4=BC=9A?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E7=9A=84=E5=AF=BC=E5=85=A5=E4=B8=BA=E6=8E=A5?= =?UTF-8?q?=E6=94=B6=E4=B8=80=E4=B8=AA=E5=8E=8B=E7=BC=A9=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/LawsClubMeetingServiceImpl.java | 147 +++++++++++++++--- .../vo/excel/LawsClubMeetingMemberExcel.java | 5 + 2 files changed, 131 insertions(+), 21 deletions(-) diff --git a/laws-modules/src/main/java/com/jero/modules/laws/club/service/impl/LawsClubMeetingServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/laws/club/service/impl/LawsClubMeetingServiceImpl.java index f7638131..f6e51489 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/club/service/impl/LawsClubMeetingServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/club/service/impl/LawsClubMeetingServiceImpl.java @@ -3,8 +3,10 @@ package com.jero.modules.laws.club.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jero.common.api.vo.Result; +import com.jero.common.constant.enums.LanguageEnum; import com.jero.common.exception.JeroBootException; import com.jero.common.system.vo.DictModel; +import com.jero.common.util.MessageUtils; import com.jero.modules.laws.club.entity.LawsClubMeeting; import com.jero.modules.laws.club.mapper.LawsClubMeetingMapper; import com.jero.modules.laws.club.service.ILawsClubMeetingService; @@ -13,6 +15,7 @@ import com.jero.modules.laws.club.vo.excel.LawsClubMeetingExcel; import com.jero.modules.laws.club.vo.excel.LawsClubMeetingMemberExcel; import com.jero.modules.oss.entity.OSSFile; import com.jero.modules.oss.service.IOSSFileService; +import com.jero.modules.split.common.FileUnZip; import com.jero.modules.system.entity.SysDepart; import com.jero.modules.system.entity.SysUser; import com.jero.modules.system.service.ISysDepartService; @@ -20,11 +23,14 @@ import com.jero.modules.system.service.ISysDictService; import com.jero.modules.system.service.ISysUserService; import com.jero.modules.tag.entity.LawsTag; import lombok.SneakyThrows; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Workbook; import org.jeecgframework.poi.excel.ExcelImportUtil; import org.jeecgframework.poi.excel.entity.ImportParams; import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; @@ -32,17 +38,19 @@ import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.util.*; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; /** *

@@ -63,6 +71,8 @@ public class LawsClubMeetingServiceImpl extends ServiceImpl lawsTags = ExcelUtil.createLawsTags(fieldTxts, mustInputFlags, fieldTypeLists); + // 生成 Excel 文件并写入字节数组 try (Workbook workbook = ExcelUtil.createWorkBookWithLawsTags(lawsTags, "参会信息", describe); - OutputStream out = response.getOutputStream()) { + ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + // 将Excel写入到字节数组 + workbook.write(baos); + byte[] excelBytes = baos.toByteArray(); + // 生成ZIP文件 + ByteArrayOutputStream baosZip = new ByteArrayOutputStream(); + try (ZipOutputStream zos = new ZipOutputStream(baosZip)) { + // 将Excel文件放入ZIP包中的目录下 + ZipEntry entry = new ZipEntry( "参会信息导入模板.xls"); + zos.putNextEntry(entry); + zos.write(excelBytes); + zos.closeEntry(); + } // 设置HTTP响应头 - response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setContentType("application/zip;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); // 确保响应流使用UTF-8编码 // 处理文件名乱码问题 - String encodedFileName = URLEncoder.encode("基本信息", StandardCharsets.UTF_8.name()).replace("+", "%20"); - response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName + ".xls"); - // 写入Excel并输出 - workbook.write(out); - out.flush(); + String encodedFileName = URLEncoder.encode("导出模板", StandardCharsets.UTF_8.name()).replace("+", "%20"); + response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName + ".zip"); + // 输出ZIP文件到响应流中 + try (OutputStream out = response.getOutputStream()) { + baosZip.writeTo(out); + out.flush(); + } } catch (IOException e) { - log.error("导出Excel失败", e); + log.error("导出模板失败", e); } } @@ -99,19 +124,33 @@ public class LawsClubMeetingServiceImpl extends ServiceImpl fileMap = multipartRequest.getFileMap(); List lawsClubMeetingList = new ArrayList<>(); + File saveDirectory = null; for (Map.Entry entity : fileMap.entrySet()) { MultipartFile file = entity.getValue();// 获取上传文件对象 try { + Map unZipFileMap = analyzeZipFile(file); + saveDirectory = unZipFileMap.get("saveDirectory"); + // 移除键为 "saveDirectory" 的文件 + unZipFileMap.remove("saveDirectory"); + // 拿到excel的导入文件 + File meetingCnTemplate = unZipFileMap.get("参会信息导入模板.xls"); + File meetingEnTemplate = unZipFileMap.get("Conference InformationImport Template.xls"); + // 确定哪个文件不为空,并赋值给 excelImportFile + File excelImportFile = (meetingCnTemplate != null) ? meetingCnTemplate : meetingEnTemplate; + // 将模板文件移除 + unZipFileMap.remove("参会信息导入模板.xls"); + unZipFileMap.remove("Conference InformationImport Template.xls"); ImportParams params = new ImportParams(); params.setNeedSave(false); - List excelList = ExcelImportUtil.importExcel(file.getInputStream(), LawsClubMeetingExcel.class, params); + List excelList = ExcelImportUtil.importExcel(new FileInputStream(excelImportFile), LawsClubMeetingExcel.class, params); excelList = excelList.stream().skip(2).filter(item -> !Objects.isNull(item)).collect(Collectors.toList()); if (CollectionUtils.isEmpty(excelList)) { throw new JeroBootException("没有数据可导入"); } // 处理分类数据 List meetingExcels = processingMeetingData(excelList); - StringBuilder msg = validateExcelData(meetingExcels); // 校验数据 + // 校验数据 + StringBuilder msg = validateExcelData(meetingExcels, unZipFileMap); if (StringUtils.isNotEmpty(msg.toString())) { return Result.error(msg.toString()); } @@ -127,6 +166,10 @@ public class LawsClubMeetingServiceImpl extends ServiceImpl meetingExcels) { + public Map analyzeZipFile(MultipartFile file) throws IOException { + int pos = file.getOriginalFilename().lastIndexOf("."); + String str = file.getOriginalFilename().substring(pos+1).toLowerCase(); + String filenameorg = file.getOriginalFilename().substring(0,pos); + String resultMsg = ""; + if (!str.equals("zip")) { + if(MessageUtils.getLanguage().getValue().equals(LanguageEnum.CN.getValue())){ + resultMsg = "请上传zip文件"; + }else{ + resultMsg = "Please upload a zip file"; + } + throw new JeroBootException(resultMsg); + } + String path = uploadpath + File.separator + "modal" + File.separator + filenameorg + System.currentTimeMillis(); + File saveDirectory = new File(path); + if(!saveDirectory.isDirectory()){ + saveDirectory.mkdir(); + } + FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path +File.separator+ file.getOriginalFilename())); + try { + //解压缩 + String zipEntryName = FileUnZip.unZipFiles2(path +File.separator+ file.getOriginalFilename(), path); + // 1.获取其中的文件, + List fileList = FileUnZip.readExcelFile(zipEntryName); + if (fileList.size() < 1) { + FileUnZip.deleteDir(saveDirectory); + if(MessageUtils.getLanguage().getValue().equals(LanguageEnum.CN.getValue())){ + resultMsg = "没有可导入的数据,请检查!"; + }else{ + resultMsg = "No data to import, please check"; + } + throw new JeroBootException(resultMsg); + } + Map fileMap = fileList.stream().collect(Collectors.toMap(File::getName, Function.identity())); + File meetingCnTemplate = fileMap.get("参会信息导入模板.xls"); + File meetingEnTemplate = fileMap.get("Conference InformationImport Template.xls"); + if (Objects.isNull(meetingCnTemplate) && Objects.isNull(meetingEnTemplate)) { + //删除原上传文件 + FileUnZip.deleteDir(saveDirectory); + if(MessageUtils.getLanguage().getValue().equals(LanguageEnum.CN.getValue())){ + resultMsg = "压缩包内没有Excel模板文件"; + }else{ + resultMsg = "In the cabinet did not upload the Excel template file"; + } + throw new JeroBootException(resultMsg); + } + fileMap.put("saveDirectory", saveDirectory); + return fileMap; + } catch (StringIndexOutOfBoundsException e){ + //删除原上传文件 + FileUnZip.deleteDir(saveDirectory); + if(MessageUtils.getLanguage().getValue().equals(LanguageEnum.CN.getValue())){ + resultMsg = "读取失败,压缩包内需要把所有导入文件放入一个父级文件夹下"; + }else{ + resultMsg = "The data fails to be read. In the compressed package, all imported files need to be placed in a parent folder"; + } + throw new JeroBootException(resultMsg); + } + } + + + private StringBuilder validateExcelData(List meetingExcels, Map unZipFileMap) throws IOException{ StringBuilder msg = new StringBuilder(); // 拿到这个流程的名称 List specializedModuleList = sysDictService.getDictItems("professional_module"); @@ -235,14 +339,15 @@ public class LawsClubMeetingServiceImpl extends ServiceImpl ossFiles = iossFileService.list(new LambdaQueryWrapper() - .eq(OSSFile::getFileName, lawsClubMeetingMemberExcel.getMeetingFileId()) - .orderByDesc(OSSFile::getCreateTime) - .last("LIMIT 1")); - if (CollectionUtils.isEmpty(ossFiles)) { - msgSub.append(getErrorMsg(i, "参会报告:" + lawsClubMeetingMemberExcel.getMeetingFileId() + "不存在;")); + File file = unZipFileMap.get(lawsClubMeetingMemberExcel.getMeetingFileId()); + if (Objects.isNull(file)) { + msgSub.append(getErrorMsg(i, "参会报告:" + lawsClubMeetingMemberExcel.getMeetingFileId() + "在压缩包内不存在;")); }else { - lawsClubMeetingMemberExcel.setMeetingFileId(ossFiles.get(0).getId()); + FileInputStream input = new FileInputStream(file); + MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), "application/octet-stream", input); + // 上传该文件 + Result ossFileResult = iossFileService.commonUpload(multipartFile, ""); + lawsClubMeetingMemberExcel.setMeetingFileId(ossFileResult.getResult().getId()); } } } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/club/vo/excel/LawsClubMeetingMemberExcel.java b/laws-modules/src/main/java/com/jero/modules/laws/club/vo/excel/LawsClubMeetingMemberExcel.java index 6935c106..b3747ae2 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/club/vo/excel/LawsClubMeetingMemberExcel.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/club/vo/excel/LawsClubMeetingMemberExcel.java @@ -1,5 +1,6 @@ package com.jero.modules.laws.club.vo.excel; +import com.jero.common.aspect.annotation.Dict; import lombok.Data; import org.jeecgframework.poi.excel.annotation.Excel; import java.io.Serializable; @@ -8,15 +9,19 @@ import java.io.Serializable; public class LawsClubMeetingMemberExcel implements Serializable { @Excel(name = "参会人员", width = 20) + @Dict(dictTable = "sys_user", dicCode = "id", dicText = "realname") private String memberId; @Excel(name = "参会部门", width = 20) + @Dict(dictTable = "sys_depart", dicCode = "id", dicText = "depart_name") private String departId; @Excel(name = "参会部门专业模块", width = 20) + @Dict(dicCode = "professional_module") private String meetingSpecializedModule; @Excel(name = "参会报告", width = 20) + @Dict(dictTable = "oss_file", dicCode = "id", dicText = "file_name") private String meetingFileId; @Excel(name = "备注", width = 20)