package org.dromara.zhishu.service.factory;

import org.dromara.zhishu.service.strategy.*;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ShopGoodsFactory implements ApplicationContextAware {

    private static ApplicationContext context;

    private static final Map<Integer, Class<? extends ShopGoodsStrategy>> strategyClasses = new HashMap<>();

    static {
        // 可以继续添加其他的消息类型对应的策略
        strategyClasses.put(1, ShopGoodsPddStrategy.class);
        strategyClasses.put(2, ShopGoodsKWStrategy.class);
        strategyClasses.put(3, ShopGoodsWlnStrategy.class);
    }

    public static ShopGoodsStrategy getStrategy(Integer type) {
        Class<? extends ShopGoodsStrategy> strategyClass = strategyClasses.get(type);
        if (strategyClass != null) {
            return context.getBean(strategyClass);
        }
        return null;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
}
