package com.skr.mdm.api.cmd;

import com.skr.mdm.api.cmd.annotation.CmdAdminHandler;
import com.skr.mdm.enums.CmdAdminEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author nfq
 * @date 2021/10/27 15:49
 */
@Slf4j
@Component
@Deprecated
public class CmdAdminHandlerFactory implements BeanPostProcessor {

    private static final Map<String, CmdAdminProcessHandler> CMD_ADMIN_MAP = new ConcurrentHashMap<>(16);

    public static CmdAdminProcessHandler getStrategy(String cmdType) {
        return CMD_ADMIN_MAP.get(cmdType);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean.getClass().isAnnotationPresent(CmdAdminHandler.class)) {
            CmdAdminHandler annotation = bean.getClass().getAnnotation(CmdAdminHandler.class);
            CmdAdminEnum[] cmdTypes = annotation.cmdType();
            for (CmdAdminEnum cmdType : cmdTypes) {
                CMD_ADMIN_MAP.put(cmdType.getCmd(), (CmdAdminProcessHandler) bean);
            }
        }
        return bean;
    }
}