public without sharing class StateCityUtil {

    private static Map<String, String> states;
    private static Map<String, String> cities;

    public StateCityUtil() {
        init_state_city();
    }

    // 初始化省市信息
    private static void init_state_city() {
        if (states == null || states.size() <= 0) {
            states = new Map<String, String>();
            List<Address_Level__c> lst_state = [select Id, Name from Address_Level__c];
            for (Address_Level__c level1 : lst_state) {
                states.put(level1.Name, level1.Id);
            }
        }
        if (cities == null || cities.size() <= 0) {
            cities = new Map<String, String>();
            List<Address_Level2__c> lst_city = [select Id, Name from Address_Level2__c];
            for (Address_Level2__c level2 : lst_city) {
                cities.put(level2.Name, level2.Id);
            }
        }
    }

    // 设置会议上的省
    public static String get_state(String state_name) {
        if (String.isBlank(state_name)) {
            return null;
        }
        if (states == null || states.size() <= 0) {
            init_state_city();
        }
        // 处理空格 或许后续还有别的处理
        state_name = state_name.trim().replace(' ', '');
        for(String state : states.keySet()) {
            if (state_name.contains(state) || state.contains(state_name)) {
                return states.get(state);
            }
        }
        return null;
    }

    // 设置会议上的市
    public static String get_city(String city_name) {
        if (String.isBlank(city_name)) {
            return null;
        }
        if (cities == null || cities.size() <= 0) {
            init_state_city();
        }
        // 处理空格 或许后续还有别的处理
        city_name = city_name.trim().replace(' ', '');
        for(String city : cities.keySet()) {
            if (city_name.contains(city) || city.contains(city_name)) {
                return cities.get(city);
            }
        }
        return null;
    }
}