
package com.tmerclub.cloud.payment.listener;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.payment.constant.PaySysType;
import com.tmerclub.cloud.common.cache.constant.LockCacheNames;
import com.tmerclub.cloud.common.constant.PayType;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.payment.bo.RefundInfoResultBO;
import com.tmerclub.cloud.payment.constant.RefundStatus;
import com.tmerclub.cloud.payment.manager.PayManager;
import com.tmerclub.cloud.payment.model.PayInfo;
import com.tmerclub.cloud.payment.model.RefundInfo;
import com.tmerclub.cloud.payment.service.PayInfoService;
import com.tmerclub.cloud.payment.service.RefundInfoService;
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 TRACK
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_REFUND_UPDATE_REFUND_STATUS, consumerGroup = RocketMqConstant.ORDER_REFUND_UPDATE_REFUND_STATUS)
public class OrderRefundUpdateRefundStatusConsumer implements RocketMQListener<List<Long>> {

    @Autowired
    private RefundInfoService refundInfoService;
    @Autowired
    private PayInfoService payInfoService;
    @Autowired
    private PayManager payManager;
    @Autowired
    private RedissonClient redissonClient;

    private static final Logger logger = LoggerFactory.getLogger(OrderRefundUpdateRefundStatusConsumer.class);

    @Override
    public void onMessage(List<Long> refundIds) {
        if (CollUtil.isEmpty(refundIds)) {
            return;
        }
        for (Long refundId : refundIds) {
            RLock lock = redissonClient.getLock(LockCacheNames.LOCK_REFUND_HANDLE_PREFIX + refundId);
            lock.lock();
            try {
                RefundInfo refundInfo = refundInfoService.getByRefundId(refundId);
                if (Objects.isNull(refundInfo)) {
                    continue;
                }
                if (Objects.nonNull(refundInfo.getCallbackTime()) || Objects.equals(refundInfo.getRefundStatus(), RefundStatus.REFUNDED.value())) {
                    logger.error("退款单号为{}的订单已经回调过", refundId);
                    continue;
                }
                if (!Objects.equals(refundInfo.getPayType(), PayType.ALIPAY.value())) {
                    logger.error("退款单号为{}的订单不是由支付宝退款", refundId);
                    continue;
                }
                PayInfo payInfo = payInfoService.getByPayId(refundInfo.getPayId());
                RefundInfoResultBO refundInfoResultBO = payManager.getRefundInfo(refundInfo.getPayType(), refundInfo.getPayId(),payInfo.getPaySysType(), refundInfo.getRefundId());
                if (refundInfoResultBO.getRefundSuccess()) {
                    if(Objects.equals(payInfo.getPaySysType(), PaySysType.DEFAULT.value())) {
                        // 如果普通支付是预售订单时，此处会有两个,此时会有两种情况
                        // 情况1，都是微信支付此时只需要退款成功即可
                        // 情况2，其中一个是余额支付，因为这是直接成功没有回调，所以同上
                        refundInfo = refundInfoService.getByRefundIdAndWxPay(refundInfoResultBO.getRefundId());
                    } else {
                        refundInfo = refundInfoService.getByRefundId(refundInfoResultBO.getRefundId());
                    }
                    logger.info("退款单号为{}的订单开始进行订单回调", refundId);
                    refundInfoService.refundSuccess(refundInfo);
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
