Commit 31daad043cfeb0b586df16d012fcc1ebbbde5709

Authored by yeqianyong
1 parent b29ae7df

楚江ERP-发货单批量打印

... ... @@ -46,6 +46,7 @@ import java.math.BigDecimal;
46 46 import java.math.RoundingMode;
47 47 import java.nio.file.Files;
48 48 import java.nio.file.Path;
  49 +import java.nio.file.Paths;
49 50 import java.time.format.DateTimeFormatter;
50 51 import java.util.ArrayList;
51 52 import java.util.Comparator;
... ... @@ -201,8 +202,8 @@ public class ShipmentsOrderInfoController extends DefaultBaseController {
201 202 */
202 203 @ApiOperation("发货单批量打印")
203 204 @GetMapping("/batchPrintShipmentsOrder")
204   - public void batchPrintShipmentsOrder(@Valid ExportShipmentsOrderVo vo, HttpServletResponse response) {
205   - List<ShipmentsOrderInfo> orderInfoList = shipmentsOrderInfoService.listByIds(vo.getIds());
  205 + public void batchPrintShipmentsOrder(@Valid @RequestBody ExportShipmentsOrderVo vo, HttpServletResponse response) {
  206 + List<ShipmentsOrderInfo> orderInfoList = shipmentsOrderInfoService.queryByIds(vo.getIds());
206 207 if (CollectionUtils.isEmpty(orderInfoList)) {
207 208 throw new DefaultClientException("发货单不存在!");
208 209 }
... ... @@ -214,11 +215,11 @@ public class ShipmentsOrderInfoController extends DefaultBaseController {
214 215 // 创建临时目录
215 216 Path tempDir = null;
216 217 File zipFile = null;
  218 + Path baseDir = Paths.get("/tmp");
217 219 try {
218 220 // 设置响应头
219   - ResponseUtil.setZipResponseHead(response, "发货单.zip");
220   - // 创建临时目录存放Excel文件
221   - tempDir = Files.createTempDirectory("/tmp/shipments_order_export");
  221 + ResponseUtil.setZipResponseHead(response, "发货单批量打印_" + System.currentTimeMillis() + ".zip"); // 创建临时目录存放Excel文件
  222 + tempDir = Files.createTempDirectory(baseDir, "shipments_order_export");
222 223
223 224 List<String> shipmentsOrderIds = new ArrayList<>();
224 225 for (ShipmentsOrderInfo orderInfo : orderInfoList) {
... ... @@ -247,7 +248,9 @@ public class ShipmentsOrderInfoController extends DefaultBaseController {
247 248 }
248 249 }
249 250 // 创建ZIP压缩包
250   - createZipFile(excelFiles, "发货单批量导出_" + System.currentTimeMillis() + ".zip");
  251 + zipFile = createZipFile(excelFiles, tempDir);
  252 + // 将ZIP文件写入响应流
  253 + writeZipToResponse(zipFile, response);
251 254 } catch (FileNotFoundException e) {
252 255 log.error("发货单批量打印失败: ", e);
253 256 throw new DefaultClientException("模板文件不存在: " + templatePath);
... ... @@ -399,10 +402,12 @@ public class ShipmentsOrderInfoController extends DefaultBaseController {
399 402
400 403
401 404 /**
402   - * 创建ZIP压缩包
  405 + * 创建ZIP压缩包并返回文件对象
403 406 */
404   - private void createZipFile(List<File> excelFiles, String zipFileName) throws IOException {
405   - File zipFile = File.createTempFile("/tmp/shipments_order_export", ".zip");
  407 + private File createZipFile(List<File> excelFiles, Path tempDir) throws IOException {
  408 + String zipFileName = "发货单批量打印_" + System.currentTimeMillis() + ".zip";
  409 + File zipFile = new File(tempDir.toFile(), zipFileName);
  410 +
406 411 try (FileOutputStream fos = new FileOutputStream(zipFile);
407 412 ZipOutputStream zos = new ZipOutputStream(fos)) {
408 413 byte[] buffer = new byte[1024];
... ... @@ -418,6 +423,23 @@ public class ShipmentsOrderInfoController extends DefaultBaseController {
418 423 }
419 424 }
420 425 }
  426 + return zipFile;
  427 + }
  428 +
  429 +
  430 + /**
  431 + * 将ZIP文件写入HTTP响应
  432 + */
  433 + private void writeZipToResponse(File zipFile, HttpServletResponse response) throws IOException {
  434 + try (FileInputStream fis = new FileInputStream(zipFile);
  435 + OutputStream os = response.getOutputStream()) {
  436 + byte[] buffer = new byte[4096];
  437 + int bytesRead;
  438 + while ((bytesRead = fis.read(buffer)) != -1) {
  439 + os.write(buffer, 0, bytesRead);
  440 + }
  441 + os.flush();
  442 + }
421 443 }
422 444
423 445 /**
... ... @@ -425,17 +447,17 @@ public class ShipmentsOrderInfoController extends DefaultBaseController {
425 447 */
426 448 private void cleanupTempFiles(Path tempDir, File zipFile) {
427 449 try {
428   - // 删除临时目录
  450 + // 删除临时目录及其所有内容
429 451 if (tempDir != null && Files.exists(tempDir)) {
430 452 Files.walk(tempDir)
431 453 .sorted(Comparator.reverseOrder())
432 454 .map(Path::toFile)
433 455 .forEach(File::delete);
434 456 }
435   - // 清理
436   - if (zipFile != null && zipFile.exists()) {
437   - zipFile.delete();
438   - }
  457 + // 单独删除ZIP文件(如果存在)
  458 + if (zipFile != null && zipFile.exists()) {
  459 + zipFile.delete();
  460 + }
439 461 } catch (IOException e) {
440 462 log.warn("清理临时文件失败", e);
441 463 }
... ...
... ... @@ -116,6 +116,12 @@ public class ShipmentsOrderInfo extends BaseEntity implements BaseDto {
116 116 private String createById;
117 117
118 118 /**
  119 + * 创建人姓名
  120 + */
  121 + @TableField(exist = false)
  122 + private String createBy;
  123 +
  124 + /**
119 125 * 更新人ID
120 126 */
121 127 @TableField(fill = FieldFill.INSERT_UPDATE)
... ...
... ... @@ -366,6 +366,14 @@ public class ShipmentsOrderInfoServiceImpl extends BaseMpServiceImpl<ShipmentsOr
366 366 getBaseMapper().update(updateWrapper);
367 367 }
368 368
  369 + @Override
  370 + public List<ShipmentsOrderInfo> queryByIds(List<String> ids) {
  371 + if (CollectionUtils.isEmpty(ids)) {
  372 + return Collections.emptyList();
  373 + }
  374 + return getBaseMapper().queryByIds(ids);
  375 + }
  376 +
369 377
370 378 /**
371 379 * 处理批次数据
... ...
... ... @@ -29,4 +29,13 @@ public interface ShipmentsOrderInfoMapper extends BaseMapper<ShipmentsOrderInfo>
29 29 * @param orderInfos 数据集合
30 30 */
31 31 void batchAdd(List<ShipmentsOrderInfo> orderInfos);
  32 +
  33 +
  34 + /**
  35 + * 根据主键批量查询
  36 + *
  37 + * @param ids 主键集合
  38 + * @return List<ShipmentsOrderInfo>
  39 + */
  40 + List<ShipmentsOrderInfo> queryByIds(@Param("ids") List<String> ids);
32 41 }
... ...
... ... @@ -91,4 +91,12 @@ public interface ShipmentsOrderInfoService extends BaseMpService<ShipmentsOrderI
91 91 * @param status 状态
92 92 */
93 93 void updateStatus(String id, String status);
  94 +
  95 + /**
  96 + * 根据主键批量查询
  97 + *
  98 + * @param ids 主键集合
  99 + * @return List<ShipmentsOrderInfo>
  100 + */
  101 + List<ShipmentsOrderInfo> queryByIds(List<String> ids);
94 102 }
... ...
... ... @@ -33,6 +33,7 @@
33 33 tb.delivery_type,
34 34 tb.destination,
35 35 tb.status,
  36 + u.name as create_by,
36 37 tb.create_by_id,
37 38 tb.update_by_id,
38 39 tb.create_time,
... ... @@ -40,6 +41,7 @@
40 41 FROM shipments_order_info AS tb
41 42 LEFT JOIN base_data_customer c ON tb.customer_id = c.id
42 43 LEFT JOIN base_data_workshop w ON tb.workshop_id = w.id
  44 + LEFT JOIN sys_user u ON tb.create_by_id = u.id
43 45 </sql>
44 46
45 47 <select id="query" resultMap="ShipmentsOrderInfo">
... ... @@ -102,4 +104,12 @@
102 104 )
103 105 </foreach>
104 106 </insert>
  107 +
  108 + <select id="queryByIds" resultType="com.lframework.xingyun.sc.entity.ShipmentsOrderInfo">
  109 + <include refid="ShipmentsOrderInfo_sql"/>
  110 + where tb.id in
  111 + <foreach collection="ids" open="(" separator="," close=")" item="item">
  112 + #{item}
  113 + </foreach>
  114 + </select>
105 115 </mapper>
... ...