台桌批量生成桌码接口
This commit is contained in:
parent
d323ac3ef9
commit
d4035a4d0f
|
|
@ -7,6 +7,7 @@ import com.czg.account.dto.table.ShopTableDTO;
|
|||
import com.czg.account.entity.ShopTable;
|
||||
import com.czg.account.service.ShopTableService;
|
||||
import com.czg.annotation.SaAdminCheckPermission;
|
||||
import com.czg.exception.ApiNotPrintException;
|
||||
import com.czg.resp.CzgResult;
|
||||
import com.czg.sa.StpKit;
|
||||
import com.czg.utils.PageUtil;
|
||||
|
|
@ -36,12 +37,11 @@ public class ShopTableController {
|
|||
*/
|
||||
@SaAdminCheckPermission("shopTable: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) {
|
||||
return CzgResult.failure("单次最多可获取100个");
|
||||
throw new ApiNotPrintException("单次最多可获取100个");
|
||||
}
|
||||
shopTableService.createQrCode(StpKit.USER.getShopId(), num, response);
|
||||
return CzgResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -34,6 +34,8 @@ import java.util.ArrayList;
|
|||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* 台桌配置 服务层实现。
|
||||
|
|
@ -82,54 +84,48 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
|
|||
|
||||
@Override
|
||||
public void createQrCode(Long shopId, Integer num, HttpServletResponse response) throws IOException {
|
||||
// 查询shop下面有多少台桌
|
||||
// 查询 shop 下面有多少台桌
|
||||
long count = queryChain().eq(ShopTable::getShopId, shopId).count();
|
||||
if (count == 0) {
|
||||
throw new ApiNotPrintException("未添加台桌,请先添加台桌");
|
||||
}
|
||||
|
||||
// 2. 创建临时目录存储二维码
|
||||
File tempDir = FileUtil.mkdir(FileUtil.getTmpDir() + "/shop_qr_temp");
|
||||
List<File> qrCodeFiles = new ArrayList<>();
|
||||
// 获取可用 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();
|
||||
|
||||
// 获取可用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();
|
||||
long maxId = idList.getLast();
|
||||
long maxId = idList.isEmpty() ? 1 : idList.getLast();
|
||||
|
||||
// 3. 生成唯一二维码
|
||||
for (int i = 0; i < num; i++) {
|
||||
String tableCode = shopId.toString();
|
||||
if (i > idList.size() - 1) {
|
||||
tableCode = tableCode + ++maxId;
|
||||
}else {
|
||||
tableCode = tableCode + idList.get(i);
|
||||
}
|
||||
|
||||
tableCode = tableCode + RandomUtil.randomNumbers(8);
|
||||
// 生成二维码
|
||||
BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, out);
|
||||
|
||||
// 保存二维码到文件
|
||||
File qrFile = new File(tempDir, tableCode + ".png");
|
||||
FileUtil.writeBytes(out.toByteArray(), qrFile);
|
||||
qrCodeFiles.add(qrFile);
|
||||
}
|
||||
|
||||
// 4. 生成 ZIP 文件
|
||||
File zipFile = new File(FileUtil.getTmpDir(), "shop_qrcodes_" + shopId + ".zip");
|
||||
ZipUtil.zip(zipFile, false, qrCodeFiles.toArray(new File[0]));
|
||||
|
||||
// 5. 返回 ZIP 文件
|
||||
// 设置 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());
|
||||
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip");
|
||||
|
||||
// 6. 清理临时文件
|
||||
FileUtil.del(tempDir);
|
||||
FileUtil.del(zipFile);
|
||||
// 使用 ZipOutputStream 将二维码写入 zip
|
||||
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
|
||||
for (int i = 0; i < num; i++) {
|
||||
String tableCode = shopId.toString();
|
||||
if (i > idList.size() - 1) {
|
||||
tableCode = tableCode + ++maxId;
|
||||
} else {
|
||||
tableCode = tableCode + idList.get(i);
|
||||
}
|
||||
tableCode = tableCode + RandomUtil.randomNumbers(8);
|
||||
|
||||
// 生成二维码
|
||||
BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300);
|
||||
|
||||
// 转换为字节数组
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ImgUtil.write(qrImage, ImgUtil.IMAGE_TYPE_PNG, out);
|
||||
byte[] qrBytes = out.toByteArray();
|
||||
|
||||
// 添加到 ZIP
|
||||
zipOut.putNextEntry(new ZipEntry(tableCode + ".png"));
|
||||
zipOut.write(qrBytes);
|
||||
zipOut.closeEntry();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue