buli
2022-05-13 2f4492ee18f90274582fcc2bb06f5e9bf64136e8
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
public without sharing class AWSServiceTool2 {
 
    static Map<string,PIHelper.PIIntegration> staticResourceBuffer = new Map<string,PIHelper.PIIntegration>();
    static PIHelper.PIIntegration getPIIntegration(string sobject_name){
        system.debug('sobject_name='+sobject_name);
        if(!staticResourceBuffer.containsKey(sobject_name)){
            PIHelper.PIIntegration staticResource =  PIHelper.getPIIntegrationInfo(sobject_name);
            staticResourceBuffer.put(sobject_name,staticResource);
        }
        return staticResourceBuffer.get(sobject_name);
    }
    
    public static boolean EncryptPushData(List<string> Ids){
        if(ids == null || ids.size() == 0){
            return false;
        }
        
        Id rid = Ids[0];
        string sobject_name = rid.getSobjectType().getDescribe().getName();
        string sql = 'select id ';
        
        PIHelper.PIIntegration staticResource = getPIIntegration(sobject_name);
        for(PI_Field_Policy_Detail__c detail :staticResource.PIDetails){
            sql += ',' + detail.SF_Field_API_Name__c+',' + detail.SF_Field_Encrypted_API__c;
        }
        sql += ' ,AWS_Data_Id__c from ' + sobject_name + ' where id in :Ids';
        system.debug('sql='+sql);
        List<Sobject> lso = Database.query(sql);
        system.debug('lso.size()='+lso.size());
        if(lso.size()==0){
            return false;
        }
        return EncryptPushCore(Json.serialize(lso),sobject_name);
    }
    
    @future(callout=true)
    public static void EncryptPushFuture(string json_list,string sobject_name){
        EncryptPushCore(json_list,sobject_name);
    }
    
    // List<Rental_Apply__c> temps = [select id,AWS_Data_Id__c,name, direct_shippment_address__c,  Direct_Shippment_Address_Encrypt__c, Phone_number__c,  Phone_Number_Encrypt__c,CreatedDate   from Rental_Apply__c where AWS_Data_Id__c != null order by CreatedDate desc limit 2];
    public static boolean EncryptPushCore(string json_list,string sobject_name){
        system.debug('enter EncryptPushCore');
        system.debug(json_list);
        if(string.isBlank(json_list) || string.isBlank(sobject_name)){
 
            system.debug('json_list or sobject_name is null');
            return false;
        }
        
        //调用滨璜接口更新
        PIHelper.PIIntegration staticResource =  getPIIntegration(sobject_name);
        system.debug('staticResource.token='+staticResource.token);
        if(String.isBlank(staticResource.token)){
            System.debug('获取aws token 失败');
            return false;
        }
        List<object> newobjectList = (List<object>)Json.deserializeUntyped(json_list);
        Map<Id,Sobject> newMap = new Map<Id,Sobject>();
        
        for(object obj : newobjectList){
            Map<string,object> mobj = (Map<string,object>)obj;
            if(mobj.containsKey('Id')){
                Sobject sobj_temp = (Sobject)Json.deserialize(Json.serialize(obj), Type.forName(sobject_name));
                newMap.put(sobj_temp.Id,sobj_temp);
            }
        }
        List<Map<string,object>> insert_list = new List<Map<string,object>>();
        List<Map<string,object>> update_list = new List<Map<string,object>>();
        List<sobject> newList = newMap.values();
        for(Sobject ra : newList){
            Map<string,object> mso = new Map<string,object>();
            
            
            for(PI_Field_Policy_Detail__c detail : staticResource.PIDetails){
                if(ra.isSet(detail.SF_Field_API_Name__c)){
                    mso.put(detail.AWS_Field_API__c,ra.get(detail.SF_Field_API_Name__c));
                    mso.put(detail.AWS_Encrypted_Field_API__c,ra.get(detail.SF_Field_Encrypted_API__c));
                }
            }
            mso.put('sfRecordId',ra.Id);
            string aws_id = string.valueOf(ra.get('AWS_Data_Id__c'));
            system.debug('aws_id='+aws_id);
            if(string.isBlank(aws_id)){
                insert_list.add(mso);
            }else{
                system.debug('aws_id.lenth='+aws_id.length());
                mso.put('dataId',aws_id);
                update_list.add(mso);
            }
            
        }
        List<object> objList = new List<object>();
        List<Sobject> updateList = new List<Sobject>();
        List<object> temp = null;
        system.debug('insert_list.size()='+insert_list.size());
        system.debug('update_list.size()='+update_list.size());
        //if(true)return;
        if(insert_list.size() > 0){
            system.debug('url='+staticResource.newEncryptUrl);
            temp = PostAws(Json.serialize(insert_list),staticResource.newEncryptUrl,staticResource.token);
            if(temp != null){
                objList.addAll(temp);
            }
        }
        
        
        if(update_list.size() > 0){
            system.debug('url='+staticResource.updateEncryptUrl);
            temp = PostAws(Json.serialize(update_list),staticResource.updateEncryptUrl,staticResource.token);
            if(temp != null){
                objList.addAll(temp);
            }
        }
        
        system.debug('objList.size()='+objList.size());
        if(objList.size()==0){
            return false;
        }
        
       
        for(object obj : objList){
            Map<string,object> obj_map = (Map<string,object>)obj;
            string sfRecordId = null;
            string dataId = null;
            if(obj_map.containsKey('sfRecordId')){
                sfRecordId = string.valueOf(obj_map.get('sfRecordId'));
            }else{
                system.debug('obj_map.containsKey(\'sfRecordId\')='+obj_map.containsKey('sfRecordId'));
                continue;
            }
            
            if(obj_map.containsKey('dataId')){
                dataId = string.valueOf(obj_map.get('dataId'));
            }else{
                system.debug('obj_map.containsKey(\'dataId\')='+obj_map.containsKey('dataId'));
                continue;
            }
            
            
            if(newMap.containsKey(sfRecordId)){
                Sobject ra = newMap.get(sfRecordId);
                for(PI_Field_Policy_Detail__c detail : staticResource.PIDetails){
                    if(obj_map.containsKey(detail.AWS_Field_API__c)){
                        ra.put(detail.SF_Field_API_Name__c,obj_map.get(detail.AWS_Field_API__c));
                    }else{
                        system.debug('detail.AWS_Field_API__c='+detail.AWS_Field_API__c+' not in obj_map');
                    }
                    
                    if(obj_map.containsKey(detail.AWS_Encrypted_Field_API__c)){
                        ra.put(detail.SF_Field_Encrypted_API__c,obj_map.get(detail.AWS_Encrypted_Field_API__c));
                    }else{
                        system.debug('detail.AWS_Encrypted_Field_API__c='+detail.AWS_Encrypted_Field_API__c+' not in obj_map');
                    }
                }
                ra.put('AWS_Data_Id__c',dataId);
                
                updateList.add(ra);
            }else{
                system.debug('newMap.containsKey('+sfRecordId+')='+newMap.containsKey(sfRecordId));
                continue;
            }
        }
        
        system.debug('updateList.size='+updateList.size());
        if(updateList.size()>0){
            update updateList;
        }
        
        return true;
    }
    
    static List<object> PostAws(string payload,string url,string token){
        system.debug('payload='+payload);
        AWSServiceTool.response response = AWSServiceTool.sendToPiAWS(payload, url,token);
        system.debug(response);
        Map<string,object> res_obj = (Map<string,object>)Json.deserializeUntyped(response.responseBody);
        if(res_obj == null || !res_obj.containsKey('object') ){
            System.debug('res_obj == null || !res_obj.containsKey(\'object\')');
            return null;
        }
        
        List<object> objList = (List<object>)res_obj.get('object');
        if(objList == null){
            System.debug('objList == null');
            return null;
        }
        return objList;
    }
   
}