yangys
2025-08-27 f76ac44a47aaddcf2f8c6b4a83375acdb0e1c1ed
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
 
package org.springblade.mdm.flow.service;
 
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.HistoryService;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.mdm.basesetting.machine.MachineService;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.flow.constants.FlowContants;
import org.springblade.mdm.flow.vo.ApproveTableInfo;
import org.springblade.mdm.program.entity.NcNode;
import org.springblade.mdm.program.entity.NcProgramApproved;
import org.springblade.mdm.program.service.NcNodeService;
import org.springblade.mdm.program.service.NcProgramApprovedService;
import org.springblade.system.feign.IUserClient;
import org.springblade.system.pojo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Optional;
 
/**
 * 数控程序确认表输出服务
 *
 * @author yangys
 */
@Slf4j
@Service
public class ProgramConfirmTableService {
    @Autowired
    private FlowCommonService flowCommonService;
    @Autowired
    private NcNodeService ncNodeService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private IUserClient useClient;
    @Autowired
    private NcProgramApprovedService programApprovedService;
    private static final String UNCHECK_BOX = "☐";
    private static final String CHECKED_BOX = "☑";
    static Chunk square = new Chunk("o", new Font(Font.FontFamily.ZAPFDINGBATS, 12)); // 空方框
    static Chunk check = new Chunk("4", new Font(Font.FontFamily.ZAPFDINGBATS, 12)); // 带勾方框
 
    BaseFont getBaseFont() throws DocumentException, IOException {
        return BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    }
 
    Font getChineseFont() throws DocumentException, IOException {
        BaseFont bfChinese = getBaseFont();
        return new Font(bfChinese, 12, Font.NORMAL);
    }
 
    public void exportConfirmTable(String processInstanceId, OutputStream os) throws DocumentException, IOException {
 
        BaseFont bfChinese = getBaseFont();
 
        Document document = new Document(PageSize.B5.rotate());
        PdfWriter.getInstance(document, os);
 
        document.open();
 
        FlowProgramProperties props = flowCommonService.getProgramPropertiesFromHis(processInstanceId);
 
        ApproveTableInfo appInfo = getApproveInfo(props);
        Machine machine;
        String machineCode = props.getMachineCode();
        machine = machineService.getByCode(machineCode);
 
        int flag = props.getProcessDefinitionKey().equals(FlowContants.TRY_PROCESS_KEY)?1:2;
        printPage(document, bfChinese, props, machine);
        //document.newPage();
 
 
        //printPage(document, bfChinese, props, machine, 2, appInfo);
 
        document.close();
    }
 
 
 
    void printPage(Document document, BaseFont bfChinese, FlowProgramProperties props, Machine machine) throws DocumentException, IOException {
 
        Font tableFont = new Font(bfChinese, 12, Font.NORMAL);
 
 
        PdfPTable titleTable = new PdfPTable(2);
        titleTable.setWidthPercentage(100);
        titleTable.setSpacingBefore(10f);
        titleTable.setSpacingAfter(10f);
 
        PdfPCell celBh = this.getCell("编号:",tableFont);
        titleTable.addCell(celBh);
        PdfPCell celYb = this.getCell("艺表8",tableFont);
 
        titleTable.addCell(celYb);
 
        document.add(titleTable);
 
 
        //document.add(title);
        // 创建表格(4列)
        PdfPTable table = new PdfPTable(6);
        table.setWidths(new float[]{1, 1.3f, 1f, 1.3f,1, 1.5f});
 
        Font ft = this.getChineseFont();
        // 添加表头
        PdfPCell tempCell = getCell("一一四厂",tableFont);
 
        //
        table.addCell(tempCell);
 
        tempCell = getCell("数控程序确认表",tableFont);
        table.addCell(tempCell);
        table.addCell(getCell("工序号", ft));
        table.addCell(getCell("工艺版次", ft));
        table.addCell(getCell("机床型号", ft));
        table.addCell(getCell("工段负责人/单元负\n责人派出时间", ft));
 
 
        table.addCell(getCell(props.getDrawingNo(), ft));//零组件号
 
        table.addCell(getCell(props.getProcessNo(), ft));//工序号
 
        table.addCell(getCell(props.getCraftEdition(), ft));//工艺版次
        table.addCell(getCell(machine.getName(), ft));//机床型号
        table.addCell(getCell("", ft));//负责人,工段负责人派出时间
 
        //中间大格
        PdfPCell cell = new PdfPCell();
        cell.setColspan(5);
        cell.setCalculatedHeight(400);
        Paragraph mainPara1 = new Paragraph();
        mainPara1.setLeading(30f, 0f);
        mainPara1.setFont(ft);
        mainPara1.setAlignment(Element.ALIGN_LEFT); // 对齐方式
        mainPara1.setIndentationLeft(20); // 左缩进(像素)
        mainPara1.setIndentationRight(20); // 右缩进
        mainPara1.setSpacingBefore(10); // 段前间距
        mainPara1.setSpacingAfter(10); // 段后间距
 
 
        mainPara1.add("数控程序编制任务安排:\n");
        cell.addElement(mainPara1);
 
        Paragraph line2Para = new Paragraph();
        line2Para.setLeading(30f, 0f);
        line2Para.setFont(ft);
        line2Para.setAlignment(Element.ALIGN_LEFT); // 对齐方式
        line2Para.setIndentationLeft(40); // 左缩进(像素)
        line2Para.add("请");
 
        Font underLineFt = new Font(bfChinese, 12, Font.UNDERLINE);
 
        //String personName = record.getUserNickname();
        Chunk block1 = square;
        Chunk block2 = check;
 
        cell.addElement(line2Para);
 
        Paragraph fangzhen = new Paragraph();
        fangzhen.setAlignment(Element.ALIGN_LEFT); // 对齐方式
        fangzhen.setIndentationLeft(20); // 左缩进(像素)
        fangzhen.setIndentationRight(20); // 右缩进
        fangzhen.setSpacingBefore(10); // 段前间距
        fangzhen.setSpacingAfter(10); // 段后间距
        fangzhen.setFont(ft);
        fangzhen.setLeading(30f, 0f);
        fangzhen.add("是否需要仿真(是");
        fangzhen.add(check);//固定是仿真
        fangzhen.add(" 否");
        fangzhen.add(square);
        fangzhen.add(")\n\n");
 
        cell.addElement(fangzhen);
        table.addCell(cell);
        //下一行
        PdfPCell cell21 = getCell("数控程序编号", ft);// PdfPCell();
        cell21.setColspan(2);
        //cell21.setPhrase(new Phrase("数控程序编号",ft));
        table.addCell(cell21);
        table.addCell(getCell("编制/日期", ft));
        table.addCell(getCell("校对/日期", ft));
        table.addCell(getCell("批准/日期", ft));
 
        PdfPCell cell2 = getCell("00001", ft);
        cell2.setColspan(2);
 
        //cell2.addElement(new Phrase(programPackageNode.getProgramNo(),ft));//数控程序编号
        //cell2.addElement(new Phrase("00001",ft));
        table.addCell(cell2);
 
        // 设置表格宽度占页面宽度的100%
        table.setWidthPercentage(100);
 
        // 添加表格到文档
        document.add(table);
 
        Phrase lastPhrase = new Phrase("注:工段派工应预先计划、合理安排数控程序编制。立加、数铣、数车等单道工序程序(两轴程序)需提前1个工作日,卧式加工中心单道工序(含多轴程序)需提前3个工作日。 ", ft);
        document.add(lastPhrase);
    }
 
