排队取号保存号码

This commit is contained in:
SongZhang 2024-09-13 15:14:03 +08:00
parent 5e8601745b
commit a73530df84
3 changed files with 46 additions and 0 deletions

View File

@ -21,6 +21,8 @@ public interface RedisConstant {
String LOCK_KEY = "LOCK:";
String CREATE_ORDER = "CREATE_ORDER";
// 排队取号全局号码
String TABLE_CALL_NUMBER = "TABLE_CALL_NUMBER:";
static String getCurrentOrderKey(String tableId, String shopId) {
return CURRENT_TABLE_ORDER + shopId + ":" + tableId;
@ -37,4 +39,8 @@ public interface RedisConstant {
}
return key.toString();
}
static String getTableCallNumKey(Integer shopId, Integer callTableId) {
return TABLE_CALL_NUMBER + shopId + ":" + callTableId;
}
}

View File

@ -70,4 +70,7 @@ public class TbCallQueue {
@Column(name = "sub_state")
private Integer subState;
@Column(name = "call_num")
private String callNum;
}

View File

@ -5,6 +5,7 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.ysk.cashier.cons.RedisConstant;
import cn.ysk.cashier.dto.calltable.*;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.entity.TbCallQueue;
@ -16,14 +17,17 @@ import cn.ysk.cashier.pojo.shop.TbShopInfo;
import cn.ysk.cashier.pojo.shop.TbShopUser;
import cn.ysk.cashier.repository.shop.TbShopInfoRepository;
import cn.ysk.cashier.service.app.TbCallService;
import cn.ysk.cashier.utils.Utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
@Service
@ -34,6 +38,7 @@ public class TbCallServiceImpl implements TbCallService {
private final TbCallQueueService callQueueService;
private final TbShopUserMapper shopUserMapper;
private final TbShopInfoRepository shopInfoRepository;
private final StringRedisTemplate redisTemplate;
@Override
public Object add(CallTableDTO addCallTableDTO) {
@ -76,6 +81,35 @@ public class TbCallServiceImpl implements TbCallService {
.eq(TbCallTable::getShopId, baseCallTableDTO.getShopId()));
}
private String getCallNumber(Integer shopId, TbCallTable callTable) {
return Utils.runFunAndCheckKey(() -> {
String callNumKey = RedisConstant.getTableCallNumKey(shopId, callTable.getId());
String value = redisTemplate.opsForValue().get(callNumKey);
AtomicReference<String> newVal = new AtomicReference<>("");
// 初始化
if (StrUtil.isBlank(value)) {
Boolean setFlag = Utils.runFunAndRetry(() -> redisTemplate.opsForValue().setIfAbsent(callNumKey, callTable.getStart().toString()), flag -> !flag,
r -> newVal.set(redisTemplate.opsForValue().get(callNumKey)));
if (setFlag) {
return callTable.getPrefix() + callTable.getStart();
}else if (StrUtil.isNotBlank(newVal.get())){
value = String.valueOf((Integer.parseInt(newVal.get()) + 1));
redisTemplate.opsForValue().set(callNumKey, value);
return callTable.getPrefix() + value;
}else {
throw new BadRequestException("生成排队号码失败");
}
}else {
value = String.valueOf((Integer.parseInt(value) + 1));
redisTemplate.opsForValue().set(callNumKey, value);
return callTable.getPrefix() + value;
}
}, redisTemplate, RedisConstant.getLockKey("UPDATE_TABLE", shopId, callTable.getId()));
}
@Override
public Object takeNumber(TakeNumberDTO takeNumberDTO) {
TbShopInfo shopInfo = shopInfoRepository.findById(takeNumberDTO.getShopId()).orElse(null);
@ -90,6 +124,8 @@ public class TbCallServiceImpl implements TbCallService {
throw new BadRequestException("桌型不存在");
}
// 查询当前
// 拿取系统内部用户信息
TbCallQueue callQueue;
if (takeNumberDTO.getUserId() != null) {
@ -133,6 +169,7 @@ public class TbCallServiceImpl implements TbCallService {
callQueue.setSubState(1);
}
callQueue.setCallNum(getCallNumber(takeNumberDTO.getShopId(), callTable));
callQueue.setCreateTime(DateUtil.date().toInstant());
callQueue.setShopId(shopInfo.getId());
callQueue.setShopName(shopInfo.getShopName());