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) {
|
|
}
|
}
|