|
1
|
1
|
package com.lframework.xingyun.sc.procurement.controller.weighing;
|
|
2
|
2
|
|
|
|
3
|
+import cn.hutool.core.convert.NumberChineseFormatter;
|
|
|
4
|
+import cn.hutool.core.io.resource.ClassPathResource;
|
|
3
|
5
|
import com.lframework.starter.web.core.annotations.security.HasPermission;
|
|
4
|
6
|
import com.lframework.starter.web.core.components.resp.InvokeResult;
|
|
5
|
7
|
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
|
|
6
|
8
|
import com.lframework.starter.web.core.components.resp.PageResult;
|
|
7
|
9
|
import com.lframework.starter.web.core.controller.DefaultBaseController;
|
|
|
10
|
+import com.lframework.xingyun.sc.bo.procurement.GetProcurementWeighingTicketApplyLineBo;
|
|
8
|
11
|
import com.lframework.xingyun.sc.bo.procurement.GetProcurementWeighingTicketBo;
|
|
9
|
12
|
import com.lframework.xingyun.sc.bo.procurement.WeighingTicketPlanBo;
|
|
10
|
13
|
import com.lframework.xingyun.sc.procurement.entity.ProcurementPlanLine;
|
|
...
|
...
|
@@ -15,14 +18,38 @@ import com.lframework.xingyun.sc.procurement.vo.weighing.QueryProcurementWeighin |
|
15
|
18
|
import com.lframework.xingyun.sc.procurement.vo.weighing.QueryWeighingTicketPlanVo;
|
|
16
|
19
|
import com.lframework.xingyun.sc.procurement.vo.weighing.UpdateProcurementWeighingTicketVo;
|
|
17
|
20
|
import com.lframework.xingyun.sc.procurement.vo.weighing.WeighingTicketAttachmentVo;
|
|
|
21
|
+import com.lframework.xingyun.sc.utils.ExcelUtil;
|
|
|
22
|
+import com.lframework.xingyun.sc.utils.ResponseUtil;
|
|
18
|
23
|
import io.swagger.annotations.Api;
|
|
19
|
24
|
import io.swagger.annotations.ApiImplicitParam;
|
|
20
|
25
|
import io.swagger.annotations.ApiOperation;
|
|
|
26
|
+import java.io.File;
|
|
|
27
|
+import java.io.FileInputStream;
|
|
|
28
|
+import java.io.FileNotFoundException;
|
|
|
29
|
+import java.io.FileOutputStream;
|
|
|
30
|
+import java.io.IOException;
|
|
|
31
|
+import java.io.InputStream;
|
|
|
32
|
+import java.math.BigDecimal;
|
|
|
33
|
+import java.nio.file.Files;
|
|
|
34
|
+import java.time.format.DateTimeFormatter;
|
|
|
35
|
+import java.util.HashMap;
|
|
21
|
36
|
import java.util.List;
|
|
|
37
|
+import java.util.Map;
|
|
|
38
|
+import javax.servlet.http.HttpServletResponse;
|
|
22
|
39
|
import javax.annotation.Resource;
|
|
23
|
40
|
import javax.validation.Valid;
|
|
24
|
41
|
import javax.validation.constraints.NotBlank;
|
|
25
|
42
|
import lombok.extern.slf4j.Slf4j;
|
|
|
43
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
44
|
+import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
45
|
+import org.apache.poi.ss.usermodel.CellType;
|
|
|
46
|
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
47
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
48
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
49
|
+import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
|
50
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
51
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
52
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
26
|
53
|
import org.springframework.validation.annotation.Validated;
|
|
27
|
54
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
28
|
55
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
...
|
...
|
@@ -113,7 +140,7 @@ public class ProcurementWeighingTicketController extends DefaultBaseController { |
|
113
|
140
|
@HasPermission({"warehouse-manage:weight-ticket:uploadSigned"})
|
|
114
|
141
|
@PutMapping("/attachment/sign")
|
|
115
|
142
|
public InvokeResult<Void> uploadSignAttachment(@RequestBody @Valid WeighingTicketAttachmentVo vo) {
|
|
116
|
|
- procurementWeighingTicketService.uploadSignAttachment(vo.getId(), vo.getFileIds(), vo.getFileNames());
|
|
|
143
|
+ procurementWeighingTicketService.uploadSignAttachment(vo.getId(), vo.getFileIds(), vo.getFileNames(), vo.getDriver());
|
|
117
|
144
|
return InvokeResultBuilder.success();
|
|
118
|
145
|
}
|
|
119
|
146
|
|
|
...
|
...
|
@@ -141,4 +168,226 @@ public class ProcurementWeighingTicketController extends DefaultBaseController { |
|
141
|
168
|
procurementWeighingTicketService.delete(id);
|
|
142
|
169
|
return InvokeResultBuilder.success();
|
|
143
|
170
|
}
|
|
|
171
|
+
|
|
|
172
|
+ /**
|
|
|
173
|
+ * 打印过磅码单
|
|
|
174
|
+ *
|
|
|
175
|
+ * @param exportType 导出类型 PDF-导出PDF文件,其他/默认-导出Excel文件
|
|
|
176
|
+ * @param templateType 模板类型 NO_REMARK-无备注,其他/默认-有备注
|
|
|
177
|
+ */
|
|
|
178
|
+ @ApiOperation("打印过磅码单(exportType=PDF导出PDF文件,默认Excel;templateType=NO_REMARK无备注模板,默认有备注)")
|
|
|
179
|
+ @HasPermission({"warehouse-manage:weight-ticket:print"})
|
|
|
180
|
+ @GetMapping("/export")
|
|
|
181
|
+ public void export(@NotBlank(message = "ID不能为空!") String id, String exportType, String templateType,
|
|
|
182
|
+ HttpServletResponse response) throws IOException {
|
|
|
183
|
+
|
|
|
184
|
+ GetProcurementWeighingTicketBo data = procurementWeighingTicketService.findDetail(id);
|
|
|
185
|
+
|
|
|
186
|
+ // 模板类型:NO_REMARK-无备注(procurementWeighingTicket2.xlsx),默认有备注(procurementWeighingTicket.xlsx)
|
|
|
187
|
+ String templateFile = "NO_REMARK".equals(templateType)
|
|
|
188
|
+ ? "templates/procurementWeighingTicket2.xlsx" : "templates/procurementWeighingTicket.xlsx";
|
|
|
189
|
+
|
|
|
190
|
+ try {
|
|
|
191
|
+ try (InputStream inputStream = new ClassPathResource(templateFile).getStream();
|
|
|
192
|
+ Workbook workbook = new XSSFWorkbook(inputStream)) {
|
|
|
193
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
194
|
+
|
|
|
195
|
+ // 申请数量:一行两条(品种代号、数量),不足两条保留一行,超出向下叠加
|
|
|
196
|
+ List<GetProcurementWeighingTicketApplyLineBo> applyLines = data.getApplyLines();
|
|
|
197
|
+ final int APPLY_START_ROW = 4; // 第5行(Excel) = 索引4,模板预留1行
|
|
|
198
|
+ int dataSize = applyLines == null ? 0 : applyLines.size();
|
|
|
199
|
+ int needRows = Math.max(1, (dataSize + 1) / 2);
|
|
|
200
|
+ int needInsert = needRows - 1;
|
|
|
201
|
+ if (needInsert > 0) {
|
|
|
202
|
+ int shiftStart = APPLY_START_ROW + 1;
|
|
|
203
|
+ int shiftEnd = sheet.getLastRowNum();
|
|
|
204
|
+ if (shiftEnd >= shiftStart) {
|
|
|
205
|
+ sheet.shiftRows(shiftStart, shiftEnd, needInsert, true, true);
|
|
|
206
|
+ }
|
|
|
207
|
+ // 复制模板申请数量行样式到新插入的每一行
|
|
|
208
|
+ Row templateRow = sheet.getRow(APPLY_START_ROW);
|
|
|
209
|
+ for (int k = 1; k <= needInsert; k++) {
|
|
|
210
|
+ int newRowIdx = APPLY_START_ROW + k;
|
|
|
211
|
+ Row newRow = sheet.getRow(newRowIdx);
|
|
|
212
|
+ if (newRow == null) {
|
|
|
213
|
+ newRow = sheet.createRow(newRowIdx);
|
|
|
214
|
+ }
|
|
|
215
|
+ if (templateRow != null) {
|
|
|
216
|
+ newRow.setHeight(templateRow.getHeight());
|
|
|
217
|
+ for (int c = 0; c < 8; c++) {
|
|
|
218
|
+ Cell tc = templateRow.getCell(c);
|
|
|
219
|
+ Cell nc = newRow.getCell(c);
|
|
|
220
|
+ if (nc == null) {
|
|
|
221
|
+ nc = newRow.createCell(c);
|
|
|
222
|
+ }
|
|
|
223
|
+ if (tc != null) {
|
|
|
224
|
+ nc.setCellStyle(tc.getCellStyle());
|
|
|
225
|
+ }
|
|
|
226
|
+ }
|
|
|
227
|
+ // 新行补充合并单元格,与模板申请数量行一致:C:D(数量1)、E:F(品种代号2)、G:H(数量2)
|
|
|
228
|
+ sheet.addMergedRegion(new CellRangeAddress(newRowIdx, newRowIdx, 2, 3));
|
|
|
229
|
+ sheet.addMergedRegion(new CellRangeAddress(newRowIdx, newRowIdx, 4, 5));
|
|
|
230
|
+ sheet.addMergedRegion(new CellRangeAddress(newRowIdx, newRowIdx, 6, 7));
|
|
|
231
|
+ }
|
|
|
232
|
+ }
|
|
|
233
|
+ // "申请数量"标签(A列)纵向合并到最后一行申请行
|
|
|
234
|
+ sheet.addMergedRegion(new CellRangeAddress(APPLY_START_ROW, APPLY_START_ROW + needRows - 1, 0, 0));
|
|
|
235
|
+ // 合并后"申请数量"标签设置水平/垂直居中(克隆原样式避免破坏边框)
|
|
|
236
|
+ Cell aCell = sheet.getRow(APPLY_START_ROW).getCell(0);
|
|
|
237
|
+ if (aCell != null) {
|
|
|
238
|
+ CellStyle centerStyle = workbook.createCellStyle();
|
|
|
239
|
+ centerStyle.cloneStyleFrom(aCell.getCellStyle());
|
|
|
240
|
+ centerStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
241
|
+ centerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
242
|
+ for (int r = APPLY_START_ROW; r <= APPLY_START_ROW + needRows - 1; r++) {
|
|
|
243
|
+ Cell ac = sheet.getRow(r).getCell(0);
|
|
|
244
|
+ if (ac != null) {
|
|
|
245
|
+ ac.setCellStyle(centerStyle);
|
|
|
246
|
+ }
|
|
|
247
|
+ }
|
|
|
248
|
+ }
|
|
|
249
|
+ }
|
|
|
250
|
+
|
|
|
251
|
+ for (int i = 0; i < dataSize; i++) {
|
|
|
252
|
+ GetProcurementWeighingTicketApplyLineBo line = applyLines.get(i);
|
|
|
253
|
+ int rowIdx = APPLY_START_ROW + i / 2;
|
|
|
254
|
+ if (i % 2 == 0) {
|
|
|
255
|
+ // 第1条:B列品种代号、C列数量(C:D合并)
|
|
|
256
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 1, nvl(line.getCategory()));
|
|
|
257
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 2, line.getQuantity());
|
|
|
258
|
+ } else {
|
|
|
259
|
+ // 第2条:E列品种代号(E:F合并)、G列数量(G:H合并)
|
|
|
260
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 4, nvl(line.getCategory()));
|
|
|
261
|
+ ExcelUtil.setCellValue(sheet, rowIdx, 6, line.getQuantity());
|
|
|
262
|
+ }
|
|
|
263
|
+ }
|
|
|
264
|
+
|
|
|
265
|
+ // 占位符替换
|
|
|
266
|
+ DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
267
|
+ Map<String, Object> dataMap = new HashMap<>();
|
|
|
268
|
+ // 日期只取年月日
|
|
|
269
|
+ dataMap.put("firstGrossWeightTime",
|
|
|
270
|
+ data.getFirstGrossWeightTime() == null ? "" : data.getFirstGrossWeightTime().format(dateFormatter));
|
|
|
271
|
+ dataMap.put("supplierName", nvl(data.getSupplierName()));
|
|
|
272
|
+ dataMap.put("code", nvl(data.getCode()));
|
|
|
273
|
+ dataMap.put("receiveUnitName", nvl(data.getReceiveUnitName()));
|
|
|
274
|
+ dataMap.put("materialName", nvl(data.getMaterialName()));
|
|
|
275
|
+ dataMap.put("totalGrossWeight", formatDecimal(data.getTotalGrossWeight()));
|
|
|
276
|
+ dataMap.put("actualImpurityWeight", formatDecimal(data.getActualImpurityWeight()));
|
|
|
277
|
+ dataMap.put("netWeight", formatDecimal(data.getNetWeight()));
|
|
|
278
|
+ dataMap.put("netWeightUpper", buildNetWeightUpper(data.getNetWeight()));
|
|
|
279
|
+ dataMap.put("documentRemark", nvl(data.getDocumentRemark()));
|
|
|
280
|
+ // 审核人:供料复核=驾驶员,收料复核=分厂质检员,监磅=供应科计量员
|
|
|
281
|
+ dataMap.put("driver", nvl(data.getDriver()));
|
|
|
282
|
+ dataMap.put("qualityInspectorName", nvl(data.getQualityInspectorName()));
|
|
|
283
|
+ dataMap.put("weigherName", nvl(data.getWeigherName()));
|
|
|
284
|
+ dataMap.put("createBy", nvl(data.getCreateBy()));
|
|
|
285
|
+ processTemplateSheet(sheet, dataMap);
|
|
|
286
|
+
|
|
|
287
|
+ if ("PDF".equals(exportType)) {
|
|
|
288
|
+ // 导出 PDF:先写临时 .xlsx 文件,再调 LibreOffice 转换输出
|
|
|
289
|
+ ResponseUtil.setPDFResponseHead(response, nvl(data.getCode()) + "-过磅码单.pdf");
|
|
|
290
|
+
|
|
|
291
|
+ File tempExcel = File.createTempFile("weighing_ticket_" + nvl(data.getCode()), ".xlsx");
|
|
|
292
|
+ try {
|
|
|
293
|
+ try (FileOutputStream fos = new FileOutputStream(tempExcel)) {
|
|
|
294
|
+ workbook.write(fos);
|
|
|
295
|
+ }
|
|
|
296
|
+ // 调用 LibreOffice 转 PDF
|
|
|
297
|
+ File pdfFile = ExcelUtil.convertExcelToPdf(tempExcel,
|
|
|
298
|
+ "/usr/bin/libreoffice --headless --convert-to pdf --outdir %s %s");
|
|
|
299
|
+ try (InputStream pdfIn = new FileInputStream(pdfFile)) {
|
|
|
300
|
+ byte[] buffer = new byte[8192];
|
|
|
301
|
+ int len;
|
|
|
302
|
+ while ((len = pdfIn.read(buffer)) != -1) {
|
|
|
303
|
+ response.getOutputStream().write(buffer, 0, len);
|
|
|
304
|
+ }
|
|
|
305
|
+ response.getOutputStream().flush();
|
|
|
306
|
+ } finally {
|
|
|
307
|
+ if (pdfFile != null && pdfFile.exists()) {
|
|
|
308
|
+ Files.delete(pdfFile.toPath());
|
|
|
309
|
+ }
|
|
|
310
|
+ }
|
|
|
311
|
+ } finally {
|
|
|
312
|
+ if (tempExcel != null && tempExcel.exists()) {
|
|
|
313
|
+ Files.delete(tempExcel.toPath());
|
|
|
314
|
+ }
|
|
|
315
|
+ }
|
|
|
316
|
+ } else {
|
|
|
317
|
+ // 默认导出 Excel
|
|
|
318
|
+ ResponseUtil.setExcelResponseHead(response, nvl(data.getCode()) + "-过磅码单.xlsx");
|
|
|
319
|
+ workbook.write(response.getOutputStream());
|
|
|
320
|
+ response.getOutputStream().flush();
|
|
|
321
|
+ }
|
|
|
322
|
+ } catch (FileNotFoundException e) {
|
|
|
323
|
+ throw new RuntimeException("模板文件不存在: " + templateFile, e);
|
|
|
324
|
+ } catch (IOException e) {
|
|
|
325
|
+ throw new RuntimeException("无法读取模板文件: " + templateFile, e);
|
|
|
326
|
+ }
|
|
|
327
|
+ } catch (Exception e) {
|
|
|
328
|
+ log.error("打印过磅码单失败, id={}, {}", id, e.getMessage(), e);
|
|
|
329
|
+ }
|
|
|
330
|
+ }
|
|
|
331
|
+
|
|
|
332
|
+ /**
|
|
|
333
|
+ * 按单 Sheet 替换模板占位符(${...} / #{...})
|
|
|
334
|
+ */
|
|
|
335
|
+ private void processTemplateSheet(Sheet sheet, Map<String, Object> dataMap) {
|
|
|
336
|
+ for (Row row : sheet) {
|
|
|
337
|
+ for (Cell cell : row) {
|
|
|
338
|
+ if (cell.getCellTypeEnum() == CellType.STRING) {
|
|
|
339
|
+ String cellValue = cell.getStringCellValue();
|
|
|
340
|
+ String newValue = replacePlaceholders(cellValue, dataMap);
|
|
|
341
|
+ if (!cellValue.equals(newValue)) {
|
|
|
342
|
+ cell.setCellValue(newValue);
|
|
|
343
|
+ }
|
|
|
344
|
+ }
|
|
|
345
|
+ }
|
|
|
346
|
+ }
|
|
|
347
|
+ }
|
|
|
348
|
+
|
|
|
349
|
+ private String replacePlaceholders(String text, Map<String, Object> dataMap) {
|
|
|
350
|
+ if (text == null || text.isEmpty()) {
|
|
|
351
|
+ return text;
|
|
|
352
|
+ }
|
|
|
353
|
+
|
|
|
354
|
+ String result = text;
|
|
|
355
|
+ for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
|
|
|
356
|
+ String placeholder = "${" + entry.getKey() + "}";
|
|
|
357
|
+ String value = entry.getValue() != null ? entry.getValue().toString() : "";
|
|
|
358
|
+ value = value.replace("\\n", "\n");
|
|
|
359
|
+ result = result.replace(placeholder, value);
|
|
|
360
|
+ }
|
|
|
361
|
+
|
|
|
362
|
+ for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
|
|
|
363
|
+ String placeholder = "#{" + entry.getKey() + "}";
|
|
|
364
|
+ String value = entry.getValue() != null ? entry.getValue().toString() : "";
|
|
|
365
|
+ value = value.replace("\\n", "\n");
|
|
|
366
|
+ result = result.replace(placeholder, value);
|
|
|
367
|
+ }
|
|
|
368
|
+
|
|
|
369
|
+ return result;
|
|
|
370
|
+ }
|
|
|
371
|
+
|
|
|
372
|
+ /**
|
|
|
373
|
+ * 净重大写:按模板"X万X仟X佰X拾X点X公斤"格式由净重自动生成(壹贰叁大写、位名万仟佰拾、小数读"点"、单位公斤)
|
|
|
374
|
+ */
|
|
|
375
|
+ private String buildNetWeightUpper(BigDecimal netWeight) {
|
|
|
376
|
+ if (netWeight == null) {
|
|
|
377
|
+ return "";
|
|
|
378
|
+ }
|
|
|
379
|
+ // isUseTraditional=true 输出大写(壹贰叁…),位名与模板一致(万仟佰拾),小数读"点"
|
|
|
380
|
+ return NumberChineseFormatter.format(netWeight.doubleValue(), true) + "公斤";
|
|
|
381
|
+ }
|
|
|
382
|
+
|
|
|
383
|
+ private String nvl(Object o) {
|
|
|
384
|
+ return o == null ? "" : o.toString();
|
|
|
385
|
+ }
|
|
|
386
|
+
|
|
|
387
|
+ private String formatDecimal(BigDecimal bd) {
|
|
|
388
|
+ if (bd == null) {
|
|
|
389
|
+ return "";
|
|
|
390
|
+ }
|
|
|
391
|
+ return bd.stripTrailingZeros().toPlainString();
|
|
|
392
|
+ }
|
|
144
|
393
|
} |
...
|
...
|
|