桌码生成策略修改

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,57 @@
package com.czg.account.dto;
import java.io.Serializable;
import java.time.LocalDateTime;
import com.alibaba.fastjson2.annotation.JSONField;
import java.io.Serial;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 实体类
*
* @author zs
* @since 2025-02-19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ShopTableCodeDTO implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private Long id;
/**
* 店铺id
*/
private Long shopId;
/**
* 桌码
*/
private String tableCode;
/**
* 状态 待绑定0 已绑定1
*/
private Integer state;
/**
* 创建时间
*/
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
/**
* 绑定时间
*/
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime bindTime;
}

View File

@ -25,4 +25,9 @@ public class ShopInfoByCodeDTO {
* 台桌信息
*/
private ShopTable shopTable;
/**
* 是否会员
*/
private boolean isVip;
}

View File

@ -0,0 +1,64 @@
package com.czg.account.entity;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.io.Serial;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
/**
* 实体类
*
* @author zs
* @since 2025-02-19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("tb_shop_table_code")
@Accessors(chain = true)
public class ShopTableCode implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id(keyType = KeyType.Auto)
private Long id;
/**
* 店铺id
*/
private Long shopId;
/**
* 桌码
*/
private String tableCode;
/**
* 状态 待绑定0 已绑定1
*/
private Integer state;
/**
* 创建时间
*/
@Column(onInsertValue = "now()")
private LocalDateTime createTime;
/**
* 绑定时间
*/
private LocalDateTime bindTime;
}

View File

@ -0,0 +1,14 @@
package com.czg.account.service;
import com.mybatisflex.core.service.IService;
import com.czg.account.entity.ShopTableCode;
/**
* 服务层
*
* @author zs
* @since 2025-02-19
*/
public interface ShopTableCodeService extends IService<ShopTableCode> {
}

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>

View File

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