From 77a6a525cdac39be521fa52a3e786481d80eece9 Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Fri, 27 Sep 2024 17:12:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=90=E5=88=B6=E5=8F=B0=E6=A1=8C=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E4=BA=BA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/cashierservice/redis/RedisCst.java | 2 + .../service/ProductService.java | 121 +++++++++--------- 2 files changed, 65 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/redis/RedisCst.java b/src/main/java/com/chaozhanggui/system/cashierservice/redis/RedisCst.java index ef1b23d..6f03ed1 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/redis/RedisCst.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/redis/RedisCst.java @@ -32,6 +32,8 @@ public class RedisCst { public static final String SONG_PAY_LOCK = "song_pay_lock:"; public static final String ORDER_PRINT_PRO = "ORDER_PRINT_PRODUCT:"; public static final String ORDER_PRINT = "ORDER_PRINT:"; + // 选择人数锁 + public static final String CHOSE_TABLE_COUNT = "CHOSE_TABLE_COUNT"; static final String CURRENT_TABLE_ORDER = "CURRENT_TABLE_ORDER:"; diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java index c8e459c..9518d0a 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java @@ -32,6 +32,7 @@ import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -102,6 +103,8 @@ public class ProductService { private MpOrderInfoMapper mpOrderInfoMapper; @Autowired private RedisUtil redisUtil; + @Autowired + private StringRedisTemplate stringRedisTemplate; public ProductService(ShopUtils shopUtils) { this.shopUtils = shopUtils; @@ -878,73 +881,75 @@ public class ProductService { } public TbCashierCart choseCount(ChoseCountDTO choseCountDTO) { - ShopEatTypeInfoDTO shopEatTypeInfoDTO = shopUtils.checkEatModel(choseCountDTO.getTableId(), choseCountDTO.getShopId()); + return Utils.runFunAndCheckKey(() -> { + ShopEatTypeInfoDTO shopEatTypeInfoDTO = shopUtils.checkEatModel(choseCountDTO.getTableId(), choseCountDTO.getShopId()); - TbShopInfo shopInfo = tbShopInfoMapper.selectByPrimaryKey(choseCountDTO.getShopId()); - if (shopInfo == null) throw new MsgException("店铺信息不存在"); + TbShopInfo shopInfo = tbShopInfoMapper.selectByPrimaryKey(choseCountDTO.getShopId()); + if (shopInfo == null) throw new MsgException("店铺信息不存在"); - if (shopInfo.getIsTableFee() != null && shopInfo.getIsTableFee() == 1) { - throw new MsgException("当前店铺无需选择餐位费"); - } + if (shopInfo.getIsTableFee() != null && shopInfo.getIsTableFee() == 1) { + throw new MsgException("当前店铺无需选择餐位费"); + } - TbShopTable shopTable = mpShopTableMapper.selectOne(new LambdaQueryWrapper() - .eq(TbShopTable::getQrcode, choseCountDTO.getTableId())); - if (shopTable == null) { - throw new MsgException("台桌不存在"); - } + TbShopTable shopTable = mpShopTableMapper.selectOne(new LambdaQueryWrapper() + .eq(TbShopTable::getQrcode, choseCountDTO.getTableId())); + if (shopTable == null) { + throw new MsgException("台桌不存在"); + } - if (shopTable.getMaxCapacity() < choseCountDTO.getNum()) { - throw new MsgException("当前台桌最大人数为: " + shopTable.getMaxCapacity()); - } + if (shopTable.getMaxCapacity() < choseCountDTO.getNum()) { + throw new MsgException("当前台桌最大人数为: " + shopTable.getMaxCapacity()); + } - Integer userId = TokenUtil.getUserId(); + Integer userId = TokenUtil.getUserId(); - TbCashierCart tbCashierCart = getSeatCartInfo(choseCountDTO.getShopId(), choseCountDTO.getTableId(), userId, shopEatTypeInfoDTO); + TbCashierCart tbCashierCart = getSeatCartInfo(choseCountDTO.getShopId(), choseCountDTO.getTableId(), userId, shopEatTypeInfoDTO); - if (tbCashierCart == null) { - tbCashierCart = new TbCashierCart(); - tbCashierCart.setStatus("create"); - tbCashierCart.setCreatedAt(System.currentTimeMillis()); - tbCashierCart.setTableId(choseCountDTO.getTableId()); - tbCashierCart.setName("客座费"); - tbCashierCart.setSalePrice(shopInfo.getTableFee()); - tbCashierCart.setShopId(String.valueOf(choseCountDTO.getShopId())); - tbCashierCart.setTradeDay(DateUtils.getDay()); - tbCashierCart.setStatus("create"); - tbCashierCart.setTotalAmount(new BigDecimal(choseCountDTO.getNum()).multiply(shopInfo.getTableFee())); - tbCashierCart.setPlaceNum(1); - tbCashierCart.setProductId("-999"); - tbCashierCart.setSkuId("-999"); - tbCashierCart.setPackFee(BigDecimal.ZERO); - tbCashierCart.setNumber(choseCountDTO.getNum()); - tbCashierCart.setTotalNumber(choseCountDTO.getNum()); - tbCashierCart.setUseType(shopEatTypeInfoDTO.getUseType()); - tbCashierCart.setPlatformType(PlatformTypeEnum.MINI_APP.getValue()); - tbCashierCart.setUserId(userId); - mpCashierCartMapper.insert(tbCashierCart); - } else { - tbCashierCart.setTotalAmount(new BigDecimal(choseCountDTO.getNum()).multiply(shopInfo.getTableFee())); - tbCashierCart.setNumber(choseCountDTO.getNum()); - tbCashierCart.setTotalNumber(choseCountDTO.getNum()); - tbCashierCart.setUseType(shopEatTypeInfoDTO.getUseType()); - tbCashierCart.setPlatformType(PlatformTypeEnum.MINI_APP.getValue()); - tbCashierCart.setUserId(userId); - mpCashierCartMapper.updateById(tbCashierCart); - } + if (tbCashierCart == null) { + tbCashierCart = new TbCashierCart(); + tbCashierCart.setStatus("create"); + tbCashierCart.setCreatedAt(System.currentTimeMillis()); + tbCashierCart.setTableId(choseCountDTO.getTableId()); + tbCashierCart.setName("客座费"); + tbCashierCart.setSalePrice(shopInfo.getTableFee()); + tbCashierCart.setShopId(String.valueOf(choseCountDTO.getShopId())); + tbCashierCart.setTradeDay(DateUtils.getDay()); + tbCashierCart.setStatus("create"); + tbCashierCart.setTotalAmount(new BigDecimal(choseCountDTO.getNum()).multiply(shopInfo.getTableFee())); + tbCashierCart.setPlaceNum(1); + tbCashierCart.setProductId("-999"); + tbCashierCart.setSkuId("-999"); + tbCashierCart.setPackFee(BigDecimal.ZERO); + tbCashierCart.setNumber(choseCountDTO.getNum()); + tbCashierCart.setTotalNumber(choseCountDTO.getNum()); + tbCashierCart.setUseType(shopEatTypeInfoDTO.getUseType()); + tbCashierCart.setPlatformType(PlatformTypeEnum.MINI_APP.getValue()); + tbCashierCart.setUserId(userId); + mpCashierCartMapper.insert(tbCashierCart); + } else { + tbCashierCart.setTotalAmount(new BigDecimal(choseCountDTO.getNum()).multiply(shopInfo.getTableFee())); + tbCashierCart.setNumber(choseCountDTO.getNum()); + tbCashierCart.setTotalNumber(choseCountDTO.getNum()); + tbCashierCart.setUseType(shopEatTypeInfoDTO.getUseType()); + tbCashierCart.setPlatformType(PlatformTypeEnum.MINI_APP.getValue()); + tbCashierCart.setUserId(userId); + mpCashierCartMapper.updateById(tbCashierCart); + } - // 将数据加入缓存 - String tableCartKey = RedisCst.getTableCartKey(String.valueOf(choseCountDTO.getShopId()), choseCountDTO.getTableId(), userId); - String message = redisUtil.getMessage(tableCartKey); - JSONArray jsonArray; - if (StrUtil.isNotBlank(message)) { - jsonArray = JSONObject.parseArray(message); - }else { - jsonArray = new JSONArray(); - } - jsonArray.add(tbCashierCart); - redisUtil.saveMessage(tableCartKey, jsonArray.toJSONString()); + // 将数据加入缓存 + String tableCartKey = RedisCst.getTableCartKey(String.valueOf(choseCountDTO.getShopId()), choseCountDTO.getTableId(), userId); + String message = redisUtil.getMessage(tableCartKey); + JSONArray jsonArray; + if (StrUtil.isNotBlank(message)) { + jsonArray = JSONObject.parseArray(message); + }else { + jsonArray = new JSONArray(); + } + jsonArray.add(tbCashierCart); + redisUtil.saveMessage(tableCartKey, jsonArray.toJSONString()); - return tbCashierCart; + return tbCashierCart; + }, stringRedisTemplate, RedisCst.getLockKey(RedisCst.CHOSE_TABLE_COUNT, choseCountDTO.getShopId(), choseCountDTO.getTableId())); } private TbCashierCart getSeatCartInfo(Object shopId, String tableId, Integer userId, ShopEatTypeInfoDTO shopEatTypeInfoDTO) {