package com.hys.app.service.system.factory;

import com.hys.app.service.base.plugin.express.ExpressPlatform;
import com.hys.app.model.system.vo.ExpressPlatformVO;
import com.hys.app.service.system.ExpressPlatformManager;
import com.hys.app.framework.exception.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 快递平台插件工厂类
 * @author duanmingyu
 * @version v1.0
 * @since v7.1.6
 * 2019-12-30
 */
@Component
public class ExpressFactory {

    @Autowired
    private List<ExpressPlatform> expressPlatforms;

    @Autowired
    private ExpressPlatformManager expressPlatformManager;

    /**
     *获得快递平台实例
     *
     */
    public ExpressPlatform getExpressPlatform() {

        ExpressPlatformVO expressPlatformVO = this.expressPlatformManager.getOpen();
        return this.findByPluginid(expressPlatformVO.getBean());
    }

    /**
     * 根据插件id获取快递平台插件
     *
     * @param pluginId
     * @return
     */
    private ExpressPlatform findByPluginid(String pluginId) {
        for (ExpressPlatform expressPlatform : expressPlatforms) {
            if (expressPlatform.getPluginId().equals(pluginId)) {
                return expressPlatform;
            }
        }
        //如果走到这里，说明找不到可用的快递平台插件
        throw new ResourceNotFoundException("未找到可用的快递平台插件");
    }
}
