高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
    }
}