package com.yuanchu.mom.utils;
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.yuanchu.mom.controller.InsOrderPlanController;
|
import com.yuanchu.mom.mapper.*;
|
import com.yuanchu.mom.pojo.InsOrderState;
|
import com.yuanchu.mom.pojo.InsProduct;
|
import com.yuanchu.mom.pojo.InsProductResult2;
|
import com.yuanchu.mom.pojo.InsSample;
|
import com.yuanchu.mom.service.impl.InsOrderPlanServiceImpl;
|
import org.apache.poi.xwpf.usermodel.*;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
|
import org.springframework.mock.web.MockMultipartFile;
|
import org.springframework.stereotype.Component;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.util.List;
|
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.stream.Collectors;
|
|
@Component
|
public class WordUtils {
|
|
@Resource
|
private InsOrderMapper insOrderMapper;
|
|
@Resource
|
private InsOrderStateMapper insOrderStateMapper;
|
|
@Resource
|
private InsSampleMapper insSampleMapper;
|
|
@Resource
|
private InsProductMapper insProductMapper;
|
|
@Resource
|
private InsProductResult2Mapper insProductResult2Mapper;
|
|
private void writeText(XWPFParagraph xwpfParagraph, String text, Integer size, String bold) {
|
// 创建新的文本运行
|
XWPFRun run = xwpfParagraph.createRun();
|
// 设置新的数据
|
run.setText(text);
|
if (ObjectUtils.isNotEmpty(size)) {
|
run.setFontSize(size);
|
}
|
if (ObjectUtils.isNotEmpty(bold)) {
|
run.setBold(true);
|
}
|
}
|
|
//生成电路试验的站点报告
|
public MultipartFile generateWord(String note, String term, InsOrderState insOrderState) {
|
AtomicInteger index = new AtomicInteger();
|
// 创建一个空的Word文档
|
XWPFDocument document = new XWPFDocument();
|
//创建一个段落标题
|
XWPFParagraph paragraph = document.createParagraph();
|
writeText(paragraph, term + "电路参数", 20, "加粗");
|
|
//查询样品
|
List<InsSample> insSamples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery().eq(InsSample::getInsOrderId, insOrderState.getInsOrderId()));
|
insSamples.forEach(insSample -> {
|
//查询项目
|
List<InsProduct> insProducts = insProductMapper.selectList(Wrappers.<InsProduct>lambdaQuery()
|
.eq(InsProduct::getInsSampleId, insSample.getId())
|
.eq(InsProduct::getSonLaboratory, "电路试验"));
|
//获取最大端口数量,角度数量,频段数量
|
List<InsProductResult2> insProductResult2s = insProductResult2Mapper.selectList(Wrappers.<InsProductResult2>lambdaQuery()
|
.eq(InsProductResult2::getNum, insOrderState.getNum())
|
.in(InsProductResult2::getInsProductId, insProducts.stream().map(InsProduct::getId).collect(Collectors.toList())));
|
int ports = 0;
|
int angles = 0;
|
for (InsProductResult2 insProductResult2 : insProductResult2s) {
|
if (insProductResult2.getPort().split(",").length > ports) {
|
ports = insProductResult2.getPort().split(",").length;
|
}
|
if (insProductResult2.getAngle().split(",").length > angles) {
|
angles = insProductResult2.getAngle().split(",").length;
|
}
|
}
|
List<String> frequencyses = insProductResult2s.stream().map(InsProductResult2::getFrequency).distinct().collect(Collectors.toList());
|
int frequencys = frequencyses.size();
|
List<String> inspectionItemSubclass = insProducts.stream().map(InsProduct::getInspectionItemSubclass).collect(Collectors.toList());
|
//先判断是1简单版还是0复杂版
|
if (insOrderState.getVersion() == 1) {
|
//(列数是端口数+6,行数是(驻波比*2+隔离度+互调*(角度+1)+1)*频段+1)
|
int portRow = ports % 8 == 0 ? ports / 8 : ports / 8 + 1;
|
//行数
|
int rows = 0;
|
if (inspectionItemSubclass.contains("电压驻波比")) {
|
rows += 2 * portRow;
|
}
|
if (inspectionItemSubclass.contains("同极化隔离度")) {
|
rows += 1 * portRow;
|
}
|
if (inspectionItemSubclass.contains("异极化隔离度")) {
|
rows += 1 * portRow;
|
}
|
if (inspectionItemSubclass.contains("互调")) {
|
rows += (angles + 1) * portRow;
|
}
|
//列数
|
int cols = ports > 8 ? 14 : ports + 6;
|
//创建一个表格
|
XWPFTable table = document.createTable((rows + 1) * frequencys + 1, cols);
|
// 填充表格内容
|
for (int rowIndex = 0; rowIndex < (rows + 1) * frequencys + 1; rowIndex++) {
|
XWPFTableRow row = table.getRow(rowIndex);
|
for (int colIndex = 0; colIndex < cols; colIndex++) {
|
XWPFTableCell cell = row.getCell(colIndex);
|
if (rowIndex == 0) {
|
//第一行
|
if (colIndex == 0) {
|
//第一列
|
cell.setText("序号");
|
} else if (colIndex == 1) {
|
//第二列
|
cell.setText("检验项目");
|
} else if (colIndex == 2) {
|
//第三列
|
cell.setText("单位");
|
} else if (colIndex == 3) {
|
//第四列
|
cell.setText("标准与要求");
|
} else if (colIndex == cols - 1) {
|
//最后一列
|
cell.setText("检验结论");
|
} else if (colIndex == 4){
|
//其余列
|
cell.setText("检验结果");
|
}
|
}
|
else if (rowIndex == 1 || rowIndex % (rows + 1) == 1) {
|
if (colIndex==0) {
|
//频段所在行
|
cell.setText("频段:" + frequencyses.get(rowIndex / (rows + 1)));
|
}
|
}
|
else {
|
//portRow端口行数
|
if (inspectionItemSubclass.contains("电压驻波比")) {
|
|
}
|
if (inspectionItemSubclass.contains("同极化隔离度")) {
|
|
}
|
if (inspectionItemSubclass.contains("异极化隔离度")) {
|
|
}
|
if (inspectionItemSubclass.contains("互调")) {
|
|
}
|
}
|
}
|
if (rowIndex == 0) {
|
//第一行的横向合并
|
mergeCellsHorizontal(row, 4, cols - 2);
|
} else if (rowIndex == 1 || rowIndex % (rows + 1) == 1) {
|
//频段行的横向合并
|
mergeCellsHorizontal(row, 0, cols - 1);
|
}
|
}
|
} else {
|
|
}
|
});
|
// 写入到文件
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
try {
|
document.write(outputStream);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
byte[] bytes = outputStream.toByteArray();
|
return new MockMultipartFile(term + "电路参数", term + "电路参数.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", bytes);
|
}
|
|
// 竖向合并单元格
|
private static void mergeCellsVertical(XWPFTable table, int columnIndex, int fromRow, int toRow) {
|
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
|
if (rowIndex == fromRow) {
|
// 第一个单元格保留,设为合并起始
|
CTTcPr tcPr = table.getRow(rowIndex).getCell(columnIndex).getCTTc().addNewTcPr();
|
tcPr.addNewVMerge().setVal(STMerge.RESTART);
|
} else {
|
// 非第一个单元格设为合并继续
|
CTTcPr tcPr = table.getRow(rowIndex).getCell(columnIndex).getCTTc().addNewTcPr();
|
tcPr.addNewVMerge().setVal(STMerge.CONTINUE);
|
}
|
}
|
}
|
|
// 横向合并单元格
|
private static void mergeCellsHorizontal(XWPFTableRow row, int fromCell, int toCell) {
|
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
|
if (cellIndex == fromCell) {
|
// 第一个单元格保留,设为合并起始
|
CTTcPr tcPr = row.getCell(cellIndex).getCTTc().addNewTcPr();
|
tcPr.addNewHMerge().setVal(STMerge.RESTART);
|
} else {
|
// 非第一个单元格设为合并继续
|
CTTcPr tcPr = row.getCell(cellIndex).getCTTc().addNewTcPr();
|
tcPr.addNewHMerge().setVal(STMerge.CONTINUE);
|
}
|
}
|
}
|
|
}
|