
package com.tmerclub.cloud.payment.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.tmerclub.cloud.api.order.feign.OrderFeignClient;
import com.tmerclub.cloud.common.order.bo.OrderIdWithRefundIdBO;
import com.tmerclub.cloud.common.order.bo.PayRefundBO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.payment.model.PayInfo;
import com.tmerclub.cloud.payment.service.PayInfoService;
import com.tmerclub.cloud.payment.service.RefundInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
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;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.OPEN_ALLINPAY_TOPIC,consumerGroup = RocketMqConstant.OPEN_ALLINPAY_ORDER_TOPIC)
public class OpenAllinpayOrderConsumer implements RocketMQListener<Integer> {
    private static final Logger LOGGER = LoggerFactory.getLogger(OpenAllinpayOrderConsumer.class);
    @DubboReference
    private OrderFeignClient orderFeignClient;
    @Autowired
    private PayInfoService payInfoService;
    @Autowired
    private RefundInfoService refundInfoService;

    /**
     * 开启通联，将待采购订单和支付定金未支付尾款的订单变成已取消状态，并且进行退款
     */
    @Override
    public void onMessage(Integer paySysType) {
        // 发送退款服务进行退款的通知
        ServerResponseEntity<List<OrderIdWithRefundIdBO>> listServerResponseEntity = orderFeignClient.listWaitPurchaseAndUnPayBalanceOrderIds(paySysType);
        if(!listServerResponseEntity.isSuccess()){
            LOGGER.error("通联开启发送消息失败");
            return;
        }
        List<OrderIdWithRefundIdBO> orderList = listServerResponseEntity.getData();
        List<Long> orderIds = orderList.stream().map(OrderIdWithRefundIdBO::getOrderId).collect(Collectors.toList());

        List<PayInfo> payInfos = payInfoService.listByOrderIds(orderIds);
        if (CollectionUtil.isEmpty(payInfos)) {
            return;
        }
        Map<Long, Long> orderIdWithRefundIdMap = orderList.stream().collect(Collectors.toMap(OrderIdWithRefundIdBO::getOrderId, OrderIdWithRefundIdBO::getRefundId));
        for (PayInfo payInfo : payInfos) {
            PayRefundBO payRefundBO = new PayRefundBO();
            payRefundBO.setRefundId(orderIdWithRefundIdMap.get(Long.valueOf(payInfo.getOrderIds())));
            payRefundBO.setRefundAmount(payInfo.getPayAmount());
            payRefundBO.setOrderId(Long.valueOf(payInfo.getOrderIds()));
            payRefundBO.setPayId(payInfo.getPayId());
            payRefundBO.setUnSuccessGroupOrder(1);
            refundInfoService.doRefund(payRefundBO);
        }
    }
}
