
package com.tmerclub.cloud.order.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.api.payment.feign.PaymentFeignClient;
import com.tmerclub.cloud.common.cache.constant.LockCacheNames;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.order.constant.ReturnProcessStatusEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.order.service.OrderRefundService;
import com.tmerclub.cloud.order.vo.OrderRefundVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

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

    @Autowired
    private OrderRefundService orderRefundService;
    @Autowired
    private RedissonClient redissonClient;
    @DubboReference
    private PaymentFeignClient paymentFeignClient;

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

    @Override
    public void onMessage(List<Long> refundIds) {
        LOG.info("取消退款订单回调开始... message: {}", Json.toJsonString(refundIds));
        if (CollectionUtil.isEmpty(refundIds)) {
            return;
        }
        if (CollectionUtils.isNotEmpty(refundIds)) {
            for (Long refundId : refundIds) {
                RLock lock = redissonClient.getLock(LockCacheNames.LOCK_REFUND_HANDLE_PREFIX + refundId);
                lock.lock();
                try {
                    OrderRefundVO orderRefundDb = orderRefundService.getByRefundId(refundId);
                    if (Objects.isNull(orderRefundDb)) {
                        continue;
                    }
                    if (Objects.equals(orderRefundDb.getReturnMoneySts(), ReturnProcessStatusEnum.PROCESSING.value())) {
                        // 商家处理状态，查询退款是否成功
                        ServerResponseEntity<Boolean> serverResponse = paymentFeignClient.checkRefundStatus(refundId);
                        if (!serverResponse.isSuccess()) {
                            throw new LuckException(serverResponse.getMsg());
                        }
                        if (serverResponse.getData()) {
                            LOG.info("退款订单{}已经退款完成并回调", refundId);
                            continue;
                        }
                    }
                    if (ReturnProcessStatusEnum.shouldClous(orderRefundDb.getReturnMoneySts())) {
                        LOG.info("退款订单超时关闭" + orderRefundDb.getRefundId());
                        orderRefundService.cancelWhenTimeOut(orderRefundDb);
                    }
                } finally {
                    lock.unlock();
                }
            }
        }
    }
}
