global class RentalTaskDispatchBatch implements Database.Batchable<sObject>  {
    public RentalTaskDispatchBatch() {

    }

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

        String query = 'SELECT Id,	Rental_Apply__c,HadLostReport_Detail__c,Confirm_Lost_Date__c,Loaner_Giveup_Time__c ,Is_Body__c FROM Rental_Apply_Equipment_Set_Detail__c '  +
        ' where (HadLostReport_Detail__c = false and Confirm_Lost_Date__c != null and Loaner_Giveup_Time__c = null)'+
        ' or ( HadLostReport_Detail__c = false and Confirm_Lost_Date__c = null and Loaner_Giveup_Time__c = null '+
        ' and Check_lost_Item_F__c  = \'欠品\' and Return_DeliverySlip__r.Name = null and Lost_item_giveup__c = false )';
        //Rental_Apply_Equipment_Set_Detail__c HadLostReport_Detail__c 是否填写过遗失报告
        //	Confirm_Lost_Date__c 确认遗失时间  Loaner_Giveup_Time__c 欠品断念时间  Check_lost_Item_F__c 欠品确认结果
        // Return_DeliverySlip__r.Name 回寄运输单: 备品运输单号  	Lost_item_giveup__c 放弃欠品回收
        System.debug('query---'+query);
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Rental_Apply_Equipment_Set_Detail__c> rentalApplyList) {
        //main(tasklist);
        Set<Id> loseIds = new Set<Id>();
        Set<Id> deficitIds = new Set<Id>();
        Set<Id> zhutiIds = new Set<Id>(); // 主体
        for (Rental_Apply_Equipment_Set_Detail__c raesd : rentalApplyList){

            if( raesd.Is_Body__c){
                zhutiIds.add(raesd.Rental_Apply__c);
            }

            if(raesd.Confirm_Lost_Date__c == null){
                deficitIds.add(raesd.Rental_Apply__c);
            }else{
                loseIds.add(raesd.Rental_Apply__c);
            }
            

        }
        if(loseIds.size() > 0){
            List<Rental_Apply__c> loseRentalApplyList = [select id,name from Rental_Apply__c where id in :loseIds];
            List<Task__c> loseTaskList=  new List<Task__c>();
            for(Rental_Apply__c rentalApply : loseRentalApplyList){
                Task__c tsk = new Task__c();
                tsk.RentalApply__c = rentalApply.id;
                tsk.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('RentalLose').getRecordTypeId();
                tsk.distributionCount__c = 1;
                if(zhutiIds.contains(rentalApply.id)){
                    tsk.isMainPart__c = true;
                }
                loseTaskList.add(tsk);
            }
            insert loseTaskList;
        }

        if(deficitIds.size() > 0){
            List<Rental_Apply__c> deficitRentalApplyList = [select id,name from Rental_Apply__c where id in :deficitIds];
            List<Task__c> deficitTaskList=  new List<Task__c>();
            for(Rental_Apply__c rentalApply : deficitRentalApplyList){
                Task__c tsk = new Task__c();
                tsk.RentalApply__c = rentalApply.id;
                tsk.recordtypeId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('RentalDeficit').getRecordTypeId();
                tsk.distributionCount__c = 1;
                deficitTaskList.add(tsk);
            }
            insert deficitTaskList;
        }



    }

    global void finish(Database.BatchableContext BC) {
    
    }
}