package com.czg.mq;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.czg.config.RabbitConstants;
import com.czg.order.entity.MqLog;
import com.czg.order.service.MqLogService;
import com.czg.order.service.OrderInfoCustomService;
import com.czg.order.service.OrderInfoRpcService;
import com.czg.service.RedisService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 订单mq消息处理器
 *
 * @author Administrator
 */
@Component
@Slf4j
public class OrderMqListener {

    @Resource
    private OrderInfoRpcService orderInfoRpcService;
    @Resource
    private MqLogService mqLogService;
    @Resource
    private OrderInfoCustomService orderInfoCustomService;
    @Resource
    private RedisService redisService;

    /**
     * 订单上菜
     */
    @RabbitListener(queues = {"${spring.profiles.active}-" + RabbitConstants.Queue.ORDER_PRODUCT_STATUS_QUEUE})
    public void orderDetailUp(String info) {
        if (!info.contains("UP_ORDER_DETAIL:")) {
            return;
        }
        info = info.replace("UP_ORDER_DETAIL:", "");
        log.info("接收到修改菜品状态mq, info: {}", info);
        String finalInfo = info;
        redisService.debounce("UP_ORDER_DETAIL:" + info, 5, () -> {
            orderInfoCustomService.updateOrderDetailStatus(Long.valueOf(finalInfo));

        });
    }

    @RabbitListener(queues = {"${spring.profiles.active}-" + RabbitConstants.Queue.ORDER_STOCK_QUEUE})
    public void orderStockSubtract(String orderId) {
        long startTime = DateUtil.date().getTime();
        log.info("接收到订单支付成功扣减库存消息：{}", orderId);
        MqLog mqLog = new MqLog().setQueue(RabbitConstants.Queue.ORDER_STOCK_QUEUE).setMsg(orderId).setType("orderStockSubtract").setPlat("java.order").setCreateTime(DateUtil.date().toLocalDateTime());
        try {
            orderInfoRpcService.paySuccessCallback(Long.valueOf(orderId));
        } catch (Exception e) {
            log.error("订单支付成功扣减库存失败", e);
            String errorInfo = ExceptionUtil.stacktraceToString(e);
            mqLog.setErrInfo(errorInfo);
            mqLog.setDuration(DateUtil.date().getTime() - startTime);
            mqLog.setFailTime(DateUtil.date().toLocalDateTime());
            mqLogService.save(mqLog);
        }
    }

    @RabbitListener(queues = {"${spring.profiles.active}-" + RabbitConstants.Queue.ORDER_CANCEL_QUEUE})
    public void orderStockRecover(String orderId) {
        long startTime = DateUtil.date().getTime();
        log.info("接收到订单取消恢复库存消息：{}", orderId);
        try {
            orderInfoRpcService.orderCancelCallback(Long.valueOf(orderId));
        } catch (Exception e) {
            MqLog mqLog = new MqLog().setQueue(RabbitConstants.Queue.ORDER_CANCEL_QUEUE).setMsg(orderId).setType("orderStockRecover").setPlat("java.order").setCreateTime(DateUtil.date().toLocalDateTime());
            log.error("订单取消恢复库存失败", e);
            String errorInfo = ExceptionUtil.stacktraceToString(e);
            mqLog.setErrInfo(errorInfo);
            mqLog.setDuration(DateUtil.date().getTime() - startTime);
            mqLog.setFailTime(DateUtil.date().toLocalDateTime());
            mqLogService.save(mqLog);
        }
    }

    @RabbitListener(queues = {"${spring.profiles.active}-" + RabbitConstants.Queue.ORDER_REFUND_QUEUE})
    public void orderStockReturn(String jsonObjStr) {
        long startTime = DateUtil.date().getTime();
        log.info("接收到订单退款返还库存消息：{}", jsonObjStr);
        try {
            orderInfoRpcService.orderRefundCallback(JSONObject.parseObject(jsonObjStr));
        } catch (Exception e) {
            MqLog mqLog = new MqLog().setQueue(RabbitConstants.Queue.ORDER_REFUND_QUEUE).setMsg(jsonObjStr).setType("orderStockReturn").setPlat("java.order").setCreateTime(DateUtil.date().toLocalDateTime());
            log.error("订单退款返还库存失败", e);
            String errorInfo = ExceptionUtil.stacktraceToString(e);
            mqLog.setErrInfo(errorInfo);
            mqLog.setDuration(DateUtil.date().getTime() - startTime);
            mqLog.setFailTime(DateUtil.date().toLocalDateTime());
            mqLogService.save(mqLog);
        }
    }





}
