package com.cusc.nirvana.user.rnr.fp.handler;

import com.cusc.nirvana.user.rnr.fp.dto.MessageBizTypeEnum;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author stayAnd
 * @date 2022/3/31
 */
@Component
public class MessageCallBackContext implements ApplicationContextAware, InitializingBean {

    private ApplicationContext context;


    private Map<String, MessageCallbackHandler> messageCallbackHandlerMap;

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


    public MessageCallbackHandler getWorkBackHandler(int bizType) {
        MessageBizTypeEnum messageBizTypeEnum = MessageBizTypeEnum.getBizType(bizType);
        if (null != messageBizTypeEnum) {
            return messageCallbackHandlerMap.get(messageBizTypeEnum.getMessageCallBackHandlerName());
        }
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        messageCallbackHandlerMap = context.getBeansOfType(MessageCallbackHandler.class);
    }
}
