高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
global class NFM503InfoFileBatch implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful {
    public String TenId;
    public String bidInfoFileID;
    Boolean IsNeedExecute = false;  //2021-06-28 mzy  WLIG-BYHD79  SFDC环境batch合并调查  是否符合执行条件
 
    //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];
 
        try {
            String token;
            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);
            if (string.isblank(token) || timeslot > 1800000) {
                NFMUtil.response response = NFMUtil.receiveToken();
                if (String.isBlank(response.responseBody)) {
                    bidInfoFile.ErrorMessage__c = '503token:' + response.status;
                }
                token = response.responseBody;
                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);
            // 文件大小超过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;
        } catch (Exception ex) {
            if (sp != null) {
                Database.rollback(sp);
            }
            bidInfoFile.ErrorMessage__c = '503抛出异常:' + ex.getMessage() + '\n' + ex.getStackTraceString();
        }
        update bidInfoFile;
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}