台桌批量生成桌码接口

This commit is contained in:
张松 2025-02-18 16:11:32 +08:00
parent f00b51001a
commit d323ac3ef9
9 changed files with 115 additions and 4 deletions

View File

@ -13,9 +13,12 @@ import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
/**
* 台桌管理
* @author Administrator
@ -26,6 +29,20 @@ public class ShopTableController {
@Resource
private ShopTableService shopTableService;
/**
* 批量生成台桌码
* 权限标识: shopTable:code
* @return 台桌列表
*/
@SaAdminCheckPermission("shopTable:code")
@PostMapping("/code")
public CzgResult<Page<ShopTable>> createCode(@RequestParam Integer num, HttpServletResponse response) throws IOException {
if (num > 100) {
return CzgResult.failure("单次最多可获取100个");
}
shopTableService.createQrCode(StpKit.USER.getShopId(), num, response);
return CzgResult.success();
}
/**
* 获取台桌列表

View File

@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.czg.utils.ServletUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
@ -16,6 +17,8 @@ import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import java.net.http.HttpResponse;
/**
* 方法调用统一切面处理
@ -40,7 +43,7 @@ public class ControllerAspect {
// 如果请求参数中包含HttpServletRequest剔除HttpServletRequest
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof HttpServletRequest) {
if (args[i] instanceof HttpServletRequest || args[i] instanceof HttpServletResponse) {
args[i] = null;
}else if (args[i] instanceof MultipartFile) {
args[i] = "上传图片";

View File

@ -1,8 +1,12 @@
package com.czg.account.service;
import com.czg.account.dto.table.ShopTableAddDTO;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.service.IService;
import com.czg.account.entity.ShopTable;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 台桌配置 服务层
@ -13,4 +17,6 @@ import com.czg.account.entity.ShopTable;
public interface ShopTableService extends IService<ShopTable> {
Boolean add(Long shopId, ShopTableAddDTO shopTableAddDTO);
void createQrCode(Long shopId, Integer num, HttpServletResponse response) throws IOException;
}

View File

@ -23,6 +23,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>

View File

@ -35,6 +35,7 @@
<alipay-sdk-java.version>4.40.54.ALL</alipay-sdk-java.version>
<aliyun-sms.version>2.0.24</aliyun-sms.version>
<aliyun.oss.version>2.8.3</aliyun.oss.version>
<zxing.version>3.5.3</zxing.version>
</properties>
<dependencyManagement>
@ -45,6 +46,13 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${zxing.version}</version>
</dependency>
<!-- 阿里云oss -->
<dependency>
<groupId>com.aliyun.oss</groupId>

View File

@ -1,21 +1,37 @@
package com.czg.service.account.service.impl;
import cn.dev33.satoken.context.SaHolder;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.czg.account.dto.table.ShopTableAddDTO;
import com.czg.account.entity.ShopArea;
import com.czg.account.service.ShopAreaService;
import com.czg.enums.ShopTableStatusEnum;
import com.czg.exception.ApiNotPrintException;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.account.entity.ShopTable;
import com.czg.account.service.ShopTableService;
import com.czg.service.account.mapper.ShopTableMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@ -63,4 +79,57 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
}
return true;
}
@Override
public void createQrCode(Long shopId, Integer num, HttpServletResponse response) throws IOException {
// 查询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();
long maxId = 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 文件
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);
}
}

View File

@ -37,7 +37,7 @@
where a.user_id = #{userInfoId}
</select>
<select id="selectAssetsSummary" resultType="com.czg.account.dto.user.userinfo.UserInfoAssetsSummaryDTO">
select sum(IFNULL(b.amount, 0)) amount, sum(IFNULL(b.account_points, 0)) points, sum(IFNULL(c.over_num, 0)) couponNum
select sum(IFNULL(b.amount, 0)) as amount, sum(IFNULL(b.account_points, 0)) as points, sum(IFNULL(c.over_num, 0)) as couponNum
from tb_user_info as a
left join tb_shop_user as b on a.id = b.user_id
left join tb_shop_activate_in_record as c on c.shop_id = b.shop_id

View File

@ -28,7 +28,7 @@ public class Main {
// String packageName = "product";
// String packageName = "order";
String tableName = "tb_shop_staff_permission";
String tableName = "tb_shop_area";
String author = "zs";
//是否生成DTO实体 默认生成
boolean isGenerateDto = true;

View File

@ -90,7 +90,11 @@
<!-- end -->
<!-- maven下载不到的包放这里下载完成后注释掉 -->
<!-- <dependency>-->
<!-- <groupId>com.google.zxing</groupId>-->
<!-- <artifactId>core</artifactId>-->
<!-- <version>3.5.3</version>-->
<!-- </dependency>-->
<!-- maven下载不到的包放这里下载完成后注释掉 -->
</dependencies>