package com.bonus.canteen.core.order.mq;

import com.bonus.canteen.core.common.utils.MqPayload;
import com.bonus.canteen.core.order.mq.bo.OrderCancelBO;
import com.bonus.canteen.core.order.service.IOrderInfoService;
import com.bonus.common.houqin.mq.MQListener;
import com.bonus.common.houqin.mq.MQMessageListener;
import com.bonus.common.houqin.utils.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.Objects;

@MQMessageListener(
   group = "order-order-v3-async-timeout",
   topic = "order",
   tag = "order-v3-async-timeout"
)
public class OrderMqListenerTimeout implements MQListener<MqPayload<String>> {
   private static final Logger log = LoggerFactory.getLogger(OrderMqListenerTimeout.class);

   @Autowired
   @Lazy
   private IOrderInfoService orderInfoService;

   public void onMessage(MqPayload<String> payload) {
      log.info("[订单超时MQ]收到消息 {}", payload);
      OrderCancelBO payloadData = JacksonUtil.readValue(payload.getData(), OrderCancelBO.class);
      if(Objects.nonNull(payloadData)) {
         try {
            this.orderInfoService.timeOutCancel(payloadData.getOrderId(), "system");
         }catch (Exception ex) {
            log.error("[订单超时MQ]处理异常", ex);
         }
      }else {
         log.error("[订单超时MQ]收到消息为空");
      }
   }
}