    PdfPCell getCell(String text, Font font) throws DocumentException, IOException {
        PdfPCell cell = new PdfPCell();
        cell.setPadding(10);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        if (StringUtils.isNotBlank(text)) {
            Phrase p = new Phrase(text, font);
            p.setLeading(25);
            cell.setPhrase(p);
        }
 
        return cell;
    }
 
    ApproveTableInfo getApproveInfo(FlowProgramProperties props){
        //编制
        R<User> leaderR = useClient.userInfoById(props.getTeamLeaderId());
        String leaderName = "";
        if (leaderR.isSuccess()) {
            leaderName = leaderR.getData().getName();
        }
        //编制
        R<User> programmerR = useClient.userInfoById(props.getProgrammerId());
        String programmerName = "";
        if (programmerR.isSuccess()) {
            programmerName = programmerR.getData().getName();
        }
        Machine machine;
        String machineCode = props.getMachineCode();
        machine = machineService.getByCode(machineCode);
 
        //校对
        R<User> checkerR = useClient.userInfoById(props.getCheckerId());
        String checkerName = "";
        if (checkerR.isSuccess()) {
            checkerName = checkerR.getData().getName();
        }
        //审核
        R<User> seniorR = useClient.userInfoById(props.getCheckerId());
        String senioirName = "";
        if (seniorR.isSuccess()) {
            senioirName = seniorR.getData().getName();
        }
        ApproveTableInfo appInfo = new ApproveTableInfo();
        appInfo.setTeamLeaderName(leaderName);
        appInfo.setProgrammerName(programmerName);
        appInfo.setCheckerName(checkerName);
        appInfo.setSeniorName(senioirName);
 
        //获取时间信息
        List<HistoricTaskInstance> hisTasks = historyService.createHistoricTaskInstanceQuery()
            .processInstanceId(props.getProcessInstanceId()).orderByTaskCreateTime().desc().list();
 
        for(HistoricTaskInstance task : hisTasks){
            if(appInfo.getProgrammingDate()== null && task.getTaskDefinitionKey().contains("program")){
                appInfo.setProgrammingDate(task.getEndTime());
            }
 
            if(appInfo.getCheckDate()== null && task.getTaskDefinitionKey().contains("check")){
                appInfo.setCheckDate(task.getEndTime());
            }
 
            if(appInfo.getApproveDate()== null && task.getTaskDefinitionKey().contains("approve")){
                appInfo.setApproveDate(task.getEndTime());
            }
        }
 
        //查询数控程序编号
        Optional<NcProgramApproved> appOpt = programApprovedService.lambdaQuery().eq(NcProgramApproved::getProcessInstanceId, props.getProcessInstanceId()).oneOpt();
        if(appOpt.isEmpty()){
            appInfo.setProgramNo("(未生成)");
        }else{
            NcNode programPkgNameNode = this.ncNodeService.getById(appOpt.get().getNcNodeId());
            appInfo.setProgramNo(programPkgNameNode.getProgramNo());
        }
        return appInfo;
    }
 
}