台桌批量生成桌码接口

This commit is contained in:
张松 2025-02-18 16:18:11 +08:00
parent d323ac3ef9
commit d4035a4d0f
2 changed files with 38 additions and 42 deletions

View File

@ -7,6 +7,7 @@ import com.czg.account.dto.table.ShopTableDTO;
import com.czg.account.entity.ShopTable; import com.czg.account.entity.ShopTable;
import com.czg.account.service.ShopTableService; import com.czg.account.service.ShopTableService;
import com.czg.annotation.SaAdminCheckPermission; import com.czg.annotation.SaAdminCheckPermission;
import com.czg.exception.ApiNotPrintException;
import com.czg.resp.CzgResult; import com.czg.resp.CzgResult;
import com.czg.sa.StpKit; import com.czg.sa.StpKit;
import com.czg.utils.PageUtil; import com.czg.utils.PageUtil;
@ -36,12 +37,11 @@ public class ShopTableController {
*/ */
@SaAdminCheckPermission("shopTable:code") @SaAdminCheckPermission("shopTable:code")
@PostMapping("/code") @PostMapping("/code")
public CzgResult<Page<ShopTable>> createCode(@RequestParam Integer num, HttpServletResponse response) throws IOException { public void createCode(@RequestParam Integer num, HttpServletResponse response) throws IOException {
if (num > 100) { if (num > 100) {
return CzgResult.failure("单次最多可获取100个"); throw new ApiNotPrintException("单次最多可获取100个");
} }
shopTableService.createQrCode(StpKit.USER.getShopId(), num, response); shopTableService.createQrCode(StpKit.USER.getShopId(), num, response);
return CzgResult.success();
} }
/** /**

View File

@ -34,6 +34,8 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/** /**
* 台桌配置 服务层实现 * 台桌配置 服务层实现
@ -88,15 +90,20 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
throw new ApiNotPrintException("未添加台桌,请先添加台桌"); throw new ApiNotPrintException("未添加台桌,请先添加台桌");
} }
// 2. 创建临时目录存储二维码
File tempDir = FileUtil.mkdir(FileUtil.getTmpDir() + "/shop_qr_temp");
List<File> qrCodeFiles = new ArrayList<>();
// 获取可用 id // 获取可用 id
List<Integer> idList = queryChain().isNull(ShopTable::getTableCode).select("id").orderBy(ShopTable::getId, false).page(new Page<>(1, num)).getRecords().stream().map(ShopTable::getId).toList(); List<Integer> idList = queryChain().isNull(ShopTable::getTableCode)
long maxId = idList.getLast(); .select("id").orderBy(ShopTable::getId, false)
.page(new Page<>(1, num)).getRecords().stream()
.map(ShopTable::getId).toList();
// 3. 生成唯一二维码 long maxId = idList.isEmpty() ? 1 : idList.getLast();
// 设置 ZIP 响应头
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip");
// 使用 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 = shopId.toString(); String tableCode = shopId.toString();
if (i > idList.size() - 1) { if (i > idList.size() - 1) {
@ -104,32 +111,21 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
} else { } else {
tableCode = tableCode + idList.get(i); tableCode = tableCode + idList.get(i);
} }
tableCode = tableCode + RandomUtil.randomNumbers(8); tableCode = tableCode + RandomUtil.randomNumbers(8);
// 生成二维码 // 生成二维码
BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300); BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300);
// 转换为字节数组
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, out); ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, out);
byte[] qrBytes = out.toByteArray();
// 保存二维码到文件 // 添加到 ZIP
File qrFile = new File(tempDir, tableCode + ".png"); zipOut.putNextEntry(new ZipEntry(tableCode + ".png"));
FileUtil.writeBytes(out.toByteArray(), qrFile); zipOut.write(qrBytes);
qrCodeFiles.add(qrFile); zipOut.closeEntry();
} }
}
// 4. 生成 ZIP 文件
File zipFile = new File(FileUtil.getTmpDir(), "shop_qrcodes_" + shopId + ".zip");
ZipUtil.zip(zipFile, false, qrCodeFiles.toArray(new File[0]));
// 5. 返回 ZIP 文件
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName());
response.setContentLength((int) zipFile.length());
IoUtil.copy(FileUtil.getInputStream(zipFile), response.getOutputStream());
// 6. 清理临时文件
FileUtil.del(tempDir);
FileUtil.del(zipFile);
} }
} }