
package com.tmerclub.cloud.payment.listener;

import com.tmerclub.cloud.api.payment.constant.PaySysType;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.payment.bo.PayInfoResultBO;
import com.tmerclub.cloud.payment.constant.PayEntry;
import com.tmerclub.cloud.payment.manager.PayNoticeManager;
import com.tmerclub.cloud.payment.model.PayInfo;
import com.tmerclub.cloud.payment.service.PayInfoService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 订单余额支付成功监听
 *
 * @author tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.BALANCE_PAY_TOPIC, consumerGroup = RocketMqConstant.BALANCE_PAY_TOPIC)
public class BalancePayConsumer implements RocketMQListener<Long> {

    @Autowired
    private PayInfoService payInfoService;
    @Autowired
    private PayNoticeManager payNoticeManager;

    /**
     * 订单退款 支付服务，开始进行退款啦
     */
    @Override
    public void onMessage(Long payId) {
        PayInfo payInfo = payInfoService.getByPayId(payId);
        PayInfoResultBO payInfoResultBO = new PayInfoResultBO();
        payInfoResultBO.setPaySuccess(true);
        payInfoResultBO.setPayId(payId);
        payInfoResultBO.setCallbackContent("余额支付成功");
        payInfoResultBO.setPayAmount(payInfo.getPayAmount());
        payInfoResultBO.setBizPayNo(payId.toString());
        // 只有商城余额才有用到，这里应该直接是0
        payInfoResultBO.setPaySysType(PaySysType.DEFAULT.value());

        if (Objects.equals(payInfo.getPayEntry(), PayEntry.ORDER.value())) {
            // 订单支付回调
            payNoticeManager.noticeOrder(payInfoResultBO, payInfo, PaySysType.DEFAULT.value());
        } else if (Objects.equals(payInfo.getPayEntry(), PayEntry.VIP.value())) {
            // 购买会员回调
            payNoticeManager.noticeBuyVip(payInfoResultBO, payInfo);
        }
    }
}
