fix: 小程序点单增加取餐码
This commit is contained in:
@@ -50,6 +50,9 @@ public class RedisCst {
|
||||
public static final String DINE_IN_TABLE_CART = "DINE_IN_TABLE_CART:";
|
||||
// 当前台桌人数
|
||||
public static final String CURRENT_TABLE_SEAR_COUNT = "CURRENT_TABLE_SEAR_COUNT:";
|
||||
// 取餐码
|
||||
public static final String MASTER_ID = "SHOP:CODE:USER:pc:";
|
||||
|
||||
|
||||
|
||||
public static String getCurrentOrderKey(String tableId, String shopId) {
|
||||
@@ -87,4 +90,8 @@ public class RedisCst {
|
||||
public static String getCurrentTableSeatCount(Object shopId, String tableId) {
|
||||
return CURRENT_TABLE_SEAR_COUNT + (shopId + ":" + tableId);
|
||||
}
|
||||
|
||||
public static String getMasterIdKey(String shopId, String day, String tableId) {
|
||||
return MASTER_ID + shopId + ":" + day + ":" + tableId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -983,9 +984,19 @@ public class CartService {
|
||||
return tbActivateOutRecord;
|
||||
|
||||
}
|
||||
public synchronized void addGlobalCode(String day, String clientType, String shopId) {
|
||||
String code = stringRedisTemplate.opsForValue().get("SHOP:CODE:" + clientType + ":" + shopId + ":" + day);
|
||||
if (StrUtil.isBlank(code)) {
|
||||
stringRedisTemplate.opsForValue().set("SHOP:CODE:" + clientType + ":" + shopId + ":" + day, "1");
|
||||
} else {
|
||||
stringRedisTemplate.opsForValue().get("SHOP:CODE:" + clientType + ":" + shopId + ":" + (Integer.parseInt(code) + 1));
|
||||
}
|
||||
stringRedisTemplate.opsForValue().setIfAbsent("SHOP:CODE:SET" + clientType + ":" + shopId + ":" + day, "1");
|
||||
}
|
||||
|
||||
|
||||
private TbOrderInfo createOrderInfoWithCoupon(CreateOrderDTO createOrderDTO, OrderPriceDTO priceDTO, ShopEatTypeInfoDTO eatTypeInfoDTO,
|
||||
TbOrderInfo orderInfo, TbCashierCart seatCart, TbShopUser shopUser, TbShopTable shopTable, OrderCouponInfoDTO couponInfoDTO) {
|
||||
TbOrderInfo orderInfo, TbCashierCart seatCart, TbShopUser shopUser, TbShopTable shopTable, OrderCouponInfoDTO couponInfoDTO, String masterId) {
|
||||
Integer placeNum = getCurrentPlaceNum(eatTypeInfoDTO.getTableId(), eatTypeInfoDTO.getShopInfo().getId(), eatTypeInfoDTO);
|
||||
// 修改订单信息
|
||||
if (orderInfo == null) {
|
||||
@@ -1005,8 +1016,16 @@ public class CartService {
|
||||
}
|
||||
orderInfo.setMerchantId(merchantAccount.getId().toString());
|
||||
orderInfo.setIsPostpaid(eatTypeInfoDTO.isDineInAfter() ? 1 : 0);
|
||||
if (eatTypeInfoDTO.isIncrMaterId()) {
|
||||
String key = RedisCst.getMasterIdKey(String.valueOf(createOrderDTO.getShopId()), DateUtils.getDay(), orderInfo.getTableId());
|
||||
stringRedisTemplate.delete(key);
|
||||
}else {
|
||||
addGlobalCode(DateUtils.getDay(), "pc", String.valueOf(createOrderDTO.getShopId()));
|
||||
}
|
||||
}
|
||||
|
||||
// 更新取餐号
|
||||
orderInfo.setMasterId(masterId);
|
||||
orderInfo.setOutNumber(getOutNumber(createOrderDTO.getShopId().toString()).toString());
|
||||
orderInfo.setUpdatedAt(System.currentTimeMillis());
|
||||
orderInfo.setSettlementAmount(priceDTO.getTotalAmount());
|
||||
@@ -1038,9 +1057,8 @@ public class CartService {
|
||||
}
|
||||
|
||||
private OrderPriceDTO createOrderDetailWithCoupon(List<TbCashierCart> fullCashierCarts, Integer orderId,
|
||||
Integer shopId, ShopEatTypeInfoDTO shopEatTypeInfoDTO, boolean onlyChangeCoupon) {
|
||||
Integer shopId, ShopEatTypeInfoDTO shopEatTypeInfoDTO, boolean onlyChangeCoupon, String masterId) {
|
||||
OrderPriceDTO priceDTO = new OrderPriceDTO();
|
||||
|
||||
List<Integer> cartIds = fullCashierCarts.stream().map(TbCashierCart::getId).collect(Collectors.toList());
|
||||
// 查询历史orderDetail
|
||||
List<TbOrderDetail> oldOrderDetailList = orderDetailService
|
||||
@@ -1056,6 +1074,9 @@ public class CartService {
|
||||
});
|
||||
|
||||
for (TbCashierCart cashierCart : fullCashierCarts) {
|
||||
// if (StrUtil.isNotBlank(cashierCart.getMasterId())) {
|
||||
cashierCart.setMasterId(masterId);
|
||||
// }
|
||||
// 设置打包费
|
||||
if (shopEatTypeInfoDTO.isTakeout() && !TableConstant.CashierCart.ID.equals(cashierCart.getProductId())) {
|
||||
cashierCart.setTableId("");
|
||||
@@ -1248,6 +1269,7 @@ public class CartService {
|
||||
// 更新购物车信息
|
||||
if (!resetCouponList.isEmpty()) {
|
||||
// 取消之前使用的历史券
|
||||
|
||||
ArrayList<Integer> resetCartIds = new ArrayList<>();
|
||||
resetCouponList.forEach(item -> {
|
||||
if (item.getUserCouponId() == null) {
|
||||
@@ -1344,6 +1366,42 @@ public class CartService {
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getMasterId(String shopId, String tableId ) {
|
||||
String day = DateUtils.getDay();
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
// 当前台桌码
|
||||
String key = RedisCst.getMasterIdKey(shopId, day, tableId);
|
||||
String userCode = stringRedisTemplate.opsForValue().get(key);
|
||||
|
||||
if (StringUtils.isEmpty(userCode) || "null".equals(userCode) || "#null".equals(userCode)) {
|
||||
String code = "#" + generateOrderCode(day, "pc", String.valueOf(shopId));
|
||||
stringRedisTemplate.opsForValue().set(key, code);
|
||||
return code;
|
||||
} else {
|
||||
return userCode;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized String generateOrderCode(String day, String clientType, String shopId) {
|
||||
String code = stringRedisTemplate.opsForValue().get("SHOP:CODE:" + clientType + ":" + shopId + ":" + day);
|
||||
// 使用顺序递增的计数器生成取餐码
|
||||
String orderCode = "";
|
||||
if (StringUtils.isEmpty(code) || "null".equals(code)) {
|
||||
orderCode = "1";
|
||||
stringRedisTemplate.opsForValue().set("SHOP:CODE:" + clientType + ":" + shopId + ":" + day, "1");
|
||||
} else {
|
||||
orderCode = String.valueOf(Integer.parseInt(code.replace("#", "")) + 1);
|
||||
}
|
||||
stringRedisTemplate.opsForValue().set("SHOP:CODE:" + clientType + ":" + shopId + ":" + day, orderCode);
|
||||
boolean flag = Boolean.TRUE.equals(stringRedisTemplate.opsForValue().setIfAbsent("SHOP:CODE:SET" + clientType + ":" + shopId + ":" + day, orderCode));
|
||||
if (flag) {
|
||||
return generateOrderCode(day, clientType, shopId);
|
||||
}
|
||||
// 增加计数器
|
||||
|
||||
return orderCode;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Result createOrder(CreateOrderDTO orderDTO) {
|
||||
try {
|
||||
@@ -1413,12 +1471,14 @@ public class CartService {
|
||||
}
|
||||
|
||||
// 创建订单详情数据
|
||||
OrderPriceDTO detailPriceDTO = createOrderDetailWithCoupon(cartInfoDTO.getCashierCarts(), cartInfoDTO.getOrderId(), orderDTO.getShopId(), shopEatTypeInfoDTO, orderDTO.isOnlyChangeCoupon());
|
||||
String masterId = getMasterId(shopId, shopEatTypeInfoDTO.getTableId());
|
||||
OrderPriceDTO detailPriceDTO = createOrderDetailWithCoupon(cartInfoDTO.getCashierCarts(), cartInfoDTO.getOrderId(),
|
||||
orderDTO.getShopId(), shopEatTypeInfoDTO, orderDTO.isOnlyChangeCoupon(), masterId);
|
||||
detailPriceDTO.setProductDiscountAmount(discountAmount);
|
||||
|
||||
// 是否是第一次创建订单
|
||||
orderInfo = createOrderInfoWithCoupon(orderDTO, detailPriceDTO, shopEatTypeInfoDTO,
|
||||
orderInfo, cartInfoDTO.getSeatCart(), tbShopUser, shopTable, couponInfo);
|
||||
orderInfo, cartInfoDTO.getSeatCart(), tbShopUser, shopTable, couponInfo, masterId);
|
||||
|
||||
if (orderInfo.getId() == null) {
|
||||
mpOrderInfoMapper.insert(orderInfo);
|
||||
@@ -1460,6 +1520,7 @@ public class CartService {
|
||||
mpOrderInfoMapper.updateById(orderInfo);
|
||||
|
||||
if (!orderDTO.isOnlyChangeCoupon()) {
|
||||
|
||||
String tableCartKey = RedisCst.getTableCartKey(shopId, shopEatTypeInfoDTO.isOpenDineIn() ? tableId : null, Integer.valueOf(userId));
|
||||
redisUtil.deleteByKey(tableCartKey);
|
||||
}
|
||||
|
||||
@@ -684,6 +684,12 @@ public class PayService {
|
||||
|
||||
// 重置台桌状态
|
||||
if (StrUtil.isNotBlank(orderInfo.getTableId())) {
|
||||
// 增加台桌取餐码
|
||||
if (TableConstant.OrderInfo.UseType.DINE_IN_AFTER.equalsVals(orderInfo.getUseType())) {
|
||||
String key = RedisCst.getMasterIdKey(String.valueOf(orderInfo.getShopId()), DateUtils.getDay(), orderInfo.getTableId());
|
||||
stringRedisTemplate.delete(key);
|
||||
}
|
||||
|
||||
TbShopTable shopTable = mpShopTableService.selectByQrcode(orderInfo.getTableId());
|
||||
if (shopTable.getAutoClear() != null && shopTable.getAutoClear() == 1) {
|
||||
mpShopTableService.updateStateByQrcode(orderInfo.getTableId(), TableConstant.ShopTable.State.IDLE);
|
||||
@@ -1330,6 +1336,12 @@ public class PayService {
|
||||
|
||||
// 重置台桌状态
|
||||
if (StrUtil.isNotBlank(orderInfo.getTableId())) {
|
||||
// 增加台桌取餐码
|
||||
if (TableConstant.OrderInfo.UseType.DINE_IN_AFTER.equalsVals(orderInfo.getUseType())) {
|
||||
String key = RedisCst.getMasterIdKey(String.valueOf(orderInfo.getShopId()), DateUtils.getDay(), orderInfo.getTableId());
|
||||
stringRedisTemplate.delete(key);
|
||||
}
|
||||
|
||||
TbShopTable shopTable = mpShopTableService.selectByQrcode(orderInfo.getTableId());
|
||||
if (shopTable.getAutoClear() != null && shopTable.getAutoClear() == 1) {
|
||||
mpShopTableService.updateStateByQrcode(orderInfo.getTableId(), TableConstant.ShopTable.State.IDLE);
|
||||
|
||||
Reference in New Issue
Block a user