global class NFM603Batch implements Database.Batchable < sObject > , Database.AllowsCallouts {
    public String query;
    public List<String> repairIdList;
    global NFM603Batch(){}

    global NFM603Batch(List<String> repairIdList) {
        this.repairIdList = repairIdList;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {

        // 如果前一个603的batch正在执行，则后一个等待5s再执行
        List<AsyncApexJob> lstJobs = [SELECT Id FROM AsyncApexJob WHERE Id !=: bc.getJobId() AND (Status = 'Preparing' OR Status = 'Processing') AND ApexClass.Name = 'NFM603Batch'];
        if (lstJobs != null && lstJobs.size() > 0) {
            Long startTime = DateTime.now().getTime();
            Long finishTime = DateTime.now().getTime();
            while ((finishTime - startTime) < 5000) {
                //sleep for 5s
                finishTime = DateTime.now().getTime();
            }
        }

        if (this.repairIdList != null) {
            return Database.getQueryLocator([SELECT Id,AwaitToSendAWS__c FROM Repair__c WHERE Id IN:repairIdList]);
        }
        return Database.getQueryLocator([SELECT Id,AwaitToSendAWS__c FROM Repair__c WHERE AwaitToSendAWS__c = true]);
    }

    global void execute(Database.BatchableContext BC, list<Repair__c> repairList) {

        List < String > repairOrderIdList = new List < String >();

        for (Repair__c repair: repairList) {
            repairOrderIdList.add(repair.Id);
            repair.AwaitToSendAWS__c = false;
        }
        
        NFM603Controller.executefuture(null,repairOrderIdList);
        update repairList;
        
    }

    global void finish(Database.BatchableContext BC) {

    }
}