|
1
|
1
|
package com.lframework.xingyun.sc.controller.shipments;
|
|
2
|
2
|
|
|
|
3
|
+import cn.hutool.core.io.resource.ClassPathResource;
|
|
|
4
|
+import com.lframework.starter.web.core.utils.JsonUtil;
|
|
|
5
|
+import com.lframework.starter.web.inner.entity.SysUser;
|
|
|
6
|
+import com.lframework.starter.web.inner.service.system.SysUserService;
|
|
3
|
7
|
import com.lframework.xingyun.sc.bo.shipments.order.GetShipmentsOrderInfoBo;
|
|
4
|
8
|
import com.lframework.xingyun.sc.bo.shipments.order.QueryShipmentsOrderInfoBo;
|
|
5
|
9
|
import com.lframework.xingyun.sc.bo.shipments.plan.ShipmentsPlanDetailBo;
|
|
6
|
10
|
import com.lframework.xingyun.sc.entity.ShipmentsPlanDetail;
|
|
7
|
11
|
import com.lframework.xingyun.sc.service.shipments.ShipmentsPlanDetailService;
|
|
|
12
|
+import com.lframework.xingyun.sc.utils.ExcelUtil;
|
|
|
13
|
+import com.lframework.xingyun.sc.utils.LatexFormulaExcelExporterUtil;
|
|
|
14
|
+import com.lframework.xingyun.sc.utils.ResponseUtil;
|
|
8
|
15
|
import com.lframework.xingyun.sc.vo.shipments.order.*;
|
|
9
|
16
|
import com.lframework.xingyun.sc.service.shipments.ShipmentsOrderInfoService;
|
|
10
|
17
|
import com.lframework.xingyun.sc.entity.ShipmentsOrderInfo;
|
|
...
|
...
|
@@ -13,6 +20,7 @@ import com.lframework.starter.web.core.components.resp.PageResult; |
|
13
|
20
|
import com.lframework.starter.web.core.components.resp.InvokeResult;
|
|
14
|
21
|
|
|
15
|
22
|
import javax.annotation.Resource;
|
|
|
23
|
+import javax.servlet.http.HttpServletResponse;
|
|
16
|
24
|
import javax.validation.constraints.NotBlank;
|
|
17
|
25
|
import io.swagger.annotations.ApiImplicitParam;
|
|
18
|
26
|
import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
|
|
...
|
...
|
@@ -20,6 +28,12 @@ import com.lframework.starter.common.exceptions.impl.DefaultClientException; |
|
20
|
28
|
import io.swagger.annotations.ApiOperation;
|
|
21
|
29
|
import com.lframework.starter.common.utils.CollectionUtil;
|
|
22
|
30
|
import io.swagger.annotations.Api;
|
|
|
31
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
32
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
33
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
34
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
35
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
36
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
23
|
37
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
|
24
|
38
|
import com.lframework.starter.web.core.controller.DefaultBaseController;
|
|
25
|
39
|
import com.lframework.starter.web.core.annotations.security.HasPermission;
|
|
...
|
...
|
@@ -27,8 +41,19 @@ import org.springframework.validation.annotation.Validated; |
|
27
|
41
|
import org.springframework.web.bind.annotation.*;
|
|
28
|
42
|
|
|
29
|
43
|
import javax.validation.Valid;
|
|
|
44
|
+import java.io.*;
|
|
|
45
|
+import java.math.BigDecimal;
|
|
|
46
|
+import java.math.RoundingMode;
|
|
|
47
|
+import java.nio.file.Files;
|
|
|
48
|
+import java.nio.file.Path;
|
|
|
49
|
+import java.time.format.DateTimeFormatter;
|
|
|
50
|
+import java.util.ArrayList;
|
|
|
51
|
+import java.util.Comparator;
|
|
30
|
52
|
import java.util.List;
|
|
|
53
|
+import java.util.Map;
|
|
31
|
54
|
import java.util.stream.Collectors;
|
|
|
55
|
+import java.util.zip.ZipEntry;
|
|
|
56
|
+import java.util.zip.ZipOutputStream;
|
|
32
|
57
|
|
|
33
|
58
|
/**
|
|
34
|
59
|
* 发货单 Controller
|
|
...
|
...
|
@@ -36,6 +61,7 @@ import java.util.stream.Collectors; |
|
36
|
61
|
*/
|
|
37
|
62
|
@Api(tags = "发货单")
|
|
38
|
63
|
@Validated
|
|
|
64
|
+@Slf4j
|
|
39
|
65
|
@RestController
|
|
40
|
66
|
@RequestMapping("/shipments/order")
|
|
41
|
67
|
public class ShipmentsOrderInfoController extends DefaultBaseController {
|
|
...
|
...
|
@@ -45,6 +71,8 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { |
|
45
|
71
|
private ShipmentsOrderInfoService shipmentsOrderInfoService;
|
|
46
|
72
|
@Resource
|
|
47
|
73
|
private ShipmentsPlanDetailService shipmentsPlanDetailService;
|
|
|
74
|
+ @Resource
|
|
|
75
|
+ private SysUserService sysUserService;
|
|
48
|
76
|
|
|
49
|
77
|
|
|
50
|
78
|
/**
|
|
...
|
...
|
@@ -164,4 +192,252 @@ public class ShipmentsOrderInfoController extends DefaultBaseController { |
|
164
|
192
|
shipmentsOrderInfoService.saveSignInTicket(vo);
|
|
165
|
193
|
return InvokeResultBuilder.success();
|
|
166
|
194
|
}
|
|
|
195
|
+
|
|
|
196
|
+
|
|
|
197
|
+ /**
|
|
|
198
|
+ * 发货单批量打印
|
|
|
199
|
+ * @param vo 打印参数
|
|
|
200
|
+ * @param response 响应体
|
|
|
201
|
+ */
|
|
|
202
|
+ @ApiOperation("发货单批量打印")
|
|
|
203
|
+ @GetMapping("/batchPrintShipmentsOrder")
|
|
|
204
|
+ public void batchPrintShipmentsOrder(@Valid ExportShipmentsOrderVo vo, HttpServletResponse response) {
|
|
|
205
|
+ List<ShipmentsOrderInfo> orderInfoList = shipmentsOrderInfoService.listByIds(vo.getIds());
|
|
|
206
|
+ if (CollectionUtils.isEmpty(orderInfoList)) {
|
|
|
207
|
+ throw new DefaultClientException("发货单不存在!");
|
|
|
208
|
+ }
|
|
|
209
|
+ // 加载模板文件
|
|
|
210
|
+ String templatePath = "templates/shipmentsOrderTemplate_02.xlsx";
|
|
|
211
|
+ if ("TYPE_1".equals(vo.getType())) {
|
|
|
212
|
+ templatePath = "templates/shipmentsOrderTemplate_01.xlsx";
|
|
|
213
|
+ }
|
|
|
214
|
+ // 创建临时目录
|
|
|
215
|
+ Path tempDir = null;
|
|
|
216
|
+ File zipFile = null;
|
|
|
217
|
+ try {
|
|
|
218
|
+ // 设置响应头
|
|
|
219
|
+ ResponseUtil.setZipResponseHead(response, "发货单.zip");
|
|
|
220
|
+ // 创建临时目录存放Excel文件
|
|
|
221
|
+ tempDir = Files.createTempDirectory("/tmp/shipments_order_export");
|
|
|
222
|
+
|
|
|
223
|
+ List<String> shipmentsOrderIds = new ArrayList<>();
|
|
|
224
|
+ for (ShipmentsOrderInfo orderInfo : orderInfoList) {
|
|
|
225
|
+ String id = orderInfo.getId();
|
|
|
226
|
+ shipmentsOrderIds.add(id);
|
|
|
227
|
+ }
|
|
|
228
|
+ List<ShipmentsPlanDetail> detailList = shipmentsPlanDetailService.listByShipmentOrderId(shipmentsOrderIds);
|
|
|
229
|
+ if (CollectionUtils.isEmpty(detailList)) {
|
|
|
230
|
+ throw new DefaultClientException("发货单明细不存在!");
|
|
|
231
|
+ }
|
|
|
232
|
+ // 按发货单ID分组明细数据
|
|
|
233
|
+ Map<String, List<ShipmentsPlanDetail>> shipmentsOrderDetailMap = detailList.stream()
|
|
|
234
|
+ .collect(Collectors.groupingBy(ShipmentsPlanDetail::getShipmentOrderId));
|
|
|
235
|
+ // 为每个发货单生成Excel文件
|
|
|
236
|
+ List<File> excelFiles = new ArrayList<>();
|
|
|
237
|
+ for (ShipmentsOrderInfo orderInfo : orderInfoList) {
|
|
|
238
|
+ String id = orderInfo.getId();
|
|
|
239
|
+ List<ShipmentsPlanDetail> details = shipmentsOrderDetailMap.get(id);
|
|
|
240
|
+ if (CollectionUtils.isEmpty(details)) {
|
|
|
241
|
+ throw new DefaultClientException("发货单明细不存在!");
|
|
|
242
|
+ }
|
|
|
243
|
+ // 生成单个Excel文件(使用模板引擎)
|
|
|
244
|
+ File excelFile = generateExcelWithTemplate(orderInfo, details, templatePath, tempDir, vo.getType());
|
|
|
245
|
+ if (excelFile != null) {
|
|
|
246
|
+ excelFiles.add(excelFile);
|
|
|
247
|
+ }
|
|
|
248
|
+ }
|
|
|
249
|
+ // 创建ZIP压缩包
|
|
|
250
|
+ createZipFile(excelFiles, "发货单批量导出_" + System.currentTimeMillis() + ".zip");
|
|
|
251
|
+ } catch (FileNotFoundException e) {
|
|
|
252
|
+ log.error("发货单批量打印失败: ", e);
|
|
|
253
|
+ throw new DefaultClientException("模板文件不存在: " + templatePath);
|
|
|
254
|
+ } catch (IOException e) {
|
|
|
255
|
+ log.error("发货单批量打印失败: ", e);
|
|
|
256
|
+ throw new DefaultClientException("无法读取模板文件: " + templatePath);
|
|
|
257
|
+ } catch (Exception e) {
|
|
|
258
|
+ log.error("发货单批量打印失败: ", e);
|
|
|
259
|
+ throw new DefaultClientException(e.getMessage());
|
|
|
260
|
+ } finally {
|
|
|
261
|
+ // 清理临时文件
|
|
|
262
|
+ cleanupTempFiles(tempDir, zipFile);
|
|
|
263
|
+ }
|
|
|
264
|
+ }
|
|
|
265
|
+
|
|
|
266
|
+
|
|
|
267
|
+ /**
|
|
|
268
|
+ * 生成excel文件
|
|
|
269
|
+ *
|
|
|
270
|
+ * @param orderInfo 发货单
|
|
|
271
|
+ * @param detailList 发货单明细
|
|
|
272
|
+ * @param templatePath 模板文件路径
|
|
|
273
|
+ * @param tempDir 临时目录
|
|
|
274
|
+ * @return File
|
|
|
275
|
+ * @throws IOException 抛出异常
|
|
|
276
|
+ */
|
|
|
277
|
+ private File generateExcelWithTemplate(ShipmentsOrderInfo orderInfo, List<ShipmentsPlanDetail> detailList, String templatePath, Path tempDir
|
|
|
278
|
+ , String type) throws IOException {
|
|
|
279
|
+ ClassPathResource templateResource = new ClassPathResource(templatePath);
|
|
|
280
|
+ try (InputStream inputStream = templateResource.getStream();
|
|
|
281
|
+ Workbook workbook = new XSSFWorkbook(inputStream)) {
|
|
|
282
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
283
|
+ // 发货单明细开始行
|
|
|
284
|
+ int startRow = 4;
|
|
|
285
|
+ DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
286
|
+ for (int i = startRow + 1; i < startRow + detailList.size(); i++) {
|
|
|
287
|
+ ExcelUtil.copyRow(workbook, sheet, startRow, i);
|
|
|
288
|
+ }
|
|
|
289
|
+ BigDecimal totalQuantity = BigDecimal.ZERO;
|
|
|
290
|
+ BigDecimal totalActualQuantity = BigDecimal.ZERO;
|
|
|
291
|
+ BigDecimal totalNum = BigDecimal.ZERO;
|
|
|
292
|
+ BigDecimal totalPrice = BigDecimal.ZERO;
|
|
|
293
|
+ for (ShipmentsPlanDetail detail : detailList) {
|
|
|
294
|
+ BigDecimal quantity = detail.getQuantity() == null ? BigDecimal.ZERO : detail.getQuantity();
|
|
|
295
|
+ BigDecimal actualQuantity = detail.getActualShipmentQuantity() == null ? BigDecimal.ZERO : new BigDecimal(detail.getActualShipmentQuantity());
|
|
|
296
|
+ BigDecimal num = detail.getNum() == null ? BigDecimal.ZERO : new BigDecimal(detail.getNum());
|
|
|
297
|
+
|
|
|
298
|
+ totalQuantity = totalQuantity.add(quantity);
|
|
|
299
|
+ totalActualQuantity = totalActualQuantity.add(actualQuantity);
|
|
|
300
|
+ totalNum = totalNum.add(num);
|
|
|
301
|
+ // 计算总金额
|
|
|
302
|
+ if (detail.getSalesPrice() != null) {
|
|
|
303
|
+ BigDecimal result = actualQuantity.multiply(detail.getSalesPrice());
|
|
|
304
|
+ totalPrice = totalPrice.add(result);
|
|
|
305
|
+ }
|
|
|
306
|
+ }
|
|
|
307
|
+ for (ShipmentsPlanDetail detail : detailList) {
|
|
|
308
|
+ ExcelUtil.setCellValue(sheet, startRow, 0, detail.getOrderNo());
|
|
|
309
|
+ ExcelUtil.setCellValue(sheet, startRow, 1, detail.getBrand());
|
|
|
310
|
+ // 处理规格数据
|
|
|
311
|
+ List<LatexFormulaExcelExporterUtil.FormulaComponent> formulaComponentList = new ArrayList<>(3);
|
|
|
312
|
+ if (detail.getThickness() != null) {
|
|
|
313
|
+ LatexFormulaExcelExporterUtil.FormulaComponent formulaComponent = new LatexFormulaExcelExporterUtil.FormulaComponent();
|
|
|
314
|
+ formulaComponent.setBase(detail.getThickness());
|
|
|
315
|
+ formulaComponent.setSup(detail.getThicknessTolPos());
|
|
|
316
|
+ formulaComponent.setSub(detail.getThicknessTolNeg());
|
|
|
317
|
+ formulaComponentList.add(formulaComponent);
|
|
|
318
|
+ }
|
|
|
319
|
+ if (detail.getWidth() != null) {
|
|
|
320
|
+ LatexFormulaExcelExporterUtil.FormulaComponent formulaComponent = new LatexFormulaExcelExporterUtil.FormulaComponent();
|
|
|
321
|
+ formulaComponent.setBase(detail.getWidth());
|
|
|
322
|
+ formulaComponent.setSup(detail.getWidthTolPos());
|
|
|
323
|
+ formulaComponent.setSub(detail.getWidthTolNeg());
|
|
|
324
|
+ formulaComponentList.add(formulaComponent);
|
|
|
325
|
+ }
|
|
|
326
|
+ if (detail.getLength() != null) {
|
|
|
327
|
+ LatexFormulaExcelExporterUtil.FormulaComponent formulaComponent = new LatexFormulaExcelExporterUtil.FormulaComponent();
|
|
|
328
|
+ formulaComponent.setBase(detail.getLength());
|
|
|
329
|
+ formulaComponent.setSup(detail.getLengthTolPos());
|
|
|
330
|
+ formulaComponent.setSub(detail.getLengthTolNeg());
|
|
|
331
|
+ formulaComponentList.add(formulaComponent);
|
|
|
332
|
+ }
|
|
|
333
|
+ String latex = LatexFormulaExcelExporterUtil.convertToLatex(formulaComponentList);
|
|
|
334
|
+ if (StringUtils.isNotBlank(latex)) {
|
|
|
335
|
+ LatexFormulaExcelExporterUtil.insertLatexImageToCell(workbook, sheet, latex, startRow, 4, 1, 7);
|
|
|
336
|
+ }
|
|
|
337
|
+ // 处理订单类型
|
|
|
338
|
+ String orderType = handleContractType(detail.getContractType());
|
|
|
339
|
+
|
|
|
340
|
+ ExcelUtil.setCellValue(sheet, startRow, 9, detail.getQuantity());
|
|
|
341
|
+ if ("TYPE_2".equals(type)) {
|
|
|
342
|
+ ExcelUtil.setCellValue(sheet, startRow, 10, detail.getActualShipmentQuantity());
|
|
|
343
|
+ ExcelUtil.setCellValue(sheet, startRow, 11, detail.getNum());
|
|
|
344
|
+ ExcelUtil.setCellValue(sheet, startRow, 12, detail.getSalesPrice());
|
|
|
345
|
+ ExcelUtil.setCellValue(sheet, startRow, 13, orderType);
|
|
|
346
|
+ ExcelUtil.setCellValue(sheet, startRow, 14, detail.getYieldBatchNo());
|
|
|
347
|
+ } else {
|
|
|
348
|
+ ExcelUtil.setCellValue(sheet, startRow, 13, detail.getPackagingFee());
|
|
|
349
|
+ ExcelUtil.setCellValue(sheet, startRow, 15, orderType);
|
|
|
350
|
+ }
|
|
|
351
|
+ startRow++;
|
|
|
352
|
+ }
|
|
|
353
|
+ // 准备模板数据
|
|
|
354
|
+ Map<String, Object> dataMap = JsonUtil.parseMap(JsonUtil.toJsonString(orderInfo), String.class, Object.class);
|
|
|
355
|
+ dataMap.put("createDate", dateFormatter.format(orderInfo.getCreateTime()));
|
|
|
356
|
+ SysUser user = sysUserService.findById(orderInfo.getCreateById());
|
|
|
357
|
+ dataMap.put("createBy", user.getName());
|
|
|
358
|
+ dataMap.put("totalQuantity", totalQuantity.setScale(2, RoundingMode.HALF_UP));
|
|
|
359
|
+ dataMap.put("totalActualQuantity", totalActualQuantity.setScale(2, RoundingMode.HALF_UP));
|
|
|
360
|
+ dataMap.put("totalNum", totalNum.setScale(2, RoundingMode.HALF_UP));
|
|
|
361
|
+ dataMap.put("totalPrice", totalPrice.setScale(2, RoundingMode.HALF_UP));
|
|
|
362
|
+ // 流水号
|
|
|
363
|
+ String serialNumber = "";
|
|
|
364
|
+ // 处理模板中的占位符
|
|
|
365
|
+ ExcelUtil.processTemplate(workbook, dataMap);
|
|
|
366
|
+ // 生成文件名
|
|
|
367
|
+ String fileName = String.format("发货单_%s.xlsx", serialNumber);
|
|
|
368
|
+ File excelFile = new File(tempDir.toFile(), fileName);
|
|
|
369
|
+ // 保存Excel文件
|
|
|
370
|
+ try (FileOutputStream fos = new FileOutputStream(excelFile)) {
|
|
|
371
|
+ workbook.write(fos);
|
|
|
372
|
+ }
|
|
|
373
|
+ return excelFile;
|
|
|
374
|
+ } catch (IOException e) {
|
|
|
375
|
+ log.error("生成发货单Excel失败, 发货单ID: {}", orderInfo.getId(), e);
|
|
|
376
|
+ return null;
|
|
|
377
|
+ }
|
|
|
378
|
+ }
|
|
|
379
|
+
|
|
|
380
|
+ /**
|
|
|
381
|
+ * 处理订单类型
|
|
|
382
|
+ *
|
|
|
383
|
+ * @param contractType 合同类型
|
|
|
384
|
+ * @return String
|
|
|
385
|
+ */
|
|
|
386
|
+ private String handleContractType(String contractType) {
|
|
|
387
|
+ String result = "";
|
|
|
388
|
+ if ("DISTRIB_STD".equals(contractType) || "DIST_STOCK_CONTRACT".equals(contractType)
|
|
|
389
|
+ || "DRAFT_DIST_AGMT".equals(contractType)) {
|
|
|
390
|
+ result = "经销";
|
|
|
391
|
+ } else if ("INTL_STD_CONTRACT".equals(contractType) || "INTL_INVENTORY_AGMT".equals(contractType)
|
|
|
392
|
+ || "INTL_OPEN_SPEC_AGMT".equals(contractType)) {
|
|
|
393
|
+ result = "外贸";
|
|
|
394
|
+ } else if ("PROCESS_STD_AGMT".equals(contractType)) {
|
|
|
395
|
+ result = "加工";
|
|
|
396
|
+ }
|
|
|
397
|
+ return result;
|
|
|
398
|
+ }
|
|
|
399
|
+
|
|
|
400
|
+
|
|
|
401
|
+ /**
|
|
|
402
|
+ * 创建ZIP压缩包
|
|
|
403
|
+ */
|
|
|
404
|
+ private void createZipFile(List<File> excelFiles, String zipFileName) throws IOException {
|
|
|
405
|
+ File zipFile = File.createTempFile("/tmp/shipments_order_export", ".zip");
|
|
|
406
|
+ try (FileOutputStream fos = new FileOutputStream(zipFile);
|
|
|
407
|
+ ZipOutputStream zos = new ZipOutputStream(fos)) {
|
|
|
408
|
+ byte[] buffer = new byte[1024];
|
|
|
409
|
+ for (File excelFile : excelFiles) {
|
|
|
410
|
+ try (FileInputStream fis = new FileInputStream(excelFile)) {
|
|
|
411
|
+ ZipEntry zipEntry = new ZipEntry(excelFile.getName());
|
|
|
412
|
+ zos.putNextEntry(zipEntry);
|
|
|
413
|
+ int length;
|
|
|
414
|
+ while ((length = fis.read(buffer)) > 0) {
|
|
|
415
|
+ zos.write(buffer, 0, length);
|
|
|
416
|
+ }
|
|
|
417
|
+ zos.closeEntry();
|
|
|
418
|
+ }
|
|
|
419
|
+ }
|
|
|
420
|
+ }
|
|
|
421
|
+ }
|
|
|
422
|
+
|
|
|
423
|
+ /**
|
|
|
424
|
+ * 清理临时文件
|
|
|
425
|
+ */
|
|
|
426
|
+ private void cleanupTempFiles(Path tempDir, File zipFile) {
|
|
|
427
|
+ try {
|
|
|
428
|
+ // 删除临时目录
|
|
|
429
|
+ if (tempDir != null && Files.exists(tempDir)) {
|
|
|
430
|
+ Files.walk(tempDir)
|
|
|
431
|
+ .sorted(Comparator.reverseOrder())
|
|
|
432
|
+ .map(Path::toFile)
|
|
|
433
|
+ .forEach(File::delete);
|
|
|
434
|
+ }
|
|
|
435
|
+ // 清理
|
|
|
436
|
+ if (zipFile != null && zipFile.exists()) {
|
|
|
437
|
+ zipFile.delete();
|
|
|
438
|
+ }
|
|
|
439
|
+ } catch (IOException e) {
|
|
|
440
|
+ log.warn("清理临时文件失败", e);
|
|
|
441
|
+ }
|
|
|
442
|
+ }
|
|
167
|
443
|
} |
...
|
...
|
|