定时执行 batch
global class LogistiInfoSchedu implements Schedulable, Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
public Set<Id> updateOiIdSet;
global void execute(SchedulableContext sc) {
LogistiInfoSchedu lo = new LogistiInfoSchedu();
database.executeBatch(lo,90);
}
//Start 方法里面查询的是系统中发货状态不为到达的发货申请的记录
global Database.QueryLocator start(Database.BatchableContext bc) {
updateOiIdSet = new Set<Id>();
List<Order_Inquiry__c> qiList = new List<Order_Inquiry__c>();
qiList = [select id,OrderStatus__c,OTNumber__c,SAP_order_number__c from Order_Inquiry__c where OrderStatus__c != 'delivered' and OrderStatus__c != 'epod' and OTNumber__c != NULL AND OTNumber__c != '' AND (ApprovalStatus__c = 'Approved' OR ApprovalStatus__c = 'Auto Approved')];
System.debug('LogistiInfoSchedu 查到的数量:'+qiList.size());
return Database.getQueryLocator([select id,OrderStatus__c,OTNumber__c,SAP_order_number__c from Order_Inquiry__c where OrderStatus__c != 'delivered' and OrderStatus__c != 'epod' and OTNumber__c != NULL AND OTNumber__c != '' AND (ApprovalStatus__c = 'Approved' OR ApprovalStatus__c = 'Auto Approved')]);
}
global void execute(Database.BatchableContext BC,list<Order_Inquiry__c> scope) {
//调用接口解析xml 数据
LogisticsInfoInterface lo = new LogisticsInfoInterface();
Set<String> otmNymberSet = new Set<String>();
for(Order_Inquiry__c ord : scope){
otmNymberSet.add(ord.OTNumber__c);
}
system.debug('Batch批次中要处理:'+otmNymberSet);
system.debug('Batch批次中要处理的数量:'+otmNymberSet.size());
// httpResponse xmlRes = lo.getLogisticsInfo(otmNymberSet);
// String responsStr = xmlRes.getBody().Replace('</time>','</theTime>');
// responsStr = responsStr.Replace('<time>','<theTime>');
SaveDeliveryInfo s = new SaveDeliveryInfo();
s.getResponseXML(otmNymberSet);
//updateOiIdSet.addAll(s.locationUpdateIdSet);
}
global void finish(Database.BatchableContext BC) {
if(updateOiIdSet.size() > 0){
UpdateLocationBatch thisBatch = new UpdateLocationBatch(updateOiIdSet);
thisBatch.startUpdateLocation();
}
}
}
Finish 中调用其他batch
global class UpdateLocationBatch implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
public Set<Id> locationUpdateIdSet;
global UpdateLocationBatch() {
locationUpdateIdSet = new Set<Id>();
}
global UpdateLocationBatch(Set<Id> orderIdSet) {
locationUpdateIdSet = new Set<Id>();
if(orderIdSet != NULL){
locationUpdateIdSet = orderIdSet;
}
}
/**
* @Function [该Batch的启动方法]
*/
public void startUpdateLocation(){
UpdateLocationBatch thisBatch = new UpdateLocationBatch(locationUpdateIdSet);
Database.executeBatch(thisBatch, 1);
}
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([SELECT Id, Latitude__c, Longitude__c, Location__c
FROM Order_Inquiry__c
WHERE Id IN: locationUpdateIdSet
AND Latitude__c != NULL
AND Longitude__c != NULL]);
}
global void execute(Database.BatchableContext BC, list<Order_Inquiry__c> scope) {
Order_Inquiry__c thisOrder = scope.get(0);
GetCurrentLocationService loc = new GetCurrentLocationService();
thisOrder.Location__c = loc.getCurrentLocation(thisOrder.Latitude__c, thisOrder.Longitude__c);
System.debug('thisOrder.Location__c='+thisOrder.Location__c);
update thisOrder;
}
global void finish(Database.BatchableContext BC) {
}
}