global class NFM605Batch implements Database.Batchable < sObject > , Database.AllowsCallouts {
    public String query;
    private List < Id > idList;
    public String qDate;
    public Date queryDate;
    public Boolean reissueBatch = false;
    global NFM605Batch(List < Id > idList) {
        this.idList = idList;
    }
    // eg:19000101
    global NFM605Batch(String querydate) {
        this.qDate = querydate;
    }
    // 指定数据
    global NFM605Batch(Boolean reissueBatch) {
        this.reissueBatch = reissueBatch;
    }
    global NFM605Batch() {}

    global Database.QueryLocator start(Database.BatchableContext bc) {
        if (this.idList != null && this.idList.size() != 0) {
            System.debug('+++++++++++指定数据Id+++++++++++' );

            return Database.getQueryLocator([
                SELECT id,NewMaintenance_Contract__c,reissueBatch__c 
                FROM NewMaintenanceReport_Task__c 
                WHERE id in : idList]);
        } else if (String.isNotBlank(this.qDate)) {

            queryDate = NFMUtil.parseStr2Date(qDate);

            return Database.getQueryLocator([
                SELECT Id,Name,NewMaintenance_Contract__c,reissueBatch__c 
                FROM NewMaintenanceReport_Task__c 
                WHERE CorrespondingPeriod__c =:queryDate]);
        } else if(this.reissueBatch){
            System.debug('+++++++++++数据量大时将数据的reissueBatch__c设置为true+++++++++++' );

            return Database.getQueryLocator([
                SELECT id,NewMaintenance_Contract__c,reissueBatch__c 
                FROM NewMaintenanceReport_Task__c 
                WHERE reissueBatch__c = true]);
        } else {
            System.debug('+++++++++++指定默认昨天+++++++++++' );

            queryDate = Date.today().addDays(-1); 
            return Database.getQueryLocator([
                SELECT Id,Name,NewMaintenance_Contract__c,reissueBatch__c 
                FROM NewMaintenanceReport_Task__c 
                WHERE CorrespondingPeriod__c =:queryDate ]);
        }
    }
    global void execute(Database.BatchableContext BC, list < NewMaintenanceReport_Task__c > scope) {
        List < Id > reportIdList = new  List < Id >();
        List <Id> ncIdList = new List <Id>();

        List<NewMaintenanceReport_Task__c> updateList = new List<NewMaintenanceReport_Task__c>();
        
        Map < Id, Set < Id >> tempIdMap = new Map < Id, Set < Id >> ();

        for (NewMaintenanceReport_Task__c task: scope) {
            if (task.reissueBatch__c) {
                task.reissueBatch__c = false; 
                updateList.add(task);   
            }
            Id contractId = task.NewMaintenance_Contract__c;
            Set < Id > idSet = new Set < Id > ();
            if (tempIdMap.containsKey(contractId)) {
                idSet = tempIdMap.get(contractId);
            }
            idSet.add(task.Id);
            tempIdMap.put(contractId, idSet);

        }
        // 查找符合的数据
        List < Repair__c > repairList = [
            SELECT Id, VM_Maintenance_Contract__c, Maintenance_Contract__c, MaintenanceContractType__c
            FROM Repair__c
            WHERE Maintenance_Contract__c IN: tempIdMap.keySet() 
            AND MaintenanceContractType__c = '服务合同'
            AND RepairSubOrder__c != ''
        ];

        for (Repair__c repair :repairList) {
            
            for (Id taskId: tempIdMap.get(repair.Maintenance_Contract__c)) {
                reportIdList.add(taskId);    
            }
        }




        if (reportIdList.size() >0 ) {
            NFM605Controller.executeNotFuture('', reportIdList);
        }
        
        if (updateList.size() > 0) {
            update updateList;
        }
    }
    global void finish(Database.BatchableContext BC) {
        System.debug('finish----->NFM605Controller');
    }
}