
package com.tmerclub.cloud.order.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.order.constant.PlatformInterventionStatus;
import com.tmerclub.cloud.order.dto.OrderRefundInterventionDTO;
import com.tmerclub.cloud.order.service.OrderRefundService;
import com.tmerclub.cloud.order.vo.OrderRefundVO;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author tmerclub
 * @date 2021/1/7
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.REFUND_INTERVENTION_CANAL_TOPIC, consumerGroup = RocketMqConstant.REFUND_INTERVENTION_CANAL_TOPIC)
public class RefundIntervertionCanalConsumer implements RocketMQListener<List<Long>> {

    @Autowired
    private OrderRefundService orderRefundService;

    private static final Logger LOG = LoggerFactory.getLogger(RefundIntervertionCanalConsumer.class);

    @Override
    public void onMessage(List<Long> refundIds) {
        LOG.info("退款平台介入超时回调开始... message: {}", Json.toJsonString(refundIds));
        if (CollectionUtil.isEmpty(refundIds)) {
            return;
        }
        OrderRefundInterventionDTO orderRefundParam = new OrderRefundInterventionDTO();
        orderRefundParam.setPlatformMessage("平台介入超时,系统自动拒绝介入");
        for (Long refundId : refundIds) {
            OrderRefundVO orderRefundVO = orderRefundService.getByRefundId(refundId);
            orderRefundVO.setPlatformInterventionStatus(PlatformInterventionStatus.REJECT.value());
            orderRefundService.disagreeIntervention(orderRefundParam, orderRefundVO);
        }
    }
}
