package link.at17.mid.tushare.data.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class StockCodeUtil {
    
    private static final Pattern EM_CODE_PATTERN = Pattern.compile("^(\\d{1,2})\\.(.*?)$");
    
    /**
     * 转为标准形式的代码，最终仅支持 6 位数字 + .SZ 或 .SH 的形式，否则会抛出异常
     * @param code
     * @return
     * @throws UnsupportedOperationException
     */
    public static String eastmoneyToTushare(String code) throws UnsupportedOperationException {
        if (StringUtils.isEmpty(code)) {
            return code;
        }
        Matcher m = EM_CODE_PATTERN.matcher(code);
        if (m.matches()) {
            String sec = m.group(1);
            String nudeCode = m.group(2);
            
            if ("1".equals(sec)) {
                return nudeCode + ".SH";
            }
            else if ("0".equals(sec)) {
                return nudeCode + ".SZ";
            }
            else {
                
            }
            return code;
        }
        return null;
    }

}
