台桌批量生成桌码接口
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.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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 台桌配置 服务层实现。
|
* 台桌配置 服务层实现。
|
||||||
|
|
@ -82,54 +84,48 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createQrCode(Long shopId, Integer num, HttpServletResponse response) throws IOException {
|
public void createQrCode(Long shopId, Integer num, HttpServletResponse response) throws IOException {
|
||||||
// 查询shop下面有多少台桌
|
// 查询 shop 下面有多少台桌
|
||||||
long count = queryChain().eq(ShopTable::getShopId, shopId).count();
|
long count = queryChain().eq(ShopTable::getShopId, shopId).count();
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
throw new ApiNotPrintException("未添加台桌,请先添加台桌");
|
throw new ApiNotPrintException("未添加台桌,请先添加台桌");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 创建临时目录存储二维码
|
// 获取可用 id
|
||||||
File tempDir = FileUtil.mkdir(FileUtil.getTmpDir() + "/shop_qr_temp");
|
List<Integer> idList = queryChain().isNull(ShopTable::getTableCode)
|
||||||
List<File> qrCodeFiles = new ArrayList<>();
|
.select("id").orderBy(ShopTable::getId, false)
|
||||||
|
.page(new Page<>(1, num)).getRecords().stream()
|
||||||
|
.map(ShopTable::getId).toList();
|
||||||
|
|
||||||
// 获取可用id
|
long maxId = idList.isEmpty() ? 1 : idList.getLast();
|
||||||
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();
|
|
||||||
|
|
||||||
// 3. 生成唯一二维码
|
// 设置 ZIP 响应头
|
||||||
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 文件
|
|
||||||
response.setContentType("application/zip");
|
response.setContentType("application/zip");
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName());
|
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip");
|
||||||
response.setContentLength((int) zipFile.length());
|
|
||||||
IoUtil.copy(FileUtil.getInputStream(zipFile), response.getOutputStream());
|
|
||||||
|
|
||||||
// 6. 清理临时文件
|
// 使用 ZipOutputStream 将二维码写入 zip
|
||||||
FileUtil.del(tempDir);
|
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
|
||||||
FileUtil.del(zipFile);
|
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