package com.cf.imes.module.system.service.pay.factory;

import cn.hutool.core.util.ObjectUtil;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.module.system.service.pay.factory.impl.alipay.AlipayPcPayHandler;
import com.cf.imes.module.system.service.pay.factory.impl.AbstractPayHandler;
import com.cf.imes.module.system.service.pay.factory.impl.wechat.WechatNativePayHandler;
import com.cf.imes.module.system.enums.pay.PayChannelCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import static com.cf.imes.module.system.enums.ErrorCodeConstants.PAY_CHANNEL_NOT_SUPPORT;

/**
 * 支付处理器工厂
 *
 * @author Gqr
 * @since 2025/4/15 15:43
 */
@Component
public class PayHandlerFactory {
    private final ApplicationContext applicationContext;

    @Autowired
    public PayHandlerFactory(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * 根据渠道编码获取对应的支付处理器
     *
     * @param channelCode
     * @return
     */
    public AbstractPayHandler getPayHandler(int channelCode) {
        PayChannelCodeEnum payChannelCodeEnum = PayChannelCodeEnum.fromType(channelCode);
        if (ObjectUtil.isNull(payChannelCodeEnum)) {
            throw new ServiceException(PAY_CHANNEL_NOT_SUPPORT);
        }
        switch (payChannelCodeEnum) {
            // 支付宝电脑支付
            case ALIPAY_PC:
                return applicationContext.getBean(AlipayPcPayHandler.class);
            // 微信native支付
            case WECHAT_NATIVE:
                return applicationContext.getBean(WechatNativePayHandler.class);
            default:
                throw new ServiceException(PAY_CHANNEL_NOT_SUPPORT);
        }
    }
}
