|
@@ -0,0 +1,271 @@
|
|
|
+package com.rongwei.bscommon.sys.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import com.aspose.cells.*;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.rongwei.bsentity.domain.ZhcxInspectionReportManageDo;
|
|
|
+import com.rongwei.bscommon.sys.dao.ZhcxInspectionReportManageDao;
|
|
|
+import com.rongwei.bscommon.sys.service.ZhcxInspectionReportManageService;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.rongwei.rwadmincommon.system.domain.SysDictDo;
|
|
|
+import com.rongwei.rwadmincommon.system.service.SysDictService;
|
|
|
+import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
+import com.rongwei.rwcommon.utils.StringUtils;
|
|
|
+import com.rongwei.rwcommoncomponent.excel.aspose.ExcelUtils;
|
|
|
+import com.rongwei.rwcommoncomponent.excel.utils.ExcelHelpUtils;
|
|
|
+import com.rongwei.rwcommoncomponent.file.service.SysFileItemService;
|
|
|
+import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 检查报告管理表 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author wm
|
|
|
+ * @since 2024-10-30
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ZhcxInspectionReportManageServiceImpl extends ServiceImpl<ZhcxInspectionReportManageDao, ZhcxInspectionReportManageDo> implements ZhcxInspectionReportManageService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysFileItemService sysFileItemService;
|
|
|
+ @Autowired
|
|
|
+ private ExcelUtils excelUtils;
|
|
|
+ @Autowired
|
|
|
+ private SysDictService sysDictService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void downloadExcel(HttpServletResponse response, ZhcxInspectionReportManageDo zhcxInspectionReportManageDo) {
|
|
|
+ String id = zhcxInspectionReportManageDo.getId();
|
|
|
+ if(StringUtils.isEmpty(id)){
|
|
|
+ throw new CustomException("参数异常,请联系管理员排查");
|
|
|
+ }
|
|
|
+ SysFileItemDo templateFile = sysFileItemService.getById("c100d791c13a4ae08a2030476759b50d");
|
|
|
+ Map<String, Object> map = this.getMap(Wrappers.<ZhcxInspectionReportManageDo>lambdaQuery()
|
|
|
+ .eq(ZhcxInspectionReportManageDo::getId, id)
|
|
|
+ .eq(ZhcxInspectionReportManageDo::getDeleted, "0"));
|
|
|
+ if(!excelUtils.GetLicense()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Object dataappendix = map.get("DATAAPPENDIX");
|
|
|
+ if(dataappendix != null){
|
|
|
+ if (StringUtils.isNotBlank(dataappendix.toString())) {
|
|
|
+ String[] splitFile = dataappendix.toString().split("\\^_\\^");
|
|
|
+ map.put("total", "本报告包括数据附页"+splitFile.length+"份。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Workbook wb = ExcelHelpUtils.getWorkbook(templateFile.getFullpath());
|
|
|
+ Worksheet sheet0 = wb.getWorksheets().get(0);
|
|
|
+ Cells cells = sheet0.getCells();
|
|
|
+ for (int row = 0; row < cells.getMaxDataRow() + 1; row++) {
|
|
|
+ for (int col = 0; col < cells.getMaxDataColumn() + 1; col++) {
|
|
|
+ Cell cell = cells.get(row, col);
|
|
|
+ String cellValue = cell.getStringValue();
|
|
|
+ // 检查占位符并替换对应的数据
|
|
|
+ if (cellValue.startsWith("${") && cellValue.endsWith("}")) {
|
|
|
+ String key = cellValue.substring(2, cellValue.length() - 1);
|
|
|
+ Object value = map.get(key);
|
|
|
+ if (value != null) {
|
|
|
+ // 将占位符替换为实际值
|
|
|
+ if (key.equals("ISSUES")) {
|
|
|
+ List<SysDictDo> list = sysDictService.getDictsByType("inspection_report_manage_issues");
|
|
|
+ List<SysDictDo> collect = list.stream().filter(item -> !item.getPid().equals("-1")).collect(Collectors.toList());
|
|
|
+ int maxLength = collect.stream()
|
|
|
+ .map(SysDictDo -> SysDictDo.getValue().length())
|
|
|
+ .max(Integer::compare)
|
|
|
+ .orElse(0);
|
|
|
+ StringBuilder textValue = new StringBuilder();
|
|
|
+ for (int i = 0; i < collect.size(); i++) {
|
|
|
+ int length = collect.get(i).getValue().length();
|
|
|
+ int i1 = maxLength - length;
|
|
|
+ String join = String.join("", Collections.nCopies(i1, " "));
|
|
|
+ if (collect.get(i).getValue().equals(value)) {
|
|
|
+ textValue.append(" R ").append(collect.get(i).getValue()).append(join);
|
|
|
+ } else {
|
|
|
+ textValue.append(" £ ").append(collect.get(i).getValue()).append(join);
|
|
|
+ }
|
|
|
+ if((i+1) % 5 == 0){
|
|
|
+ textValue.append("\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 创建一个样式对象
|
|
|
+ Style style = cell.getStyle();
|
|
|
+
|
|
|
+ // 设置水平左对齐
|
|
|
+ style.setHorizontalAlignment(TextAlignmentType.LEFT);
|
|
|
+ style.setVerticalAlignment(TextAlignmentType.CENTER);
|
|
|
+ // 设置向右缩进(单位是缩进级别)
|
|
|
+ // 缩进级别为2,可根据需要调整
|
|
|
+ //style.setIndentLevel(1);
|
|
|
+
|
|
|
+ // 设置字体
|
|
|
+ Font font = style.getFont();
|
|
|
+ font.setName("Wingdings 2");
|
|
|
+ font.setSize(10);
|
|
|
+ cell.setStyle(style);
|
|
|
+ cell.putValue(textValue);
|
|
|
+ }else if(key.equals("FILES")){
|
|
|
+ Range mergedRange = cell.getMergedRange();
|
|
|
+ // 确定合并单元格的起始行和列
|
|
|
+ int startRow = mergedRange.getFirstRow();
|
|
|
+ int startCol = mergedRange.getFirstColumn();
|
|
|
+ if (StringUtils.isNotBlank(value.toString())) {
|
|
|
+ String[] splitFile = value.toString().split("\\^_\\^");
|
|
|
+ for(int i = 0; i < splitFile.length; i++){
|
|
|
+ if(i > 0){
|
|
|
+ startCol = startCol+3;
|
|
|
+ }
|
|
|
+ String fileid = splitFile[i].split("-;-")[1];
|
|
|
+ SysFileItemDo fileItemDo = sysFileItemService.getById(fileid);
|
|
|
+ if (fileItemDo != null) {
|
|
|
+ try (BufferedInputStream inputStream = FileUtil.getInputStream(fileItemDo.getFullpath())) {
|
|
|
+ int pictureIndex = sheet0.getPictures().add(startRow, startCol, inputStream, 100, 100); // 设定宽度和高度
|
|
|
+ Picture picture = sheet0.getPictures().get(pictureIndex);
|
|
|
+
|
|
|
+ // 获取合并单元格的宽度和高度
|
|
|
+ double mergedCellWidth = sheet0.getCells().getColumnWidthPixel(startCol) * mergedRange.getColumnCount(); // 合并了多列
|
|
|
+ double mergedCellHeight = sheet0.getCells().getRowHeightPixel(startRow); // 行高
|
|
|
+
|
|
|
+ // 设置图片的大小和位置(确保图片适应合并单元格)
|
|
|
+ picture.setWidth((int) mergedCellWidth); // 图片宽度
|
|
|
+ picture.setHeight((int) mergedCellHeight); // 图片高度
|
|
|
+ cell.putValue("");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }else if(key.equals("DATE")){
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String format = sdf.format((Date) value);
|
|
|
+ cell.putValue(format);
|
|
|
+ } else{
|
|
|
+ cell.putValue(value);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ cell.putValue("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Worksheet sheet1 = wb.getWorksheets().get(1);
|
|
|
+ Cells cells2 = sheet1.getCells();
|
|
|
+ //Object value = map.get("DATAAPPENDIX");
|
|
|
+ for (int row = 0; row < cells2.getMaxDataRow() + 1; row++) {
|
|
|
+ for (int col = 0; col < cells2.getMaxDataColumn() + 1; col++) {
|
|
|
+ Cell cell = cells2.get(row, col);
|
|
|
+ String cellValue = cell.getStringValue();
|
|
|
+ // 检查占位符并替换对应的数据
|
|
|
+ if (cellValue.startsWith("${") && cellValue.endsWith("}")) {
|
|
|
+ String key = cellValue.substring(2, cellValue.length() - 1);
|
|
|
+ Object value = map.get(key);
|
|
|
+ if (value != null) {
|
|
|
+ // 将占位符替换为实际值
|
|
|
+ if(key.equals("DATAAPPENDIX")){
|
|
|
+ Range mergedRange = cell.getMergedRange();
|
|
|
+ // 确定合并单元格的起始行和列
|
|
|
+ int startRow = mergedRange.getFirstRow();
|
|
|
+ int startCol = mergedRange.getFirstColumn();
|
|
|
+ if (StringUtils.isNotBlank(value.toString())) {
|
|
|
+ String[] splitFile = value.toString().split("\\^_\\^");
|
|
|
+ for(int i = 0; i < splitFile.length; i++){
|
|
|
+ try {
|
|
|
+ int newSheetIndex = wb.getWorksheets().addCopy(1);
|
|
|
+ Worksheet newSheet = wb.getWorksheets().get(newSheetIndex);
|
|
|
+ // 替换占位符
|
|
|
+ replacePlaceholders(newSheet, map,splitFile[i]);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //cell.putValue("");
|
|
|
+ } else{
|
|
|
+ cell.putValue(value);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ cell.putValue("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ wb.getWorksheets().removeAt(1);
|
|
|
+ try {
|
|
|
+ response.setContentType("application/octet-stream;charset=ISO8859-1");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=测试下载");
|
|
|
+ wb.save(response.getOutputStream(), SaveFormat.XLSX);
|
|
|
+ wb.dispose();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("填充失败:{}", e);
|
|
|
+ throw new CustomException("填充失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void replacePlaceholders(Worksheet sheet, Map<String, Object> data, String splitFile) {
|
|
|
+ Cells cells = sheet.getCells();
|
|
|
+
|
|
|
+ // 遍历单元格并替换占位符
|
|
|
+ for (int row = 0; row < cells.getMaxDataRow() + 1; row++) {
|
|
|
+ for (int col = 0; col < cells.getMaxDataColumn() + 1; col++) {
|
|
|
+ Cell cell = cells.get(row, col);
|
|
|
+ String cellValue = cell.getStringValue();
|
|
|
+
|
|
|
+ // 检查占位符并替换对应的数据
|
|
|
+ if (cellValue.startsWith("${") && cellValue.endsWith("}")) {
|
|
|
+ String key = cellValue.substring(2, cellValue.length() - 1);
|
|
|
+ Object value = data.get(key);
|
|
|
+ if (value != null) {
|
|
|
+
|
|
|
+ if(key.equals("DATAAPPENDIX")){
|
|
|
+ Range mergedRange = cell.getMergedRange();
|
|
|
+ // 确定合并单元格的起始行和列
|
|
|
+ int startRow = mergedRange.getFirstRow();
|
|
|
+ int startCol = mergedRange.getFirstColumn();
|
|
|
+ String fileid = splitFile.split("-;-")[1];
|
|
|
+ SysFileItemDo fileItemDo = sysFileItemService.getById(fileid);
|
|
|
+ if (fileItemDo != null) {
|
|
|
+ try (BufferedInputStream inputStream = FileUtil.getInputStream(fileItemDo.getFullpath())) {
|
|
|
+ int pictureIndex = sheet.getPictures().add(startRow, startCol, inputStream, 100, 100); // 设定宽度和高度
|
|
|
+ Picture picture = sheet.getPictures().get(pictureIndex);
|
|
|
+
|
|
|
+ // 获取合并单元格的宽度和高度
|
|
|
+ double mergedCellWidth = sheet.getCells().getColumnWidthPixel(startCol) * mergedRange.getColumnCount(); // 合并了多列
|
|
|
+ double mergedCellHeight = sheet.getCells().getRowHeightPixel(startRow) * mergedRange.getRowCount(); // 行高
|
|
|
+
|
|
|
+ // 设置图片的大小和位置(确保图片适应合并单元格)
|
|
|
+ picture.setWidth((int) mergedCellWidth); // 图片宽度
|
|
|
+ picture.setHeight((int) mergedCellHeight); // 图片高度
|
|
|
+ cell.putValue("");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cell.putValue("");
|
|
|
+ }else{
|
|
|
+ // 替换占位符
|
|
|
+ cell.putValue(value);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果没有对应的数据,清空单元格
|
|
|
+ cell.putValue("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|