buli
2022-03-10 a90c9ecfc5118547d0a92b2fee2779eca95e09a5
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
public  class SoqlHelper {
 
    public static string DistinctQueryFields(string sql)
    {
        string query_fields = sql;
        if (query_fields == null) {
            return null;
        }
        query_fields = query_fields.trim();
        Integer sel_i = query_fields.indexOfIgnoreCase('select ');
        string sel = 'select ';
        
        if ( sel_i < 0) {
            sel = '';
        }
 
        string whe = '';
        Integer whe_i = query_fields.indexOfIgnoreCase(' from ');
        if ( whe_i >= 0) {
            whe = query_fields.substring(whe_i);
        }
        string query = query_fields.substring(sel_i < 0 ? 0 : sel.length(), whe_i < 0 ? query_fields.length() : whe_i);
        Set<string> ss = new Set<string>();
        for(string f : query.split(',')){
            string s = f.trim().toLowerCase();
            //System.debug('s='+s);
            ss.add(s);
        }
 
        return sel + QueryFields(ss) + whe;
 
    }
    
    public static string WId(string wid){
        return '\'' + wid + '\'';
    }
   
    public static string QueryFields(Set<string> lo){
        return QueryFields(new List<string>(lo));
    }
    
    public static string QueryFields(List<object> lo){
        return string.join(lo, ',');
    }
 
    public static string ToInCondition(List<AggregateResult> lar ,string key)
    {
        if(lar==null || lar.size() == 0)
        {
            return null;
        }
        
        string ss = ' ( ';
        boolean b = true;
        for(AggregateResult ar : lar)
        {
            if(ar.get(key)==null)continue;
            if( b )
            {
                b = false;
                ss += '\''+ar.get(key)+'\'';
            }
            else
            {
                ss += ',\''+ar.get(key)+'\'';
            }
            
        }
        ss+=') ';
        return ss;
    }
    
    public static string ToInCondition(Set<string> so){
        return ToInCondition(new List<string>(so));
    }
    
    public static string ToInCondition(List<object> lo)
    {
        Set<object> so = new Set<object>(lo);
        lo = new List<object>(so);
        return '(\''+ string.join(lo, '\',\'')+'\')';
    }
    
    public static void InsertList(List<Sobject> so_list)
    {
        if(so_list == null || so_list.size() == 0)return;
        insert so_list;
    }
    
    public static void UpdateList(List<Sobject> so_list)
    {
        if(so_list == null || so_list.size() == 0)return;
        update so_list;
    }
    
    public static Map<string,Database.UpsertResult> UpsertList(List<Sobject> so_list, Schema.SObjectField field)
    {
        Map<string,Database.UpsertResult> mid = new Map<string,Database.UpsertResult>();
        if(so_list == null || so_list.size() == 0)return mid;
        List<Database.UpsertResult> urList2 = Database.upsert( so_list, field, false);
        
        Set<String> accBasicRecordIdSet = new Set<String>();
        integer index = 0;
        string errorString = '';
        string field_str = field.getDescribe().getName();
        for(Database.UpsertResult ur : urList2){
            if(!ur.IsSuccess()){
                object fv = so_list[index].get(field_str);
                
                mid.put(fv+'',ur);
                errorString = '';
                for(Database.Error errs: ur.getErrors()){
                    errorString += errs.getMessage() + ';';
                }
                //LogHelper.CreateRunTimeLog('runtime error', errorString, false, fv+'');
            }
            index++;
        }
        return mid;
    }
    
    public static void DeleteList(List<Sobject> so_list)
    {
        if(so_list == null || so_list.size() == 0)return;
        delete so_list;
    }
 
 
    @future
    public static void DeleteListAsync(List<Id> id_list)
    {
        if(id_list == null || id_list.size() == 0)return;
        Database.delete(id_list);
    }
    
}