feat: 企标宣贯-带文件导出

This commit is contained in:
2023-09-25 10:13:52 +08:00
parent 5c46f4a347
commit 335163e1c5
8 changed files with 237 additions and 28 deletions
@@ -1059,7 +1059,7 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
zipOut.putNextEntry(fileEntry);
// 将文件取出存入流中
bytes = getMinioFileContent(ossFile);
bytes = ossFileService.getMinioFileContent(ossFile);
zipOut.write(bytes, 0, bytes.length);
zipOut.closeEntry();
}
@@ -1071,29 +1071,6 @@ public class LawsCommonServiceImpl implements ILawsCommonService {
}
}
private byte[] getMinioFileContent(OSSFile file) throws Exception {
String fileUrl = file.getUrl();
String minioUrl = MinioUtil.getMinioUrl();
minioUrl = minioUrl + MinioUtil.getBucketName() + "/";
String url = fileUrl.replace(minioUrl, "");
try (InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
) {
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
return outputStream.toByteArray();
} catch (Exception e) {
log.error(e.getMessage());
throw e;
}
}
/**
* @Author: liao
* @Date: 2023/8/24 9:53
@@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
@@ -55,4 +56,11 @@ public class EnterpriseStandardDeclareController extends JeroController<Enterpri
public void exportData(HttpServletResponse response, ESQueryCommonDTO esQueryCommonDTO) {
esDeclareService.export(response, esQueryCommonDTO, "企标宣贯", "企标宣贯");
}
@AutoLog(value = "企标宣贯-带文件导出")
@ApiOperation(value="企标宣贯-带文件导出", notes="企标宣贯-带文件导出")
@PostMapping("/exportDataWithFile")
public void exportDataWithFile(HttpServletResponse response, ESQueryCommonDTO esQueryCommonDTO) {
esDeclareService.exportDataWithFile(response, esQueryCommonDTO, "企标宣贯", "企标宣贯");
}
}
@@ -0,0 +1,61 @@
package com.jero.modules.laws.enterprise.entity.dto;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* @author: Mzaxd
* @Date: 2023/9/7 14:53
*/
@Data
public class EnterpriseStandardDeclareExPortDto {
@ApiModelProperty(value = "主键id")
private String id;
@Excel(name = "企标编号", width = 15, orderNum = "1")
@ApiModelProperty(value = "企标编号")
private String standardNo;
@Excel(name = "企标名称", width = 15, orderNum = "1")
@ApiModelProperty(value = "企标名称")
private String standardName;
@Excel(name = "发布日期", width = 15, orderNum = "1", format = "yyyy-MM-dd")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")
@ApiModelProperty(value = "发布日期")
private Date releaseDate;
@Excel(name = "实施日期", width = 15, orderNum = "1", format = "yyyy-MM-dd")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")
@ApiModelProperty(value = "实施日期")
private Date implementationDate;
@Excel(name = "宣贯日期", width = 15, orderNum = "1", format = "yyyy-MM-dd")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")
@ApiModelProperty(value = "宣贯日期")
private Date declareDate;
@ApiModelProperty(value = "宣贯人id")
private String declareUser;
@Excel(name = "宣贯人姓名", width = 15, orderNum = "1")
@ApiModelProperty(value = "宣贯人姓名")
private String declareUserName;
@ApiModelProperty(value = "相关材料id")
private String declareFile;
@Excel(name = "相关材料", width = 15, orderNum = "1")
@ApiModelProperty(value = "相关材料文件名")
private String declareFileName;
}
@@ -3,9 +3,9 @@ package com.jero.modules.laws.enterprise.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jero.modules.laws.enterprise.entity.EnterpriseStandardDeclare;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jero.modules.laws.enterprise.entity.dto.ESPlanQueryDTO;
import com.jero.modules.laws.enterprise.entity.dto.ESQueryCommonDTO;
import com.jero.modules.laws.enterprise.entity.vo.EnterpriseStandardDeclareVo;
import com.jero.modules.oss.entity.OSSFile;
import javax.servlet.http.HttpServletResponse;
@@ -33,4 +33,13 @@ public interface EnterpriseStandardDeclareService extends IService<EnterpriseSta
* @param sheetName
*/
void export(HttpServletResponse response, ESQueryCommonDTO esQueryCommonDTO, String fileName, String sheetName);
/**
* 企标宣贯带文件导出
* @param response
* @param parameterMap
* @param fileName
* @param sheetName
*/
void exportDataWithFile(HttpServletResponse response, ESQueryCommonDTO parameterMap, String fileName, String sheetName);
}
@@ -1,16 +1,19 @@
package com.jero.modules.laws.enterprise.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.modules.laws.enterprise.entity.EnterpriseStandardDeclare;
import com.jero.modules.laws.enterprise.entity.dto.ESPlanQueryDTO;
import com.jero.modules.laws.enterprise.entity.dto.ESQueryCommonDTO;
import com.jero.modules.laws.enterprise.entity.dto.EnterpriseStandardDeclareExPortDto;
import com.jero.modules.laws.enterprise.entity.vo.EnterpriseStandardDeclareVo;
import com.jero.modules.laws.enterprise.entity.vo.EnterpriseStandardPlanVo;
import com.jero.modules.laws.enterprise.service.EnterpriseStandardDeclareService;
import com.jero.modules.laws.enterprise.mapper.EnterpriseStandardDeclareMapper;
import com.jero.modules.laws.enterprise.util.BeanCopyUtils;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
@@ -20,7 +23,12 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author ThinkBook
@@ -36,6 +44,9 @@ public class EnterpriseStandardDeclareServiceImpl extends ServiceImpl<Enterprise
@Resource
private EnterpriseStandardDeclareMapper esDeclareMapper;
@Resource
private IOSSFileService ossFileService;
@Override
public IPage<EnterpriseStandardDeclareVo> queryPageList(ESQueryCommonDTO esDeclare, Integer pageNo, Integer pageSize) {
IPage<EnterpriseStandardDeclareVo> page = new Page<>(pageNo, pageSize);
@@ -65,6 +76,80 @@ public class EnterpriseStandardDeclareServiceImpl extends ServiceImpl<Enterprise
throw new JeroBootException("导出excel异常", e);
}
}
@Override
public void exportDataWithFile(HttpServletResponse response, ESQueryCommonDTO esQueryCommonDTO, String fileName, String sheetName) {
try {
// 获取数据
IPage<EnterpriseStandardDeclareVo> enterpriseStandardDeclareVoIPage = this.queryPageList(esQueryCommonDTO, 1, Integer.MAX_VALUE);
List<EnterpriseStandardDeclareVo> records = enterpriseStandardDeclareVoIPage.getRecords();
// 设置文件字段
this.setRecordFile(records);
List<EnterpriseStandardDeclareExPortDto> enterpriseStandardDeclareExPorts = BeanCopyUtils.copyBeanList(records, EnterpriseStandardDeclareExPortDto.class);
// 设置导出参数
ExportParams exportParams = new ExportParams(null, sheetName); // 可以为标题添加更多的配置
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, EnterpriseStandardDeclareExPortDto.class, enterpriseStandardDeclareExPorts);
// 设置响应头和内容类型
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
// 使用 ByteArrayOutputStream 来捕获 Excel 数据
ByteArrayOutputStream excelOutputStream = new ByteArrayOutputStream();
workbook.write(excelOutputStream);
workbook.close();
// 准备ZIP
// 获取编码后的文件名
String encodedZipName = URLEncoder.encode(fileName + ".zip", "UTF-8");
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=" + encodedZipName);
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "no-store");
response.setHeader("Expires", "0");
ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
// 将Excel加入ZIP
ZipEntry excelEntry = new ZipEntry(fileName + ".xls");
zipOut.putNextEntry(excelEntry);
byte[] bytes = excelOutputStream.toByteArray();
zipOut.write(bytes, 0, bytes.length);
zipOut.closeEntry();
List<String> fileIds = records.stream().map(EnterpriseStandardDeclareVo::getDeclareFile).collect(Collectors.toList());
LambdaQueryWrapper<OSSFile> ossFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
ossFileLambdaQueryWrapper.in(OSSFile::getId, fileIds);
List<OSSFile> files = ossFileService.list(ossFileLambdaQueryWrapper);
Map<String, OSSFile> ossFileMap = files.stream().collect(Collectors.toMap(OSSFile::getId, ossFile -> ossFile));
// 将文件和文件夹加入ZIP
for (EnterpriseStandardDeclareVo vo : records) {
OSSFile ossFile = ossFileMap.get(vo.getDeclareFile());
ZipEntry fileEntry = new ZipEntry(vo.getDeclareFileName());
zipOut.putNextEntry(fileEntry);
// 将文件取出存入流中
bytes = ossFileService.getMinioFileContent(ossFile);
zipOut.write(bytes, 0, bytes.length);
zipOut.closeEntry();
}
zipOut.close();
} catch (Exception e) {
log.error("导出zip异常", e);
throw new JeroBootException("导出zip异常", e);
}
}
/**
* 设置导出的Excel中的相关文件字段
* @param records
*/
private void setRecordFile(List<EnterpriseStandardDeclareVo> records) {
records.forEach(record -> record.setDeclareFileName(record.getStandardNo().replace("/", "_") + "/" + record.getDeclareFileName()));
}
}
@@ -0,0 +1,36 @@
package com.jero.modules.laws.enterprise.util;
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Mzaxd
* @since 2022-11-22 15:36
*/
public class BeanCopyUtils {
private BeanCopyUtils() {
}
public static <V> V copyBean(Object source, Class<V> clazz){
V result = null;
//创建目标对象
try {
result = clazz.newInstance();
//实现属性copy
BeanUtils.copyProperties(source, result);
} catch (Exception e) {
e.printStackTrace();
}
//返回结果
return result;
}
public static <O,V> List<V> copyBeanList(List<O> list, Class<V> clazz) {
return list.stream()
.map(o -> copyBean(o, clazz))
.collect(Collectors.toList());
}
}