OTA-硬件版本生成word文件。

This commit is contained in:
wangzhijiang
2023-05-15 16:45:06 +08:00
parent 78f41a59e7
commit 59c99bb025
2 changed files with 107 additions and 5 deletions
@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jero.common.exception.JeroBootException;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.ota.entity.*;
import com.jero.modules.ota.enums.OtaModuleEnum;
@@ -13,21 +14,29 @@ import com.jero.modules.ota.mapper.OtaBaSjSjxtbhMapper;
import com.jero.modules.ota.service.IOtaBaCxTxjlService;
import com.jero.modules.ota.service.IOtaBaSjGnxtwhService;
import com.jero.modules.ota.service.IOtaBaSjSjxtbhService;
import com.jero.modules.ota.service.IOtaBaSjSjyxpgService;
import com.jero.modules.ota.utils.ComparisonUtil;
import com.jero.modules.ota.utils.ImportFileUtil;
import com.jero.modules.project.util.WordUtil;
import com.jero.modules.split.common.FileUnZip;
import com.jero.modules.system.service.ISysDictService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;
@@ -61,6 +70,9 @@ public class OtaBaSjSjxtbhServiceImpl extends ServiceImpl<OtaBaSjSjxtbhMapper, O
@Value(value = "${ota.uploadPath}")
private String uploadPath;
@Autowired
private IOtaBaSjSjyxpgService otaBaSjSjyxpgService;
/**
* 保存
*/
@@ -236,6 +248,21 @@ public class OtaBaSjSjxtbhServiceImpl extends ServiceImpl<OtaBaSjSjxtbhMapper, O
Workbook wb = ImportFileUtil.readExcel(upFile);
Sheet s = wb.getSheetAt(0);
int rows = s.getPhysicalNumberOfRows();
List<OtaBaSjSjxtbh> otaBaSjSjxtbh = this.getByOtaId(otaId);
List<String> oldYjbbList = otaBaSjSjxtbh.stream().map(OtaBaSjSjxtbh::getYjbb).distinct().collect(Collectors.toList());
List<String> oldTempFileNameList = new ArrayList<>();
oldYjbbList.forEach(sjSjxtbh -> {
oldTempFileNameList.add(sjSjxtbh.replaceAll("","") + ".docx");
});
int tempFileIndex = 1;
String tempFilePath = uploadPath + "tempFile" + "/" + otaId;
File tempFile = new File(tempFilePath);
if(!tempFile.exists()){
tempFile.mkdirs();
}
List<String> tempFileNames = new ArrayList<>();
for (int i = 4; i < rows; i++) {
OtaBaSjSjxtbh xtbh = new OtaBaSjSjxtbh();
Row row = s.getRow(i);
@@ -253,7 +280,17 @@ public class OtaBaSjSjxtbhServiceImpl extends ServiceImpl<OtaBaSjSjxtbhMapper, O
cell = row.getCell(1);
xtbh.setSjhbgcs(ImportFileUtil.getCellValue(cell, wb));
cell = row.getCell(3);
xtbh.setYjbb(ImportFileUtil.getCellValue(cell, wb));
String yjbb = ImportFileUtil.getCellValue(cell, wb);
if(yjbb.length() > 50){
String fileName = "附件" + tempFileIndex + ".docx";
String tempFileUrl = WordUtil.createWord(tempFilePath, fileName, yjbb);
if (StringUtils.isNotEmpty(tempFileUrl)) {
tempFileNames.add(fileName);
}
yjbb = "见附件" + tempFileIndex;
tempFileIndex ++;
}
xtbh.setYjbb(yjbb);
cell = row.getCell(4);
xtbh.setSjqrjbb(ImportFileUtil.getCellValue(cell, wb));
cell = row.getCell(5);
@@ -279,6 +316,43 @@ public class OtaBaSjSjxtbhServiceImpl extends ServiceImpl<OtaBaSjSjxtbhMapper, O
list.add(xtbh);
}
if(CollectionUtils.isNotEmpty(tempFileNames)){
List<String> newClfjList = new ArrayList<>();
List<String> oldClfjList = new ArrayList<>();
OtaBaSjSjyxpg otaBaSjSjyxpg = this.otaBaSjSjyxpgService.queryByOtaId(otaId, null);
if(StringUtils.isNotEmpty(otaBaSjSjyxpg.getClfj())){
List<OSSFile> clfjFileList = this.ossFileService.getFileInfos(otaBaSjSjyxpg.getClfj());
oldClfjList = clfjFileList.stream().filter(clfjFile -> {
boolean flag = true;
for (String oldTempFileName : oldTempFileNameList) {
if (StringUtils.equals(clfjFile.getFileName(), oldTempFileName)) {
flag = false;
break;
}
}
return flag;
}).map(OSSFile::getId).collect(Collectors.toList());
}
// 读取临时文件,上传至服务器。
for (String tempFileName : tempFileNames) {
try {
FileInputStream input = new FileInputStream(tempFilePath + "/" + tempFileName);
MultipartFile multipartFile = new MockMultipartFile(tempFileName, tempFileName, "text/plain", input);
OSSFile ossFile = this.ossFileService.uploadLocalOfCos(multipartFile, "", null,null);
if (ObjectUtils.isNotEmpty(ossFile)) {
newClfjList.add(ossFile.getId());
}
} catch (IOException e) {
e.printStackTrace();
}
}
if(ObjectUtils.isNotEmpty(otaBaSjSjyxpg)){
newClfjList.addAll(oldClfjList);
otaBaSjSjyxpg.setClfj(StringUtils.join(newClfjList.stream().distinct().collect(Collectors.toList()), ","));
this.otaBaSjSjyxpgService.updateById(otaBaSjSjyxpg);
}
}
FileUnZip.deleteDir(tempFile);
return list;
}
@@ -1,6 +1,9 @@
package com.jero.modules.project.util;
import cn.hutool.core.collection.CollectionUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.docx4j.Docx4J;
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
@@ -22,10 +25,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;
import javax.xml.bind.JAXBElement;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -632,5 +632,33 @@ public class WordUtil {
addImageInlineAndTextToPackage(wordMLPackage, bytes, "这是图3,name.png,看图片的嵌入方式");
wordMLPackage.save(new File("d:/opt/测试" + System.currentTimeMillis() + ".docx"));
}
/**
* 创建word文件。
* @param path
* @param fileName
* @param content
* @return
*/
public static String createWord(String path,String fileName,String content){
String result = "";
try {
result = path + "/" +fileName;
XWPFDocument document = new XWPFDocument();
// 段落
XWPFParagraph paragraph = document.createParagraph();
// 添加文本
XWPFRun run = paragraph.createRun();
run.setText(content);
FileOutputStream out = new FileOutputStream(new File(result));
document.write(out);
out.close();
log.info(fileName + " Word文件创建成功!");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}