扫码上菜实现

This commit is contained in:
张松
2025-11-25 15:32:54 +08:00
parent c5597a39dc
commit 973019dd94
6 changed files with 94 additions and 0 deletions

View File

@@ -8,6 +8,8 @@ import com.czg.config.RabbitConstants;
import com.czg.order.entity.MqLog;
import com.czg.order.service.MqLogService;
import com.czg.order.service.OrderInfoRpcService;
import com.czg.order.service.OrderInfoService;
import com.czg.service.order.utils.FunUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
@@ -26,6 +28,29 @@ public class OrderMqListener {
private OrderInfoRpcService orderInfoRpcService;
@Resource
private MqLogService mqLogService;
@Resource
private OrderInfoService orderInfoService;
@Resource
private FunUtil funUtil;
/**
* 订单上菜
*/
@RabbitListener(queues = {"${spring.profiles.active}-" + RabbitConstants.Queue.ORDER_PRODUCT_STATUS_QUEUE})
public void orderDetailUp(String info) {
if (!info.contains("UP_ORDER_DETAIL:")) {
return;
}
log.info("接收到修改菜品状态mq, info: {}", info);
String finalInfo = info;
funUtil.debounce("UP_ORDER_DETAIL:" + info, 5, () -> {
orderInfoService.updateOrderDetailStatus(Long.valueOf(finalInfo));
});
info = info.replace("UP_ORDER_DETAIL:", "");
System.out.println(info);
}
@RabbitListener(queues = {"${spring.profiles.active}-" + RabbitConstants.Queue.ORDER_STOCK_QUEUE})
public void orderStockSubtract(String orderId) {
@@ -80,4 +105,7 @@ public class OrderMqListener {
}
}

View File

@@ -131,6 +131,7 @@ public class OrderDetail implements Serializable {
* 状态: in-production 制作中;wait_out 待取餐;refunding 退款中; part_refund 部分退单; refund-退单; done 完成;
*/
private String status;
private String subStatus;
/**
* 当前下单次数

View File

@@ -68,4 +68,7 @@ public interface OrderInfoService extends IService<OrderInfo> {
Boolean updatePayOrderId(Long orderId, Long paymentId, String payType, String remark);
void updateOrderDetailStatus(Long orderDetailId);
}

View File

@@ -6,6 +6,23 @@ import lombok.Getter;
* @author Administrator
*/
public interface TableValueConstant {
interface OrderDetail {
@Getter
enum SubStatus {
PENDING_PREP("PENDING_PREP", "待起菜"),
READY_TO_SERVE("READY_TO_SERVE", "待出菜"),
SENT_OUT("SENT_OUT", "已出菜"),
DELIVERED("DELIVERED", "已上菜"),
EXPIRED("EXPIRED", "已超时");
private final String code;
private final String msg;
SubStatus(String code, String msg) {
this.code = code;
this.msg = msg;
}
}
}
interface MemberExpFlow {
@Getter
enum Type {

View File

@@ -1722,4 +1722,27 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
mapper.updatePayOrderId(orderId, paymentId, payType, remark);
return true;
}
@Override
public void updateOrderDetailStatus(Long orderDetailId) {
OrderDetail orderDetail = orderDetailService.getById(orderDetailId);
if (orderDetail == null) {
log.warn("订单详情不存在");
}
if (!orderDetail.getSubStatus().equals(TableValueConstant.OrderDetail.SubStatus.READY_TO_SERVE.getCode())
|| !orderDetail.getSubStatus().equals(TableValueConstant.OrderDetail.SubStatus.SENT_OUT.getCode())) {
log.warn("订单详情状态不正确");
return;
}
if (TableValueConstant.OrderDetail.SubStatus.READY_TO_SERVE.getCode().equals(orderDetail.getSubStatus())) {
orderDetail.setSubStatus(TableValueConstant.OrderDetail.SubStatus.SENT_OUT.getCode());
}
if (TableValueConstant.OrderDetail.SubStatus.SENT_OUT.getCode().equals(orderDetail.getSubStatus())) {
orderDetail.setSubStatus(TableValueConstant.OrderDetail.SubStatus.DELIVERED.getCode());
}
orderDetailService.updateById(orderDetail);
}
}

View File

@@ -99,5 +99,27 @@ public class FunUtil {
return result;
}
/**
* 防抖函数:在指定秒数内相同 Key 的任务只会执行一次
* @param key 防抖使用的 Redis Key
* @param seconds 防抖时间(秒)
* @param task 要执行的业务逻辑
* @return true 执行了任务false 在防抖期内被拦截
*/
public boolean debounce(String key, long seconds, Runnable task) {
try {
Boolean success = redisTemplate.opsForValue().setIfAbsent(
key, "1", seconds, TimeUnit.SECONDS
);
if (Boolean.TRUE.equals(success)) {
task.run();
return true;
}
return false;
} catch (Exception e) {
log.error("防抖函数执行失败 key={} err={}", key, e.getMessage(), e);
return false;
}
}
}