package com.brilliance.isc.common.sysmod.objname;

import com.brilliance.isc.common.context.SpringEnvContext;
import com.brilliance.isc.common.exception.CommonServiceException;
import com.brilliance.isc.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.stereotype.Component;

@Component
public class ObjNameBuilderManager {

    private static Logger logger = LoggerFactory.getLogger(ObjNameBuilderManager.class);

    public String getObjName(String objType, String ownref, String name) {
        IObjNameBuilder objNameBuilder = null;
        try {
            if (StringUtils.isEmpty(objType)) {
                throw new CommonServiceException("交易种类为空，无法获取ObjName");
            }
            String type = objType.toLowerCase().substring(0, 2) + "t.objname";
            objNameBuilder = SpringEnvContext.getBean(type);
        } catch (NoSuchBeanDefinitionException e) {
            logger.warn("没有找到交易注册类，使用默认注册类");
            objNameBuilder = SpringEnvContext.getBean("default.objname");
        }
        return objNameBuilder.buildObjName(ownref, name);
    }
}
