package com.fs.store.service;

import com.fs.store.service.channel.PaymentHandler;
import com.fs.store.service.channel.PaymentHandlerHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ExpressToWxHolder {
    private static ApplicationContext applicationContext;

    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) {
        ExpressToWxHolder.applicationContext = applicationContext;
    }

    public static ExpressToWxService findBest(Integer type,String orderCode) {
        String[] beanNames = applicationContext.getBeanNamesForType(ExpressToWxService.class);

        for (String beanName : beanNames) {
            ExpressToWxService handler = (ExpressToWxService) applicationContext.getBean(beanName);
            if (handler.support(type)) {
                handler.setOrderCode(orderCode);
                return handler;
            }
        }
        throw new IllegalArgumentException(String.format(String.format("对应类型 %d 没有被找到！",type)));
    }
}
