Li Jun
2022-03-31 1b8c8764c39fc546ca1b85ff1810ae51be74331c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
global class NFM503InfoFileBatch implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful {
    public String TenId;
    public String bidInfoFileID;
    //add staic sushanhu 20220302 start
    public static String transUrl;
    public static String transId;
    public static String token;
    public static  integer isSuccess=0; 
    public static List<String> sfRecordIds =new List<String>();
    //add staic sushanhu 20220302 end
    Boolean IsNeedExecute = false;  //2021-06-28 mzy  WLIG-BYHD79  SFDC环境batch合并调查  是否符合执行条件
    //add nfm 503 aws response start sushanhu 20220301
    global class File{
        public String key;
        public String name;
    }
    //add nfm 503 aws response sushanhu 20220301 end
    //2021-06-28  mzy update  千里马-Batch start
    global NFM503InfoFileBatch() {
        this.IsNeedExecute = true;
    }
    //2021-06-28 mzy  update  千里马-Batch end
    global NFM503InfoFileBatch(String TenId) {
        this.TenId = TenId;
    }
 
    global NFM503InfoFileBatch(String bidInfoFileID, boolean a) {
        this.bidInfoFileID = bidInfoFileID;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'select Id, Tender_information__c, infoAddress__c, ' +
                        'Tender_information__r.InfoType__c, isProcessed__c, ' +
                        ' ErrorMessage__c from bidInfoFile__c where isProcessed__c = false ';
        if (String.isNotBlank(TenId)) {
            query += 'and Tender_information__r.Id =: TenId';
        }
        if (String.isNotBlank(bidInfoFileID)) {
            query += 'and Id =: bidInfoFileID';
        }
 
        return Database.getQueryLocator( query );
    }
 
    global void execute(Database.BatchableContext BC, list<bidInfoFile__c> bidInfoFileList) {
        Savepoint sp;
        bidInfoFile__c bidInfoFile = bidInfoFileList[0];
        List<String> queryfileList = new List<String>();
        queryfileList.add(bidInfoFile.infoAddress__c);
        try {
           //update 同staic 20220302 satrt
            // String token;
            //update 同staic 20220302 end
            Datetime oldTime;
            // 从转换表中获取token
            BatchIF_Transfer__c token503 = [Select ID, NFM501_Token__c
                                            FROM BatchIF_Transfer__c Where Table__c = 'NFM501Token'];
            token = token503.NFM501_Token__c;
            // 从转换表中获取获取完token的时间
            BatchIF_Transfer__c oldTime503 = [Select ID, NFM501_Gain_End_Time__c
                                              FROM BatchIF_Transfer__c Where Table__c = 'NFM501GainEndTime'];
            oldTime = oldTime503.NFM501_Gain_End_Time__c;
 
            Long timeslot;
            Datetime newTime = System.now();
            if (oldTime == null) {
                timeslot = 2800000;
            } else {
                // 当前时间与获取token结束时间的时间差
                timeslot = newTime.getTime() - oldTime.getTime();
            }
            // System.debug('++++1++++' + token + '  : ' + timeslot);
            PIHelper.piIntegration pi =PIHelper.getPIIntegrationInfo('NFM503');
            if (string.isblank(token) || timeslot > 1800000) {
                //UP TO AWAS TOKEN 20220225 SUSHANHU START
                // NFMUtil.response response = NFMUtil.getAWSToken();
                // //UP TO AWAS TOKEN 20220225 SUSHANHU END
                // if (String.isBlank(response.responseBody)) {
                //     bidInfoFile.ErrorMessage__c = '503token:' + response.status;
                // }
                token = pi.token;
                oldTime = Datetime.now();
                token503.NFM501_Token__c = token;
                oldTime503.NFM501_Gain_End_Time__c = oldTime;
            }
            // // 存放超过12M的附件
            // List<Attachment> TenOtherAttList = new List<Attachment>();
            // // 存放所有附件
            // List<String> FileList = new List<String>();
            // // 如果文件大小超过12M更新
            // List< Tender_information__c> updateTenderList = new  List< Tender_information__c>();
            // // 获取接口3中数据
            // NFMUtil.response503 response = NFMUtil.getFileData(token, bidInfoFile.infoAddress__c);
            //UP TO NEW  AWS method sushanhu start 20220301
            // List< Tender_information__c> updateTenderList = new  List< Tender_information__c>();
            List<FileAddress__c> fileList = new List<FileAddress__c>();
            transUrl =pi.searchUrl;
            NFMUtil.response response = NFMUtil.getAWSQLMData(pi.newUrl ,JSON.serialize(queryfileList), token);
            system.debug('aws result'+response.responseBody);
            Map<String, Object> result = (Map<String, Object>)JSON.deserializeUntyped(response.responseBody);
            system.debug('NFM503 aws result--'+response.responseBody+'status'+response.status);
            transId =(String)result.get('txId');
            //UP TO NEW  AWS method sushanhu start 20220301 
            // 新得存储 不需要判断文件超过12m
            // // 文件大小超过12M
            // if (response.Name.equals('文件大小超过12M')) {
            //     id tendID = bidInfoFile.Tender_information__c;
            //     Tender_information__c tempTender = new Tender_information__c();
            //     tempTender.id = tendID;
            //     tempTender.File_Surpass_12M__c = true;
            //     if (String.isBlank(tempTender.Overstep_12M_infofile__c)) {
            //         tempTender.Overstep_12M_infofile__c = bidInfoFile.infoAddress__c;
            //     } else {
            //         if (!tempTender.Overstep_12M_infofile__c.contains(bidInfoFile.infoAddress__c)) {
            //             // List<String> urlList = singleFile.split(',');
            //             tempTender.Overstep_12M_infofile__c += bidInfoFile.infoAddress__c + ',';
            //         }
            //     }
            //     updateTenderList.add(tempTender);
            // }
            // // 将获取到的数据存成附件
            // Attachment WebAtt = new Attachment();
            // WebAtt.ParentId = bidInfoFile.Tender_information__c;
            // WebAtt.Body = response.responseBody;
            // WebAtt.Name = bidInfoFile.Tender_information__r.InfoType__c + ':' + response.Name;
            // if (!response.Name.equals('文件大小超过12M')) {
            //     TenOtherAttList.add(WebAtt);
            // }
            // sp = Database.setSavepoint();
 
            // if (updateTenderList.size() > 0) {
            //     update updateTenderList;
            // }
 
            // System.debug('----1----' + TenOtherAttList);
            // if (TenOtherAttList.size() > 0 ) {
            //     upsert TenOtherAttList;
            // }
 
            // bidInfoFileList[0].isProcessed__c = true;
            // bidInfoFileList[0].ErrorMessage__c = '';
 
            // update bidInfoFileList;
            // if (System.Test.isRunningTest()) {
            //     throw new ControllerUtil.myException('aaa');
            // }
            // return;
            String statusCode =String.valueOf(result.get('status')) ;
            system.debug('statuscode aws-- '+statusCode);
            if (!'0'.equals(statusCode)) {
                System.debug('response.responseBody:' + response.responseBody);
                return;
            }
            Map<String, Object> fileMap = (Map<String, Object >)result.get('object');
            system.debug('fileMap'+fileMap.toString());
            Map<String, Object> fileVO =(Map<String, Object>)fileMap.get(bidInfoFile.infoAddress__c);
 
            String Name =(String)fileVO.get('name');
            // if (Name.equals('文件大小超过12M')) {
            //     id tendID = bidInfoFile.Tender_information__c;
            //     Tender_information__c tempTender = new Tender_information__c();
            //     tempTender.id = tendID;
            //     tempTender.File_Surpass_12M__c = true;
            //     if (String.isBlank(tempTender.Overstep_12M_infofile__c)) {
            //         tempTender.Overstep_12M_infofile__c = bidInfoFile.infoAddress__c;
            //     } else {
            //         if (!tempTender.Overstep_12M_infofile__c.contains(bidInfoFile.infoAddress__c)) {
            //             // List<String> urlList = singleFile.split(',');
            //             tempTender.Overstep_12M_infofile__c += bidInfoFile.infoAddress__c + ',';
            //         }
            //     }
            //     updateTenderList.add(tempTender);
            // }
           
            //将获取的AWS文件地址存储
            FileAddress__c file = new FileAddress__c();
            file.ParentRecordId__c = bidInfoFile.Tender_information__c;
            file.FileName__c = bidInfoFile.Tender_information__r.InfoType__c + ':' + Name;
            file.DownloadLink__c =pi.undeleteUrl+(String)fileVO.get('key')+'&fileName='+file.FileName__c;
            file.ViewLink__c = pi.queryUrl+(String)fileVO.get('key');
            file.AWS_File_Key__c =(String)fileVO.get('key');
            fileList.add(file);
            sp = Database.setSavepoint();
 
            System.debug('----1----' + fileList);
            if (fileList.size() > 0 ) {
                upsert fileList;
            }
 
            bidInfoFileList[0].isProcessed__c = true;
            bidInfoFileList[0].ErrorMessage__c = '';
 
            update bidInfoFileList;
            // add 确认事务 sushanhu 20220302 start
            
            if (fileList.size() > 0 ) {
            for (FileAddress__c fileAddress : fileList) {
                system.debug('fileAddress.Id---'+json.serialize(fileAddress));
                sfRecordIds.add(fileAddress.Id);
                system.debug('fileAddress.Id---'+fileAddress.Id);
            }
            }
            system.debug('成功的token'+token);
            isSuccess =1;
           
            // if (!confirm) {
            //     //回滚
            //     if (sp != null) {
            //         Database.rollback(sp);
            //     }
            // }
            // add 确认事务 sushanhu 20220302 end
            if (System.Test.isRunningTest()) {
                throw new ControllerUtil.myException('aaa');
            }
            return;
        } catch (Exception ex) {
            if (sp != null) {
                Database.rollback(sp);
            }
            // //add 确认事务 20220302 sushanhu start
            // system.debug('失败的token'+token);
            //  PIHelper.confirmFileTrans('NFM503',0,'',transId,token,transUrl);
            //add 确认事务 20220302 sushanhu end
            bidInfoFile.ErrorMessage__c = '503抛出异常:' + ex.getMessage() + '\n' + ex.getStackTraceString();
            system.debug('ErrorMessage'+bidInfoFile.ErrorMessage__c);
        }
        update bidInfoFile;
    }
 
    global void finish(Database.BatchableContext BC) {
        // add confirm transaction for pipl  sushanhu 20220314 start
        if (isSuccess==0) {
            PIHelper.confirmFileTrans('NFM503',isSuccess,'',transId,token,transUrl);
        }else {
            PIHelper.confirmFileTrans('NFM503',isSuccess,JSON.serialize(sfRecordIds),transId,token,transUrl);
        }
         
         // add confirm transaction for pipl  sushanhu 20220314 end
    }
}