zss
2024-09-27 a14a26c04bc5863248b9a9d387610a143c3a4efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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);
            }
        }
    }
 
}