桌码生成策略修改

This commit is contained in:
张松
2025-02-19 09:50:26 +08:00
parent a3e1e45a4e
commit c51120b64c
10 changed files with 216 additions and 16 deletions

View File

@@ -0,0 +1,14 @@
package com.czg.service.account.mapper;
import com.mybatisflex.core.BaseMapper;
import com.czg.account.entity.ShopTableCode;
/**
* 映射层。
*
* @author zs
* @since 2025-02-19
*/
public interface ShopTableCodeMapper extends BaseMapper<ShopTableCode> {
}

View File

@@ -42,6 +42,8 @@ public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> i
private MerchantRegisterService merchantRegisterService;
@Resource
private ShopTableService shopTableService;
@Resource
private ShopUserService shopUserService;
private ShopInfo getShopInfo(Long shopId) {
ShopInfo shopInfo = getById(shopId);
@@ -152,6 +154,8 @@ public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> i
// 计算距离,单位:米
distance = GeoUtil.getDistance(Long.parseLong(shopInfo.getLat()), Long.parseLong(shopInfo.getLng()), Long.parseLong(lat), Long.parseLong(lng));
}
return new ShopInfoByCodeDTO(distance, shopInfo, shopTable);
ShopUser shopUser = shopUserService.queryChain().eq(ShopUser::getShopId, shopInfo.getId()).eq(ShopUser::getUserId, StpKit.USER.getLoginIdAsLong()).one();
return new ShopInfoByCodeDTO(distance, shopInfo, shopTable, shopUser != null && shopUser.getIsVip() != null && shopUser.getIsVip() == 1);
}
}

View File

@@ -0,0 +1,18 @@
package com.czg.service.account.service.impl;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.account.entity.ShopTableCode;
import com.czg.account.service.ShopTableCodeService;
import com.czg.service.account.mapper.ShopTableCodeMapper;
import org.springframework.stereotype.Service;
/**
* 服务层实现。
*
* @author zs
* @since 2025-02-19
*/
@Service
public class ShopTableCodeServiceImpl extends ServiceImpl<ShopTableCodeMapper, ShopTableCode> implements ShopTableCodeService{
}

View File

@@ -6,7 +6,9 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.czg.account.dto.table.ShopTableAddDTO;
import com.czg.account.entity.ShopTableArea;
import com.czg.account.entity.ShopTableCode;
import com.czg.account.service.ShopAreaService;
import com.czg.account.service.ShopTableCodeService;
import com.czg.enums.ShopTableStatusEnum;
import com.czg.exception.ApiNotPrintException;
import com.mybatisflex.core.paginate.Page;
@@ -24,6 +26,8 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -37,6 +41,8 @@ import java.util.zip.ZipOutputStream;
public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable> implements ShopTableService{
@Resource
private ShopAreaService shopAreaService;
@Resource
private ShopTableCodeService shopTableCodeService;
@Override
public Boolean add(Long shopId, ShopTableAddDTO shopTableAddDTO) {
@@ -81,27 +87,24 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
}
// 获取可用 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<ShopTableCode> records = shopTableCodeService.queryChain().orderBy(ShopTableCode::getId, false).page(new Page<>(1, 1)).getRecords();
Map<String, Long> codeMap = shopTableCodeService.queryChain().eq(ShopTableCode::getShopId, shopId).select(ShopTableCode::getTableCode, ShopTableCode::getId)
.list().stream().collect(Collectors.toMap(ShopTableCode::getTableCode, ShopTableCode::getId));
long maxId = idList.isEmpty() ? 1 : idList.getLast();
long maxId = records.isEmpty() ? 0 : records.getFirst().getId();
// 设置 ZIP 响应头
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=shop_qrcodes_" + shopId + ".zip");
response.setHeader("Content-Disposition", STR."attachment; filename=shop_qrcodes_\{shopId}.zip");
ArrayList<ShopTableCode> codeList = new ArrayList<>();
// 使用 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);
String tableCode = generateCode(1, shopId, ++maxId, codeMap);
codeMap.put(tableCode, maxId);
codeList.add(new ShopTableCode().setShopId(shopId).setTableCode(tableCode));
// 生成二维码
BufferedImage qrImage = QrCodeUtil.generate(tableCode, 300, 300);
@@ -112,10 +115,24 @@ public class ShopTableServiceImpl extends ServiceImpl<ShopTableMapper, ShopTable
byte[] qrBytes = out.toByteArray();
// 添加到 ZIP
zipOut.putNextEntry(new ZipEntry(tableCode + ".png"));
zipOut.putNextEntry(new ZipEntry(STR."\{tableCode}.png"));
zipOut.write(qrBytes);
zipOut.closeEntry();
}
}
shopTableCodeService.saveBatch(codeList);
}
private String generateCode(Integer count, Long shopId, Long id, Map<String, Long> map) {
if (count > 100) {
throw new ApiNotPrintException("桌码生成失败");
}
String tableCode = STR."40\{shopId % 10}\{id % 10}\{RandomUtil.randomNumbers(7)}";
if (map.containsKey(tableCode)) {
generateCode(++count, shopId, id + 1, map);
}
return tableCode;
}
}

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.czg.service.account.mapper.ShopTableCodeMapper">
</mapper>