桌码打包下载失败修复

This commit is contained in:
张松
2025-02-27 19:11:13 +08:00
parent 63b46f25bd
commit ff6e40d1c9

View File

@@ -93,48 +93,60 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
throw new ApiNotPrintException("未添加台桌,请先添加台桌"); throw new ApiNotPrintException("未添加台桌,请先添加台桌");
} }
// 获取可用 id // 获取当前店铺下的最大ID确保只查询当前shopId的记录
List<ShopTableCode> records = shopTableCodeService.queryChain().orderBy(ShopTableCode::getId, false).page(new Page<>(1, 1)).getRecords(); List<ShopTableCode> records = shopTableCodeService.queryChain()
Map<String, Long> codeMap = shopTableCodeService.queryChain().eq(ShopTableCode::getShopId, shopId).select(ShopTableCode::getTableCode, ShopTableCode::getId) .eq(ShopTableCode::getShopId, shopId)
.list().stream().collect(Collectors.toMap(ShopTableCode::getTableCode, ShopTableCode::getId)); .orderBy(ShopTableCode::getId, false)
.page(new Page<>(1, 1))
.getRecords();
long maxId = records.isEmpty() ? 0 : records.getFirst().getId(); long maxId = records.isEmpty() ? 0 : records.getFirst().getId();
// 设置 ZIP 响应头 // 获取当前店铺下所有已存在的tableCode避免重复
response.setContentType("application/octet-stream"); Map<String, Long> codeMap = shopTableCodeService.queryChain()
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip"); .eq(ShopTableCode::getShopId, shopId)
.select(ShopTableCode::getTableCode, ShopTableCode::getId)
.list()
.stream()
.collect(Collectors.toMap(ShopTableCode::getTableCode, ShopTableCode::getId));
ArrayList<ShopTableCode> codeList = new ArrayList<>(); // 预生成所有二维码数据和记录避免在写入ZIP过程中出现异常
List<ShopTableCode> codeList = new ArrayList<>();
List<byte[]> qrBytesList = new ArrayList<>();
List<String> fileNames = new ArrayList<>();
// 使用 ZipOutputStream 直接将二维码写入 ZIP
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
String tableCode = generateCode(1, shopId, ++maxId, codeMap); String tableCode = generateCode(1, shopId, ++maxId, codeMap);
codeMap.put(tableCode, maxId); codeMap.put(tableCode, maxId);
codeList.add(new ShopTableCode().setShopId(shopId).setTableCode(tableCode)); codeList.add(new ShopTableCode()
.setShopId(shopId)
.setTableCode(tableCode));
// 生成二维码并写入 ByteArrayOutputStream // 生成二维码图片
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 生成二维码
BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300); BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300);
// 转换为字节数组 ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, byteArrayOutputStream); ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, baos);
byte[] qrBytes = byteArrayOutputStream.toByteArray(); qrBytesList.add(baos.toByteArray());
fileNames.add(tableCode + ".png");
// 创建一个新的 ZipEntry
zipOut.putNextEntry(new ZipEntry(tableCode + ".png"));
zipOut.write(qrBytes);
zipOut.closeEntry(); // 确保当前文件条目被关闭
} }
// 刷新并完成文件输出 // 先保存数据库记录确保生成ZIP前无异常
zipOut.flush(); shopTableCodeService.saveBatch(codeList);
// 设置响应头
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip");
// 写入ZIP文件
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
for (int i = 0; i < num; i++) {
ZipEntry entry = new ZipEntry(fileNames.get(i));
zipOut.putNextEntry(entry);
zipOut.write(qrBytesList.get(i));
zipOut.closeEntry();
}
zipOut.finish(); zipOut.finish();
} }
// 保存二维码信息
shopTableCodeService.saveBatch(codeList);
} }