buli
2022-04-06 2d4a8d2dcad5a17127d2c73c48ddc4b67ec79448
force-app/main/default/classes/NFM502Controller.cls
@@ -1,6 +1,11 @@
public with sharing class NFM502Controller implements Queueable {
   public String rowData_id;
    //add staic sushanhu 20220302 start
    public static String transUrl;
    public static String transId;
    public static String token;
    public static List<String> sfRecordIds =new List<String>();
    //add staic sushanhu 20220302 end
   public NFM502Controller(String rowData_id) {
      this.rowData_id = rowData_id;
   }
@@ -46,7 +51,9 @@
      // Savepoint sp = Database.setSavepoint();
      try {
         String token;
            //update 同staic 20220302 satrt
            // String token;
            //update 同staic 20220302 end
         Datetime oldTime;
         // 从转换表中获取token
         BatchIF_Transfer__c token502 = [Select ID, NFM501_Token__c
@@ -75,11 +82,13 @@
         }
         // System.debug('++++1++++' + token + '  : ' + timeslot);
         if (string.isblank(token) || timeslot > 1800000) {
            NFMUtil.response response = NFMUtil.receiveToken();
                // NFMUtil.response response = NFMUtil.receiveToken();
                //update to aws token sushanhu 20220301  start
                NFMUtil.response response = NFMUtil.getAWSToken();
            //判断rowdata中数据获取成功与否,如果失败重发三次,如果大于三次则手动操作
            if (String.isBlank(response.responseBody)) {
               System.debug('response.responseBody:' + response.responseBody);
               iflog502.ErrorLog__c = '502token:' + response.status;
                    iflog502.ErrorLog__c = '502token:' + response.responseBody;
               // rowData.NFM501_Web_Annex_Count__c = 0;
               if (!Manual_execution502) {
                  NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
@@ -92,6 +101,7 @@
            oldTime = Datetime.now();
            token502.NFM501_Token__c = token;
            oldTime502.NFM501_Gain_End_Time__c = oldTime;
                //update to aws token sushanhu 20220301  end
         }
         //关联附件与招投标项目(通过Id)
@@ -116,7 +126,12 @@
         }
         //循环URL
         List<Attachment> TenAttList = new List<Attachment>();
            // List<Attachment> TenAttList = new List<Attachment>();
            //update to aws pi sushanhu 20220301 start
            List<FileAddress__c> fileList = new List<FileAddress__c>();
            List<String> queryUrlList = new List<String>();
            Map<String, NFM501Controller.ListItem> queryMap = new Map<String, NFM501Controller.ListItem>();
            //update to aws pi sushanhu 20220301 end
         for (NFM501Controller.ListItem QLMWebAtt : getQLMData502.data.list1) {
            if (QLMWebAtt.projectId == null) {
               iflog502.ErrorLog__c += 'Error! [' + QLMWebAtt.projectId + ']NotExist. This information is skipped.\n';
@@ -129,11 +144,69 @@
                                       ']Is 香港特别行政区(澳门特别行政区,台湾省). This information is skipped.\n';
               continue;
            }
                //update to aws pi sushanhu 20220301 start
                queryUrlList.add(QLMWebAtt.infoQianlimaUrl);
                queryMap.put(QLMWebAtt.infoQianlimaUrl,QLMWebAtt);
                //update to aws pi sushanhu 20220301 start
            //  //调用接口3
            //  NFMUtil.response response = NFMUtil.getQLMData(NFMUtil.NFM502_ENDPOINT + QLMWebAtt.infoQianlimaUrl, token);
            //  if (String.isBlank(response.responseBody)) {
            //      System.debug('response.responseBody:' + response.responseBody);
            //      iflog502.ErrorLog__c = '502接口调用:' + response.status;
            //      rowData.NFM501_Web_Annex_Count__c = 0;
            //      if (!Manual_execution502) {
            //          NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
            //      }
            //      //更新日志数据
            //      update token502;
            //      update oldTime502;
            //      return;
            //  }
            //  //解析后的code报错处理
            //  string NFM502responseBody = response.responseBody;
            //  Map<String, Object> Body502 = (Map<String, Object>) JSON.deserializeUntyped(NFM502responseBody);
            //  if (!Body502.get('code').equals('0')) {
            //      System.debug('-------9-------');
            //      iflog502.ErrorLog__c = '502解析:' + Body502.get('msg').tostring() ;
            //      if (!Manual_execution502) {
            //          NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
            //      }
            //      update token502;
            //      update oldTime502;
            //      return;
            //  }
            //  System.debug('Body502.data:' + Body502.get('data').tostring() + '---------'
            //               + Body502.get('msg').tostring() + '-------' + Body502.get('code').tostring());
            //  //获取网页信息转存为附件
            //  //截切数据(使数据成为解析的格式)
            //  Integer start = NFM502responseBody.indexOf('"infoHtml":"');
            //  Integer theEnd = NFM502responseBody.lastIndexOf('"},"msg');
            //  NFM502responseBody = NFM502responseBody.substring(start + 12, theEnd);
            //  //将其转换为附件
            //  // System.debug('---------' + NFM502responseBody);
            //  Attachment WebAtt = new Attachment();
            //  // System.debug('projectId:' + QLMWebAtt.projectId);
                // if (TenMap.containskey(QLMWebAtt.projectId)) {
                //  WebAtt.ParentId = TenMap.get(QLMWebAtt.projectId).Id;
                //  WebAtt.Body = Blob.valueOf(NFM502responseBody);
                //  WebAtt.Name = TenMap.get(QLMWebAtt.projectId).InfoType__c + ':' + QLMWebAtt.infoTitle + '.html';
                //  TenAttList.add(WebAtt);
                // }
             }
            //update to aws pi  sushanhu 20220301 start
            PIHelper.piIntegration pi =PIHelper.getPIIntegrationInfo('NFM502');
            transUrl=pi.searchUrl;
            //调用接口3
            NFMUtil.response response = NFMUtil.getQLMData(NFMUtil.NFM502_ENDPOINT + QLMWebAtt.infoQianlimaUrl, token);
            if (String.isBlank(response.responseBody)) {
            system.debug('Payload for NFM 520:'+JSON.serialize(queryUrlList));
            NFMUtil.response response = NFMUtil.getAWSQLMData(pi.newUrl ,JSON.serialize(queryUrlList), token);
            Map<String, Object> result = (Map<String, Object>)JSON.deserializeUntyped(response.responseBody);
            String statusCode =(String)result.get('status');
            transId =(String)result.get('txId');
            if (!'0'.equals(statusCode)) {
               System.debug('response.responseBody:' + response.responseBody);
               iflog502.ErrorLog__c = '502接口调用:' + response.status;
                iflog502.ErrorLog__c = '502接口调用:' + (String)result.get('message');
               rowData.NFM501_Web_Annex_Count__c = 0;
               if (!Manual_execution502) {
                  NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
@@ -147,9 +220,9 @@
            //解析后的code报错处理
            string NFM502responseBody = response.responseBody;
            Map<String, Object> Body502 = (Map<String, Object>) JSON.deserializeUntyped(NFM502responseBody);
            if (!Body502.get('code').equals('0')) {
            if (!String.valueOf(Body502.get('status')).equals('0')) {
               System.debug('-------9-------');
               iflog502.ErrorLog__c = '502解析:' + Body502.get('msg').tostring() ;
                iflog502.ErrorLog__c = '502解析:' + Body502.get('message').tostring() ;
               if (!Manual_execution502) {
                  NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
               }
@@ -157,23 +230,22 @@
               update oldTime502;
               return;
            }
            System.debug('Body502.data:' + Body502.get('data').tostring() + '---------'
                         + Body502.get('msg').tostring() + '-------' + Body502.get('code').tostring());
            System.debug('Body502.data:' + Body502.get('object').tostring() + '---------'
                         + Body502.get('message').tostring() + '-------' + Body502.get('status').tostring());
            //获取网页信息转存为附件
            //截切数据(使数据成为解析的格式)
            Integer start = NFM502responseBody.indexOf('"infoHtml":"');
            Integer theEnd = NFM502responseBody.lastIndexOf('"},"msg');
            NFM502responseBody = NFM502responseBody.substring(start + 12, theEnd);
            //将其转换为附件
            // System.debug('---------' + NFM502responseBody);
            Attachment WebAtt = new Attachment();
            // System.debug('projectId:' + QLMWebAtt.projectId);
            //获取aws返回的地址并存储
            Map<String, Object> fileMap = (Map<String, Object >)result.get('object');
            for(String url:queryUrlList){
                NFM501Controller.ListItem QLMWebAtt = queryMap.get(url);
            if (TenMap.containskey(QLMWebAtt.projectId)) {
               WebAtt.ParentId = TenMap.get(QLMWebAtt.projectId).Id;
               WebAtt.Body = Blob.valueOf(NFM502responseBody);
               WebAtt.Name = TenMap.get(QLMWebAtt.projectId).InfoType__c + ':' + QLMWebAtt.infoTitle + '.html';
               TenAttList.add(WebAtt);
                    FileAddress__c file =new FileAddress__c();
                    file.ParentRecordId__c = TenMap.get(QLMWebAtt.projectId).Id;
                    file.FileName__c = TenMap.get(QLMWebAtt.projectId).InfoType__c + ':' + QLMWebAtt.infoTitle + '.html';
                    file.DownloadLink__c =pi.undeleteUrl+(String)fileMap.get(url)+'&fileName='+file.FileName__c;
                    file.ViewLink__c = pi.queryUrl+(String)fileMap.get(url) ;
                    file.AWS_File_Key__c =(String)fileMap.get(url) ;
                    fileList.add(file);
            }
         }
@@ -185,22 +257,44 @@
            }
         }
         List<Attachment> DeleAttList = [select id, name, ParentId from Attachment
                                         where name in :UrlList and ParentId in :TenIdSet];
         if (DeleAttList.size() > 0) {
            delete DeleAttList;
            List<FileAddress__c> DeleFileList = [select id, FileName__c, ParentRecordId__c from FileAddress__c
                                            where FileName__c in :UrlList and ParentRecordId__c in :TenIdSet];
            if (DeleFileList.size() > 0) {
                delete DeleFileList;
         }
         System.debug('----1----' + TenAttList);
         if (TenAttList.size() > 0) {
            upsert TenAttList;
            System.debug('----1----' + fileList);
            if (fileList.size() > 0) {
                upsert fileList;
            }
            //确认事务
            for (FileAddress__c file : fileList) {
                system.debug('file--'+json.serialize(file));
                system.debug('file.id'+file.Id);
                sfRecordIds.add(file.Id);
            }
            //update to aws pi  sushanhu 20220301 end
            //  PIHelper.confirmFileTrans('NFM502',1,JSON.serialize(sfRecordIds),transId,token,transUrl);
            if (fileList.size() > 0) {
                PIHelper.insertConfirmTrans('NFM502',1,JSON.serialize(sfRecordIds),transId,0,transUrl,null);
            }else{
                PIHelper.insertConfirmTrans('NFM502',0,JSON.serialize(sfRecordIds),transId,0,transUrl,null);
         }
            // if (!confirm) {
            //  //回滚
            // }
         rowData.NFM501_Web_Annex_Count__c = 0;
      } catch (Exception ex) {
         // Database.rollback(sp);
         // System.debug(Logginglevel.ERROR, 'QLMData_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
         // System.debug(Logginglevel.ERROR, 'QLMData_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
         // logstr += '\n' + ex.getMessage();
            //add 事务确认 sushanhu 20220302 satrt
            //  PIHelper.confirmFileTrans('NFM502',0,'',transId,token,transUrl);
             PIHelper.insertConfirmTrans('NFM502',0,JSON.serialize(sfRecordIds),transId,0,transUrl,null);
            //add 事务确认 sushanhu 20220302 end
         iflog502.ErrorLog__c = '502抛出异常:' + ex.getMessage() + '\n'
                                + ex.getStackTraceString() + '\n' + iflog502.ErrorLog__c;
         if (!Manual_execution502) {